diff options
3 files changed, 91 insertions, 2 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java index 2709175c5c..b7d994745f 100644 --- a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java @@ -68,6 +68,8 @@ public class ComboBoxConnector extends AbstractListingConnector */ private boolean forceDataSourceUpdate = false; + private boolean initialSelectionChangePending = true; + @Override protected void init() { super.init(); @@ -138,8 +140,13 @@ public class ComboBoxConnector extends AbstractListingConnector "selectedItemIcon" }) private void onSelectionChange() { if (getWidget().selectedOptionKey != getState().selectedItemKey) { - getWidget().selectedOptionKey = null; - getWidget().currentSuggestion = null; + if (initialSelectionChangePending) { + getWidget().selectedOptionKey = getState().selectedItemKey; + } else { + getWidget().selectedOptionKey = null; + getWidget().currentSuggestion = null; + } + initialSelectionChangePending = false; } clearNewItemHandlingIfMatch(getState().selectedItemCaption); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItem.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItem.java new file mode 100644 index 0000000000..174d136fd5 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItem.java @@ -0,0 +1,55 @@ +package com.vaadin.tests.components.combobox; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.LongStream; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.ComboBox; + +public class ComboBoxScrollToSelectedItem extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + List<Item> items = new ArrayList<>(); + + LongStream.range(1, 100) + .forEach(l -> items.add(new Item(l, "item:" + l))); + Item selectedItem = new Item(50l, "SHOW ME"); + items.set(50, selectedItem); + + ComboBox<Item> box = new ComboBox<>("items", items); + box.setItemCaptionGenerator(Item::getName); + box.setScrollToSelectedItem(true); + box.setValue(selectedItem); + + addComponent(box); + } + + public class Item { + private Long id; + private String name; + + public Item(long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItemTest.java b/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItemTest.java new file mode 100644 index 0000000000..312be6083d --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItemTest.java @@ -0,0 +1,27 @@ +package com.vaadin.tests.components.combobox; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.ComboBoxElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class ComboBoxScrollToSelectedItemTest extends MultiBrowserTest { + + @Test + public void initialOpeningShouldScrollToSelected() { + openTestURL(); + + ComboBoxElement cb = $(ComboBoxElement.class).first(); + cb.openPopup(); + + WebElement selected = cb.getSuggestionPopup() + .findElement(By.className("gwt-MenuItem-selected")); + assertNotNull(selected); + assertEquals("SHOW ME", selected.getText()); + } +} |