From bbf6dc2f29cff9a9fde815e921ee470855df8ddd Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Wed, 24 Jul 2019 16:49:25 +0300 Subject: [PATCH] resolve conflict --- .../client/ui/combobox/ComboBoxConnector.java | 17 +++++- .../ComboBoxScrollToSelectedItem.java | 55 +++++++++++++++++++ .../ComboBoxScrollToSelectedItemTest.java | 27 +++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItem.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItemTest.java 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 2ef091569d..c15405540a 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 @@ -62,6 +62,14 @@ public class ComboBoxConnector extends AbstractListingConnector */ private String pendingNewItemValue = null; + /** + * If this flag is toggled, even unpaged data sources should be updated on + * reset. + */ + private boolean forceDataSourceUpdate = false; + + private boolean initialSelectionChangePending = true; + @Override protected void init() { super.init(); @@ -127,8 +135,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 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 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()); + } +} -- 2.39.5