Skip to content

Commit

Permalink
fix(menu): ensure submenu closes when enter key is pressed
Browse files Browse the repository at this point in the history
This fix ensures that when a menuitem with a href has a key press of enter, that the broswer
navigates to the href and closes the submenu

fixes #4792
  • Loading branch information
DipperTheDan committed Sep 29, 2022
1 parent c5b005e commit 04f5045
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/menu/__internal__/submenu/submenu.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ const Submenu = React.forwardRef(
setCharacterString("");
}

if (Events.isEnterKey(event)) {
/* timeout enforces that the "closeSubmenu" method will be run after
the browser navigates to the specified href of the menu-item. */
setTimeout(() => closeSubmenu());
}

if (href && index === undefined) {
if (
Events.isEnterKey(event) ||
Expand Down
34 changes: 34 additions & 0 deletions src/components/menu/__internal__/submenu/submenu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,40 @@ describe("Submenu component", () => {
).toBeFocused();
});
});

describe("when enter key pressed", () => {
it("it should navigate to href and close submenu", () => {
jest.useFakeTimers();
document.dispatchEvent(tabKey);
wrapper.update();

expect(document.activeElement).toMatchObject(
wrapper
.find(StyledSubmenu)
.find(StyledMenuItemWrapper)
.at(1)
.find("a")
);

act(() => {
wrapper
.find(StyledMenuItemWrapper)
.at(1)
.props()
.onKeyDown(events.enter);
});

wrapper.update();

act(() => {
jest.runAllTimers();
});

wrapper.update();

expect(wrapper.find(StyledSubmenu).exists()).toEqual(false);
});
});
});
});

Expand Down

0 comments on commit 04f5045

Please sign in to comment.