Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeView: Add indication of empty directory #5168

Merged
merged 8 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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/lovely-shirts-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

TreeView: Adds indication of no nodes in a tree item, allows for `aria-expanded even if the item is empty.
9 changes: 5 additions & 4 deletions packages/react/src/TreeView/TreeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ describe('Markup', () => {
expect(treeitem).toHaveAttribute('aria-expanded', 'true')

treeitem = getByLabelText(/Item 2/)
expect(treeitem).not.toHaveAttribute('aria-expanded')
expect(treeitem).toHaveAttribute('aria-expanded', 'false')

await user.click(getByText(/Item 2/))
expect(treeitem).not.toHaveAttribute('aria-expanded')
expect(treeitem).toHaveAttribute('aria-expanded', 'true')
})

it('should render with containIntrinsicSize', () => {
Expand Down Expand Up @@ -1537,7 +1537,7 @@ describe('Asyncronous loading', () => {
expect(parentItem).toHaveAttribute('aria-expanded', 'true')
})

it('should remove `aria-expanded` if no content is loaded in', async () => {
it('should update `aria-expanded` if no content is loaded in', async () => {
function Example() {
const [state, setState] = React.useState<SubTreeState>('loading')
const timeoutId = React.useRef<ReturnType<typeof setTimeout> | null>(null)
Expand Down Expand Up @@ -1584,6 +1584,7 @@ describe('Asyncronous loading', () => {
jest.runAllTimers()
})

expect(treeitem).not.toHaveAttribute('aria-expanded')
expect(treeitem).toHaveAttribute('aria-expanded', 'true')
expect(getByLabelText('No items found')).toBeInTheDocument()
})
})
11 changes: 10 additions & 1 deletion packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
aria-labelledby={ariaLabel ? undefined : ariaLabelledby || labelId}
aria-describedby={`${leadingVisualId} ${trailingVisualId}`}
aria-level={level}
aria-expanded={isSubTreeEmpty ? undefined : isExpanded}
aria-expanded={isExpanded}
aria-current={isCurrentItem ? 'true' : undefined}
aria-selected={isFocused ? 'true' : 'false'}
data-has-leading-action={slots.leadingAction ? true : undefined}
Expand Down Expand Up @@ -697,6 +697,7 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
ref={ref}
>
{state === 'loading' ? <LoadingItem ref={loadingItemRef} count={count} /> : children}
{isSubTreeEmpty && state !== 'loading' ? <EmptyItem /> : null}
</ul>
)
}
Expand Down Expand Up @@ -785,6 +786,14 @@ const LoadingItem = React.forwardRef<HTMLElement, LoadingItemProps>(({count}, re
)
})

const EmptyItem = React.forwardRef<HTMLElement>((props, ref) => {
return (
<Item id={useId()} ref={ref}>
<Text sx={{color: 'fg.muted'}}>No items found</Text>
</Item>
)
})

function useSubTree(children: React.ReactNode) {
return React.useMemo(() => {
const subTree = React.Children.toArray(children).find(
Expand Down
Loading