aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnna Koskinen <Ansku@users.noreply.github.com>2019-11-19 15:20:21 +0200
committerGitHub <noreply@github.com>2019-11-19 15:20:21 +0200
commit626624982832f81baecbfb0e992f250d64961f36 (patch)
tree220a134faa383f7db49b5f3ddb5259a4a61246e8
parentd047db5d1587f01386769d361d5a501c27a0f141 (diff)
downloadvaadin-framework-626624982832f81baecbfb0e992f250d64961f36.tar.gz
vaadin-framework-626624982832f81baecbfb0e992f250d64961f36.zip
Fix for TwinColSelect not correctly retaining visible selection (#11799) (#11816)
There is a bug in TwinColSelect loging, it retains selection by indexes not by values after being sorted when new item has been added. This is a fixed by changing updateListBox method to retain the selection as it is being called after selection is being done. Fixes: #11287
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VTwinColSelect.java35
1 files changed, 32 insertions, 3 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VTwinColSelect.java b/client/src/main/java/com/vaadin/client/ui/VTwinColSelect.java
index 97167847a5..7e41f0a6ac 100644
--- a/client/src/main/java/com/vaadin/client/ui/VTwinColSelect.java
+++ b/client/src/main/java/com/vaadin/client/ui/VTwinColSelect.java
@@ -25,6 +25,7 @@ import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
+import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.ClickEvent;
@@ -37,6 +38,7 @@ import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
@@ -80,6 +82,7 @@ public class VTwinColSelect extends Composite implements MultiSelectWidget,
private static final int VISIBLE_COUNT = 10;
private static final int DEFAULT_COLUMN_COUNT = 10;
+ private static int scheduledScrollToItem = -1;
private final DoubleClickListBox optionsListBox;
@@ -327,15 +330,28 @@ public class VTwinColSelect extends Composite implements MultiSelectWidget,
private static void updateListBox(ListBox listBox,
List<JsonObject> options) {
+ List<String> selected = new ArrayList<String>();
+ // Retain right visible selection, see #11287
+ for (int i = 0; i < listBox.getItemCount(); ++i) {
+ if (listBox.isItemSelected(i)) {
+ selected.add(listBox.getItemText(i));
+ }
+ }
for (int i = 0; i < options.size(); i++) {
final JsonObject item = options.get(i);
// reuse existing option if possible
+ String caption = MultiSelectWidget.getCaption(item);
if (i < listBox.getItemCount()) {
- listBox.setItemText(i, MultiSelectWidget.getCaption(item));
+ listBox.setItemText(i, caption);
listBox.setValue(i, MultiSelectWidget.getKey(item));
} else {
- listBox.addItem(MultiSelectWidget.getCaption(item),
- MultiSelectWidget.getKey(item));
+ listBox.addItem(caption, MultiSelectWidget.getKey(item));
+ }
+ boolean isSelected = selected.contains(caption);
+ listBox.setItemSelected(i, isSelected);
+ if (isSelected) {
+ // Ensure that last selected item is visible
+ scrollToView(listBox,i);
}
}
// remove extra
@@ -344,6 +360,19 @@ public class VTwinColSelect extends Composite implements MultiSelectWidget,
}
}
+ private static void scrollToView(ListBox listBox, int i) {
+ if (scheduledScrollToItem == -1) {
+ scheduledScrollToItem = i;
+ Scheduler.get().scheduleDeferred(() -> {
+ Element el = (Element) listBox.getElement().getChild(scheduledScrollToItem);
+ el.scrollIntoView();
+ scheduledScrollToItem = -1;
+ });
+ } else {
+ scheduledScrollToItem = i;
+ }
+ }
+
private static boolean[] getSelectionBitmap(ListBox listBox) {
final boolean[] selectedIndexes = new boolean[listBox.getItemCount()];
for (int i = 0; i < listBox.getItemCount(); i++) {