Skip to content

Commit

Permalink
fix(Treevieew):do not add aria-describedby attribute when empty leadi…
Browse files Browse the repository at this point in the history
…ng/trailing visual (#5196)

* fix(Treevieew):do not add aria-describedby attribute when empty leading/trailing visual

* Create tender-dodos-walk.md
  • Loading branch information
francinelucca authored Nov 5, 2024
1 parent 33c5086 commit 49cbff2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tender-dodos-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

fix(Treevieew):do not add aria-describedby attribute when empty leading/trailing visual
13 changes: 13 additions & 0 deletions packages/react/src/TreeView/TreeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ describe('Markup', () => {
expect(noDescription).toHaveAccessibleDescription(' ')
})

it('should not have aria-describedby when no leading or trailing visual', () => {
const {getByLabelText} = renderWithTheme(
<TreeView aria-label="Test tree">
<TreeView.Item id="item-1">Item 1</TreeView.Item>
<TreeView.Item id="item-2">Item 2</TreeView.Item>
</TreeView>,
)

const noDescription = getByLabelText(/Item 1/)
expect(noDescription).not.toHaveAccessibleDescription()
expect(noDescription).not.toHaveAttribute('aria-describedby')
})

it('should include `aria-expanded` when a SubTree contains content', async () => {
const user = userEvent.setup({
advanceTimers: jest.advanceTimersByTime,
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 @@ -458,6 +458,11 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
[onSelect, setIsExpandedWithCache, toggle],
)

const ariaDescribedByIds = [
slots.leadingVisual ? leadingVisualId : null,
slots.trailingVisual ? trailingVisualId : null,
].filter(Boolean)

return (
<ItemContext.Provider
value={{
Expand All @@ -480,7 +485,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
role="treeitem"
aria-label={ariaLabel}
aria-labelledby={ariaLabel ? undefined : ariaLabelledby || labelId}
aria-describedby={`${leadingVisualId} ${trailingVisualId}`}
aria-describedby={ariaDescribedByIds.length ? ariaDescribedByIds.join(' ') : undefined}
aria-level={level}
aria-expanded={(isSubTreeEmpty && (!isExpanded || !hasSubTree)) || expanded === null ? undefined : isExpanded}
aria-current={isCurrentItem ? 'true' : undefined}
Expand Down

0 comments on commit 49cbff2

Please sign in to comment.