Skip to content

Commit

Permalink
ActionMenu falsy value bugfix (#912)
Browse files Browse the repository at this point in the history
* allow falsey values to trigger item selection callback

* add a test to assert that falsey items can be selected

* add changeset
  • Loading branch information
joshfarrant authored Feb 3, 2025
1 parent d9a95b2 commit 5966728
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-years-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react-brand': patch
---

Fixed a bug in the `ActionMenu` component where items with falsy values (eg `""`) would not trigger the `onSelect` callback when selected.
19 changes: 19 additions & 0 deletions packages/react/src/ActionMenu/ActionMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,23 @@ describe('ActionMenu', () => {
{timeout: 100},
)
})

it('should allow falsey items to be selected', () => {
const mockOnSelect = jest.fn()

const {getByRole} = render(
<ActionMenu onSelect={mockOnSelect} selectionVariant="single">
<ActionMenu.Button>Open menu</ActionMenu.Button>
<ActionMenu.Overlay aria-label="Actions">
<ActionMenu.Item value="test">Test string</ActionMenu.Item>
<ActionMenu.Item value="">Empty string</ActionMenu.Item>
</ActionMenu.Overlay>
</ActionMenu>,
)

fireEvent.click(getByRole('button', {name: 'Open menu'}))
fireEvent.click(getByRole('menuitemradio', {name: 'Empty string'}))

expect(mockOnSelect).toHaveBeenCalledWith('')
})
})
8 changes: 3 additions & 5 deletions packages/react/src/ActionMenu/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,9 @@ const _ActionMenuRoot = memo(

const handleItemSelection = useCallback(
(newValue: string) => {
if (newValue) {
handleOnSelect(newValue)
toggleMenu()
anchorElementRef.current?.focus()
}
handleOnSelect(newValue)
toggleMenu()
anchorElementRef.current?.focus()
},
[handleOnSelect, toggleMenu, anchorElementRef],
)
Expand Down

0 comments on commit 5966728

Please sign in to comment.