I want a MultiSelectCombobox
(doc) with custom values
(setAllowCustomValue
) using Flow (not Lit or React). This works except
I can’t deselect the last custom-added item with the mouse. It works
with the keyboard but not with the mouse.
This only applies to the last custom-added item. If I add another
custom-added item then the next-to-last one works again.
The code is based on this snippet from doc:
public ComboBoxCustomEntry2() {
ComboBox<String> comboBox = new ComboBox<>("Browser");
comboBox.setAllowCustomValue(true);
comboBox.addCustomValueSetListener(e -> {
String customValue = e.getDetail();
items.add(customValue);
comboBox.setItems(items);
comboBox.setValue(customValue);
});
add(comboBox);
comboBox.setItems(items);
comboBox.setHelperText("Select or type a browser");
}
What it looks like:
My code:
// LabelComboBox.java
public class LabelComboBox extends MultiSelectComboBox<Label> {
private Set<Label> availableLabels = new HashSet<>();
public LabelComboBox(LabelCategoryKey labelCategory) {
super();
initLabelSelector();
}
private void initLabelSelector() {
availableLabels = new HashSet<>();
setWidthFull();
setLabel(getTranslation("<translation string>"));
setAllowCustomValue(true);
setItems(availableLabels);
setItemLabelGenerator(Label::getLabel);
addCustomValueSetListener(e -> {
var customLabel = new Label();
customLabel.setLabel(e.getDetail());
availableLabels.add(customLabel);
// Needed for the items to be selected
updateSelection(Set.of(customLabel), Collections.emptySet());
setItemsAndRetainSelectedItems();
refreshValue();
});
}
// `getValue()` resets `items` but we want to keep it
private void setItemsAndRetainSelectedItems() {
var selected = getValue();
setItems(availableLabels);
setValue(selected);
}
}
// Label.java
public class Label {
private Long id;
private String label;
// Getter/setters, equals/hashCode elided
}