Skip to content

Shadcn-ui based tree view, with multi-selection, drag, and more!

License

Notifications You must be signed in to change notification settings

neigebaie/shadcn-ui-tree-view

Repository files navigation

Shadcn/ui Tree View

A feature-rich tree view component for shadcn/ui with checkboxes, context menus, search, and visual feedback.

screenshot

Installation

  1. Install dependencies:
npm install framer-motion @radix-ui/react-context-menu @radix-ui/react-collapsible @radix-ui/react-hover-card
  1. Install required shadcn/ui components:
npx shadcn-ui@latest add button
npx shadcn-ui@latest add badge
npx shadcn-ui@latest add input
npx shadcn-ui@latest add context-menu
npx shadcn-ui@latest add collapsible
npx shadcn-ui@latest add hover-card
  1. Install the tree view component:
npx shadcn-ui@latest add "https://github.com/neigebaie/shadcn-ui-tree-view/releases/download/v1.1.0/schema.json"

Usage

TreeViewItem Interface

interface TreeViewItem {
  id: string;
  name: string;
  type: string;
  children?: TreeViewItem[];
  checked?: boolean;
}

TreeViewProps Interface

interface TreeViewProps {
  className?: string;
  data: TreeViewItem[];
  title?: string;
  showExpandAll?: boolean;
  showCheckboxes?: boolean;
  searchPlaceholder?: string;
  iconMap?: Record<string, React.ReactNode>;
  menuItems?: TreeViewMenuItem[];
  onCheckChange?: (item: TreeViewItem, checked: boolean) => void;
  onAction?: (action: string, items: TreeViewItem[]) => void;
}

interface TreeViewMenuItem {
  id: string;
  label: string;
  icon?: React.ReactNode;
  action: (items: TreeViewItem[]) => void;
}

Example

import { TreeView } from "@/components/ui/tree-view";
import { Globe, Folder, FolderOpen, File } from "lucide-react";

const data = [
  {
    id: "1",
    name: "Root",
    type: "region",
    children: [
      {
        id: "1.1",
        name: "Folder 1",
        type: "store",
        children: [
          {
            id: "1.1.1",
            name: "Subfolder",
            type: "department",
            children: [
              { id: "1.1.1.1", name: "File 1", type: "item" },
              { id: "1.1.1.2", name: "File 2", type: "item" },
            ],
          },
        ],
      },
    ],
  },
];

const customIconMap = {
  region: <Globe className="h-4 w-4 text-purple-500" />,
  store: <Folder className="h-4 w-4 text-blue-500" />,
  department: <FolderOpen className="h-4 w-4 text-green-500" />,
  item: <File className="h-4 w-4 text-orange-500" />,
};

const menuItems = [
  {
    id: "download",
    label: "Download",
    icon: <Download className="h-4 w-4" />,
    action: (items) => console.log("Downloading:", items),
  },
];

export default function Demo() {
  const handleCheckChange = (item: TreeViewItem, checked: boolean) => {
    console.log(`Item ${item.name} checked:`, checked);
  };

  return (
    <TreeView
      data={data}
      title="Tree View Demo"
      showCheckboxes={true}
      iconMap={customIconMap}
      menuItems={menuItems}
      onCheckChange={handleCheckChange}
    />
  );
}

Features

  • Search functionality
  • Selection capabilities (single click, range, multi-select, drag select)
  • Folder management system with expand/collapse and item counting for collapsed folders
  • Visual feedback system with animations and level-specific icons
  • Context menu with file/folder specific actions

License

Licensed under the MIT License. See the LICENSE file for more details.

About

Shadcn-ui based tree view, with multi-selection, drag, and more!

Resources

License

Stars

Watchers

Forks