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: Preserve expanded state of nested items #2538

Merged
merged 15 commits into from
Nov 10, 2022
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
16 changes: 16 additions & 0 deletions .changeset/sour-readers-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@primer/react": patch
---

TreeView: Preserve expanded state of nested items when parent item is collapsed.

**Breaking change**

`TreeView.Item` and `TreeView.LinkItem` now require an `id` prop:

```diff
- <TreeView.Item>...</TreeView.Item>
+ <TreeView.Item id="unqiue-id">...</TreeView.Item>
```

This is not in a major release because TreeView is still a `draft` component.
46 changes: 27 additions & 19 deletions docs/content/TreeView.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ description: A hierarchical list of items where nested items can be expanded and
<Box sx={{maxWidth: 400}}>
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView.Item>
<TreeView.Item id="src">
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
src
<TreeView.SubTree>
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="src/Avatar.tsx" href="#">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Expand All @@ -28,7 +28,7 @@ description: A hierarchical list of items where nested items can be expanded and
<StyledOcticon icon={DiffAddedIcon} color="success.fg" aria-label="added" />
</TreeView.TrailingVisual>
</TreeView.LinkItem>
<TreeView.LinkItem href="#" current>
<TreeView.LinkItem id="src/Button.tsx" href="#" current>
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Expand All @@ -39,7 +39,7 @@ description: A hierarchical list of items where nested items can be expanded and
</TreeView.LinkItem>
</TreeView.SubTree>
</TreeView.Item>
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="package.json" href="#">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Expand All @@ -59,31 +59,31 @@ description: A hierarchical list of items where nested items can be expanded and
<Box sx={{maxWidth: 400}}>
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="src" href="#">
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
src
<TreeView.SubTree>
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="src/Avatar.tsx" href="#">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Avatar.tsx
</TreeView.LinkItem>
<TreeView.LinkItem href="#" current>
<TreeView.LinkItem id="src/Button" href="#" current>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
Button
<TreeView.SubTree>
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="src/Button/Button.tsx" href="#">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Button.tsx
</TreeView.LinkItem>
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="src/Button/Button.test.tsx" href="#">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Expand All @@ -93,7 +93,7 @@ description: A hierarchical list of items where nested items can be expanded and
</TreeView.LinkItem>
</TreeView.SubTree>
</TreeView.LinkItem>
<TreeView.LinkItem href="#">
<TreeView.LinkItem id="package.json" href="#">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Expand All @@ -115,11 +115,13 @@ function ControlledTreeView() {
<Button onClick={() => setExpanded(!expanded)}>{expanded ? 'Collapse' : 'Expand'}</Button>
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView.Item expanded={expanded} onExpandedChange={setExpanded}>
<TreeView.Item id="src" expanded={expanded} onExpandedChange={setExpanded}>
src
<TreeView.SubTree>
<TreeView.LinkItem href="#">Avatar.tsx</TreeView.LinkItem>
<TreeView.LinkItem href="#" current>
<TreeView.LinkItem id="src/Avatar.tsx" href="#">
Avatar.tsx
</TreeView.LinkItem>
<TreeView.LinkItem id="src/Button.tsx" href="#" current>
Button.tsx
</TreeView.LinkItem>
</TreeView.SubTree>
Expand All @@ -141,14 +143,16 @@ To render stateful visuals, pass a render function to `TreeView.LeadingVisual` o
<Box sx={{maxWidth: 400}}>
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView.Item>
<TreeView.Item id="src">
<TreeView.LeadingVisual>
{({isExpanded}) => (isExpanded ? <FileDirectoryOpenFillIcon /> : <FileDirectoryFillIcon />)}
</TreeView.LeadingVisual>
src
<TreeView.SubTree>
<TreeView.LinkItem href="#">Avatar.tsx</TreeView.LinkItem>
<TreeView.LinkItem href="#" current>
<TreeView.LinkItem id="src/Avatar.tsx" href="#">
Avatar.tsx
</TreeView.LinkItem>
<TreeView.LinkItem id="src/Button.tsx" href="#" current>
Button.tsx
</TreeView.LinkItem>
</TreeView.SubTree>
Expand All @@ -164,14 +168,16 @@ Since stateful directory icons are a common use case for TreeView, we provide a
<Box sx={{maxWidth: 400}}>
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView.Item>
<TreeView.Item id="src">
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
src
<TreeView.SubTree>
<TreeView.LinkItem href="#">Avatar.tsx</TreeView.LinkItem>
<TreeView.LinkItem href="#" current>
<TreeView.LinkItem id="src/Avatar.tsx" href="#">
Avatar.tsx
</TreeView.LinkItem>
<TreeView.LinkItem id="src/Button.tsx" href="#" current>
Button.tsx
</TreeView.LinkItem>
</TreeView.SubTree>
Expand All @@ -197,6 +203,7 @@ See [Storybook](https://primer.style/react/storybook?path=/story/components-tree
### TreeView.Item

<PropsTable>
<PropsTableRow name="id" type="string" required description="Unique identifier for the item." />
<PropsTableRow name="children" type="React.ReactNode" required />
<PropsTableRow
name="current"
Expand Down Expand Up @@ -230,6 +237,7 @@ See [Storybook](https://primer.style/react/storybook?path=/story/components-tree
### TreeView.LinkItem

<PropsTable>
<PropsTableRow name="id" type="string" required description="Unique identifier for the item." />
<PropsTableRow name="children" type="React.ReactNode" required />
<PropsTableRow
name="href"
Expand Down
Loading