From 44d4eab3535e89410e4fe4aa54c1912f701769ff Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Tue, 9 Jan 2018 14:42:49 +0100 Subject: [PATCH] Fix space key on selection checkbox to select grid row (#10388) Fixes #10221 --- .../selection/MultiSelectionRenderer.java | 19 ++++++++++++++++++- .../components/grid/GridSelectionTest.java | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java index c9b4f0dac1..58576d41c1 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java @@ -30,6 +30,9 @@ import com.google.gwt.dom.client.TableRowElement; import com.google.gwt.dom.client.TableSectionElement; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.KeyCodes; +import com.google.gwt.event.dom.client.KeyUpEvent; +import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.event.dom.client.MouseDownEvent; import com.google.gwt.event.dom.client.MouseDownHandler; import com.google.gwt.event.dom.client.TouchStartEvent; @@ -39,6 +42,7 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.Event.NativePreviewHandler; import com.google.gwt.user.client.ui.CheckBox; +import com.vaadin.client.VConsole; import com.vaadin.client.WidgetUtil; import com.vaadin.client.renderers.ClickableRenderer; import com.vaadin.client.widget.grid.CellReference; @@ -81,7 +85,7 @@ public class MultiSelectionRenderer */ private final class CheckBoxEventHandler implements MouseDownHandler, TouchStartHandler, ClickHandler, - GridEnabledHandler, GridSelectionAllowedHandler { + GridEnabledHandler, GridSelectionAllowedHandler, KeyUpHandler { private final CheckBox checkBox; /** @@ -116,6 +120,17 @@ public class MultiSelectionRenderer event.stopPropagation(); } + @Override + public void onKeyUp(KeyUpEvent event) { + if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || !checkBox.isEnabled()) { + return; + } + int logicalRow = getLogicalRowIndex(grid, checkBox.getElement()); + setSelected(logicalRow, !isSelected(logicalRow)); + event.preventDefault(); + event.stopPropagation(); + } + @Override public void onEnabled(boolean enabled) { updateEnable(); @@ -608,11 +623,13 @@ public class MultiSelectionRenderer checkBox.sinkBitlessEvent(BrowserEvents.MOUSEDOWN); checkBox.sinkBitlessEvent(BrowserEvents.TOUCHSTART); checkBox.sinkBitlessEvent(BrowserEvents.CLICK); + checkBox.sinkBitlessEvent(BrowserEvents.KEYUP); // Add handlers checkBox.addMouseDownHandler(handler); checkBox.addTouchStartHandler(handler); checkBox.addClickHandler(handler); + checkBox.addKeyUpHandler(handler); grid.addEnabledHandler(handler); grid.addSelectionAllowedHandler(handler); diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java index ba29ec807f..87a29085fa 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java @@ -445,6 +445,24 @@ public class GridSelectionTest extends GridBasicsTest { } + @Test + public void spaceKeyOnSelectionCheckboxShouldToggleRowSelection() { + openTestURL(); + setSelectionModelMulti(); + + getSelectionCheckbox(1).sendKeys(Keys.SPACE); + assertSelected(1); + + getSelectionCheckbox(2).sendKeys(Keys.SPACE); + assertSelected(1, 2); + + getSelectionCheckbox(2).sendKeys(Keys.SPACE); + assertSelected(1); + + getSelectionCheckbox(1).sendKeys(Keys.SPACE); + assertSelected(); + } + private void assertSelected(Integer... selected) { GridElement grid = getGridElement(); HashSet expected = new HashSet( -- 2.39.5