Skip to content

Commit 82d6273

Browse files
fix: delete selected key when input is cleared (#2529) (#2574)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 1b06cc2 commit 82d6273

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

  • vaadin-combo-box-flow-parent
    • vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test
    • vaadin-combo-box-flow/src/main/resources/META-INF/resources/frontend

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.vaadin.flow.component.combobox.testbench.ComboBoxElement;
2020
import com.vaadin.tests.AbstractComponentIT;
2121
import com.vaadin.flow.testutil.TestPath;
22+
import com.vaadin.testbench.ElementQuery;
2223
import com.vaadin.testbench.TestBenchElement;
2324
import org.junit.Assert;
2425
import org.junit.Before;
@@ -60,6 +61,28 @@ public void valueIsCorrectlyClearedWithClearButtonBeforeOpened() {
6061
.getText());
6162
}
6263

64+
@Test
65+
public void openPopup_clearButton_selectedItemIsReset() {
66+
String comboBoxId = ClearValuePage.COMBO_BOX_WITH_CLEAR_BUTTON_ID;
67+
ComboBoxElement comboBox = $(ComboBoxElement.class).id(comboBoxId);
68+
69+
comboBox.openPopup();
70+
comboBox.closePopup();
71+
72+
comboBox.$("[part~='clear-button']").get(0).click();
73+
74+
comboBox.openPopup();
75+
76+
TestBenchElement overlay = $("vaadin-combo-box-overlay").first();
77+
ElementQuery<TestBenchElement> items = overlay
78+
.$("vaadin-combo-box-item");
79+
80+
items.all()
81+
.forEach(item -> Assert.assertFalse(
82+
"Item is not selected after clear button click",
83+
item.hasAttribute("selected")));
84+
}
85+
6386
@Test
6487
public void valueIsCorrectlySetToNull() {
6588
Assert.assertNull(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ 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+
}));
263268

264269
comboBox.$connector.enableClientValidation = tryCatchWrapper(function( enable ){
265270
if ( comboBox.$ ){

0 commit comments

Comments
 (0)