"use client"; import { KeyboardEvent } from "react"; import { Checkbox } from "@/components/shadcn/checkbox"; import { cn } from "@/lib/utils"; import { TreeLeafProps } from "@/types/tree"; import { TreeItemLabel } from "./tree-item-label"; import { TreeSpinner } from "./tree-spinner"; import { TreeStatusIndicator } from "./tree-status-indicator"; import { getTreeLeafPadding } from "./utils"; /** * TreeLeaf component for rendering leaf nodes (nodes without children). * * Features: * - Selection via checkbox or click * - Loading spinner state * - Custom rendering via renderItem prop * - Indentation based on nesting level * - Keyboard navigation support (Enter/Space to select) */ export function TreeLeaf({ item, level, selectedIds, onSelectionChange, showCheckboxes, renderItem, }: TreeLeafProps) { const isSelected = selectedIds.includes(item.id); const shouldReplaceCheckboxWithState = showCheckboxes && (item.isLoading || Boolean(item.status)); const statusIcon = !item.isLoading && item.status ? ( ) : null; const handleSelect = () => { if (!item.disabled) { onSelectionChange(item.id, item); } }; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "Enter" || event.key === " ") { event.preventDefault(); handleSelect(); } }; return (
{!showCheckboxes && item.isLoading && } {!showCheckboxes && statusIcon} {showCheckboxes && shouldReplaceCheckboxWithState && ( <> {item.isLoading && } {statusIcon} )} {showCheckboxes && !shouldReplaceCheckboxWithState && ( e.stopPropagation()} /> )} {/* Same shrink wrapper as TreeNode, or a long id overruns its neighbours. */}
{renderItem ? ( renderItem({ item, level, isLeaf: true, isSelected, hasChildren: false, }) ) : ( )}
); }