Tree
A hierarchy whose branches open and close, for a file tree or an outline. The keyboard follows the WAI-ARIA tree pattern.
Import
Copy code
import { Tree } from '@k8ordo/ui';Usage
Pass the nodes as a tree of { id, label, icon?, children? }. Up and Down move between the visible nodes. Right opens a closed branch, and on an open one moves to its first child. Left closes an open branch, or moves to the parent. Home and End go to the first and last node, and Enter or Space selects. Typing a character moves to the next node starting with it.
- src
- components
- index.ts
- package.json
- README.md
Copy code
<Tree
defaultExpandedIds={['src']}
items={[
{
id: 'src',
label: 'src',
children: [
{
id: 'components',
label: 'components',
children: [{ id: 'button', label: 'button.tsx' }],
},
{ id: 'index', label: 'index.ts' },
],
},
{ id: 'readme', label: 'README.md' },
]}
label="Files"
/>Controlled expansion and selection
The open branches (expandedIds) and the selection (selectedId) can be passed in. onChange receives the id of the node picked.
- src
- components
- button.tsx
- card.tsx
- index.ts
- package.json
- README.md
Selected: card
Copy code
const [expandedIds, setExpandedIds] = useState<readonly string[]>(['src']);
const [selectedId, setSelectedId] = useState<string | null>(null);
<Tree
expandedIds={expandedIds}
items={files}
label="Files"
onChange={setSelectedId}
onExpandedChange={setExpandedIds}
selectedId={selectedId}
/>Props
items- Type:
readonly TreeItem[] - Default: -
label- Type:
string - Default: -
defaultExpandedIds?- Type:
readonly string[] - Default: -
defaultSelectedId?- Type:
string|null - Default:
null expandedIds?- Type:
readonly string[] - Default: -
onChange?- Type:
(id: string) => void - Default: -
onExpandedChange?- Type:
(ids: readonly string[]) => void - Default: -
selectedId?- Type:
string|null - Default: -
| Prop | Type | Default |
|---|---|---|
items | readonly TreeItem[] | - |
label | string | - |
defaultExpandedIds? | readonly string[] | - |
defaultSelectedId? | string|null | null |
expandedIds? | readonly string[] | - |
onChange? | (id: string) => void | - |
onExpandedChange? | (ids: readonly string[]) => void | - |
selectedId? | string|null | - |
Inherited attributes (except the ones set internally): HTMLAttributes<HTMLUListElement>