tree-view.tsx


Overview

tree-view.tsx is a React component file that implements a highly customizable and accessible Tree View UI component. It enables rendering hierarchical data structures with expandable/collapsible nodes and selectable leaf and node items. The component supports:

This file is designed to be used in client-side React applications and is especially suited for file explorers, navigation trees, or any UI requiring hierarchical data display.


Exports

TreeView

The main Tree View component. Accepts tree data and renders the entire hierarchical structure.

TreeDataItem

TypeScript interface defining the shape of each tree item.


Detailed API

Interface: TreeDataItem

Represents a single node or leaf in the tree.

Property

Type

Description

id

string

Unique identifier for the tree item.

name

string

Display name of the item shown in the tree.

icon

any (React Component)

Optional default icon for the item.

selectedIcon

any (React Component)

Optional icon to display when the item is selected.

openIcon

any (React Component)

Optional icon to display when the node is expanded/open.

children

TreeDataItem[]

Optional array of child items, indicating this item is a node.

actions

React.ReactNode

Optional React elements (e.g., buttons) shown when the item is selected.

onClick

() => void

Optional click handler triggered when the item is selected.


Component: TreeView

Props

Name

Type

Default

Description

data

TreeDataItem[] \

TreeDataItem

Required

initialSelectedItemId

string

undefined

The id of the initially selected tree item.

onSelectChange

[(item: TreeDataItem \

undefined) => void](/projects/311/73394)

undefined

expandAll

boolean

false

If true, expands all nodes by default.

defaultNodeIcon

any (React Component)

undefined

Default icon for tree nodes (items with children).

defaultLeafIcon

any (React Component)

undefined

Default icon for leaf items (items without children).

className

string

undefined

Optional additional CSS classes for the container div.

...others

React.HTMLAttributes

Other native div attributes passed through.

Usage Example

import { TreeView, TreeDataItem } from './tree-view';
import { FolderIcon, FileIcon } from './icons';

const data: TreeDataItem[] = [
  {
    id: '1',
    name: 'Documents',
    children: [
      { id: '2', name: 'Resume.pdf', icon: FileIcon },
      { id: '3', name: 'CoverLetter.docx', icon: FileIcon },
    ],
    icon: FolderIcon,
  },
];

export function App() {
  const handleSelect = (item?: TreeDataItem) => {
    console.log('Selected:', item?.name);
  };

  return (
    <TreeView
      data={data}
      initialSelectedItemId="2"
      onSelectChange={handleSelect}
      defaultNodeIcon={FolderIcon}
      defaultLeafIcon={FileIcon}
    />
  );
}

Internal Components & Functions

TreeItem

TreeNode

TreeLeaf

AccordionTrigger & AccordionContent

TreeIcon

TreeActions


Implementation Details & Algorithms


Interaction With Other Parts of the System

This component is designed to be a standalone UI element but can be integrated into larger applications wherever hierarchical data visualization is needed.


Mermaid Diagram: Component Structure

componentDiagram
    direction TB
    component TreeView {
        +data: TreeDataItem[] | TreeDataItem
        +initialSelectedItemId?: string
        +onSelectChange?: (item) => void
        +expandAll?: boolean
        +defaultNodeIcon?: any
        +defaultLeafIcon?: any
    }
    component TreeItem {
        +data: TreeDataItem[] | TreeDataItem
        +selectedItemId?: string
        +handleSelectChange(item)
        +expandedItemIds: string[]
        +defaultNodeIcon?: any
        +defaultLeafIcon?: any
    }
    component TreeNode {
        +item: TreeDataItem
        +handleSelectChange(item)
        +expandedItemIds: string[]
        +selectedItemId?: string
        +defaultNodeIcon?: any
        +defaultLeafIcon?: any
    }
    component TreeLeaf {
        +item: TreeDataItem
        +selectedItemId?: string
        +handleSelectChange(item)
        +defaultLeafIcon?: any
    }
    component AccordionTrigger
    component AccordionContent
    component TreeIcon
    component TreeActions

    TreeView --> TreeItem : renders
    TreeItem --> TreeNode : if has children
    TreeItem --> TreeLeaf : if no children
    TreeNode --> AccordionTrigger : uses
    TreeNode --> AccordionContent : uses
    TreeNode --> TreeItem : recursive render of children
    TreeNode --> TreeIcon : renders icon
    TreeLeaf --> TreeIcon : renders icon
    TreeNode --> TreeActions : renders actions
    TreeLeaf --> TreeActions : renders actions

Summary

The tree-view.tsx file provides a robust, accessible, and flexible React Tree View component with support for nested data, selection, expansion, custom icons, and item actions. It encapsulates complex UI behaviors using Radix UI primitives and React hooks, while maintaining clean styling and extensibility.

This component is ideal for applications requiring an interactive hierarchical data display with rich customization and accessible interactions.