diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/FocusUtil.java | 23 | ||||
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/VWindow.java | 10 |
2 files changed, 32 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/FocusUtil.java b/client/src/main/java/com/vaadin/client/ui/FocusUtil.java index 16b3f68456..9a0a4ecea4 100644 --- a/client/src/main/java/com/vaadin/client/ui/FocusUtil.java +++ b/client/src/main/java/com/vaadin/client/ui/FocusUtil.java @@ -15,6 +15,7 @@ */ package com.vaadin.client.ui; +import com.google.gwt.dom.client.Element; import com.google.gwt.user.client.ui.Focusable; import com.google.gwt.user.client.ui.Widget; @@ -94,4 +95,26 @@ public class FocusUtil { return focusable.getElement().getTabIndex(); } + + public static native Element[] getFocusableChildren(Element parent) + /*-{ + var focusableChildren = parent.querySelectorAll('[type][tabindex]:not([tabindex="-1"]), [role=button][tabindex]:not([tabindex="-1"])'); + return focusableChildren; + }-*/; + + public static void focusOnFirstFocusableElement(Element parent) + { + Element[] focusableChildren = getFocusableChildren(parent); + if (focusableChildren.length > 0) { + focusableChildren[0].focus(); + } + } + + public static void focusOnLastFocusableElement(Element parent) + { + Element[] focusableChildren = getFocusableChildren(parent); + if (focusableChildren.length > 0) { + focusableChildren[focusableChildren.length - 1].focus(); + } + } } diff --git a/client/src/main/java/com/vaadin/client/ui/VWindow.java b/client/src/main/java/com/vaadin/client/ui/VWindow.java index e7618ed59b..e53997ec17 100644 --- a/client/src/main/java/com/vaadin/client/ui/VWindow.java +++ b/client/src/main/java/com/vaadin/client/ui/VWindow.java @@ -429,14 +429,18 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, Roles.getDialogRole().setAriaLabelledbyProperty(getElement(), Id.of(headerText)); - // Handlers to Prevent tab to leave the window + // Handlers to Prevent tab to leave the window (by circulating focus) // and backspace to cause browser navigation topEventBlocker = event -> { + if (!getElement().isOrHasChild(WidgetUtil.getFocusedElement())) { + return; + } NativeEvent nativeEvent = event.getNativeEvent(); if (nativeEvent.getEventTarget().cast() == topTabStop && nativeEvent.getKeyCode() == KeyCodes.KEY_TAB && nativeEvent.getShiftKey()) { nativeEvent.preventDefault(); + FocusUtil.focusOnLastFocusableElement(this.getElement()); } if (nativeEvent.getEventTarget().cast() == topTabStop && nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) { @@ -445,11 +449,15 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, }; bottomEventBlocker = event -> { + if (!getElement().isOrHasChild(WidgetUtil.getFocusedElement())) { + return; + } NativeEvent nativeEvent = event.getNativeEvent(); if (nativeEvent.getEventTarget().cast() == bottomTabStop && nativeEvent.getKeyCode() == KeyCodes.KEY_TAB && !nativeEvent.getShiftKey()) { nativeEvent.preventDefault(); + FocusUtil.focusOnFirstFocusableElement(this.getElement()); } if (nativeEvent.getEventTarget().cast() == bottomTabStop && nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) { |