summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-06-05 15:06:25 +0300
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-07-11 10:01:02 +0300
commit3ed0b1a26c053634ed8db8df8be83e0fdf36e0af (patch)
treeecb507a7783b1d3510a80276e018094f3154722a
parent17171359b54d56ed7e744e384a7b65685eb5d310 (diff)
downloadvaadin-framework-3ed0b1a26c053634ed8db8df8be83e0fdf36e0af.tar.gz
vaadin-framework-3ed0b1a26c053634ed8db8df8be83e0fdf36e0af.zip
Show empty selection caption in ComboBox (#9468)
Fixes #9079
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VComboBox.java35
-rw-r--r--client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java4
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaption.java2
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelecting.java2
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaptionTest.java20
5 files changed, 56 insertions, 7 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VComboBox.java b/client/src/main/java/com/vaadin/client/ui/VComboBox.java
index 2a76907170..787d82caec 100644
--- a/client/src/main/java/com/vaadin/client/ui/VComboBox.java
+++ b/client/src/main/java/com/vaadin/client/ui/VComboBox.java
@@ -243,12 +243,12 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
return $entry(function(e) {
var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX;
var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY;
-
+
// IE8 has only delta y
if (isNaN(deltaY)) {
deltaY = -0.5*e.wheelDelta;
}
-
+
@com.vaadin.client.ui.VComboBox.JsniUtil::moveScrollFromEvent(*)(widget, deltaX, deltaY, e, e.deltaMode);
});
}-*/;
@@ -1669,6 +1669,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
* field even for filtering.
*/
private boolean textInputEnabled = true;
+ private String emptySelectionCaption = "";
private final DataReceivedHandler dataReceivedHandler = new DataReceivedHandler();
@@ -1949,7 +1950,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
dataReceivedHandler.cancelPendingPostFiltering();
currentSuggestion = null;
- setText("");
+ setText(getEmptySelectionCaption());
setSelectedItemIcon(null);
if (!"".equals(selectedOptionKey) || selectedOptionKey != null) {
@@ -1958,6 +1959,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
connector.sendSelection(null);
// currentPage = 0;
}
+
updatePlaceholder();
suggestionPopup.hide();
@@ -2028,7 +2030,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
if (selectedKey == null || "".equals(selectedKey)) {
currentSuggestion = null; // #13217
selectedOptionKey = null;
- setText("");
+ setText(getEmptySelectionCaption());
}
// some item selected
for (ComboBoxSuggestion suggestion : currentSuggestions) {
@@ -2305,7 +2307,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
// just fetch selected information from state
String text = connector.getState().selectedItemCaption;
- setText(text == null ? "" : text);
+ setText(text == null ? getEmptySelectionCaption() : text);
setSelectedItemIcon(connector.getState().selectedItemIcon);
selectedOptionKey = (connector.getState().selectedItemKey);
if (selectedOptionKey == null || "".equals(selectedOptionKey)) {
@@ -2820,4 +2822,27 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
public boolean getNullSelectionItemShouldBeVisible() {
return nullSelectionAllowed && "".equals(lastFilter);
}
+
+ /**
+ * Gets the empty selection caption.
+ *
+ * @return the empty selection caption
+ */
+ public String getEmptySelectionCaption() {
+ return emptySelectionCaption;
+ }
+
+ /**
+ * Sets the empty selection caption for this VComboBox. The text is
+ * displayed in the text input when nothing is selected.
+ *
+ * @param emptySelectionCaption
+ * the empty selection caption
+ */
+ public void setEmptySelectionCaption(String emptySelectionCaption) {
+ this.emptySelectionCaption = emptySelectionCaption;
+ if (selectedOptionKey == null) {
+ setText(emptySelectionCaption);
+ }
+ }
}
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 7fec2ae8e0..9d37afc8a5 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
@@ -105,9 +105,11 @@ public class ComboBoxConnector extends AbstractListingConnector
suggestions.remove(0);
addEmptySelectionItem();
}
+ getWidget().setEmptySelectionCaption(getState().emptySelectionCaption);
}
- @OnStateChange({ "selectedItemKey", "selectedItemCaption", "selectedItemIcon" })
+ @OnStateChange({ "selectedItemKey", "selectedItemCaption",
+ "selectedItemIcon" })
private void onSelectionChange() {
getDataReceivedHandler().updateSelectionFromServer(
getState().selectedItemKey, getState().selectedItemCaption,
diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaption.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaption.java
index 3d32c54a7e..4326d8ac60 100644
--- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaption.java
+++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaption.java
@@ -18,6 +18,7 @@ package com.vaadin.tests.components.combobox;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
@@ -27,6 +28,7 @@ import com.vaadin.ui.ComboBox;
* @author Vaadin Ltd
*
*/
+@Widgetset("com.vaadin.DefaultWidgetSet")
public class ComboBoxEmptyCaption extends AbstractTestUI {
@Override
diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelecting.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelecting.java
index 1a1ac3da03..259e3026dc 100644
--- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelecting.java
+++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelecting.java
@@ -3,12 +3,14 @@ package com.vaadin.tests.components.combobox;
import java.util.ArrayList;
import java.util.List;
+import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
+@Widgetset("com.vaadin.DefaultWidgetSet")
public class ComboBoxSelecting extends AbstractReindeerTestUI {
protected ComboBox<String> comboBox;
protected List<String> items = new ArrayList<>();
diff --git a/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaptionTest.java b/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaptionTest.java
index 0cb890dfda..0efea9cae9 100644
--- a/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaptionTest.java
+++ b/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaptionTest.java
@@ -21,6 +21,8 @@ import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.openqa.selenium.Keys;
+import org.openqa.selenium.interactions.Actions;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.ComboBoxElement;
@@ -74,11 +76,27 @@ public class ComboBoxEmptyCaptionTest extends MultiBrowserTest {
"item6", "item7", "item8", "item9", "item10");
}
+ @Test
+ public void emptyItemCaptionInTextBox() {
+ ComboBoxElement combo = $(ComboBoxElement.class).first();
+
+ Assert.assertEquals("", combo.getInputField().getAttribute("value"));
+
+ // set some caption for the empty selection element
+ $(ButtonElement.class).first().click();
+
+ Assert.assertEquals("empty",
+ combo.getInputField().getAttribute("value"));
+
+ }
+
private void ensureSuggestions(ComboBoxElement element,
String... suggestions) {
element.openPopup();
- System.out.println(element.getPopupSuggestions());
Assert.assertEquals(Arrays.asList(suggestions),
new ArrayList<>(element.getPopupSuggestions()));
+ // Close popup
+ new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
}
+
}