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/hungry-rockets-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add an ability to provide custom `aria-label` and `aria-labelledby` to `TreeView.Item`
24 changes: 24 additions & 0 deletions packages/react/src/TreeView/TreeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ describe('Markup', () => {
expect(items).toHaveLength(3)
})

it('uses treeitem aria label', () => {
const {queryAllByRole} = renderWithTheme(
<>
<TreeView>
<TreeView.Item id="item-1" aria-label="Test tree item 1">
Item 1
</TreeView.Item>
<TreeView.Item id="item-2" aria-labelledby="test-description">
Item 2
</TreeView.Item>
<TreeView.Item id="item-2">Item 3</TreeView.Item>
</TreeView>
<span id="test-description">Tree item 2 description</span>
</>,
)

const items = queryAllByRole('treeitem')
expect(items).toHaveLength(3)
expect(items[0]).toHaveAccessibleName('Test tree item 1')
expect(items[1]).toHaveAccessibleName('Tree item 2 description')
expect(items[2]).toHaveAttribute('aria-labelledby')
expect(items[2]).toHaveAccessibleName('Item 3')
})

it('hides subtrees by default', () => {
const {queryByRole} = renderWithTheme(
<TreeView aria-label="Test tree">
Expand Down
7 changes: 6 additions & 1 deletion packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ Root.displayName = 'TreeView'
// TreeView.Item

export type TreeViewItemProps = {
'aria-label'?: React.AriaAttributes['aria-label']
'aria-labelledby'?: React.AriaAttributes['aria-labelledby']
id: string
children: React.ReactNode
containIntrinsicSize?: string
Expand All @@ -375,6 +377,8 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
onSelect,
children,
className,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
},
ref,
) => {
Expand Down Expand Up @@ -472,7 +476,8 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
tabIndex={0}
id={itemId}
role="treeitem"
aria-labelledby={labelId}
aria-label={ariaLabel}
aria-labelledby={ariaLabel ? undefined : ariaLabelledby || labelId}
aria-describedby={`${leadingVisualId} ${trailingVisualId}`}
aria-level={level}
aria-expanded={isSubTreeEmpty ? undefined : isExpanded}
Expand Down