Tree
Expansion, selection and check linkage for hierarchical data. Flattening and cascading logic all live in core pure functions; the component layer only wires rendering and keyboard, with an API identical to lh-ui-plus's LhTree.
Basic usage
Pass tree data via data; defaultExpandedKeys expands uncontrolled, and clicking the arrow or the node (expandOnClickNode on by default) toggles expansion. Disabled nodes cannot be selected and are skipped by keyboard navigation.
import { LhTree } from '@lh-ui/react'
export default function Demo() {
const treeData = [
{
key: 'a',
label: 'Frontend',
children: [
{ key: 'a-1', label: 'Vue' },
{ key: 'a-2', label: 'React' },
],
},
{ key: 'b', label: 'Backend', children: [{ key: 'b-1', label: 'Node' }] },
{ key: 'c', label: 'Operations', disabled: true },
]
return <LhTree data={treeData} defaultExpandedKeys={['a']} />
}
Checkable (cascade)
showCheckbox shows checkboxes: checking a parent cascades to all descendants; when all children are checked the parent checks automatically, and partial checking shows the indeterminate state.
<LhTree data={treeData} showCheckbox defaultExpandedKeys={['a', 'b']} />
Strict check (checkStrictly)
With checkStrictly, checking only affects the node itself: no cascading to descendants, no ancestor backtracking, no indeterminate state.
<LhTree data={treeData} showCheckbox checkStrictly defaultExpandedKeys={['a', 'b']} />
Controlled expansion / selection / checking
Passing a key array makes it controlled: once expandedKeys / selectedKeys / checkedKeys is passed, its changes are fully driven by external state, paired with the onExpandedKeysChange / onSelectedKeysChange / onCheckedKeysChange callbacks; passing only defaultExpandedKeys etc. uses the internal uncontrolled state. multiple allows multi-select (clicking toggles selection instead of replacing).
import { useState } from 'react'
import { LhTree } from '@lh-ui/react'
export default function Demo() {
const [selectedKeys, setSelectedKeys] = useState<string[]>(['x'])
const [checkedKeys, setCheckedKeys] = useState<string[]>(['x-1'])
return (
<LhTree
data={ctrlData}
multiple
expandOnClickNode
expandedKeys={['x']}
selectedKeys={selectedKeys}
checkedKeys={checkedKeys}
onSelectedKeysChange={setSelectedKeys}
onCheckedKeysChange={setCheckedKeys}
/>
)
}
Lazy loading
With lazy enabled, expanding an unloaded node calls the load callback (resolve commits the children and expands automatically, reject or a thrown error cancels); the arrow spins while loading. Loadable nodes must declare isLeaf: false explicitly (otherwise a node without children is inferred as a leaf and shows no arrow), and real leaves declare isLeaf: true.
<LhTree
data={[{ key: 'lazy', label: 'Click to load', isLeaf: false }]}
lazy
load={(node, resolve, reject) => {
setTimeout(() => {
if (node.key === 'lazy') {
resolve([
{ key: 'lazy-1', label: 'Child 1', isLeaf: true },
{ key: 'lazy-2', label: 'Child 2', isLeaf: true },
])
} else {
reject(new Error('nothing to load'))
}
}, 600)
}}
/>
LhTree Props
| Prop | Description | Type | Default |
|---|---|---|---|
| data | Tree data | LhTreeNode[] | — |
| showCheckbox | Show checkboxes | boolean | false |
| multiple | Multi-select (clicking toggles selection instead of replacing) | boolean | false |
| expandOnClickNode | Expand/collapse when clicking a non-leaf node | boolean | true |
| defaultExpandedKeys | Default expanded keys (uncontrolled) | string[] | [] |
| expandedKeys | Controlled expanded keys | string[] | — |
| defaultSelectedKeys | Default selected keys (uncontrolled) | string[] | [] |
| selectedKeys | Controlled selected keys | string[] | — |
| defaultCheckedKeys | Default checked keys (uncontrolled) | string[] | [] |
| checkedKeys | Controlled checked keys | string[] | — |
| checkStrictly | Strict check (per-node only, no cascade or indeterminate) | boolean | false |
| lazy | Lazy mode (calls load when expanding an unloaded node) | boolean | false |
| load | Lazy load callback (resolve commits children and expands, reject cancels) | (node: LhTreeNode, resolve: (children: LhTreeNode[]) => void, reject: (reason?: unknown) => void) => void | — |
| disabled | Disable the whole tree | boolean | false |
| sx | Shorthand layout props | SxProps | — |
| renderNode | Custom node render (plus's #node scoped slot) | (scope: { node: LhTreeNode; level: number; expanded: boolean; selected: boolean }) => ReactNode | — |
| renderEmpty | Custom empty render (plus's #empty slot) | () => ReactNode | — |
Events
| Event | Description | Callback signature | Vue equivalent |
|---|---|---|---|
| onExpandedKeysChange | Emitted when the expanded keys change | (keys: string[]) => void | update:expandedKeys |
| onSelectedKeysChange | Emitted when the selected keys change | (keys: string[]) => void | update:selectedKeys |
| onCheckedKeysChange | Emitted when the checked keys change | (keys: string[]) => void | update:checkedKeys |
| onSelect | Emitted after a node is clicked for selection / toggled | (key: string, node: LhTreeNode) => void | @select |
| onCheck | Emitted after checking changes | (keys: string[], node: LhTreeNode) => void | @check |
| onExpand | Emitted after a node expands / collapses | (key: string, expanded: boolean) => void | @expand |
| onNodeLoad | Emitted after lazy-loaded children arrive | (node: LhTreeNode, children: LhTreeNode[]) => void | @node-load |
The three key states (expanded / selected / checked) are independent: passing a key array makes it controlled, passing default*Keys uses the internal uncontrolled state, and change callbacks fire in both modes. Plus's v-model:expanded-keys etc. map to the React-idiomatic "controlled prop + default value + onXxxChange callback" trio with identical semantics.
LhTreeNode
| Field | Description | Type |
|---|---|---|
| key | Unique key | string |
| label | Display text | string |
| children | Child nodes | LhTreeNode[] |
| disabled | Disabled | boolean |
| isLeaf | Leaf or not (inferred from children; lazy branches must set false explicitly) | boolean |
Accessibility
role="tree"+role="treeitem", witharia-level/aria-expanded/aria-selected/aria-disabledfully exposed- Checkbox:
role="checkbox"+aria-checked(mixedwhen indeterminate) - Keyboard (APG tree pattern): ↑/↓ move between items (skipping disabled, looping), → expands or enters the first child, ← collapses or returns to the parent, Home/End jump to first/last, Enter activates, Space checks (with
showCheckbox) or activates; roving tabindex
ref
forwardRef<HTMLDivElement> — the root element.
Styles
import '@lh-ui/react/theme.css'
import '@lh-ui/react/style/tree'