diff options
author | Denis <denis@vaadin.com> | 2017-01-27 15:44:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-27 15:44:03 +0200 |
commit | 9ef479303bb514622ef90d95b94d912780c1812d (patch) | |
tree | fd6929a4b396cb6b1ff290ded4e129cd60415d60 /client | |
parent | 5616216306a138b3437d188e4b2df8253590abf5 (diff) | |
download | vaadin-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 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java | 26 |
1 files changed, 22 insertions, 4 deletions
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 33c3170168..2426996ba4 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 @@ -17,6 +17,7 @@ package com.vaadin.client.ui.nativeselect; import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.user.client.ui.ListBox; import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.connectors.AbstractSingleSelectConnector; import com.vaadin.client.data.DataSource; @@ -91,6 +92,21 @@ public class NativeSelectConnector getWidget().setTabIndex(getState().tabIndex); } + @OnStateChange({ "emptySelectionCaption", "emptySelectionAllowed" }) + private void onEmptySelectionCaptionChange() { + ListBox listBox = getWidget().getListBox(); + boolean hasEmptyItem = listBox.getItemCount() > 0 + && listBox.getValue(0).isEmpty(); + if (hasEmptyItem && getState().emptySelectionAllowed) { + listBox.setItemText(0, getState().emptySelectionCaption); + } else if (hasEmptyItem && !getState().emptySelectionAllowed) { + listBox.removeItem(0); + } else if (!hasEmptyItem && getState().emptySelectionAllowed) { + listBox.insertItem(getState().emptySelectionCaption, 0); + listBox.setValue(0, ""); + } + } + @Override public NativeSelectState getState() { return (NativeSelectState) super.getState(); @@ -112,9 +128,11 @@ public class NativeSelectConnector final VNativeSelect select = getWidget(); final int itemCount = select.getListBox().getItemCount(); - for (int i = range.getStart(); i < range.getEnd(); i++) { + int increment = getState().emptySelectionAllowed ? 1 : 0; + for (int i = range.getStart() + increment; i < range.getEnd() + + increment; i++) { - final JsonObject row = getDataSource().getRow(i); + final JsonObject row = getDataSource().getRow(i - increment); if (i < itemCount) { // Reuse and update an existing item @@ -127,8 +145,8 @@ public class NativeSelectConnector } } - for (int i = select.getListBox().getItemCount() - 1; i >= range - .getEnd(); i--) { + for (int i = select.getListBox().getItemCount() - 1; i >= range.getEnd() + + increment; i--) { // Remove extra items if the new dataset is smaller than the old select.getListBox().removeItem(i); } |