From 9ef479303bb514622ef90d95b94d912780c1812d Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 27 Jan 2017 15:44:03 +0200 Subject: Introduce empty selection functionality for NativeSelect. (#8336) * Introduce empty selection functionality for NativeSelect. Fixes vaadin/framework8-issues#545 --- .../ui/nativeselect/NativeSelectConnector.java | 26 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'client') 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); } -- cgit v1.2.3