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/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 ? undefined : isExpanded}
aria-current={isCurrentItem ? 'true' : undefined}
Expand Down
Loading