Skip to content

Tree Component ​

Tree Component

Tree component is used to display hierarchical data, supporting interactive operations such as selection, expand/collapse, suitable for folders, organizational structures, category management and other scenarios.

Basic Usage ​

Basic tree component usage, displaying hierarchical data.

Show CodeArrow
CopyGitHub

Selectable Tree ​

Set the checkable attribute to true to display checkboxes and enable node selection functionality.

Currently Selected Nodes:
No nodes selected
Show CodeArrow
CopyGitHub

Default Expanded and Checked ​

You can set default expanded and checked nodes through the expanded and checked attributes.

Default expanded nodes: Level 1 Node 1, Level 2 Node 1-1
Default selected nodes: Level 3 Node 1-1-2, Level 2 Node 2-1
Show CodeArrow
CopyGitHub

Accordion Mode ​

Set the accordion attribute to true to enable accordion mode, where only one sibling node can be expanded at a time.

Normal Mode:
Accordion Mode:
Show CodeArrow
CopyGitHub

Disabled State ​

Set the disabled attribute to disable the entire tree, or set the disabled field in the data to disable specific nodes.

Individual Node Disabled:
Entire Tree Disabled:
Show CodeArrow
CopyGitHub

Custom Node Content ​

You can customize node content and styles through the default slot.

Show CodeArrow
CopyGitHub

Control Node Expansion ​

You can control whether to expand/collapse nodes when clicking on them by setting the expand-on-click-node attribute.

Click Node to Expand/Collapse:
Click Arrow Only to Expand/Collapse:

You can control the expand/collapse behavior by setting the expand-on-click-node property:

  • true: Expand/collapse when clicking on the node (default behavior)
  • false: Only expand/collapse by clicking the arrow icon
Show CodeArrow
CopyGitHub

Tree API ​

Attributes ​

AttributeDescriptionTypeAccepted ValuesDefault
dataData to be displayedarray—[]
node-keyUnique identifier attribute for each tree nodestring—id
labelSpecify node label as a property value of node objectstring—label
childrenSpecify subtree as a property value of node objectstring—children
expandedArray of keys of nodes that are expanded by defaultarray—[]
checkedArray of keys of nodes that are checked by defaultarray—[]
indentHorizontal indentation between adjacent level nodes in pixelsnumber—16
checkableWhether nodes can be checkedboolean—false
accordionWhether to open only one sibling tree node at a timeboolean—false
expand-on-click-nodeWhether to expand or collapse node when clicking on itboolean—true
disabledWhether to disable the entire treeboolean—false

Events ​

Event NameDescriptionParameters
node-expandTriggered when node is expanded(nodeData, node)
node-collapseTriggered when node is collapsed(nodeData, node)
check-changeTriggered when node check state changes(checkedKeys: string[])

Methods ​

Method NameDescriptionParameters
getCheckedKeysGet currently checked node keys—
getCheckedNodesGet currently checked node data—
setCheckedKeysSet checked node keys(keys: string[])
expandAllExpand all nodes—
collapseAllCollapse all nodes—

Slots ​

Slot NameDescriptionScope Parameters
defaultCustom tree node content{ node: TreeNodeType, data: object }
emptyCustom content when tree is empty—

Data Structure ​

Each node object should contain the following fields:

typescript
interface NodeData {
  [nodeKey: string]: string; // Unique identifier, default is id
  [label: string]: string;   // Text displayed by node, default is label
  [children: string]?: NodeData[]; // Child nodes, default is children
  disabled?: boolean;        // Whether to disable node
}

Style Variables ​

The tree component uses the following style variables, which can be customized through SCSS variables:

Variable NameDescription
$font-size-defaultDefault font size
$text-color-defaultDefault text color
$border-color-defaultDefault border color
$bg-color-defaultDefault background color
$bg-color-hoverHover background color
$theme-color-primaryTheme color - Primary

Released under the MIT License