]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix space key on selection checkbox to select grid row (#10388)
authorMarco Collovati <mcollovati@gmail.com>
Tue, 9 Jan 2018 13:42:49 +0000 (14:42 +0100)
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Tue, 9 Jan 2018 13:42:49 +0000 (15:42 +0200)
Fixes #10221

client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java
uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java

index c9b4f0dac1ca6c3fad112827a17e503ad5bed491..58576d41c1805b36dfc01e82a26d6987f2f35818 100644 (file)
@@ -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<T>
      */
     private final class CheckBoxEventHandler
             implements MouseDownHandler, TouchStartHandler, ClickHandler,
-            GridEnabledHandler, GridSelectionAllowedHandler {
+            GridEnabledHandler, GridSelectionAllowedHandler, KeyUpHandler {
         private final CheckBox checkBox;
 
         /**
@@ -116,6 +120,17 @@ public class MultiSelectionRenderer<T>
             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<T>
         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);
 
index ba29ec807fe083bf360c8dc11dc29c9b46d0d880..87a29085fa459e092afe14bbea9131e1987c76b7 100644 (file)
@@ -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<Integer> expected = new HashSet<Integer>(