Skip to content

Commit

Permalink
ActionList: Prevent double key event when <button> is used (#4772)
Browse files Browse the repository at this point in the history
* Prevent double key event when `<button>` is used

* Add changeset

* Add test
  • Loading branch information
TylerJDev authored Jul 25, 2024
1 parent d6ca0cf commit 3779dbf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-peaches-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

(Behind feature flag) ActionList: Fix issue where triggering a keyboard event was possible when using the `onSelect` prop
22 changes: 21 additions & 1 deletion packages/react/src/ActionList/ActionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ describe('ActionList', () => {

it('should render ActionList.Item as li when feature flag is enabled and has proper aria role', async () => {
const {container} = HTMLRender(
<FeatureFlags flags={{primer_react_action_list_item_as_button: false}}>
<FeatureFlags flags={{primer_react_action_list_item_as_button: true}}>
<ActionList role="listbox">
<ActionList.Item role="option">Item 1</ActionList.Item>
<ActionList.Item role="option">Item 2</ActionList.Item>
Expand Down Expand Up @@ -570,4 +570,24 @@ describe('ActionList', () => {
await userEvent.tab()
expect(document.activeElement).toHaveAccessibleName('Action')
})

it('should only trigger a key event once when feature flag is enabled', async () => {
const mockOnSelect = jest.fn()
const user = userEvent.setup()
const {getByRole} = HTMLRender(
<FeatureFlags flags={{primer_react_action_list_item_as_button: true}}>
<ActionList>
<ActionList.Item onSelect={mockOnSelect}>Item 1</ActionList.Item>
</ActionList>
</FeatureFlags>,
)
const item = getByRole('button')

item.focus()

expect(document.activeElement).toBe(item)
await user.keyboard('{Enter}')

expect(mockOnSelect).toHaveBeenCalledTimes(1)
})
})
2 changes: 1 addition & 1 deletion packages/react/src/ActionList/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>(

const menuItemProps = {
onClick: clickHandler,
onKeyPress: keyPressHandler,
onKeyPress: !buttonSemantics ? keyPressHandler : undefined,
'aria-disabled': disabled ? true : undefined,
'data-inactive': inactive ? true : undefined,
'data-loading': loading && !inactive ? true : undefined,
Expand Down

0 comments on commit 3779dbf

Please sign in to comment.