Skip to content

Commit 24f6e80

Browse files
committed
fix: fire created/removed events when the value changes
Fix that the events are not fired when the value is modified from server side, and the item is not available from the removed listener when the removal originates from the client. Close #48 Close #49
1 parent c93b5b3 commit 24f6e80

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ public abstract static class ChipEvent<T> extends ComponentEvent<ChipField<T>> {
7676
private final String chipLabel;
7777
private final T item;
7878

79-
public ChipEvent(ChipField<T> source, boolean fromClient, String chipLabel) {
79+
private ChipEvent(ChipField<T> source, boolean fromClient, T item, String chipLabel) {
8080
super(source, fromClient);
8181
this.chipLabel = chipLabel;
82-
this.item = source.findItemByLabel(chipLabel).orElse(null);
82+
this.item = item;
83+
}
84+
85+
public ChipEvent(ChipField<T> source, boolean fromClient, String chipLabel) {
86+
this(source, fromClient, source.findItemByLabel(chipLabel).orElse(null), chipLabel);
8387
}
8488

8589
public String getChipLabel() {
@@ -91,19 +95,16 @@ public T getItem() {
9195
}
9296
}
9397

94-
@DomEvent("chip-removed")
9598
public static class ChipRemovedEvent<T> extends ChipEvent<T> {
96-
public ChipRemovedEvent(
97-
ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
98-
super(source, fromClient, chipLabel);
99+
public ChipRemovedEvent(ChipField<T> source, boolean fromClient, T item) {
100+
super(source, fromClient, item, source.itemLabelGenerator.apply(item));
99101
}
100102
}
101103

102-
@DomEvent("chip-created")
103104
public static class ChipCreatedEvent<T> extends ChipEvent<T> {
104105
public ChipCreatedEvent(
105-
ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
106-
super(source, fromClient, chipLabel);
106+
ChipField<T> source, boolean fromClient, T item) {
107+
super(source, fromClient, item, source.itemLabelGenerator.apply(item));
107108
}
108109
}
109110

@@ -121,6 +122,20 @@ public ChipField(String label, ItemLabelGenerator<T> itemLabelGenerator, T... av
121122
setChipLabelGenerator(itemLabelGenerator);
122123
setLabel(label);
123124
this.availableItems = DataProvider.ofCollection(Arrays.asList(availableItems));
125+
126+
addValueChangeListener(ev -> {
127+
for (T t : ev.getOldValue()) {
128+
if (!ev.getValue().contains(t)) {
129+
fireEvent(new ChipRemovedEvent<>(this, ev.isFromClient(), t));
130+
}
131+
}
132+
133+
for (T t : ev.getValue()) {
134+
if (!ev.getOldValue().contains(t)) {
135+
fireEvent(new ChipCreatedEvent<>(this, ev.isFromClient(), t));
136+
}
137+
}
138+
});
124139
}
125140

126141
@SafeVarargs
@@ -396,7 +411,6 @@ private void addSelectedItemInternal(T newItem, boolean fromClient) {
396411
setModelValue(value, fromClient);
397412
if (!fromClient) {
398413
setPresentationValue(value);
399-
fireEvent(new ChipCreatedEvent<>(this, fromClient, itemLabelGenerator.apply(newItem)));
400414
}
401415
}
402416
}
@@ -412,7 +426,7 @@ private void removeSelectedItem(T itemToRemove, boolean fromClient) {
412426
setModelValue(value, fromClient);
413427
if (!fromClient) {
414428
setPresentationValue(value);
415-
fireEvent(new ChipRemovedEvent<>(this, fromClient, itemLabelGenerator.apply(itemToRemove)));
429+
416430
}
417431
}
418432
}
@@ -421,4 +435,5 @@ private void removeSelectedItem(T itemToRemove, boolean fromClient) {
421435
public Registration addChipClickedListener(ComponentEventListener<ChipClickedEvent<T>> listener) {
422436
return addListener(ChipClickedEvent.class, (ComponentEventListener) listener);
423437
}
438+
424439
}

0 commit comments

Comments
 (0)