Skip to content

Commit

Permalink
Fix submenu anchor closing menu on Enter press
Browse files Browse the repository at this point in the history
  • Loading branch information
iansan5653 committed Mar 28, 2024
1 parent 39568d4 commit d207bf7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
11 changes: 9 additions & 2 deletions packages/react/src/ActionMenu/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,15 @@ const Anchor = React.forwardRef<HTMLElement, ActionMenuAnchorProps>(({children,
const parentActionListContext = useContext(ActionListContainerContext)
const thisActionListContext = useMemo(
() =>
isSubmenu ? {...parentActionListContext, defaultTrailingVisual: <ChevronRightIcon />} : parentActionListContext,
[isSubmenu, parentActionListContext],
isSubmenu
? {
...parentActionListContext,
defaultTrailingVisual: <ChevronRightIcon />,
// Default behavior is to close after selecting; we want to open the submenu instead
afterSelect: () => onOpen?.('anchor-click'),
}
: parentActionListContext,
[isSubmenu, onOpen, parentActionListContext],
)

return (
Expand Down
20 changes: 10 additions & 10 deletions packages/react/src/__tests__/ActionMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ describe('ActionMenu', () => {

await user.keyboard('{ArrowRight}')
expect(component.queryByRole('menu')).not.toBeInTheDocument()
expect(baseAnchor).not.toHaveAttribute('aria-expanded')
expect(baseAnchor).not.toHaveAttribute('aria-expanded', 'true')
})

it('opens submenus on enter or right arrow key press', async () => {
Expand All @@ -544,15 +544,15 @@ describe('ActionMenu', () => {
await user.click(baseAnchor)

const submenuAnchor = component.getByRole('menuitem', {name: 'Paste special'})
expect(submenuAnchor).toHaveAttribute('aria-haspopup')
expect(submenuAnchor).toHaveAttribute('aria-haspopup', 'true')
submenuAnchor.focus()
await user.keyboard('{Enter}')
expect(submenuAnchor).toHaveAttribute('aria-expanded')
expect(submenuAnchor).toHaveAttribute('aria-expanded', 'true')

const subSubmenuAnchor = component.getByRole('menuitem', {name: 'Paste from'})
subSubmenuAnchor.focus()
await user.keyboard('{ArrowRight}')
expect(subSubmenuAnchor).toHaveAttribute('aria-expanded')
expect(subSubmenuAnchor).toHaveAttribute('aria-expanded', 'true')
})

it('closes top menu on escape or left arrow key press', async () => {
Expand All @@ -568,16 +568,16 @@ describe('ActionMenu', () => {
const subSubmenuAnchor = component.getByRole('menuitem', {name: 'Paste from'})
await user.click(subSubmenuAnchor)

expect(subSubmenuAnchor).toHaveAttribute('aria-expanded')
expect(subSubmenuAnchor).toHaveAttribute('aria-expanded', 'true')

await user.keyboard('{Escape}')
expect(subSubmenuAnchor).not.toHaveAttribute('aria-expanded')
expect(submenuAnchor).toHaveAttribute('aria-expanded')
expect(subSubmenuAnchor).not.toHaveAttribute('aria-expanded', 'true')
expect(submenuAnchor).toHaveAttribute('aria-expanded', 'true')

await user.keyboard('{ArrowLeft}')
expect(submenuAnchor).not.toHaveAttribute('aria-expanded')
expect(submenuAnchor).not.toHaveAttribute('aria-expanded', 'true')

expect(baseAnchor).toHaveAttribute('aria-expanded')
expect(baseAnchor).toHaveAttribute('aria-expanded', 'true')
})

it('closes all menus when an item is selected', async () => {
Expand All @@ -596,7 +596,7 @@ describe('ActionMenu', () => {
const subSubmenuItem = component.getByRole('menuitem', {name: 'Current clipboard'})
await user.click(subSubmenuItem)

expect(baseAnchor).not.toHaveAttribute('aria-expanded')
expect(baseAnchor).not.toHaveAttribute('aria-expanded', 'true')
})
})
})

0 comments on commit d207bf7

Please sign in to comment.