Tree
A hierarchical tree with filtering and selection.
Usage
Basic
filterText takes the search text, and matching branches expand while it is set.
Loading demo...
Admin Permission Scopes
The selected key drives the details, transfer list, or audit timeline beside the tree.
Loading demo...
Best Practices
- Treat
keyas a durable identifier; never derive it from labels that may be translated or renamed. - Use
expandedKeysonly when another surface must own expansion; otherwise usedefaultExpandedKeys. - Keep permission-scope trees single-select; assign grants in
TxTransfer,DataTable, or a detail panel. - When
leaf=falsemarks a lazy placeholder, give the surrounding UI a loading or empty state;TxTreenever fetches children. - In a custom
itemslot, calltoggleExpand()andtoggleSelect()instead of duplicating state logic.
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
nodes | TreeNode[] | [] | Hierarchical data source. |
modelValue | string | number | Array<string | number> | - | Selected key, or keys with multiple; unbound, the tree keeps its own selection. |
multiple | boolean | false | Toggles selected keys as an array. |
selectable | boolean | true | Allows row and checkbox selection; expansion still works when off. |
checkable | boolean | false | Shows a checkbox per visible node, with no cascade to descendants. |
disabled | boolean | false | Disables selection for the whole tree; expansion still works. |
defaultExpandedKeys | Array<string | number> | [] | Initial uncontrolled expanded keys; changes reset expansion while expandedKeys is absent. |
defaultSelectedKeys | Array<string | number> | - | Initial uncontrolled selection; ignored while modelValue is bound. |
expandedKeys | Array<string | number> | - | Controlled expanded keys; the parent passes them back after update:expandedKeys. |
indent | number | 16 | Per-level indent in px. |
filterText | string | '' | Filter query; matching branches expand while it is non-empty. |
filterMethod | (node: TreeNode, query: string) => boolean | - | Custom matcher given the trimmed original query; the default is a lowercased label match. |
Events
| Event | Payload | Description |
|---|---|---|
update:modelValue | value: TreeValue | Fires after a selectable node is clicked or its checkbox changes. |
select | { key: TreeKey, node: TreeNode } | Fires right after update:modelValue with the selected node. |
toggle | { key: TreeKey, expanded: boolean } | Fires after a node expands or collapses. |
update:expandedKeys | keys: TreeKey[] | Next expanded keys, in controlled and uncontrolled modes. |
Slots
| Slot | Props | Description |
|---|---|---|
item | { node, level, expanded, hasChildren, selected, toggleExpand, toggleSelect, indent } | Replaces the whole row; call the slot helpers to keep selection and expansion. |
empty | - | Empty state when filtering leaves no rows; defaults to No results. |
TreeNode
| Field | Type | Description |
|---|---|---|
key | string | number | Stable unique key for selection and expansion. |
label | string | Row text and the default filter target. |
children | TreeNode[] | Child nodes. |
leaf | boolean | Set false to show an expand caret before children exist. |
disabled | boolean | Disables selecting this node; nodes with children still expand. |
icon | TxIconSource | string | Node icon; strings are treated as class icons. |
Overview
- Expansion and selection are each controlled or uncontrolled: with
expandedKeyspassed ormodelValuebound, the tree only emits and updates once the parent passes values back. - Filtering never rewrites expansion state; it only expands the ancestors of matches while active.
- Selection is flat:
checkablechanges only the control, with no tri-state or cascade. disabledandnode.disabledblock selection, but nodes still expand.- The root is
role="tree"(witharia-multiselectableundermultiple); each row is arole="treeitem"witharia-level,aria-setsize,aria-posinset, andaria-expanded/aria-selected/aria-disabledas needed. Theitemslot replaces only the row visuals, so the semantics stay.
Technologies
- Nodes are flattened into visible rows by expansion state and the current filter.
- Source:
packages/tuffex/packages/components/src/tree/.
查看源码
packages/tuffex/packages/components/src/tree/index.ts
Customization
| Theme token | Used for |
|---|---|
--tx-text-color-primary | Row label color. |
--tx-text-color-secondary | Caret and empty state. |
--tx-color-primary | Selected row tint. |
--tx-fill-color | Hover row tint. |