Tree

Ready

Part of Data Display collection

Basic Tree

Hierarchical file system with expandable/collapsible nodes

src
components
Button.tsx
Card.tsx
Input.tsx
pages
styles.css
utils.ts
public
docs
package.json
tsconfig.json

Selectable Tree

Click nodes to select them and see the selected item below

Documents
Work
Personal
Vacation Photos
Resume.pdf

Checkable Tree

Check/uncheck nodes (automatically checks/unchecks all children)

src
components
pages
styles.css
utils.ts
public
docs
package.json
tsconfig.json

Features

  • Hierarchical tree structure with unlimited nesting
  • Expand/collapse nodes with animation
  • Selectable nodes with visual feedback
  • Checkable nodes with cascade behavior
  • File type icons (folder, file, document, image, code)
  • Connection lines between parent and children
  • Default expanded nodes configuration

Installation

Import the Tree component and TreeNode type:

import { Tree, type TreeNode } from '@/components/ui/tree'

Usage Examples

Basic Tree

const data: TreeNode[] = [
  {
    id: '1',
    label: 'src',
    children: [
      { id: '1-1', label: 'Button.tsx', type: 'code' },
      { id: '1-2', label: 'Card.tsx', type: 'code' }
    ]
  },
  { id: '2', label: 'package.json', type: 'document' }
]

<Tree data={data} />

With Default Expanded Nodes

<Tree
  data={data}
  defaultExpanded={['1', '1-1']}
/>

Selectable Tree

<Tree
  data={data}
  selectable
  onSelect={(node) => console.log('Selected:', node)}
/>

Checkable Tree

<Tree
  data={data}
  checkable
  onCheck={(checkedIds) => console.log('Checked:', checkedIds)}
/>

API Reference

Tree Props

PropTypeDefaultDescription
dataTreeNode[]requiredArray of root tree nodes
defaultExpandedstring[][]IDs of initially expanded nodes
selectablebooleanfalseEnable node selection
checkablebooleanfalseEnable checkboxes
onSelect(node) => void-Callback when node is selected
onCheck(ids) => void-Callback with checked node IDs
classNamestring-Additional CSS classes

TreeNode Type

PropertyTypeRequiredDescription
idstringYesUnique node identifier
labelstringYesNode display label
type'file' | 'document' | 'image' | 'code'NoFile type for icon display
childrenTreeNode[]NoChild nodes (makes this a folder)