|
1 | 1 | import React, { PropsWithChildren, useState } from "react"; |
2 | | -import { cls, EditIcon, IconButton, Typography } from "@firecms/ui"; |
| 2 | +import { cls, EditIcon, IconButton, Typography, ExpandablePanel } from "@firecms/ui"; |
3 | 3 |
|
4 | 4 | export function NavigationGroup({ |
5 | 5 | children, |
6 | 6 | group, |
7 | 7 | minimised, |
8 | 8 | isPreview, |
9 | 9 | isPotentialCardDropTarget, |
10 | | - onEditGroup, // New prop to handle editing |
11 | | - dndDisabled // New prop to disable editing when D&D is off |
| 10 | + onEditGroup, |
| 11 | + dndDisabled, |
| 12 | + collapsed, |
| 13 | + onToggleCollapsed |
12 | 14 | }: PropsWithChildren<{ |
13 | 15 | group: string | undefined, |
14 | 16 | minimised?: boolean, |
15 | 17 | isPreview?: boolean, |
16 | 18 | isPotentialCardDropTarget?: boolean, |
17 | | - onEditGroup?: (groupName: string) => void; // Callback to open dialog |
18 | | - dndDisabled?: boolean; // Added dndDisabled prop |
| 19 | + onEditGroup?: (groupName: string) => void; |
| 20 | + dndDisabled?: boolean; |
| 21 | + collapsed?: boolean; |
| 22 | + onToggleCollapsed?: () => void; |
19 | 23 | }>) { |
20 | 24 |
|
21 | 25 | const [isHovered, setIsHovered] = useState(false); |
22 | 26 | const currentGroupName = group ?? "Views"; |
23 | 27 |
|
| 28 | + // Show caret only when not in preview and there is a toggle handler |
| 29 | + const showCaret = !isPreview && !!onToggleCollapsed; |
| 30 | + |
| 31 | + // Helper for the title content (left side) |
| 32 | + const TitleContent = ( |
| 33 | + <div className={cls("flex items-center", isPreview ? "px-1 py-0.5" : "")} |
| 34 | + > |
| 35 | + <Typography |
| 36 | + variant={isPreview ? "body2" : "caption"} |
| 37 | + component={"h2"} |
| 38 | + color="secondary" |
| 39 | + className={cls( |
| 40 | + "p-4 py-2 rounded", |
| 41 | + "font-medium uppercase text-sm text-surface-600 dark:text-surface-400" |
| 42 | + )} |
| 43 | + > |
| 44 | + {currentGroupName} |
| 45 | + </Typography> |
| 46 | + {!isPreview && onEditGroup && !dndDisabled && ( |
| 47 | + <IconButton |
| 48 | + size="smallest" |
| 49 | + onClick={(e) => { |
| 50 | + e.stopPropagation(); // Prevent toggle on click |
| 51 | + onEditGroup(currentGroupName); |
| 52 | + }} |
| 53 | + className={cls("ml-2 ", isHovered ? "opacity-100" : "opacity-0", "transition-opacity duration-100")} |
| 54 | + > |
| 55 | + <EditIcon size="smallest"/> |
| 56 | + </IconButton> |
| 57 | + )} |
| 58 | + </div> |
| 59 | + ); |
| 60 | + |
24 | 61 | return ( |
25 | 62 | <div className={cls( |
26 | 63 | !isPotentialCardDropTarget ? "my-10" : "my-6", |
27 | 64 | "transition-all duration-200 ease-in-out" |
28 | 65 | )} |
29 | 66 | > |
30 | | - <div className={`flex items-center ${isPreview ? "px-1 py-0.5 m-0" : "ml-3.5 mt-6"} `} |
| 67 | + {/* Preview: static header + content (no caret / no collapse) */} |
| 68 | + {isPreview && ( |
| 69 | + <> |
| 70 | + <div |
| 71 | + className={cls( |
| 72 | + "flex items-center justify-between w-full", |
| 73 | + "p-4 py-2" |
| 74 | + )} |
| 75 | + onMouseEnter={() => setIsHovered(true)} |
| 76 | + onMouseLeave={() => setIsHovered(false)} |
| 77 | + > |
| 78 | + {TitleContent} |
| 79 | + </div> |
| 80 | + {children} |
| 81 | + </> |
| 82 | + )} |
31 | 83 |
|
32 | | - onMouseEnter={() => setIsHovered(true)} |
33 | | - onMouseLeave={() => setIsHovered(false)}> |
34 | | - <Typography |
35 | | - variant={isPreview ? "body2" : "caption"} |
36 | | - component={"h2"} |
37 | | - color="secondary" |
38 | | - // Minimal padding and no margin for preview title |
39 | | - className={`${isPreview ? "px-1 py-0.5" : "ml-3.5"} font-medium uppercase text-sm text-surface-600 dark:text-surface-400`} |
| 84 | + {/* Interactive collapsible version when a toggle handler is provided */} |
| 85 | + {!isPreview && showCaret && ( |
| 86 | + <ExpandablePanel |
| 87 | + invisible |
| 88 | + expanded={!collapsed} |
| 89 | + onExpandedChange={(open) => { |
| 90 | + if (open !== !collapsed) { |
| 91 | + onToggleCollapsed?.(); |
| 92 | + } |
| 93 | + }} |
| 94 | + className={cls("mt-6")} |
| 95 | + titleClassName={cls( |
| 96 | + "min-h-0 p-0 border-none", |
| 97 | + "rounded-t flex items-center justify-between w-full", |
| 98 | + "hover:bg-transparent", |
| 99 | + "cursor-pointer select-none" |
| 100 | + )} |
| 101 | + innerClassName={cls("mt-4", !minimised ? "pt-0" : "")} |
| 102 | + title={ |
| 103 | + <div |
| 104 | + onMouseEnter={() => setIsHovered(true)} |
| 105 | + onMouseLeave={() => setIsHovered(false)} |
| 106 | + className="flex items-center" |
| 107 | + > |
| 108 | + {TitleContent} |
| 109 | + </div> |
| 110 | + } |
40 | 111 | > |
41 | | - {currentGroupName} |
42 | | - </Typography> |
43 | | - {!isPreview && onEditGroup && !dndDisabled && ( |
44 | | - <IconButton |
45 | | - size="smallest" |
46 | | - onClick={(e) => { |
47 | | - e.stopPropagation(); // Prevent other click events |
48 | | - onEditGroup(currentGroupName); |
49 | | - }} |
50 | | - className={cls("ml-2 ", isHovered ? "opacity-100" : "opacity-0", "transition-opacity duration-100")} |
| 112 | + {minimised ? ( |
| 113 | + <div className={cls("mt-4 p-8 bg-surface-accent-200 dark:bg-surface-accent-800 rounded-lg")} |
| 114 | + style={{ minHeight: "50px" }}> |
| 115 | + </div> |
| 116 | + ) : ( |
| 117 | + <div className={cls("mt-4", !minimised ? "pt-0" : "")}> |
| 118 | + {children} |
| 119 | + </div> |
| 120 | + )} |
| 121 | + </ExpandablePanel> |
| 122 | + )} |
| 123 | + |
| 124 | + {/* Non-collapsible (no caret) runtime, keep old behavior */} |
| 125 | + {!isPreview && !showCaret && ( |
| 126 | + <> |
| 127 | + <div |
| 128 | + className={cls( |
| 129 | + "flex items-center justify-between w-full", |
| 130 | + "mt-6" |
| 131 | + )} |
| 132 | + onMouseEnter={() => setIsHovered(true)} |
| 133 | + onMouseLeave={() => setIsHovered(false)} |
51 | 134 | > |
52 | | - <EditIcon size="smallest"/> |
53 | | - </IconButton> |
54 | | - )} |
55 | | - </div> |
| 135 | + {TitleContent} |
| 136 | + </div> |
56 | 137 |
|
57 | | - {isPreview ? ( |
58 | | - children |
59 | | - ) : minimised ? ( |
60 | | - // For minimised view in the main list |
61 | | - <div className={cls("mt-4 p-8 bg-surface-accent-200 dark:bg-surface-accent-800 rounded-lg")} |
62 | | - style={{ minHeight: "50px" }}> |
63 | | - </div> |
64 | | - ) : ( |
65 | | - // If highlighted, the parent div already has padding, so children (NavigationGroupDroppable) don't need extra margin top as much. |
66 | | - // The inner content of NavigationGroupDroppable will define its own padding if needed when active. |
67 | | - <div className={cls("mt-4", !minimised ? "pt-0" : "")}> |
68 | | - {children} |
69 | | - </div> |
| 138 | + {!collapsed && ( |
| 139 | + minimised ? ( |
| 140 | + <div className={cls("mt-4 p-8 bg-surface-accent-200 dark:bg-surface-accent-800 rounded-lg")} |
| 141 | + style={{ minHeight: "50px" }}> |
| 142 | + </div> |
| 143 | + ) : ( |
| 144 | + <div className={cls("mt-4", !minimised ? "pt-0" : "")}> |
| 145 | + {children} |
| 146 | + </div> |
| 147 | + ) |
| 148 | + )} |
| 149 | + </> |
70 | 150 | )} |
71 | 151 | </div> |
72 | 152 | ); |
|
0 commit comments