Skip to content

Commit 34e7108

Browse files
adierkensjoshblack
andauthored
Add support to ActionList and ActionList.Item for tablist and tab roles (#7109)
Co-authored-by: Josh Black <joshblack@github.com>
1 parent 0b559f0 commit 34e7108

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.changeset/early-ghosts-enter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
Add support to ActionList for 'tablist' and 'tab' roles

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,31 @@ describe('ActionList.Item', () => {
354354
expect(item).toHaveTextContent('Item, Description')
355355
expect(item).toHaveAccessibleDescription('Description')
356356
})
357+
358+
it('should add role="tab" when ActionList has role="tablist"', () => {
359+
const {getAllByRole} = HTMLRender(
360+
<ActionList role="tablist">
361+
<ActionList.Item>Tab 1</ActionList.Item>
362+
<ActionList.Item>Tab 2</ActionList.Item>
363+
<ActionList.Item>Tab 3</ActionList.Item>
364+
</ActionList>,
365+
)
366+
const tabs = getAllByRole('tab')
367+
expect(tabs[0]).toBeInTheDocument()
368+
expect(tabs).toHaveLength(3)
369+
})
370+
371+
it('should allow role="tab" on the li element', () => {
372+
const {getAllByRole} = HTMLRender(
373+
<ActionList role="tablist">
374+
<ActionList.Item role="tab">Tab 1</ActionList.Item>
375+
<ActionList.Item role="tab">Tab 2</ActionList.Item>
376+
<ActionList.Item role="tab">Tab 3</ActionList.Item>
377+
</ActionList>,
378+
)
379+
const tabs = getAllByRole('tab')
380+
expect(tabs[0]).toBeInTheDocument()
381+
expect(tabs[0].nodeType).toBe(Node.ELEMENT_NODE)
382+
expect(tabs).toHaveLength(3)
383+
})
357384
})

packages/react/src/ActionList/Item.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ const UnwrappedItem = <As extends React.ElementType = 'li'>(
123123
else inferredItemRole = 'menuitem'
124124
} else if (listRole === 'listbox') {
125125
if (selectionVariant !== undefined && !role) inferredItemRole = 'option'
126+
} else if (listRole === 'tablist') {
127+
inferredItemRole = 'tab'
126128
}
127129

128130
const itemRole = role || inferredItemRole
@@ -142,7 +144,11 @@ const UnwrappedItem = <As extends React.ElementType = 'li'>(
142144
const itemSelectionAttribute = selectionAttribute || inferredSelectionAttribute
143145
// Ensures ActionList.Item retains list item semantics if a valid ARIA role is applied, or if item is inactive
144146
const listItemSemantics =
145-
role === 'option' || role === 'menuitem' || role === 'menuitemradio' || role === 'menuitemcheckbox'
147+
itemRole === 'option' ||
148+
itemRole === 'menuitem' ||
149+
itemRole === 'menuitemradio' ||
150+
itemRole === 'menuitemcheckbox' ||
151+
itemRole === 'tab'
146152

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

0 commit comments

Comments
 (0)