summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorAnastasia Smirnova <anasmi@utu.fi>2018-10-18 15:28:23 +0300
committerSun Zhe <31067185+ZheSun88@users.noreply.github.com>2018-10-18 15:28:23 +0300
commit5e33b383fdf31f2ea15603f3a30bcb1e3c22d081 (patch)
tree41fb8f69c83178728bb5822e636e2da36042e3a8 /client
parenta52993ba9ca67c15e6ca311ca308528752b589c9 (diff)
downloadvaadin-framework-5e33b383fdf31f2ea15603f3a30bcb1e3c22d081.tar.gz
vaadin-framework-5e33b383fdf31f2ea15603f3a30bcb1e3c22d081.zip
Display the caption of the Empty selection in NativeSelect (#11191)
* Fixes #10937 - Previously if selected value is null, then index is set to -1; in current implementation if value is null and emptySelection is allowed then set the index to 0. (The position for the empty selection) - Also, if changing the allowEmptySelection on the fly, ensure, that either index is to-reset to -1 by setting the selected value to null on the client-side (the value before was null) or preserve the value(value was different than empty). * Change the test case Since in this pr the behaviour of the NS is changed, therefore old test need to be adjusted. Change: setting null as value will select empty selection. Before that nothing would be selected and value will be cleared. Behaviour change in PR: Allow selecting null as value
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VNativeSelect.java26
-rw-r--r--client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java4
2 files changed, 29 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java b/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java
index f6980050b6..f92aa05e42 100644
--- a/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java
+++ b/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java
@@ -29,6 +29,7 @@ import com.vaadin.shared.ui.nativeselect.NativeSelectState;
public class VNativeSelect extends FocusableFlowPanelComposite {
private final ListBox listBox = new ListBox();
+ private boolean emptySelectionAllowed = true;
/**
* Creates a new {@code VNativeSelect} instance.
@@ -55,7 +56,11 @@ public class VNativeSelect extends FocusableFlowPanelComposite {
*/
public void setSelectedItem(String value) {
if (value == null) {
- getListBox().setSelectedIndex(-1);
+ if (emptySelectionAllowed) {
+ getListBox().setSelectedIndex(0);
+ } else {
+ getListBox().setSelectedIndex(-1);
+ }
} else {
for (int i = 0; i < getListBox().getItemCount(); i++) {
if (Objects.equals(value, getListBox().getValue(i))) {
@@ -132,4 +137,23 @@ public class VNativeSelect extends FocusableFlowPanelComposite {
return getListBox().getVisibleItemCount();
}
+ /**
+ * Returns true if empty selection is allowed.
+ *
+ * @since
+ * @return empty selection is allowed
+ */
+ public boolean isEmptySelectionAllowed() {
+ return emptySelectionAllowed;
+ }
+
+ /**
+ * Sets true if empty selection is allowed.
+ *
+ * @since
+ * @param emptySelectionAllowed
+ */
+ public void setEmptySelectionAllowed(boolean emptySelectionAllowed) {
+ this.emptySelectionAllowed = emptySelectionAllowed;
+ }
}
diff --git a/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java b/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
index 44b6c3b831..5f17ac4e64 100644
--- a/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
@@ -95,10 +95,14 @@ public class NativeSelectConnector
ListBox listBox = getWidget().getListBox();
boolean hasEmptyItem = listBox.getItemCount() > 0
&& listBox.getValue(0).isEmpty();
+ getWidget().setEmptySelectionAllowed(getState().emptySelectionAllowed);
if (hasEmptyItem && getState().emptySelectionAllowed) {
listBox.setItemText(0, getState().emptySelectionCaption);
} else if (hasEmptyItem && !getState().emptySelectionAllowed) {
listBox.removeItem(0);
+ if (getWidget().getListBox().getSelectedIndex() == 0) {
+ getWidget().setSelectedItem(null);
+ }
} else if (!hasEmptyItem && getState().emptySelectionAllowed) {
listBox.insertItem(getState().emptySelectionCaption, 0);
listBox.setValue(0, "");