diff --git a/src/main/java/com/flowingcode/vaadin/addons/chipfield/ChipField.java b/src/main/java/com/flowingcode/vaadin/addons/chipfield/ChipField.java index 7810707..1d34650 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/chipfield/ChipField.java +++ b/src/main/java/com/flowingcode/vaadin/addons/chipfield/ChipField.java @@ -71,6 +71,48 @@ public class ChipField extends AbstractField, List> private ItemLabelGenerator itemLabelGenerator; private SerializableFunction newItemHandler; + public abstract static class ChipEvent extends ComponentEvent> { + + private final String chipLabel; + private final T item; + + public ChipEvent(ChipField source, boolean fromClient, String chipLabel) { + super(source, fromClient); + this.chipLabel = chipLabel; + this.item = source.findItemByLabel(chipLabel).orElse(null); + } + + public String getChipLabel() { + return chipLabel; + } + + public T getItem() { + return item; + } + + } + + @DomEvent("chip-removed") + public static class ChipRemovedEvent extends ChipEvent { + public ChipRemovedEvent(ChipField source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) { + super(source, fromClient, chipLabel); + } + } + + @DomEvent("chip-created") + public static class ChipCreatedEvent extends ChipEvent { + public ChipCreatedEvent(ChipField source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) { + super(source, fromClient, chipLabel); + } + } + + @DomEvent("chip-clicked") + public static class ChipClickedEvent extends ChipEvent { + public ChipClickedEvent(ChipField source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) { + super(source, fromClient, chipLabel); + } + } + @SafeVarargs public ChipField(String label, ItemLabelGenerator itemLabelGenerator, T... availableItems) { super(new ArrayList<>()); @@ -231,37 +273,6 @@ public void validate() { getElement().callJsFunction("validate"); } - // EVENTS - @DomEvent("chip-removed") - public static class ChipRemovedEvent extends ComponentEvent> { - - private final String chipLabel; - - public ChipRemovedEvent(ChipField source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) { - super(source, fromClient); - this.chipLabel = chipLabel; - } - - public String getChipLabel() { - return chipLabel; - } - } - - @DomEvent("chip-created") - public static class ChipCreatedEvent extends ComponentEvent> { - - private final String chipLabel; - - public ChipCreatedEvent(ChipField source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) { - super(source, fromClient); - this.chipLabel = chipLabel; - } - - public String getChipLabel() { - return chipLabel; - } - } - @SuppressWarnings({ "unchecked", "rawtypes" }) public Registration addChipRemovedListener(ComponentEventListener> listener) { return addListener(ChipRemovedEvent.class, (ComponentEventListener) listener); @@ -324,21 +335,6 @@ private void removeSelectedItem(T itemToRemove, boolean fromClient) { } } - @DomEvent("chip-clicked") - public static class ChipClickedEvent extends ComponentEvent> { - - private final String chipLabel; - - public ChipClickedEvent(ChipField source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) { - super(source, fromClient); - this.chipLabel = chipLabel; - } - - public String getChipLabel() { - return chipLabel; - } - } - @SuppressWarnings({ "unchecked", "rawtypes" }) public Registration addChipClickedListener(ComponentEventListener> listener) { return addListener(ChipClickedEvent.class, (ComponentEventListener) listener); diff --git a/src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java b/src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java index 4eeab7b..d216682 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java @@ -53,13 +53,13 @@ public DataProviderDemo() { })); chf.addChipCreatedListener( - ev -> Notification.show("Chip: " + ev.getChipLabel() + " Created by client: " + ev.isFromClient() + "!", + ev -> Notification.show("Chip: " + ev.getItem() + " Created by client: " + ev.isFromClient() + "!", 5000, Position.BOTTOM_START)); chf.addChipRemovedListener( - ev -> Notification.show("Chip: " + ev.getChipLabel() + " Removed by client: " + ev.isFromClient() + "!", + ev -> Notification.show("Chip: " + ev.getItem() + " Removed by client: " + ev.isFromClient() + "!", 5000, Position.BOTTOM_START)); chf.addChipClickedListener( - ev -> Notification.show("Chip: " + ev.getChipLabel() + " Clicked!", 5000, Position.BOTTOM_END)); + ev -> Notification.show("Chip: " + ev.getItem() + " Clicked!", 5000, Position.BOTTOM_END)); buttons.add(new Button("All planets", ev -> { chf.setValue(Planet.all()); diff --git a/src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java b/src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java index d4df8d9..662741f 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java @@ -85,5 +85,10 @@ public static Planet random() { return new Planet("Planet " + Integer.toString((int) Math.round(Math.random() * 36 * 36 * 36), 36).toUpperCase()); } + @Override + public String toString() { + return name; + } + }