From 8daec9a4c504ee187f147292872dcc7cb7ff804a Mon Sep 17 00:00:00 2001 From: balibabu Date: Wed, 19 Mar 2025 15:44:59 +0800 Subject: [PATCH] Feat: Alter TreeView component #3221 (#6272) ### What problem does this PR solve? Feat: Alter TreeView component #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- web/src/components/ui/tree-view.tsx | 492 ++++++++++++++-------------- 1 file changed, 246 insertions(+), 246 deletions(-) diff --git a/web/src/components/ui/tree-view.tsx b/web/src/components/ui/tree-view.tsx index 68d29d5a4..8908363b7 100644 --- a/web/src/components/ui/tree-view.tsx +++ b/web/src/components/ui/tree-view.tsx @@ -14,7 +14,7 @@ const selectedTreeVariants = cva( 'before:opacity-100 before:bg-accent/70 text-accent-foreground', ); -interface TreeDataItem { +export interface TreeDataItem { id: string; name: string; icon?: any; @@ -25,251 +25,6 @@ interface TreeDataItem { onClick?: () => void; } -const AccordionTrigger = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, children, ...props }, ref) => ( - - svg]:rotate-90', - className, - )} - {...props} - > - - {children} - - -)); -AccordionTrigger.displayName = AccordionPrimitive.Trigger.displayName; - -const AccordionContent = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, children, ...props }, ref) => ( - -
{children}
-
-)); -AccordionContent.displayName = AccordionPrimitive.Content.displayName; - -const TreeIcon = ({ - item, - isOpen, - isSelected, - default: defaultIcon, -}: { - item: TreeDataItem; - isOpen?: boolean; - isSelected?: boolean; - default?: any; -}) => { - let Icon = defaultIcon; - if (isSelected && item.selectedIcon) { - Icon = item.selectedIcon; - } else if (isOpen && item.openIcon) { - Icon = item.openIcon; - } else if (item.icon) { - Icon = item.icon; - } - return Icon ? : <>; -}; - -const TreeActions = ({ - children, - isSelected, -}: { - children: React.ReactNode; - isSelected: boolean; -}) => { - return ( -
- {children} -
- ); -}; - -const TreeNode = ({ - item, - handleSelectChange, - expandedItemIds, - selectedItemId, - defaultNodeIcon, - defaultLeafIcon, -}: { - item: TreeDataItem; - handleSelectChange: (item: TreeDataItem | undefined) => void; - expandedItemIds: string[]; - selectedItemId?: string; - defaultNodeIcon?: any; - defaultLeafIcon?: any; -}) => { - const [value, setValue] = React.useState( - expandedItemIds.includes(item.id) ? [item.id] : [], - ); - return ( - setValue(s)} - > - - { - handleSelectChange(item); - item.onClick?.(); - }} - > - - {item.name} - - {item.actions} - - - - - - - - ); -}; - -type TreeItemProps = TreeProps & { - selectedItemId?: string; - handleSelectChange: (item: TreeDataItem | undefined) => void; - expandedItemIds: string[]; - defaultNodeIcon?: any; - defaultLeafIcon?: any; -}; - -const TreeItem = React.forwardRef( - ( - { - className, - data, - selectedItemId, - handleSelectChange, - expandedItemIds, - defaultNodeIcon, - defaultLeafIcon, - ...props - }, - ref, - ) => { - if (!(data instanceof Array)) { - data = [data]; - } - return ( -
-
    - {data.map((item) => ( -
  • - {item.children ? ( - - ) : ( - - )} -
  • - ))} -
-
- ); - }, -); -TreeItem.displayName = 'TreeItem'; - -const TreeLeaf = React.forwardRef< - HTMLDivElement, - React.HTMLAttributes & { - item: TreeDataItem; - selectedItemId?: string; - handleSelectChange: (item: TreeDataItem | undefined) => void; - defaultLeafIcon?: any; - } ->( - ( - { - className, - item, - selectedItemId, - handleSelectChange, - defaultLeafIcon, - ...props - }, - ref, - ) => { - return ( -
{ - handleSelectChange(item); - item.onClick?.(); - }} - {...props} - > - - {item.name} - - {item.actions} - -
- ); - }, -); -TreeLeaf.displayName = 'TreeLeaf'; - type TreeProps = React.HTMLAttributes & { data: TreeDataItem[] | TreeDataItem; initialSelectedItemId?: string; @@ -355,4 +110,249 @@ const TreeView = React.forwardRef( ); TreeView.displayName = 'TreeView'; +type TreeItemProps = TreeProps & { + selectedItemId?: string; + handleSelectChange: (item: TreeDataItem | undefined) => void; + expandedItemIds: string[]; + defaultNodeIcon?: any; + defaultLeafIcon?: any; +}; + +const TreeItem = React.forwardRef( + ( + { + className, + data, + selectedItemId, + handleSelectChange, + expandedItemIds, + defaultNodeIcon, + defaultLeafIcon, + ...props + }, + ref, + ) => { + if (!(data instanceof Array)) { + data = [data]; + } + return ( +
+
    + {data.map((item) => ( +
  • + {item.children ? ( + + ) : ( + + )} +
  • + ))} +
+
+ ); + }, +); +TreeItem.displayName = 'TreeItem'; + +const TreeNode = ({ + item, + handleSelectChange, + expandedItemIds, + selectedItemId, + defaultNodeIcon, + defaultLeafIcon, +}: { + item: TreeDataItem; + handleSelectChange: (item: TreeDataItem | undefined) => void; + expandedItemIds: string[]; + selectedItemId?: string; + defaultNodeIcon?: any; + defaultLeafIcon?: any; +}) => { + const [value, setValue] = React.useState( + expandedItemIds.includes(item.id) ? [item.id] : [], + ); + return ( + setValue(s)} + > + + { + handleSelectChange(item); + item.onClick?.(); + }} + > + + {item.name} + + {item.actions} + + + + + + + + ); +}; + +const TreeLeaf = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes & { + item: TreeDataItem; + selectedItemId?: string; + handleSelectChange: (item: TreeDataItem | undefined) => void; + defaultLeafIcon?: any; + } +>( + ( + { + className, + item, + selectedItemId, + handleSelectChange, + defaultLeafIcon, + ...props + }, + ref, + ) => { + return ( +
{ + handleSelectChange(item); + item.onClick?.(); + }} + {...props} + > + + {item.name} + + {item.actions} + +
+ ); + }, +); +TreeLeaf.displayName = 'TreeLeaf'; + +const AccordionTrigger = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, children, ...props }, ref) => ( + + svg]:rotate-90', + className, + )} + {...props} + > + + {children} + + +)); +AccordionTrigger.displayName = AccordionPrimitive.Trigger.displayName; + +const AccordionContent = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, children, ...props }, ref) => ( + +
{children}
+
+)); +AccordionContent.displayName = AccordionPrimitive.Content.displayName; + +const TreeIcon = ({ + item, + isOpen, + isSelected, + default: defaultIcon, +}: { + item: TreeDataItem; + isOpen?: boolean; + isSelected?: boolean; + default?: any; +}) => { + let Icon = defaultIcon; + if (isSelected && item.selectedIcon) { + Icon = item.selectedIcon; + } else if (isOpen && item.openIcon) { + Icon = item.openIcon; + } else if (item.icon) { + Icon = item.icon; + } + return Icon ? : <>; +}; + +const TreeActions = ({ + children, + isSelected, +}: { + children: React.ReactNode; + isSelected: boolean; +}) => { + return ( +
+ {children} +
+ ); +}; + export { TreeView, type TreeDataItem };