From f7121436a7f3fbe22ff821ac435208a7fa064368 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Gueriaud <51313578+jcgueriaud1@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:23:06 +0300 Subject: [PATCH] feat: add setTabNavigation / isTabNavigation to MenuBar (#6554) --- .../flow/component/menubar/MenuBar.java | 22 +++++++++++++++++++ .../component/menubar/tests/MenuBarTest.java | 15 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/main/java/com/vaadin/flow/component/menubar/MenuBar.java b/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/main/java/com/vaadin/flow/component/menubar/MenuBar.java index 651c9d3b42d..9a12eba25b4 100644 --- a/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/main/java/com/vaadin/flow/component/menubar/MenuBar.java +++ b/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/main/java/com/vaadin/flow/component/menubar/MenuBar.java @@ -388,6 +388,28 @@ public boolean isReverseCollapseOrder() { return getElement().getProperty("reverseCollapse", false); } + /** + * Sets tab navigation for the menu bar. + * + * @param tabNavigation + * If {@code true}, the top-level menu items is traversable by + * tab instead of arrow keys (i.e. disabling roving tabindex) + */ + public void setTabNavigation(boolean tabNavigation) { + getElement().setProperty("tabNavigation", tabNavigation); + } + + /** + * Gets whether the menu bar uses tab navigation. + * + * @return {@code true} if the top-level menu items is traversable by tab + * instead of arrow keys (i.e. disabling roving tabindex) + * + */ + public boolean isTabNavigation() { + return getElement().getProperty("tabNavigation", false); + } + /** * Gets the internationalization object previously set for this component. *
diff --git a/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/test/java/com/vaadin/flow/component/menubar/tests/MenuBarTest.java b/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/test/java/com/vaadin/flow/component/menubar/tests/MenuBarTest.java index 17b431538b5..3f6c076f238 100644 --- a/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/test/java/com/vaadin/flow/component/menubar/tests/MenuBarTest.java +++ b/vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/test/java/com/vaadin/flow/component/menubar/tests/MenuBarTest.java @@ -108,6 +108,21 @@ public void setReverseCollapseOrder_isReverseCollapseOrder() { menuBar.getElement().getProperty("reverseCollapse", false)); } + @Test + public void isTabNavigation() { + Assert.assertFalse(menuBar.isTabNavigation()); + Assert.assertFalse( + menuBar.getElement().getProperty("tabNavigation", false)); + } + + @Test + public void setTabNavigation_isTabNavigation() { + menuBar.setTabNavigation(true); + Assert.assertTrue(menuBar.isTabNavigation()); + Assert.assertTrue( + menuBar.getElement().getProperty("tabNavigation", false)); + } + private void assertChildrenAndItems(MenuItem... expected) { Object[] menuItems = menuBar.getChildren().toArray(); Assert.assertArrayEquals(expected, menuItems);