Skip to content

Commit 3779dbf

Browse files
authored
ActionList: Prevent double key event when <button> is used (#4772)
* Prevent double key event when `<button>` is used * Add changeset * Add test
1 parent d6ca0cf commit 3779dbf

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.changeset/swift-peaches-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
(Behind feature flag) ActionList: Fix issue where triggering a keyboard event was possible when using the `onSelect` prop

packages/react/src/ActionList/ActionList.test.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ describe('ActionList', () => {
492492

493493
it('should render ActionList.Item as li when feature flag is enabled and has proper aria role', async () => {
494494
const {container} = HTMLRender(
495-
<FeatureFlags flags={{primer_react_action_list_item_as_button: false}}>
495+
<FeatureFlags flags={{primer_react_action_list_item_as_button: true}}>
496496
<ActionList role="listbox">
497497
<ActionList.Item role="option">Item 1</ActionList.Item>
498498
<ActionList.Item role="option">Item 2</ActionList.Item>
@@ -570,4 +570,24 @@ describe('ActionList', () => {
570570
await userEvent.tab()
571571
expect(document.activeElement).toHaveAccessibleName('Action')
572572
})
573+
574+
it('should only trigger a key event once when feature flag is enabled', async () => {
575+
const mockOnSelect = jest.fn()
576+
const user = userEvent.setup()
577+
const {getByRole} = HTMLRender(
578+
<FeatureFlags flags={{primer_react_action_list_item_as_button: true}}>
579+
<ActionList>
580+
<ActionList.Item onSelect={mockOnSelect}>Item 1</ActionList.Item>
581+
</ActionList>
582+
</FeatureFlags>,
583+
)
584+
const item = getByRole('button')
585+
586+
item.focus()
587+
588+
expect(document.activeElement).toBe(item)
589+
await user.keyboard('{Enter}')
590+
591+
expect(mockOnSelect).toHaveBeenCalledTimes(1)
592+
})
573593
})

packages/react/src/ActionList/Item.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>(
289289

290290
const menuItemProps = {
291291
onClick: clickHandler,
292-
onKeyPress: keyPressHandler,
292+
onKeyPress: !buttonSemantics ? keyPressHandler : undefined,
293293
'aria-disabled': disabled ? true : undefined,
294294
'data-inactive': inactive ? true : undefined,
295295
'data-loading': loading && !inactive ? true : undefined,

0 commit comments

Comments
 (0)