Skip to content

Commit

Permalink
feat: add keepFilter option (#5953)
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Jan 15, 2024
1 parent 91de754 commit ff5c995
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> comboBox = new MultiSelectComboBox<>(
"Items");
List<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,19 @@ public void setSelectedItemsOnTop() {
Assert.assertTrue(
comboBox.getElement().getProperty("selectedItemsOnTop", true));
}

@Test
public void setKeepFilter() {
MultiSelectComboBox<String> 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));
}
}

0 comments on commit ff5c995

Please sign in to comment.