Skip to content

Tree

Hierarchical tree structure for displaying nested data.

import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'

Basic Tree

Simple tree with expandable nodes.

Parent Node
Child Node 1
Leaf Node 1
Leaf Node 2
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'

function App() {
  const basicTreeData: TreeDataNode[] = [
    {
      key: '0',
      title: 'Parent Node',
      children: [
        {
          key: '0-0',
          title: 'Child Node 1',
          children: [
            { key: '0-0-0', title: 'Leaf Node 1' },
            { key: '0-0-1', title: 'Leaf Node 2' },
          ],
        },
        {
          key: '0-1',
          title: 'Child Node 2',
          children: [{ key: '0-1-0', title: 'Leaf Node 3' }],
        },
      ],
    },
  ]
  
  return (
      <Tree treeData={basicTreeData} defaultExpandedKeys={['0', '0-0']} />
    )
}

export default App

Checkable

Tree with checkbox selection.

Parent Node
Child Node 1
Leaf Node 1
Leaf Node 2
Child Node 2
Leaf Node 3

Checked: None

import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'

function App() {
  const basicTreeData: TreeDataNode[] = [
    {
      key: '0',
      title: 'Parent Node',
      children: [
        {
          key: '0-0',
          title: 'Child Node 1',
          children: [
            { key: '0-0-0', title: 'Leaf Node 1' },
            { key: '0-0-1', title: 'Leaf Node 2' },
          ],
        },
        {
          key: '0-1',
          title: 'Child Node 2',
          children: [{ key: '0-1-0', title: 'Leaf Node 3' }],
        },
      ],
    },
  ]
  
  const [checkedKeys, setCheckedKeys] = useState<string[]>([])
  
  return (
      <Tree
        treeData={basicTreeData}
        checkable
        checkedKeys={checkedKeys}
        onCheck={setCheckedKeys}
        defaultExpandedKeys={['0', '0-0', '0-1']}
      />
      <p className="mt-4 text-sm">Checked: {checkedKeys.join(', ') || 'None'}</p>
    )
}

export default App

Selectable

Tree with node selection.

Parent Node
Child Node 1
Leaf Node 1
Leaf Node 2
Child Node 2
Leaf Node 3

Selected: None

import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'

function App() {
  const basicTreeData: TreeDataNode[] = [
    {
      key: '0',
      title: 'Parent Node',
      children: [
        {
          key: '0-0',
          title: 'Child Node 1',
          children: [
            { key: '0-0-0', title: 'Leaf Node 1' },
            { key: '0-0-1', title: 'Leaf Node 2' },
          ],
        },
        {
          key: '0-1',
          title: 'Child Node 2',
          children: [{ key: '0-1-0', title: 'Leaf Node 3' }],
        },
      ],
    },
  ]
  
  const [selectedKeys, setSelectedKeys] = useState<string[]>([])
  
  return (
      <Tree
        treeData={basicTreeData}
        selectable
        selectedKeys={selectedKeys}
        onSelect={setSelectedKeys}
        defaultExpandedKeys={['0', '0-0', '0-1']}
      />
      <p className="mt-4 text-sm">Selected: {selectedKeys.join(', ') || 'None'}</p>
    )
}

export default App

Multiple Selection

Allow selecting multiple nodes.

Parent Node
Child Node 1
Leaf Node 1
Leaf Node 2
Child Node 2
Leaf Node 3
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'

function App() {
  const basicTreeData: TreeDataNode[] = [
    {
      key: '0',
      title: 'Parent Node',
      children: [
        {
          key: '0-0',
          title: 'Child Node 1',
          children: [
            { key: '0-0-0', title: 'Leaf Node 1' },
            { key: '0-0-1', title: 'Leaf Node 2' },
          ],
        },
        {
          key: '0-1',
          title: 'Child Node 2',
          children: [{ key: '0-1-0', title: 'Leaf Node 3' }],
        },
      ],
    },
  ]
  
  const [selectedKeys, setSelectedKeys] = useState<string[]>([])
  
  return (
      <Tree
        treeData={basicTreeData}
        selectable
        multiple
        selectedKeys={selectedKeys}
        onSelect={setSelectedKeys}
        defaultExpandedKeys={['0', '0-0', '0-1']}
      />
    )
}

export default App

Show Line

Display connecting lines between nodes.

src
components
Button.tsx
Input.tsx
App.tsx
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'

function App() {
  const fileTreeData: TreeDataNode[] = [
    {
      key: 'src',
      title: 'src',
      children: [
        {
          key: 'components',
          title: 'components',
          children: [
            { key: 'Button.tsx', title: 'Button.tsx' },
            { key: 'Input.tsx', title: 'Input.tsx' },
          ],
        },
        { key: 'App.tsx', title: 'App.tsx' },
      ],
    },
  ]
  
  return (
      <Tree
        treeData={fileTreeData}
        showLine
        defaultExpandedKeys={['src', 'components']}
      />
    )
}

export default App

Show Icon

Display custom icons for nodes.

src
App.tsx
index.tsx
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { FolderIcon, DocumentIcon } from '@aster-ui/icons/solid'

function App() {
  const treeDataWithIcons: TreeDataNode[] = [
    {
      key: 'src',
      title: 'src',
      icon: <FolderIcon size="sm" />,
      children: [
        { key: 'App.tsx', title: 'App.tsx', icon: <DocumentIcon size="sm" /> },
        { key: 'index.tsx', title: 'index.tsx', icon: <DocumentIcon size="sm" /> },
      ],
    },
  ]
  
  return (
      <Tree treeData={treeDataWithIcons} showIcon defaultExpandedKeys={['src']} />
    )
}

export default App

Compound Pattern

Use Tree.Node for declarative tree structure.

Parent Node
Child Node 2
import { Tree } from 'asterui'

function App() {
  return (
      <Tree defaultExpandedKeys={['parent']}>
        <Tree.Node key="parent" title="Parent Node">
          <Tree.Node key="child-1" title="Child Node 1">
            <Tree.Node key="leaf-1" title="Leaf Node 1" />
            <Tree.Node key="leaf-2" title="Leaf Node 2" />
          </Tree.Node>
          <Tree.Node key="child-2" title="Child Node 2" />
        </Tree.Node>
      </Tree>
    )
}

export default App

Checkbox Colors

Customize checkbox appearance with DaisyUI colors.

Parent Node
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'

function App() {
  const basicTreeData: TreeDataNode[] = [
    {
      key: '0',
      title: 'Parent Node',
      children: [
        {
          key: '0-0',
          title: 'Child Node 1',
          children: [
            { key: '0-0-0', title: 'Leaf Node 1' },
            { key: '0-0-1', title: 'Leaf Node 2' },
          ],
        },
        {
          key: '0-1',
          title: 'Child Node 2',
          children: [{ key: '0-1-0', title: 'Leaf Node 3' }],
        },
      ],
    },
  ]
  
  return (
      <Tree
        treeData={basicTreeData}
        checkable
        checkboxColor="success"
        checkboxSize="md"
        defaultExpandedKeys={['0']}
      />
    )
}

export default App

Check Strictly

Decouple parent-child checkbox relationship.

Parent Node
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'

function App() {
  const basicTreeData: TreeDataNode[] = [
    {
      key: '0',
      title: 'Parent Node',
      children: [
        {
          key: '0-0',
          title: 'Child Node 1',
          children: [
            { key: '0-0-0', title: 'Leaf Node 1' },
            { key: '0-0-1', title: 'Leaf Node 2' },
          ],
        },
        {
          key: '0-1',
          title: 'Child Node 2',
          children: [{ key: '0-1-0', title: 'Leaf Node 3' }],
        },
      ],
    },
  ]
  
  const [checkedKeys, setCheckedKeys] = useState<string[]>([])
  
  return (
      <Tree
        treeData={basicTreeData}
        checkable
        checkStrictly
        checkedKeys={checkedKeys}
        onCheck={setCheckedKeys}
        defaultExpandedKeys={['0']}
      />
    )
}

export default App

Async Loading

Load child nodes asynchronously on expand.

Expand to load
Expand to load
import { Tree } from 'asterui'
import type { TreeDataNode } from 'asterui'
import { useState } from 'react'

function App() {
  const [treeData, setTreeData] = useState<TreeDataNode[]>([
    { key: '0', title: 'Expand to load' },
    { key: '1', title: 'Expand to load' },
  ])
  
  const loadData = async (node: TreeDataNode) => {
    await new Promise(resolve => setTimeout(resolve, 1000))
    setTreeData(prev => {
      const update = (nodes: TreeDataNode[]): TreeDataNode[] =>
        nodes.map(n => n.key === node.key
          ? { ...n, children: [
              { key: `${n.key}-0`, title: 'Child 1', isLeaf: true },
              { key: `${n.key}-1`, title: 'Child 2', isLeaf: true },
            ]}
          : { ...n, children: n.children ? update(n.children) : undefined }
        )
      return update(prev)
    })
  }
  
  return (
      <Tree treeData={treeData} loadData={loadData} />
    )
}

export default App
PropertyDescriptionTypeDefault
treeDataTree data structure (alternative to compound pattern)TreeDataNode[][]
childrenTree.Node children for compound patternReact.ReactNode-
checkableShow checkbox for each nodebooleanfalse
checkboxColorCheckbox color (DaisyUI)'primary' | 'secondary' | 'accent' | 'neutral' | 'info' | 'success' | 'warning' | 'error''primary'
checkboxSizeCheckbox size (DaisyUI)'xs' | 'sm' | 'md' | 'lg' | 'xl''sm'
checkedKeysControlled checked keysstring[]-
defaultCheckedKeysDefault checked keysstring[][]
checkStrictlyDecouple parent-child checkbox relationshipbooleanfalse
onCheckCallback when node is checked(keys: string[], info: { node, checked }) => void-
selectableEnable node selectionbooleantrue
selectedKeysControlled selected keysstring[]-
defaultSelectedKeysDefault selected keysstring[][]
onSelectCallback when node is selected(keys: string[], info: { node, selected }) => void-
multipleAllow multiple selectionbooleanfalse
expandedKeysControlled expanded keysstring[]-
defaultExpandedKeysDefault expanded keysstring[][]
defaultExpandAllExpand all nodes by defaultbooleanfalse
autoExpandParentAuto expand parent nodesbooleantrue
onExpandCallback when node is expanded(keys: string[], info: { node, expanded }) => void-
showLineShow connecting linesbooleanfalse
showIconShow node iconsbooleanfalse
blockNodeMake nodes fill horizontal spacebooleanfalse
switcherIconCustom expand/collapse iconReactNode | ((expanded: boolean) => ReactNode)-
titleRenderCustom title render function(node: TreeDataNode) => ReactNode-
filterTreeNodeFilter function to highlight nodes(node: TreeDataNode) => boolean-
loadDataAsync data loading function(node: TreeDataNode) => Promise<void>-
onRightClickRight click handler(info: { event, node }) => void-
fieldNamesCustom field names for data mapping{ key?: string; title?: string; children?: string }-
classNameAdditional CSS classesstring-
PropertyDescriptionTypeDefault
keyUnique identifierstring-
titleDisplay titleReact.ReactNode-
childrenChild nodesTreeDataNode[]-
iconCustom iconReact.ReactNode-
disabledDisable the nodebooleanfalse
disableCheckboxDisable checkbox for the nodebooleanfalse
selectableWhether node can be selectedbooleantrue
checkableWhether node shows checkboxbooleantrue
isLeafForce node to be a leaf (no expand icon)boolean-
PropertyDescriptionTypeDefault
keyUnique identifier (React key prop)string-
titleDisplay titleReact.ReactNode-
childrenChild Tree.Node elementsReact.ReactNode-
iconCustom iconReact.ReactNode-
disabledDisable the nodebooleanfalse
disableCheckboxDisable checkbox for the nodebooleanfalse
selectableWhether node can be selectedbooleantrue
checkableWhether node shows checkboxbooleantrue
isLeafForce node to be a leaf (no expand icon)boolean-

The Tree component implements the full WAI-ARIA tree pattern:

  • role="tree" on container and role="treeitem" on nodes
  • aria-expanded indicates node expansion state
  • aria-selected indicates selection state
  • aria-checked indicates checkbox state (with "mixed" for indeterminate)
  • aria-disabled indicates disabled state
  • aria-level indicates nesting depth
KeyAction
Move focus to next visible node
Move focus to previous visible node
Expand collapsed node, or move to first child
Collapse expanded node
Enter / SpaceToggle checkbox, select node, or expand/collapse
HomeMove focus to first node
EndMove focus to last visible node

The component includes data attributes for testing:

  • data-testid="tree" on the tree container
  • data-testid="tree-node-{key}" on each node
  • data-state="selected|expanded|collapsed" indicates node state
  • data-key="{key}" provides the node key