Skip to content

Commit 4d4984a

Browse files
committed
feat: retrieve item object from chip events
Add ChipEvent as superclass of removed, created and clicked events. Update demo in order to user the new getter. Close #26
1 parent 52627ff commit 4d4984a

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

src/main/java/com/flowingcode/vaadin/addons/chipfield/ChipField.java

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ public class ChipField<T> extends AbstractField<ChipField<T>, List<T>>
7171
private ItemLabelGenerator<T> itemLabelGenerator;
7272
private SerializableFunction<String, T> newItemHandler;
7373

74+
public abstract static class ChipEvent<T> extends ComponentEvent<ChipField<T>> {
75+
76+
private final String chipLabel;
77+
private final T item;
78+
79+
public ChipEvent(ChipField<T> source, boolean fromClient, String chipLabel) {
80+
super(source, fromClient);
81+
this.chipLabel = chipLabel;
82+
this.item = source.findItemByLabel(chipLabel).orElse(null);
83+
}
84+
85+
public String getChipLabel() {
86+
return chipLabel;
87+
}
88+
89+
public T getItem() {
90+
return item;
91+
}
92+
93+
}
94+
95+
@DomEvent("chip-removed")
96+
public static class ChipRemovedEvent<T> extends ChipEvent<T> {
97+
public ChipRemovedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
98+
super(source, fromClient, chipLabel);
99+
}
100+
}
101+
102+
@DomEvent("chip-created")
103+
public static class ChipCreatedEvent<T> extends ChipEvent<T> {
104+
public ChipCreatedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
105+
super(source, fromClient, chipLabel);
106+
}
107+
}
108+
109+
@DomEvent("chip-clicked")
110+
public static class ChipClickedEvent<T> extends ChipEvent<T> {
111+
public ChipClickedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
112+
super(source, fromClient, chipLabel);
113+
}
114+
}
115+
74116
@SafeVarargs
75117
public ChipField(String label, ItemLabelGenerator<T> itemLabelGenerator, T... availableItems) {
76118
super(new ArrayList<>());
@@ -231,37 +273,6 @@ public void validate() {
231273
getElement().callJsFunction("validate");
232274
}
233275

234-
// EVENTS
235-
@DomEvent("chip-removed")
236-
public static class ChipRemovedEvent<T> extends ComponentEvent<ChipField<T>> {
237-
238-
private final String chipLabel;
239-
240-
public ChipRemovedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
241-
super(source, fromClient);
242-
this.chipLabel = chipLabel;
243-
}
244-
245-
public String getChipLabel() {
246-
return chipLabel;
247-
}
248-
}
249-
250-
@DomEvent("chip-created")
251-
public static class ChipCreatedEvent<T> extends ComponentEvent<ChipField<T>> {
252-
253-
private final String chipLabel;
254-
255-
public ChipCreatedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
256-
super(source, fromClient);
257-
this.chipLabel = chipLabel;
258-
}
259-
260-
public String getChipLabel() {
261-
return chipLabel;
262-
}
263-
}
264-
265276
@SuppressWarnings({ "unchecked", "rawtypes" })
266277
public Registration addChipRemovedListener(ComponentEventListener<ChipRemovedEvent<T>> listener) {
267278
return addListener(ChipRemovedEvent.class, (ComponentEventListener) listener);
@@ -324,21 +335,6 @@ private void removeSelectedItem(T itemToRemove, boolean fromClient) {
324335
}
325336
}
326337

327-
@DomEvent("chip-clicked")
328-
public static class ChipClickedEvent<T> extends ComponentEvent<ChipField<T>> {
329-
330-
private final String chipLabel;
331-
332-
public ChipClickedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
333-
super(source, fromClient);
334-
this.chipLabel = chipLabel;
335-
}
336-
337-
public String getChipLabel() {
338-
return chipLabel;
339-
}
340-
}
341-
342338
@SuppressWarnings({ "unchecked", "rawtypes" })
343339
public Registration addChipClickedListener(ComponentEventListener<ChipClickedEvent<T>> listener) {
344340
return addListener(ChipClickedEvent.class, (ComponentEventListener) listener);

src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public DataProviderDemo() {
5353
}));
5454

5555
chf.addChipCreatedListener(
56-
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Created by client: " + ev.isFromClient() + "!",
56+
ev -> Notification.show("Chip: " + ev.getItem() + " Created by client: " + ev.isFromClient() + "!",
5757
5000, Position.BOTTOM_START));
5858
chf.addChipRemovedListener(
59-
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Removed by client: " + ev.isFromClient() + "!",
59+
ev -> Notification.show("Chip: " + ev.getItem() + " Removed by client: " + ev.isFromClient() + "!",
6060
5000, Position.BOTTOM_START));
6161
chf.addChipClickedListener(
62-
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Clicked!", 5000, Position.BOTTOM_END));
62+
ev -> Notification.show("Chip: " + ev.getItem() + " Clicked!", 5000, Position.BOTTOM_END));
6363

6464
buttons.add(new Button("All planets", ev -> {
6565
chf.setValue(Planet.all());

src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,10 @@ public static Planet random() {
8585
return new Planet("Planet " + Integer.toString((int) Math.round(Math.random() * 36 * 36 * 36), 36).toUpperCase());
8686
}
8787

88+
@Override
89+
public String toString() {
90+
return name;
91+
}
92+
8893
}
8994

0 commit comments

Comments
 (0)