summaryrefslogtreecommitdiffstats
path: root/server/src
diff options
context:
space:
mode:
authorDenis <denis@vaadin.com>2017-01-27 15:44:03 +0200
committerGitHub <noreply@github.com>2017-01-27 15:44:03 +0200
commit9ef479303bb514622ef90d95b94d912780c1812d (patch)
treefd6929a4b396cb6b1ff290ded4e129cd60415d60 /server/src
parent5616216306a138b3437d188e4b2df8253590abf5 (diff)
downloadvaadin-framework-9ef479303bb514622ef90d95b94d912780c1812d.tar.gz
vaadin-framework-9ef479303bb514622ef90d95b94d912780c1812d.zip
Introduce empty selection functionality for NativeSelect. (#8336)
* Introduce empty selection functionality for NativeSelect. Fixes vaadin/framework8-issues#545
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main/java/com/vaadin/ui/NativeSelect.java56
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java7
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/nativeselect/NativeSelectDeclarativeTest.java21
3 files changed, 82 insertions, 2 deletions
diff --git a/server/src/main/java/com/vaadin/ui/NativeSelect.java b/server/src/main/java/com/vaadin/ui/NativeSelect.java
index 9ae3409b66..f7427568ff 100644
--- a/server/src/main/java/com/vaadin/ui/NativeSelect.java
+++ b/server/src/main/java/com/vaadin/ui/NativeSelect.java
@@ -17,6 +17,7 @@
package com.vaadin.ui;
import java.util.Collection;
+import java.util.Objects;
import com.vaadin.data.HasDataProvider;
import com.vaadin.data.provider.DataProvider;
@@ -139,4 +140,59 @@ public class NativeSelect<T> extends AbstractSingleSelect<T>
public ItemCaptionGenerator<T> getItemCaptionGenerator() {
return super.getItemCaptionGenerator();
}
+
+ /**
+ * Returns whether the user is allowed to select nothing in the combo box.
+ *
+ * @return true if empty selection is allowed, false otherwise
+ */
+ public boolean isEmptySelectionAllowed() {
+ return getState(false).emptySelectionAllowed;
+ }
+
+ /**
+ * Sets whether the user is allowed to select nothing in the combo box. When
+ * true, a special empty item is shown to the user.
+ *
+ * @param emptySelectionAllowed
+ * true to allow not selecting anything, false to require
+ * selection
+ */
+ public void setEmptySelectionAllowed(boolean emptySelectionAllowed) {
+ getState().emptySelectionAllowed = emptySelectionAllowed;
+ }
+
+ /**
+ * Returns the empty selection caption.
+ * <p>
+ * The empty string {@code ""} is the default empty selection caption.
+ *
+ * @see #setEmptySelectionAllowed(boolean)
+ * @see #isEmptySelectionAllowed()
+ * @see #setEmptySelectionCaption(String)
+ * @see #isSelected(Object)
+ *
+ * @return the empty selection caption, not {@code null}
+ */
+ public String getEmptySelectionCaption() {
+ return getState(false).emptySelectionCaption;
+ }
+
+ /**
+ * Sets the empty selection caption.
+ * <p>
+ * The empty string {@code ""} is the default empty selection caption.
+ * <p>
+ * If empty selection is allowed via the
+ * {@link #setEmptySelectionAllowed(boolean)} method (it is by default) then
+ * the empty item will be shown with the given caption.
+ *
+ * @param caption
+ * the caption to set, not {@code null}
+ * @see #isSelected(Object)
+ */
+ public void setEmptySelectionCaption(String caption) {
+ Objects.nonNull(caption);
+ getState().emptySelectionCaption = caption;
+ }
}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java
index 1677f8699b..e2a0c67f2a 100644
--- a/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java
+++ b/server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java
@@ -48,12 +48,14 @@ public class ComboBoxDeclarativeTest
int pageLength = 7;
String popupWidth = "11%";
boolean emptySelectionAllowed = false;
+ String emptySelectionCaption = "foo";
String design = String.format(
"<%s placeholder='%s' text-input-allowed='%s' page-length='%d' "
- + "popup-width='%s' empty-selection-allowed='%s' scroll-to-selected-item/>",
+ + "popup-width='%s' empty-selection-allowed='%s' "
+ + "scroll-to-selected-item empty-selection-caption='%s'/>",
getComponentTag(), placeholder, textInputAllowed, pageLength,
- popupWidth, emptySelectionAllowed);
+ popupWidth, emptySelectionAllowed, emptySelectionCaption);
ComboBox<String> comboBox = new ComboBox<>();
comboBox.setPlaceholder(placeholder);
@@ -62,6 +64,7 @@ public class ComboBoxDeclarativeTest
comboBox.setPopupWidth(popupWidth);
comboBox.setScrollToSelectedItem(true);
comboBox.setEmptySelectionAllowed(emptySelectionAllowed);
+ comboBox.setEmptySelectionCaption(emptySelectionCaption);
testRead(design, comboBox);
testWrite(design, comboBox);
diff --git a/server/src/test/java/com/vaadin/tests/server/component/nativeselect/NativeSelectDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/nativeselect/NativeSelectDeclarativeTest.java
index 2b80546e80..fea6ac3b12 100644
--- a/server/src/test/java/com/vaadin/tests/server/component/nativeselect/NativeSelectDeclarativeTest.java
+++ b/server/src/test/java/com/vaadin/tests/server/component/nativeselect/NativeSelectDeclarativeTest.java
@@ -15,6 +15,8 @@
*/
package com.vaadin.tests.server.component.nativeselect;
+import org.junit.Test;
+
import com.vaadin.tests.server.component.abstractsingleselect.AbstractSingleSelectDeclarativeTest;
import com.vaadin.ui.NativeSelect;
@@ -29,6 +31,25 @@ import com.vaadin.ui.NativeSelect;
public class NativeSelectDeclarativeTest
extends AbstractSingleSelectDeclarativeTest<NativeSelect> {
+ @Test
+ public void nativeSelectSpecificPropertiesSerialize() {
+ boolean emptySelectionAllowed = false;
+ String emptySelectionCaption = "foo";
+
+ String design = String.format(
+ "<%s empty-selection-allowed='%s' "
+ + "empty-selection-caption='%s'/>",
+ getComponentTag(), emptySelectionAllowed,
+ emptySelectionCaption);
+
+ NativeSelect<String> select = new NativeSelect<>();
+ select.setEmptySelectionAllowed(emptySelectionAllowed);
+ select.setEmptySelectionCaption(emptySelectionCaption);
+
+ testRead(design, select);
+ testWrite(design, select);
+ }
+
@Override
protected String getComponentTag() {
return "vaadin-native-select";