-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24d1912
commit 38f1494
Showing
27 changed files
with
496 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 47 additions & 49 deletions
96
components/react/src/components/stories/tree-view.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,58 @@ | ||
import { createTreeCollection } from '@ark-ui/react/tree-view' | ||
import type { Meta } from '@storybook/react' | ||
import { TreeView, type TreeViewData } from '~/components/ui/tree-view' | ||
import { TreeView } from '~/components/ui/tree-view' | ||
|
||
const meta: Meta = { | ||
title: 'Components/Tree View', | ||
} | ||
|
||
export default meta | ||
|
||
export const Base = () => { | ||
return <TreeView data={data} maxW="2xs" /> | ||
interface Node { | ||
id: string | ||
name: string | ||
children?: Node[] | ||
} | ||
|
||
const data: TreeViewData = { | ||
label: 'Root', | ||
children: [ | ||
{ | ||
value: '1', | ||
name: 'Item 1', | ||
children: [ | ||
{ | ||
value: '1.1', | ||
name: 'Item 1.1', | ||
}, | ||
{ | ||
value: '1.2', | ||
name: 'Item 1.2', | ||
children: [ | ||
{ | ||
value: '1.2.1', | ||
name: 'Item 1.2.1', | ||
}, | ||
{ | ||
value: '1.2.2', | ||
name: 'Item 1.2.2', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
value: '2', | ||
name: 'Item 2', | ||
children: [ | ||
{ | ||
value: '2.1', | ||
name: 'Item 2.1', | ||
}, | ||
{ | ||
value: '2.2', | ||
name: 'Item 2.2', | ||
}, | ||
], | ||
}, | ||
{ | ||
value: '3', | ||
name: 'Item 3', | ||
}, | ||
], | ||
const collection = createTreeCollection<Node>({ | ||
nodeToValue: (node) => node.id, | ||
nodeToString: (node) => node.name, | ||
rootNode: { | ||
id: 'ROOT', | ||
name: '', | ||
children: [ | ||
{ | ||
id: 'node_modules', | ||
name: 'node_modules', | ||
children: [ | ||
{ id: 'node_modules/zag-js', name: 'zag-js' }, | ||
{ id: 'node_modules/pandacss', name: 'panda' }, | ||
{ | ||
id: 'node_modules/@types', | ||
name: '@types', | ||
children: [ | ||
{ id: 'node_modules/@types/react', name: 'react' }, | ||
{ id: 'node_modules/@types/react-dom', name: 'react-dom' }, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'src', | ||
name: 'src', | ||
children: [ | ||
{ id: 'src/app.tsx', name: 'app.tsx' }, | ||
{ id: 'src/index.ts', name: 'index.ts' }, | ||
], | ||
}, | ||
{ id: 'panda.config', name: 'panda.config.ts' }, | ||
{ id: 'package.json', name: 'package.json' }, | ||
{ id: 'renovate.json', name: 'renovate.json' }, | ||
{ id: 'readme.md', name: 'README.md' }, | ||
], | ||
}, | ||
}) | ||
|
||
export const Base = () => { | ||
return <TreeView collection={collection} maxW="2xs" /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,56 @@ | ||
'use client' | ||
import { CheckSquareIcon, ChevronRightIcon, FileIcon, FolderIcon } from 'lucide-react' | ||
import { forwardRef } from 'react' | ||
import * as StyledTreeView from './styled/tree-view' | ||
|
||
interface Child { | ||
value: string | ||
name: string | ||
children?: Child[] | ||
} | ||
|
||
export interface TreeViewData { | ||
label: string | ||
children: Child[] | ||
} | ||
|
||
export interface TreeViewProps extends StyledTreeView.RootProps { | ||
data: TreeViewData | ||
} | ||
|
||
export const TreeView = forwardRef<HTMLDivElement, TreeViewProps>((props, ref) => { | ||
const { data, ...rootProps } = props | ||
|
||
const renderChild = (child: Child) => ( | ||
<StyledTreeView.Branch key={child.value} value={child.value}> | ||
<StyledTreeView.BranchControl> | ||
<StyledTreeView.BranchIndicator> | ||
<ChevronRightIcon /> | ||
</StyledTreeView.BranchIndicator> | ||
<StyledTreeView.BranchText>{child.name}</StyledTreeView.BranchText> | ||
</StyledTreeView.BranchControl> | ||
<StyledTreeView.BranchContent> | ||
{child.children?.map((child) => | ||
child.children ? ( | ||
renderChild(child) | ||
) : ( | ||
<StyledTreeView.Item key={child.value} value={child.value}> | ||
<StyledTreeView.ItemText>{child.name}</StyledTreeView.ItemText> | ||
</StyledTreeView.Item> | ||
), | ||
)} | ||
</StyledTreeView.BranchContent> | ||
</StyledTreeView.Branch> | ||
) | ||
|
||
export const TreeView = forwardRef<HTMLDivElement, StyledTreeView.RootProps>((props, ref) => { | ||
return ( | ||
<StyledTreeView.Root ref={ref} aria-label={data.label} {...rootProps}> | ||
<StyledTreeView.Tree>{data.children.map(renderChild)}</StyledTreeView.Tree> | ||
<StyledTreeView.Root ref={ref} {...props}> | ||
<StyledTreeView.Tree> | ||
{/* @ts-expect-error */} | ||
{props.collection.rootNode.children.map((node, index) => ( | ||
<TreeNode key={node.id} node={node} indexPath={[index]} /> | ||
))} | ||
</StyledTreeView.Tree> | ||
</StyledTreeView.Root> | ||
) | ||
}) | ||
|
||
TreeView.displayName = 'TreeView' | ||
|
||
const ChevronRightIcon = () => ( | ||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
<title>Chevron Right Icon</title> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth="2" | ||
d="m9 18l6-6l-6-6" | ||
/> | ||
</svg> | ||
) | ||
const TreeNode = (props: StyledTreeView.NodeProviderProps) => { | ||
const { node, indexPath } = props | ||
return ( | ||
<StyledTreeView.NodeProvider key={node.id} node={node} indexPath={indexPath}> | ||
{node.children ? ( | ||
<StyledTreeView.Branch> | ||
<StyledTreeView.BranchControl> | ||
<StyledTreeView.BranchText> | ||
<FolderIcon /> {node.name} | ||
</StyledTreeView.BranchText> | ||
<StyledTreeView.BranchIndicator> | ||
<ChevronRightIcon /> | ||
</StyledTreeView.BranchIndicator> | ||
</StyledTreeView.BranchControl> | ||
<StyledTreeView.BranchContent> | ||
<StyledTreeView.BranchIndentGuide /> | ||
{/* @ts-expect-error */} | ||
{node.children.map((child, index) => ( | ||
<TreeNode key={child.id} node={child} indexPath={[...indexPath, index]} /> | ||
))} | ||
</StyledTreeView.BranchContent> | ||
</StyledTreeView.Branch> | ||
) : ( | ||
<StyledTreeView.Item> | ||
<StyledTreeView.ItemIndicator> | ||
<CheckSquareIcon /> | ||
</StyledTreeView.ItemIndicator> | ||
<StyledTreeView.ItemText> | ||
<FileIcon /> | ||
{node.name} | ||
</StyledTreeView.ItemText> | ||
</StyledTreeView.Item> | ||
)} | ||
</StyledTreeView.NodeProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.