Tree

A tree view component built on rc-tree with custom styling, checkboxes, and collapsible nodes.

Import

import { Tree } from '@tidbcloud/uikit/biz'

Basic Usage

import { Tree } from '@tidbcloud/uikit/biz'
 
const treeData = [
  {
    key: '1',
    title: 'Parent 1',
    children: [
      { key: '1-1', title: 'Child 1-1' },
      { key: '1-2', title: 'Child 1-2' }
    ]
  },
  {
    key: '2',
    title: 'Parent 2',
    children: [{ key: '2-1', title: 'Child 2-1' }]
  }
]
 
function Demo() {
  return <Tree treeData={treeData} />
}

With Checkboxes

import { useState } from 'react'
import { Tree } from '@tidbcloud/uikit/biz'
 
function Demo() {
  const [checkedKeys, setCheckedKeys] = useState([])
 
  return <Tree treeData={treeData} checkable checkedKeys={checkedKeys} onCheck={(keys) => setCheckedKeys(keys)} />
}

With Lines

import { Tree } from '@tidbcloud/uikit/biz'
 
function Demo() {
  return <Tree treeData={treeData} showLine />
}

Default Expanded

import { Tree } from '@tidbcloud/uikit/biz'
 
function Demo() {
  return <Tree treeData={treeData} defaultExpandAll />
}

Controlled Expansion

import { useState } from 'react'
import { Tree } from '@tidbcloud/uikit/biz'
 
function Demo() {
  const [expandedKeys, setExpandedKeys] = useState(['1'])
 
  return <Tree treeData={treeData} expandedKeys={expandedKeys} onExpand={(keys) => setExpandedKeys(keys)} />
}

With Icons

import { Tree } from '@tidbcloud/uikit/biz'
import { IconFolder, IconFile } from '@tabler/icons-react'
 
const treeData = [
  {
    key: '1',
    title: 'Documents',
    icon: <IconFolder size={16} />,
    children: [
      { key: '1-1', title: 'file1.txt', icon: <IconFile size={16} /> },
      { key: '1-2', title: 'file2.txt', icon: <IconFile size={16} /> }
    ]
  }
]
 
function Demo() {
  return <Tree treeData={treeData} showIcon />
}

Disabled Nodes

import { Tree } from '@tidbcloud/uikit/biz'
 
const treeData = [
  {
    key: '1',
    title: 'Parent 1',
    children: [
      { key: '1-1', title: 'Child 1-1', disabled: true },
      { key: '1-2', title: 'Child 1-2' }
    ]
  }
]
 
function Demo() {
  return <Tree treeData={treeData} checkable />
}

Props

PropTypeDefaultDescription
treeDataTreeNode[]-Tree data nodes
showLineboolean | { showLeafIcon: boolean }-Show connecting lines
classNamestring-Container class
multipleboolean-Multi-select support
autoExpandParentboolean-Auto expand parents
checkablebooleanfalseShow checkboxes
disabledboolean-Disable entire tree
defaultExpandAllboolean-Expand all by default
defaultExpandParentboolean-Expand parent by default
defaultExpandedKeysstring[]-Default expanded keys
expandedKeysstring[]-Controlled expanded keys
checkedKeysstring[]-Controlled checked keys
defaultCheckedKeysstring[]-Default checked keys
selectedKeysstring[]-Controlled selected keys
defaultSelectedKeysstring[]-Default selected keys
selectablebooleantrueEnable selection
showIconbooleanfalseShow node icons
blockNodebooleanfalseFull-width nodes
switcherIconReactNode | (props) => ReactNode-Custom expand icon
onCheck(keys, info) => void-Check handler
onSelect(keys, info) => void-Select handler
onExpand(keys, info) => void-Expand handler

TreeNode Type

interface TreeNode {
  key: string
  title: ReactNode
  children?: TreeNode[]
  disabled?: boolean
  disableCheckbox?: boolean
  selectable?: boolean
  checkable?: boolean
  icon?: ReactNode
  isLeaf?: boolean
}