From fcfe27a5c85a1853fe583045ff0d689e73a5d879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Mon, 15 Jan 2024 12:00:42 +0100 Subject: [PATCH] feat: add keepFilter option --- .../MultiSelectComboBoxKeepFilterPage.java | 29 ++++++++++ .../test/MultiSelectComboBoxKeepFilterIT.java | 55 +++++++++++++++++++ .../combobox/MultiSelectComboBox.java | 24 ++++++++ .../combobox/MultiSelectComboBoxTest.java | 15 +++++ 4 files changed, 123 insertions(+) create mode 100644 vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/main/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterPage.java create mode 100644 vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterIT.java diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/main/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterPage.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/main/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterPage.java new file mode 100644 index 00000000000..fb92dcdd9f1 --- /dev/null +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/main/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterPage.java @@ -0,0 +1,29 @@ +package com.vaadin.flow.component.combobox.test; + +import com.vaadin.flow.component.checkbox.Checkbox; +import com.vaadin.flow.component.combobox.MultiSelectComboBox; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.router.Route; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +@Route("vaadin-multi-select-combo-box/keep-filter") +public class MultiSelectComboBoxKeepFilterPage extends Div { + public MultiSelectComboBoxKeepFilterPage() { + MultiSelectComboBox comboBox = new MultiSelectComboBox<>( + "Items"); + List items = IntStream.range(0, 100) + .mapToObj(i -> "Item " + (i + 1)).collect(Collectors.toList()); + comboBox.setItems(items); + + Checkbox keepFilter = new Checkbox("Keep filter"); + keepFilter.setId("keep-filter"); + keepFilter.addValueChangeListener(e -> { + comboBox.setKeepFilter(keepFilter.getValue()); + }); + + add(comboBox, keepFilter); + } +} diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterIT.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterIT.java new file mode 100644 index 00000000000..75de2f78e84 --- /dev/null +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxKeepFilterIT.java @@ -0,0 +1,55 @@ +package com.vaadin.flow.component.combobox.test; + +import com.vaadin.flow.component.combobox.testbench.MultiSelectComboBoxElement; +import com.vaadin.flow.testutil.TestPath; +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.tests.AbstractComponentIT; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.Keys; + +import java.util.List; + +@TestPath("vaadin-multi-select-combo-box/keep-filter") +public class MultiSelectComboBoxKeepFilterIT extends AbstractComponentIT { + private MultiSelectComboBoxElement comboBox; + private TestBenchElement keepFilter; + + @Before + public void init() { + open(); + comboBox = $(MultiSelectComboBoxElement.class).waitForFirst(); + keepFilter = $(TestBenchElement.class).id("keep-filter"); + } + + @Test + public void keepFilterDisabled_clearsFilterAfterSelection() { + comboBox.sendKeys("Item 1"); + comboBox.waitForLoadingFinished(); + List filteredOptions = comboBox.getOptions(); + Assert.assertEquals(12, filteredOptions.size()); + + comboBox.sendKeys(Keys.ENTER); + + Assert.assertEquals("", comboBox.getInputElementValue()); + Assert.assertEquals("", comboBox.getFilter()); + Assert.assertEquals(100, comboBox.getOptions().size()); + } + + @Test + public void keepFilterEnabled_keepsFilterAfterSelection() { + keepFilter.click(); + + comboBox.sendKeys("Item 1"); + comboBox.waitForLoadingFinished(); + List filteredOptions = comboBox.getOptions(); + Assert.assertEquals(12, filteredOptions.size()); + + comboBox.sendKeys(Keys.ENTER); + + Assert.assertEquals("Item 1", comboBox.getInputElementValue()); + Assert.assertEquals("Item 1", comboBox.getFilter()); + Assert.assertEquals(filteredOptions, comboBox.getOptions()); + } +} diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/MultiSelectComboBox.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/MultiSelectComboBox.java index 0733a8872d3..55e049a5051 100644 --- a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/MultiSelectComboBox.java +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/MultiSelectComboBox.java @@ -506,6 +506,30 @@ public void setSelectedItemsOnTop(boolean selectedItemsOnTop) { getElement().setProperty("selectedItemsOnTop", selectedItemsOnTop); } + /** + * Gets whether the filter is kept after selecting items. {@code false} by + * default. + * + * @since 24.4 + * @return {@code true} if enabled, {@code false} otherwise + */ + public boolean isKeepFilter() { + return getElement().getProperty("keepFilter", false); + } + + /** + * Enables or disables keeping the filter after selecting items. By default, + * the filter is cleared after selecting an item and the overlay shows the + * unfiltered list of items again. Enabling this option will keep the + * filter, which allows to select multiple filtered items in succession. + * + * @param keepFilter + * whether to keep the filter after selecting an item + */ + public void setKeepFilter(boolean keepFilter) { + getElement().setProperty("keepFilter", keepFilter); + } + /** * Gets the internationalization object previously set for this component. *

diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/MultiSelectComboBoxTest.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/MultiSelectComboBoxTest.java index 4369fb33b99..4b4598c1eee 100644 --- a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/MultiSelectComboBoxTest.java +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/MultiSelectComboBoxTest.java @@ -321,4 +321,19 @@ public void setSelectedItemsOnTop() { Assert.assertTrue( comboBox.getElement().getProperty("selectedItemsOnTop", true)); } + + @Test + public void setKeepFilter() { + MultiSelectComboBox comboBox = new MultiSelectComboBox<>(); + + Assert.assertFalse(comboBox.isKeepFilter()); + Assert.assertFalse( + comboBox.getElement().getProperty("keepFilter", false)); + + comboBox.setKeepFilter(true); + + Assert.assertTrue(comboBox.isKeepFilter()); + Assert.assertTrue( + comboBox.getElement().getProperty("keepFilter", true)); + } }