Fixes #10624tags/8.4.0.alpha1
* the current filter string | * the current filter string | ||||
*/ | */ | ||||
protected void setFilter(String filter) { | protected void setFilter(String filter) { | ||||
if (!Objects.equals(filter, getWidget().lastFilter)) { | |||||
if (!Objects.equals(filter, getState().currentFilterText)) { | |||||
getDataReceivedHandler().clearPendingNavigation(); | getDataReceivedHandler().clearPendingNavigation(); | ||||
rpc.setFilter(filter); | rpc.setFilter(filter); | ||||
page = 0; | page = 0; | ||||
} | } | ||||
VComboBox widget = getWidget(); | VComboBox widget = getWidget(); | ||||
int adjustment = widget.nullSelectionAllowed && filter.isEmpty() | |||||
? 1 : 0; | |||||
int startIndex = Math.max(0, | |||||
page * widget.pageLength - adjustment); | |||||
int adjustment = widget.nullSelectionAllowed && filter.isEmpty() ? 1 | |||||
: 0; | |||||
int startIndex = Math.max(0, page * widget.pageLength - adjustment); | |||||
int pageLength = widget.pageLength > 0 ? widget.pageLength | int pageLength = widget.pageLength > 0 ? widget.pageLength | ||||
: getDataSource().size(); | : getDataSource().size(); | ||||
getDataSource().ensureAvailability(startIndex, pageLength); | getDataSource().ensureAvailability(startIndex, pageLength); |
@Override | @Override | ||||
public void setFilter(String filterText) { | public void setFilter(String filterText) { | ||||
currentFilterText = filterText; | |||||
getState().currentFilterText = filterText; | |||||
filterSlot.accept(filterText); | filterSlot.accept(filterText); | ||||
} | } | ||||
}; | }; | ||||
private StyleGenerator<T> itemStyleGenerator = item -> null; | private StyleGenerator<T> itemStyleGenerator = item -> null; | ||||
private String currentFilterText; | |||||
private SerializableConsumer<String> filterSlot = filter -> { | private SerializableConsumer<String> filterSlot = filter -> { | ||||
// Just ignore when neither setDataProvider nor setItems has been called | // Just ignore when neither setDataProvider nor setItems has been called | ||||
}; | }; | ||||
}; | }; | ||||
SerializableConsumer<C> providerFilterSlot = internalSetDataProvider( | SerializableConsumer<C> providerFilterSlot = internalSetDataProvider( | ||||
dataProvider, convertOrNull.apply(currentFilterText)); | |||||
dataProvider, | |||||
convertOrNull.apply(getState(false).currentFilterText)); | |||||
filterSlot = filter -> providerFilterSlot | filterSlot = filter -> providerFilterSlot | ||||
.accept(convertOrNull.apply(filter)); | .accept(convertOrNull.apply(filter)); |
*/ | */ | ||||
public String selectedItemIcon; | public String selectedItemIcon; | ||||
/** | |||||
* Filter string that is currently in use in the suggestion listing. | |||||
* | |||||
* @since | |||||
*/ | |||||
public String currentFilterText; | |||||
} | } |
package com.vaadin.tests.components.combobox; | |||||
import com.vaadin.server.VaadinRequest; | |||||
import com.vaadin.ui.Button; | |||||
public class ComboBoxFilterClear extends ComboBoxSelecting { | |||||
@Override | |||||
protected void setup(VaadinRequest request) { | |||||
super.setup(request); | |||||
Button toggleVisibility = new Button("Toggle visibility", | |||||
e -> comboBox.setVisible(!comboBox.isVisible())); | |||||
toggleVisibility.setId("toggleVisibility"); | |||||
Button setNull = new Button("Set null", e -> comboBox.setValue(null)); | |||||
setNull.setId("setNull"); | |||||
addComponents(toggleVisibility, setNull); | |||||
} | |||||
@Override | |||||
protected String getTestDescription() { | |||||
return "Clearing selection while ComboBox is not visible should not " | |||||
+ "leave the suggestion items stuck on the previous filter"; | |||||
} | |||||
@Override | |||||
protected Integer getTicketNumber() { | |||||
return 10624; | |||||
} | |||||
} |
package com.vaadin.tests.components.combobox; | |||||
import static org.junit.Assert.assertEquals; | |||||
import java.util.List; | |||||
import org.junit.Test; | |||||
import org.openqa.selenium.Keys; | |||||
import org.openqa.selenium.WebElement; | |||||
import org.openqa.selenium.interactions.Actions; | |||||
import com.vaadin.testbench.By; | |||||
import com.vaadin.testbench.elements.ButtonElement; | |||||
import com.vaadin.testbench.elements.ComboBoxElement; | |||||
import com.vaadin.tests.tb3.MultiBrowserTest; | |||||
public class ComboBoxFilterClearTest extends MultiBrowserTest { | |||||
ComboBoxElement comboBox; | |||||
@Test | |||||
public void testFilterCleared() { | |||||
openTestURL(); | |||||
comboBox = $(ComboBoxElement.class).first(); | |||||
ButtonElement toggleVisibility = $(ButtonElement.class) | |||||
.id("toggleVisibility"); | |||||
ButtonElement setNull = $(ButtonElement.class).id("setNull"); | |||||
sendKeysToInput("b0", Keys.TAB); | |||||
assertEquals("b0", comboBox.getText()); | |||||
toggleVisibility.click(); | |||||
waitForElementNotPresent(By.className("v-filterselect")); | |||||
setNull.click(); | |||||
toggleVisibility.click(); | |||||
waitForElementPresent(By.className("v-filterselect")); | |||||
comboBox = $(ComboBoxElement.class).first(); | |||||
WebElement suggestionPopup = comboBox.getSuggestionPopup(); | |||||
List<WebElement> menuItems = suggestionPopup | |||||
.findElements(By.className("gwt-MenuItem")); | |||||
assertEquals("a0", menuItems.get(1).getText()); | |||||
} | |||||
private void sendKeysToInput(CharSequence... keys) { | |||||
// ensure mouse is located over the ComboBox to avoid hover issues | |||||
new Actions(getDriver()).moveToElement(comboBox).perform(); | |||||
comboBox.sendKeys(keys); | |||||
} | |||||
} |