|
7 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | * you may not use this file except in compliance with the License. |
9 | 9 | * You may obtain a copy of the License at |
10 | | - * |
| 10 | + * |
11 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | - * |
| 12 | + * |
13 | 13 | * Unless required by applicable law or agreed to in writing, software |
14 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | 15 | * 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>> |
68 | 68 | implements HasStyle, HasItemsAndComponents<T>, HasDataProvider<T>, HasSize { |
69 | 69 |
|
70 | 70 | public static final String CHIP_LABEL = "event.detail.chipLabel"; |
71 | | - |
| 71 | + |
72 | 72 | private DataProvider<T, ?> availableItems = DataProvider.ofCollection(new ArrayList<T>()); |
73 | 73 | private final Map<String, T> selectedItems = new HashMap<>(); |
74 | 74 | private ItemLabelGenerator<T> itemLabelGenerator; |
@@ -139,44 +139,36 @@ protected void onAttach(AttachEvent attachEvent) { |
139 | 139 | configure(); |
140 | 140 | } |
141 | 141 |
|
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) { |
151 | 143 | String function = "(function _appendChipWithoutEvent() {" + "if ($0.allowDuplicates) {" |
152 | 144 | + "$0.push('items', $1);" + "} else if ($0.items.indexOf($1) == -1) {" |
153 | 145 | + "$0.push('items', $1);}" + "$0.required = false;" |
154 | 146 | + "$0.autoValidate = false;" + "$0._value = '';" + "})()"; |
155 | | - UI.getCurrent().getPage().executeJs(function, getElement(), chip.getLabel()); |
| 147 | + UI.getCurrent().getPage().executeJs(function, getElement(), label); |
156 | 148 | } |
157 | 149 |
|
158 | | - private void removeClientChipWithoutEvent(Chip chip) { |
| 150 | + private void removeClientChipWithoutEvent(String label) { |
159 | 151 | String function = "(function _removeChipByLabel() {" |
160 | 152 | + "const index = $0.items.indexOf($1);" + "if (index != -1) {" |
161 | 153 | + "$0.items.splice('availableItems', index, 1);}" |
162 | 154 | + "})()"; |
163 | | - UI.getCurrent().getPage().executeJs(function, getElement(), chip.getLabel()); |
| 155 | + UI.getCurrent().getPage().executeJs(function, getElement(), label); |
164 | 156 | } |
165 | 157 |
|
166 | 158 | public void setAvailableItems(List<T> items) { |
167 | 159 | this.availableItems = DataProvider.ofCollection(items); |
168 | 160 | } |
169 | 161 |
|
170 | 162 | public String getLabel() { |
171 | | - return this.getElement().getProperty("label"); |
| 163 | + return getElement().getProperty("label"); |
172 | 164 | } |
173 | 165 |
|
174 | 166 | public void setLabel(String label) { |
175 | | - this.getElement().setProperty("label", label); |
| 167 | + getElement().setProperty("label", label); |
176 | 168 | } |
177 | 169 |
|
178 | 170 | 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); |
180 | 172 | } |
181 | 173 |
|
182 | 174 | public void setClosable(boolean closable) { |
@@ -315,15 +307,15 @@ public void addSelectedItem(T newItem) { |
315 | 307 | } else { |
316 | 308 | this.getValue().add(newItem); |
317 | 309 | this.selectedItems.put(itemLabelGenerator.apply(newItem), newItem); |
318 | | - this.appendClientChipWithoutEvent(generateChip(newItem)); |
| 310 | + this.appendClientChipWithoutEvent(itemLabelGenerator.apply(newItem)); |
319 | 311 | this.fireEvent(new ChipCreatedEvent<>(this, false, itemLabelGenerator.apply(newItem))); |
320 | 312 | } |
321 | 313 | } |
322 | 314 |
|
323 | 315 | public void removeSelectedItem(T itemToRemove) { |
324 | 316 | this.getValue().remove(itemToRemove); |
325 | 317 | this.selectedItems.remove(itemLabelGenerator.apply(itemToRemove), itemToRemove); |
326 | | - this.removeClientChipWithoutEvent(generateChip(itemToRemove)); |
| 318 | + this.removeClientChipWithoutEvent(itemLabelGenerator.apply(itemToRemove)); |
327 | 319 | this.fireEvent(new ChipRemovedEvent<>(this, false, itemLabelGenerator.apply(itemToRemove))); |
328 | 320 | } |
329 | 321 |
|
|
0 commit comments