import React, { type ReactNode, useState } from "react"; import { TreeEntryItem } from "./TreeView"; import { TreeDataItem, TreeDataItemWithMeta } from "./types"; import { cn } from "../../hooks"; import { VirtualList } from "../virtual-list"; const TREE_ITEM_HEIGHT = 32; /** * Props for the virtualized tree component */ export type VirtualTreeViewProps = { data: TreeDataItemWithMeta[]; getIcon: (item: T, expanded?: boolean) => ReactNode; expandedItems?: Set; onToggleItem?: (id: string) => void; onItemHover?: (id: string | null) => void; hoveredId?: string | null; isSelectable?: boolean; selectedItemIds?: Set; onSelectionChange?: (ids: Set) => void; hideExpand?: boolean; hasDescendants?: (id: string) => boolean; renderLabel?: (label: string, itemId: string) => ReactNode; className?: string; renderContextMenu?: (item: T) => ReactNode; selectionMode?: "single" | "multi"; /** Container height - required for virtualization (default: '100%') */ height?: number | string; /** Number of extra items to render outside visible area (default: 5) */ overscan?: number; }; /** * VirtualTreeView component - a virtualized tree that only renders visible items. * Use this for trees with thousands of items where performance is critical. * * @example * ```tsx * } * height={400} * expandedItems={expandedItems} * onToggleItem={toggleItem} * hasDescendants={hasDescendants} * /> * ``` */ export function VirtualTreeView({ data, getIcon, expandedItems, onToggleItem, onItemHover, hoveredId, isSelectable = false, selectedItemIds, onSelectionChange, hideExpand = false, hasDescendants = () => false, renderLabel = (label) => label, className, renderContextMenu, selectionMode = "multi", height = "100%", overscan = 5, }: VirtualTreeViewProps) { const [lastSelectedId, setLastSelectedId] = useState(null); return ( item.id} className={cn("flex-1", className)} > {(item, _index, style) => (
)}
); }