Skip to main content

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.

Basic usage
Frontend
Vue
React
Operations
Frontend
Vue
React
Backend
Node
Operations
Design
Color
Font
Engineering
Selected: x, Checked: x-1
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​

PropDescriptionTypeDefault
dataTree dataLhTreeNode[]—
showCheckboxShow checkboxesbooleanfalse
multipleMulti-select (clicking toggles selection instead of replacing)booleanfalse
expandOnClickNodeExpand/collapse when clicking a non-leaf nodebooleantrue
defaultExpandedKeysDefault expanded keys (uncontrolled)string[][]
expandedKeysControlled expanded keysstring[]—
defaultSelectedKeysDefault selected keys (uncontrolled)string[][]
selectedKeysControlled selected keysstring[]—
defaultCheckedKeysDefault checked keys (uncontrolled)string[][]
checkedKeysControlled checked keysstring[]—
checkStrictlyStrict check (per-node only, no cascade or indeterminate)booleanfalse
lazyLazy mode (calls load when expanding an unloaded node)booleanfalse
loadLazy load callback (resolve commits children and expands, reject cancels)(node: LhTreeNode, resolve: (children: LhTreeNode[]) => void, reject: (reason?: unknown) => void) => void—
disabledDisable the whole treebooleanfalse
sxShorthand layout propsSxProps—
renderNodeCustom node render (plus's #node scoped slot)(scope: { node: LhTreeNode; level: number; expanded: boolean; selected: boolean }) => ReactNode—
renderEmptyCustom empty render (plus's #empty slot)() => ReactNode—

Events​

EventDescriptionCallback signatureVue equivalent
onExpandedKeysChangeEmitted when the expanded keys change(keys: string[]) => voidupdate:expandedKeys
onSelectedKeysChangeEmitted when the selected keys change(keys: string[]) => voidupdate:selectedKeys
onCheckedKeysChangeEmitted when the checked keys change(keys: string[]) => voidupdate:checkedKeys
onSelectEmitted after a node is clicked for selection / toggled(key: string, node: LhTreeNode) => void@select
onCheckEmitted after checking changes(keys: string[], node: LhTreeNode) => void@check
onExpandEmitted after a node expands / collapses(key: string, expanded: boolean) => void@expand
onNodeLoadEmitted after lazy-loaded children arrive(node: LhTreeNode, children: LhTreeNode[]) => void@node-load
Controlled / uncontrolled

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​

FieldDescriptionType
keyUnique keystring
labelDisplay textstring
childrenChild nodesLhTreeNode[]
disabledDisabledboolean
isLeafLeaf or not (inferred from children; lazy branches must set false explicitly)boolean

Accessibility​

  • role="tree" + role="treeitem", with aria-level / aria-expanded / aria-selected / aria-disabled fully exposed
  • Checkbox: role="checkbox" + aria-checked (mixed when 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'