Skip to content

Commit

Permalink
fix #960 calling withValue(null) on a Select should clear it
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Sep 19, 2024
1 parent 81d2d90 commit d72cfb4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ private void addItemToGroup(MenuItemsGroup<T> optionsGroup, O option) {
public C withValue(V value, boolean silent) {
V oldValue = getValue();
if (!Objects.equals(value, oldValue)) {
doSetValue(value);
doSetValue(value, silent);
if (!silent) {
triggerChangeListeners(oldValue, getValue());
}
Expand Down Expand Up @@ -796,8 +796,9 @@ public C withOption(O option) {
* its behavior.
*
* @param value The value to set.
* @param silent boolean to pause triggering change handlers
*/
protected abstract void doSetValue(V value);
protected abstract void doSetValue(V value, boolean silent);

/**
* Abstract method to set a specified option for the select. Concrete implementations will define
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,21 @@ public MultiSelect<V> withValue(boolean silent, V... value) {
return withValue(Arrays.asList(value), silent);
}

protected void doSetValue(List<V> value) {
withPauseChangeListenersToggle(
true,
field ->
value.forEach(
v -> {
Optional<SelectOption<V>> optionByValue = findOptionByValue(v);
optionByValue.ifPresent(
vSelectOption -> onOptionSelected(vSelectOption, isChangeListenersPaused()));
}));
protected void doSetValue(List<V> value, boolean silent) {
if (isNull(value) || value.isEmpty()) {
clearValue(silent);
} else {
withPauseChangeListenersToggle(
true,
field ->
value.forEach(
v -> {
Optional<SelectOption<V>> optionByValue = findOptionByValue(v);
optionByValue.ifPresent(
vSelectOption ->
onOptionSelected(vSelectOption, isChangeListenersPaused()));
}));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ public Select(String label) {
setLabel(label);
}

protected void doSetValue(V value) {
protected void doSetValue(V value, boolean silent) {
findOptionByValue(value)
.ifPresent(vSelectOption -> onOptionSelected(vSelectOption, isChangeListenersPaused()));
.ifPresentOrElse(
vSelectOption -> onOptionSelected(vSelectOption, isChangeListenersPaused()),
() -> {
if (isNull(value)) {
clearValue(silent);
}
});
}

protected void doSetOption(SelectOption<V> option) {
Expand Down

0 comments on commit d72cfb4

Please sign in to comment.