Skip to content
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/early-ghosts-enter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add support to ActionList for 'tablist' and 'tab' roles
27 changes: 27 additions & 0 deletions packages/react/src/ActionList/Item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,31 @@ describe('ActionList.Item', () => {
expect(item).toHaveTextContent('Item, Description')
expect(item).toHaveAccessibleDescription('Description')
})

it('should add role="tab" when ActionList has role="tablist"', () => {
const {getAllByRole} = HTMLRender(
<ActionList role="tablist">
<ActionList.Item>Tab 1</ActionList.Item>
<ActionList.Item>Tab 2</ActionList.Item>
<ActionList.Item>Tab 3</ActionList.Item>
</ActionList>,
)
const tabs = getAllByRole('tab')
expect(tabs[0]).toBeInTheDocument()
expect(tabs).toHaveLength(3)
})

it('should allow role="tab" on the li element', () => {
const {getAllByRole} = HTMLRender(
<ActionList role="tablist">
<ActionList.Item role="tab">Tab 1</ActionList.Item>
<ActionList.Item role="tab">Tab 2</ActionList.Item>
<ActionList.Item role="tab">Tab 3</ActionList.Item>
</ActionList>,
)
const tabs = getAllByRole('tab')
expect(tabs[0]).toBeInTheDocument()
expect(tabs[0].nodeType).toBe(Node.ELEMENT_NODE)
expect(tabs).toHaveLength(3)
})
})
8 changes: 7 additions & 1 deletion packages/react/src/ActionList/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const UnwrappedItem = <As extends React.ElementType = 'li'>(
else inferredItemRole = 'menuitem'
} else if (listRole === 'listbox') {
if (selectionVariant !== undefined && !role) inferredItemRole = 'option'
} else if (listRole === 'tablist') {
inferredItemRole = 'tab'
}

const itemRole = role || inferredItemRole
Expand All @@ -142,7 +144,11 @@ const UnwrappedItem = <As extends React.ElementType = 'li'>(
const itemSelectionAttribute = selectionAttribute || inferredSelectionAttribute
// Ensures ActionList.Item retains list item semantics if a valid ARIA role is applied, or if item is inactive
const listItemSemantics =
role === 'option' || role === 'menuitem' || role === 'menuitemradio' || role === 'menuitemcheckbox'
itemRole === 'option' ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just seemed like a bug. We weren't using the inferredItemRole when checking if the item is a list item

itemRole === 'menuitem' ||
itemRole === 'menuitemradio' ||
itemRole === 'menuitemcheckbox' ||
itemRole === 'tab'

const listRoleTypes = ['listbox', 'menu', 'list']
const listSemantics = (listRole && listRoleTypes.includes(listRole)) || inactive || listItemSemantics
Expand Down
Loading