Install the Tree View component
mainInstall the Tree View component into your Shadcn UI project using the following command:
npx shadcn add "https://mrlightful.com/registry/tree-view"repository·main·Indexed 18 days ago
https://github.com/mrlightful/shadcn-tree-viewA React tree view component for shadcn/ui designed for navigating hierarchical data. It supports expansion, selection, drag-and-drop, and custom item rendering via the TreeView component and TreeDataItem interface.
Install the Tree View component into your Shadcn UI project using the following command:
npx shadcn add "https://mrlightful.com/registry/tree-view"To use the TreeView, import it and provide a data prop containing an array of TreeDataItem objects. You can also configure initial selection, selection change handlers, and custom icons.
import { TreeView, TreeDataItem } from "@/components/ui/tree-view";
const data: TreeDataItem[] = [
{
id: "1",
name: "Item 1",
children: [
{
id: "2",
name: "Item 1.1",
children: [
{
id: "3",
name: "Item 1.1.1",
},
{
id: "4",
name: "Item 1.1.2",
},
],
},
{
id: "5",
name: "Item 1.2 (disabled)",
disabled: true,
},
],
},
{
id: "6",
name: "Item 2 (draggable)",
draggable: true,
},
];
<TreeView data={data} />;The TreeView component accepts the following props:
| Prop | Type | Description |
|---|---|---|
data | TreeDataItem[] | TreeDataItem | The hierarchical data to render. |
initialSelectedItemId | string | The ID of the item that should be selected by default. |
onSelectChange | (item: TreeDataItem | undefined) => void | Callback triggered when the selection changes. |
renderItem | (params: TreeRenderItemParams) => React.ReactNode | Custom renderer for tree items. |
expandAll | boolean | If true, all nodes will be expanded by default. |
defaultNodeIcon | React.ComponentType<{ className?: string }> | The default icon used for nodes with children. |
defaultLeafIcon | React.ComponentType<{ className?: string }> | The default icon used for leaf nodes (no children). |
Each item in your tree data should follow the TreeDataItem interface. This allows for deep nesting, custom icons, and interactive states like drag-and-drop or disabled modes.
interface TreeDataItem {
id: string;
name: string;
icon?: React.ComponentType<{ className?: string }>;
selectedIcon?: React.ComponentType<{ className?: string }>;
openIcon?: React.ComponentType<{ className?: string }>;
children?: TreeDataItem[];
actions?: React.ReactNode;
onClick?: () => void;
draggable?: boolean;
droppable?: boolean;
disabled?: boolean;
className?: string;
}