Skip to content

Commit 5966728

Browse files
authored
ActionMenu falsy value bugfix (#912)
* allow falsey values to trigger item selection callback * add a test to assert that falsey items can be selected * add changeset
1 parent d9a95b2 commit 5966728

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

.changeset/kind-years-repair.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react-brand': patch
3+
---
4+
5+
Fixed a bug in the `ActionMenu` component where items with falsy values (eg `""`) would not trigger the `onSelect` callback when selected.

packages/react/src/ActionMenu/ActionMenu.test.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,23 @@ describe('ActionMenu', () => {
349349
{timeout: 100},
350350
)
351351
})
352+
353+
it('should allow falsey items to be selected', () => {
354+
const mockOnSelect = jest.fn()
355+
356+
const {getByRole} = render(
357+
<ActionMenu onSelect={mockOnSelect} selectionVariant="single">
358+
<ActionMenu.Button>Open menu</ActionMenu.Button>
359+
<ActionMenu.Overlay aria-label="Actions">
360+
<ActionMenu.Item value="test">Test string</ActionMenu.Item>
361+
<ActionMenu.Item value="">Empty string</ActionMenu.Item>
362+
</ActionMenu.Overlay>
363+
</ActionMenu>,
364+
)
365+
366+
fireEvent.click(getByRole('button', {name: 'Open menu'}))
367+
fireEvent.click(getByRole('menuitemradio', {name: 'Empty string'}))
368+
369+
expect(mockOnSelect).toHaveBeenCalledWith('')
370+
})
352371
})

packages/react/src/ActionMenu/ActionMenu.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,9 @@ const _ActionMenuRoot = memo(
166166

167167
const handleItemSelection = useCallback(
168168
(newValue: string) => {
169-
if (newValue) {
170-
handleOnSelect(newValue)
171-
toggleMenu()
172-
anchorElementRef.current?.focus()
173-
}
169+
handleOnSelect(newValue)
170+
toggleMenu()
171+
anchorElementRef.current?.focus()
174172
},
175173
[handleOnSelect, toggleMenu, anchorElementRef],
176174
)

0 commit comments

Comments
 (0)