Skip to content

Commit 09a82e1

Browse files
authored
fix: update selected key in value change listener (#2618)
1 parent 982fd5f commit 09a82e1

4 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/main/java/com/vaadin/flow/component/combobox/test/ComboBoxPage.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public ComboBoxPage() {
6262
createWithUpdateProvider();
6363
createWithValueChangeListener();
6464
createWithUpdatableValue();
65+
createWithUpdateOnValueChange();
6566
createWithPresetValue();
6667
createWithButtonRenderer();
6768
setLabelGeneratorAfterValue();
@@ -199,6 +200,20 @@ private void createWithUpdatableValue() {
199200
add(combo, message, button);
200201
}
201202

203+
private void createWithUpdateOnValueChange() {
204+
ComboBox<String> combo = new ComboBox<>();
205+
combo.setItems("1", "2", "3");
206+
combo.setValue("2");
207+
208+
combo.addValueChangeListener(e -> {
209+
String value = e.getValue();
210+
combo.setValue(value);
211+
});
212+
213+
combo.setId("update-on-change-combo");
214+
add(combo);
215+
}
216+
202217
private void setLabelGeneratorAfterValue() {
203218
ComboBox<SimpleBean> combo = new ComboBox<>();
204219
SimpleBean foo = new SimpleBean("foo");

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/ComboBoxPageIT.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import com.vaadin.flow.component.combobox.testbench.ComboBoxElement;
2727
import com.vaadin.flow.testutil.TestPath;
28+
import com.vaadin.testbench.ElementQuery;
29+
import com.vaadin.testbench.TestBenchElement;
2830

2931
import static org.junit.Assert.assertFalse;
3032

@@ -194,6 +196,30 @@ public void changeValue_IsFromClientIsSetAccordingly() {
194196
message.getText());
195197
}
196198

199+
@Test
200+
public void changeValue_oldSelectedItemKeyIsReset() {
201+
ComboBoxElement combo = $(ComboBoxElement.class)
202+
.id("update-on-change-combo");
203+
combo.openPopup();
204+
205+
TestBenchElement overlay = $("vaadin-combo-box-overlay").first();
206+
ElementQuery<TestBenchElement> items = overlay
207+
.$("vaadin-combo-box-item");
208+
209+
TestBenchElement item1 = items.get(0);
210+
TestBenchElement item2 = items.get(1);
211+
212+
Assert.assertFalse(item1.hasAttribute("selected"));
213+
Assert.assertTrue(item2.hasAttribute("selected"));
214+
215+
item1.click();
216+
217+
combo.openPopup();
218+
219+
Assert.assertTrue(item1.hasAttribute("selected"));
220+
Assert.assertFalse(item2.hasAttribute("selected"));
221+
}
222+
197223
@Test
198224
public void setValue_setLabelGenerator_selectedItemLabelUpdated() {
199225
ComboBoxElement combo = $(ComboBoxElement.class)

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/ComboBox.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ public ComboBox(int pageSize) {
309309
// `ComboBox.addCustomValueSetListener`.
310310
super.addCustomValueSetListener(e -> this.getElement()
311311
.setProperty(PROP_INPUT_ELEMENT_VALUE, e.getDetail()));
312+
313+
super.addValueChangeListener(e -> updateSelectedKey());
312314
}
313315

314316
/**
@@ -1658,6 +1660,12 @@ private void render() {
16581660
reset();
16591661
}
16601662

1663+
private void updateSelectedKey() {
1664+
// Send (possibly updated) key for the selected value
1665+
getElement().executeJs("this._selectedKey=$0",
1666+
getValue() != null ? getKeyMapper().key(getValue()) : "");
1667+
}
1668+
16611669
@ClientCallable
16621670
private void confirmUpdate(int id) {
16631671
dataCommunicator.confirmUpdate(id);
@@ -1667,9 +1675,7 @@ private void confirmUpdate(int id) {
16671675
private void setRequestedRange(int start, int length, String filter) {
16681676
dataCommunicator.setRequestedRange(start, length);
16691677
filterSlot.accept(filter);
1670-
// Send (possibly updated) key for the selected value
1671-
getElement().executeJs("this._selectedKey=$0",
1672-
getValue() != null ? getKeyMapper().key(getValue()) : "");
1678+
updateSelectedKey();
16731679
}
16741680

16751681
@ClientCallable

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/resources/META-INF/resources/frontend/comboBoxConnector.js‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,6 @@ import { ComboBoxPlaceholder } from '@vaadin/combo-box/src/vaadin-combo-box-plac
260260
}
261261
}), { once: true });
262262

263-
comboBox.addEventListener('value-changed', tryCatchWrapper((event) => {
264-
if (comboBox._selectedKey && !event.detail.value) {
265-
delete comboBox._selectedKey;
266-
}
267-
}));
268-
269263
comboBox.$connector.enableClientValidation = tryCatchWrapper(function( enable ){
270264
if ( comboBox.$ ){
271265
if ( enable){

0 commit comments

Comments
 (0)