2626import java .util .Map ;
2727import java .util .Optional ;
2828import java .util .concurrent .atomic .AtomicInteger ;
29- import java .util .function .Function ;
3029import java .util .stream .Collectors ;
3130import java .util .stream .Stream ;
3231
4746import com .vaadin .flow .data .binder .HasItemsAndComponents ;
4847import com .vaadin .flow .data .provider .DataProvider ;
4948import com .vaadin .flow .data .provider .Query ;
49+ import com .vaadin .flow .function .SerializableFunction ;
5050import com .vaadin .flow .shared .Registration ;
5151
5252import elemental .json .JsonArray ;
6969public class ChipField <T > extends AbstractField <ChipField <T >, List <T >>
7070 implements HasStyle , HasItemsAndComponents <T >, HasDataProvider <T >, HasSize {
7171
72+ public static final String CHIP_LABEL = "event.detail.chipLabel" ;
73+
7274 private DataProvider <T , ?> availableItems = DataProvider .ofCollection (new ArrayList <T >());
73- private Map <String , T > selectedItems = new HashMap <>();
75+ private final Map <String , T > selectedItems = new HashMap <>();
7476 private ItemLabelGenerator <T > itemLabelGenerator ;
75- private Function <String , T > newItemHandler ;
77+ private SerializableFunction <String , T > newItemHandler ;
7678
7779 @ SafeVarargs
7880 public ChipField (String label , ItemLabelGenerator <T > itemLabelGenerator , T ... availableItems ) {
@@ -84,14 +86,14 @@ public ChipField(String label, ItemLabelGenerator<T> itemLabelGenerator, T... av
8486
8587 @ SafeVarargs
8688 public ChipField (String label , T ... availableItems ) {
87- this (label , item -> item . toString () , availableItems );
89+ this (label , Object :: toString , availableItems );
8890 }
8991
9092 private void configure () {
9193 configureItems ();
9294 getElement ().addEventListener ("chip-created" , e -> {
9395 JsonObject eventData = e .getEventData ();
94- String chipLabel = eventData .get ("event.detail.chipLabel" ).asString ();
96+ String chipLabel = eventData .get (CHIP_LABEL ).asString ();
9597 Stream <T > streamItems = availableItems .fetch (new Query <>());
9698 Optional <T > newItem = streamItems .filter (item -> itemLabelGenerator .apply (item ).equals (chipLabel ))
9799 .findFirst ();
@@ -105,17 +107,18 @@ private void configure() {
105107 T item = this .newItemHandler .apply (chipLabel );
106108 selectedItems .put (chipLabel , item );
107109 this .setValue (new ArrayList <T >(selectedItems .values ()));
108- } else
110+ } else {
109111 throw new IllegalStateException (
110112 "Adding new items is not allowed, but still receiving new items (not present in DataProvider) from client-side. Probably wrong configuration." );
113+ }
111114 }
112- }).addEventData ("event.detail.chipLabel" );
115+ }).addEventData (CHIP_LABEL );
113116 getElement ().addEventListener ("chip-removed" , e -> {
114117 JsonObject eventData = e .getEventData ();
115- String chipLabel = eventData .get ("event.detail.chipLabel" ).asString ();
118+ String chipLabel = eventData .get (CHIP_LABEL ).asString ();
116119 T itemToRemove = selectedItems .remove (chipLabel );
117120 getValue ().remove (itemToRemove );
118- }).addEventData ("event.detail.chipLabel" );
121+ }).addEventData (CHIP_LABEL );
119122 }
120123
121124 private void configureItems () {
@@ -137,7 +140,7 @@ protected void onAttach(AttachEvent attachEvent) {
137140 }
138141
139142 private List <Chip > generateSelectedChips (List <T > itemsToGenerate ) {
140- return itemsToGenerate .stream ().map (item -> generateChip ( item ) ).collect (Collectors .toList ());
143+ return itemsToGenerate .stream ().map (this :: generateChip ).collect (Collectors .toList ());
141144 }
142145
143146 private Chip generateChip (T item ) {
@@ -241,17 +244,17 @@ public boolean isAllowAdditionalItems() {
241244 }
242245
243246 public void validate () {
244- getElement ().callFunction ("validate" );
247+ getElement ().callJsFunction ("validate" );
245248 }
246249
247250 // EVENTS
248251 @ DomEvent ("chip-removed" )
249- static public class ChipRemovedEvent <T > extends ComponentEvent <ChipField <T >> {
252+ public static class ChipRemovedEvent <T > extends ComponentEvent <ChipField <T >> {
250253
251254 private final String chipLabel ;
252255
253256 public ChipRemovedEvent (ChipField <T > source , boolean fromClient ,
254- @ EventData ("event.detail.chipLabel" ) String chipLabel ) {
257+ @ EventData (CHIP_LABEL ) String chipLabel ) {
255258 super (source , fromClient );
256259 this .chipLabel = chipLabel ;
257260 }
@@ -262,12 +265,12 @@ public String getChipLabel() {
262265 }
263266
264267 @ DomEvent ("chip-created" )
265- static public class ChipCreatedEvent <T > extends ComponentEvent <ChipField <T >> {
268+ public static class ChipCreatedEvent <T > extends ComponentEvent <ChipField <T >> {
266269
267270 private final String chipLabel ;
268271
269272 public ChipCreatedEvent (ChipField <T > source , boolean fromClient ,
270- @ EventData ("event.detail.chipLabel" ) String chipLabel ) {
273+ @ EventData (CHIP_LABEL ) String chipLabel ) {
271274 super (source , fromClient );
272275 this .chipLabel = chipLabel ;
273276 }
@@ -291,7 +294,7 @@ public void setChipLabelGenerator(ItemLabelGenerator<T> itemLabelGenerator) {
291294 this .itemLabelGenerator = itemLabelGenerator ;
292295 }
293296
294- public void setNewItemHandler (Function <String , T > handler ) {
297+ public void setNewItemHandler (SerializableFunction <String , T > handler ) {
295298 this .newItemHandler = handler ;
296299 this .setAllowAdditionalItems (true );
297300 }
@@ -306,7 +309,7 @@ public void setDataProvider(DataProvider<T, ?> dataProvider) {
306309 }
307310
308311 public void addSelectedItem (T newItem ) {
309- if (! availableItems .fetch (new Query <>()).anyMatch (item -> item .equals (newItem )) && !isAllowAdditionalItems ()) {
312+ if (availableItems .fetch (new Query <>()).noneMatch (item -> item .equals (newItem )) && !isAllowAdditionalItems ()) {
310313 throw new UnsupportedOperationException ("Cannot select item '" + newItem
311314 + "', because is not present in DataProvider, and adding new items is not permitted." );
312315 } else {
0 commit comments