|
| 1 | +import { ComponentRegistry } from '@object-ui/core'; |
| 2 | +import { ChevronRight, ChevronDown, Folder, File } from 'lucide-react'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { cn } from '@/lib/utils'; |
| 5 | + |
| 6 | +interface TreeNode { |
| 7 | + id: string; |
| 8 | + label: string; |
| 9 | + icon?: string; |
| 10 | + children?: TreeNode[]; |
| 11 | + data?: any; |
| 12 | +} |
| 13 | + |
| 14 | +const TreeNodeComponent = ({ |
| 15 | + node, |
| 16 | + level = 0, |
| 17 | + onNodeClick |
| 18 | +}: { |
| 19 | + node: TreeNode; |
| 20 | + level?: number; |
| 21 | + onNodeClick?: (node: TreeNode) => void; |
| 22 | +}) => { |
| 23 | + const [isOpen, setIsOpen] = useState(false); |
| 24 | + const hasChildren = node.children && node.children.length > 0; |
| 25 | + |
| 26 | + const handleToggle = (e: React.MouseEvent) => { |
| 27 | + e.stopPropagation(); |
| 28 | + setIsOpen(!isOpen); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleClick = () => { |
| 32 | + if (onNodeClick) { |
| 33 | + onNodeClick(node); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <div |
| 40 | + className={cn( |
| 41 | + 'flex items-center py-1.5 px-2 hover:bg-accent rounded-md cursor-pointer', |
| 42 | + 'transition-colors' |
| 43 | + )} |
| 44 | + style={{ paddingLeft: `${level * 16 + 8}px` }} |
| 45 | + onClick={handleClick} |
| 46 | + > |
| 47 | + {hasChildren ? ( |
| 48 | + <button |
| 49 | + onClick={handleToggle} |
| 50 | + className="mr-1 p-0 h-4 w-4 flex items-center justify-center hover:bg-accent/50 rounded" |
| 51 | + > |
| 52 | + {isOpen ? ( |
| 53 | + <ChevronDown className="h-4 w-4" /> |
| 54 | + ) : ( |
| 55 | + <ChevronRight className="h-4 w-4" /> |
| 56 | + )} |
| 57 | + </button> |
| 58 | + ) : ( |
| 59 | + <span className="mr-1 w-4" /> |
| 60 | + )} |
| 61 | + {node.icon === 'folder' ? ( |
| 62 | + <Folder className="h-4 w-4 mr-2 text-muted-foreground" /> |
| 63 | + ) : node.icon === 'file' ? ( |
| 64 | + <File className="h-4 w-4 mr-2 text-muted-foreground" /> |
| 65 | + ) : null} |
| 66 | + <span className="text-sm">{node.label}</span> |
| 67 | + </div> |
| 68 | + {hasChildren && isOpen && ( |
| 69 | + <div> |
| 70 | + {node.children!.map((child) => ( |
| 71 | + <TreeNodeComponent |
| 72 | + key={child.id} |
| 73 | + node={child} |
| 74 | + level={level + 1} |
| 75 | + onNodeClick={onNodeClick} |
| 76 | + /> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + ); |
| 82 | +}; |
| 83 | + |
| 84 | +ComponentRegistry.register('tree-view', |
| 85 | + ({ schema, className, ...props }) => { |
| 86 | + const handleNodeClick = (node: TreeNode) => { |
| 87 | + if (schema.onNodeClick) { |
| 88 | + schema.onNodeClick(node); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + return ( |
| 93 | + <div className={cn('border rounded-md p-2 bg-background', className)} {...props}> |
| 94 | + {schema.title && ( |
| 95 | + <h3 className="text-sm font-semibold mb-2 px-2">{schema.title}</h3> |
| 96 | + )} |
| 97 | + <div className="space-y-0.5"> |
| 98 | + {schema.nodes?.map((node: TreeNode) => ( |
| 99 | + <TreeNodeComponent |
| 100 | + key={node.id} |
| 101 | + node={node} |
| 102 | + onNodeClick={handleNodeClick} |
| 103 | + /> |
| 104 | + ))} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ); |
| 108 | + }, |
| 109 | + { |
| 110 | + label: 'Tree View', |
| 111 | + inputs: [ |
| 112 | + { name: 'title', type: 'string', label: 'Title' }, |
| 113 | + { |
| 114 | + name: 'nodes', |
| 115 | + type: 'array', |
| 116 | + label: 'Tree Nodes', |
| 117 | + description: 'Array of { id, label, icon, children, data }' |
| 118 | + }, |
| 119 | + { name: 'className', type: 'string', label: 'CSS Class' } |
| 120 | + ], |
| 121 | + defaultProps: { |
| 122 | + title: 'File Explorer', |
| 123 | + nodes: [ |
| 124 | + { |
| 125 | + id: '1', |
| 126 | + label: 'Documents', |
| 127 | + icon: 'folder', |
| 128 | + children: [ |
| 129 | + { id: '1-1', label: 'Resume.pdf', icon: 'file' }, |
| 130 | + { id: '1-2', label: 'Cover Letter.docx', icon: 'file' } |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + id: '2', |
| 135 | + label: 'Photos', |
| 136 | + icon: 'folder', |
| 137 | + children: [ |
| 138 | + { id: '2-1', label: 'Vacation', icon: 'folder', children: [ |
| 139 | + { id: '2-1-1', label: 'Beach.jpg', icon: 'file' } |
| 140 | + ]}, |
| 141 | + { id: '2-2', label: 'Family.jpg', icon: 'file' } |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + id: '3', |
| 146 | + label: 'README.md', |
| 147 | + icon: 'file' |
| 148 | + } |
| 149 | + ] |
| 150 | + } |
| 151 | + } |
| 152 | +); |
0 commit comments