Skip to content

Commit

Permalink
Merge a437190 into 9b63299
Browse files Browse the repository at this point in the history
  • Loading branch information
JelloBagel committed May 21, 2024
2 parents 9b63299 + a437190 commit 6c85cb8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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 custom aria label 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

0 comments on commit 6c85cb8

Please sign in to comment.