Skip to content

Commit 92ebb9d

Browse files
committed
refactor: use String label instead of transient Chip components
1 parent c23396e commit 92ebb9d

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -68,7 +68,7 @@ public class ChipField<T> extends AbstractField<ChipField<T>, List<T>>
6868
implements HasStyle, HasItemsAndComponents<T>, HasDataProvider<T>, HasSize {
6969

7070
public static final String CHIP_LABEL = "event.detail.chipLabel";
71-
71+
7272
private DataProvider<T, ?> availableItems = DataProvider.ofCollection(new ArrayList<T>());
7373
private final Map<String, T> selectedItems = new HashMap<>();
7474
private ItemLabelGenerator<T> itemLabelGenerator;
@@ -139,44 +139,36 @@ protected void onAttach(AttachEvent attachEvent) {
139139
configure();
140140
}
141141

142-
private List<Chip> generateSelectedChips(List<T> itemsToGenerate) {
143-
return itemsToGenerate.stream().map(this::generateChip).collect(Collectors.toList());
144-
}
145-
146-
private Chip generateChip(T item) {
147-
return new Chip(itemLabelGenerator.apply(item));
148-
}
149-
150-
private void appendClientChipWithoutEvent(Chip chip) {
142+
private void appendClientChipWithoutEvent(String label) {
151143
String function = "(function _appendChipWithoutEvent() {" + "if ($0.allowDuplicates) {"
152144
+ "$0.push('items', $1);" + "} else if ($0.items.indexOf($1) == -1) {"
153145
+ "$0.push('items', $1);}" + "$0.required = false;"
154146
+ "$0.autoValidate = false;" + "$0._value = '';" + "})()";
155-
UI.getCurrent().getPage().executeJs(function, getElement(), chip.getLabel());
147+
UI.getCurrent().getPage().executeJs(function, getElement(), label);
156148
}
157149

158-
private void removeClientChipWithoutEvent(Chip chip) {
150+
private void removeClientChipWithoutEvent(String label) {
159151
String function = "(function _removeChipByLabel() {"
160152
+ "const index = $0.items.indexOf($1);" + "if (index != -1) {"
161153
+ "$0.items.splice('availableItems', index, 1);}"
162154
+ "})()";
163-
UI.getCurrent().getPage().executeJs(function, getElement(), chip.getLabel());
155+
UI.getCurrent().getPage().executeJs(function, getElement(), label);
164156
}
165157

166158
public void setAvailableItems(List<T> items) {
167159
this.availableItems = DataProvider.ofCollection(items);
168160
}
169161

170162
public String getLabel() {
171-
return this.getElement().getProperty("label");
163+
return getElement().getProperty("label");
172164
}
173165

174166
public void setLabel(String label) {
175-
this.getElement().setProperty("label", label);
167+
getElement().setProperty("label", label);
176168
}
177169

178170
public String[] getChipsAsStrings() {
179-
return this.generateSelectedChips(super.getValue()).stream().map(Chip::getLabel).toArray(String[]::new);
171+
return super.getValue().stream().map(itemLabelGenerator).toArray(String[]::new);
180172
}
181173

182174
public void setClosable(boolean closable) {
@@ -315,15 +307,15 @@ public void addSelectedItem(T newItem) {
315307
} else {
316308
this.getValue().add(newItem);
317309
this.selectedItems.put(itemLabelGenerator.apply(newItem), newItem);
318-
this.appendClientChipWithoutEvent(generateChip(newItem));
310+
this.appendClientChipWithoutEvent(itemLabelGenerator.apply(newItem));
319311
this.fireEvent(new ChipCreatedEvent<>(this, false, itemLabelGenerator.apply(newItem)));
320312
}
321313
}
322314

323315
public void removeSelectedItem(T itemToRemove) {
324316
this.getValue().remove(itemToRemove);
325317
this.selectedItems.remove(itemLabelGenerator.apply(itemToRemove), itemToRemove);
326-
this.removeClientChipWithoutEvent(generateChip(itemToRemove));
318+
this.removeClientChipWithoutEvent(itemLabelGenerator.apply(itemToRemove));
327319
this.fireEvent(new ChipRemovedEvent<>(this, false, itemLabelGenerator.apply(itemToRemove)));
328320
}
329321

0 commit comments

Comments
 (0)