Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionMenu falsy value bugfix #912

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
joshfarrant marked this conversation as resolved.
Show resolved Hide resolved
},
[handleOnSelect, toggleMenu, anchorElementRef],
)
Expand Down
Loading