|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { useElementBoundingRect } from "@lib/hooks/useElementBoundingRect"; |
| 4 | +import { createPortal } from "@lib/utils/createPortal"; |
| 5 | +import { resolveClassNames } from "@lib/utils/resolveClassNames"; |
| 6 | +import { DragIndicator, ExpandLess, ExpandMore } from "@mui/icons-material"; |
| 7 | + |
| 8 | +import { HoveredArea, SortableListContext } from "./sortableList"; |
| 9 | +import { SortableListDropIndicator } from "./sortableListDropIndicator"; |
| 10 | +import { SortableListItemProps } from "./sortableListItem"; |
| 11 | + |
| 12 | +export type SortableListGroupProps = { |
| 13 | + id: string; |
| 14 | + title: React.ReactNode; |
| 15 | + initiallyExpanded?: boolean; |
| 16 | + startAdornment?: React.ReactNode; |
| 17 | + endAdornment?: React.ReactNode; |
| 18 | + headerStyle?: React.CSSProperties; |
| 19 | + contentStyle?: React.CSSProperties; |
| 20 | + contentWhenEmpty?: React.ReactNode; |
| 21 | + children?: React.ReactElement<SortableListItemProps>[]; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * |
| 26 | + * @param {SortableListGroupProps} props Object of properties for the SortableListGroup component (see below for details). |
| 27 | + * @param {string} props.id ID that is unique among all components inside the sortable list. |
| 28 | + * @param {React.ReactNode} props.title Title of the list item. |
| 29 | + * @param {boolean} props.initiallyExpanded Whether the group should be expanded by default. |
| 30 | + * @param {React.ReactNode} props.startAdornment Start adornment to display to the left of the title. |
| 31 | + * @param {React.ReactNode} props.endAdornment End adornment to display to the right of the title. |
| 32 | + * @param {React.CSSProperties} props.headerStyle Style object to apply to the header of the group. |
| 33 | + * @param {React.CSSProperties} props.contentStyle Style object to apply to the content of the group. |
| 34 | + * @param {React.ReactNode} props.contentWhenEmpty Content to display when the group is empty. |
| 35 | + * @param {React.ReactNode} props.children Child components to display as the content of the list item. |
| 36 | + * |
| 37 | + * @returns {React.ReactNode} A sortable list group component. |
| 38 | + */ |
| 39 | +export function SortableListGroup(props: SortableListGroupProps): React.ReactNode { |
| 40 | + const [isExpanded, setIsExpanded] = React.useState<boolean>(props.initiallyExpanded ?? true); |
| 41 | + |
| 42 | + const divRef = React.useRef<HTMLDivElement>(null); |
| 43 | + const boundingClientRect = useElementBoundingRect(divRef); |
| 44 | + const sortableListContext = React.useContext(SortableListContext); |
| 45 | + |
| 46 | + const isHovered = sortableListContext.hoveredElementId === props.id; |
| 47 | + const isHeaderHovered = |
| 48 | + isHovered && |
| 49 | + (sortableListContext.hoveredArea === HoveredArea.HEADER || |
| 50 | + sortableListContext.hoveredArea === HoveredArea.CENTER); |
| 51 | + const isDragging = sortableListContext.draggedElementId === props.id; |
| 52 | + const dragPosition = sortableListContext.dragPosition; |
| 53 | + |
| 54 | + function handleToggleExpanded() { |
| 55 | + setIsExpanded(!isExpanded); |
| 56 | + } |
| 57 | + |
| 58 | + const hasContent = props.children !== undefined && props.children.length > 0; |
| 59 | + |
| 60 | + return ( |
| 61 | + <> |
| 62 | + {isHovered && sortableListContext.hoveredArea === HoveredArea.TOP && <SortableListDropIndicator />} |
| 63 | + <div |
| 64 | + className={resolveClassNames("sortable-list-element sortable-list-group relative bg-gray-200")} |
| 65 | + data-item-id={props.id} |
| 66 | + ref={divRef} |
| 67 | + > |
| 68 | + <div |
| 69 | + className={resolveClassNames("z-30 w-full h-full absolute left-0 top-0 bg-blue-500", { |
| 70 | + hidden: !isDragging, |
| 71 | + })} |
| 72 | + ></div> |
| 73 | + <Header |
| 74 | + onToggleExpanded={handleToggleExpanded} |
| 75 | + expanded={isExpanded} |
| 76 | + expandable={hasContent} |
| 77 | + hovered={isHeaderHovered} |
| 78 | + {...props} |
| 79 | + /> |
| 80 | + {isDragging && |
| 81 | + dragPosition && |
| 82 | + createPortal( |
| 83 | + <div |
| 84 | + className={resolveClassNames( |
| 85 | + "flex h-8 bg-blue-50 text-sm items-center gap-1 border-b border-b-gray-300 absolute z-50 opacity-75" |
| 86 | + )} |
| 87 | + style={{ |
| 88 | + left: dragPosition.x, |
| 89 | + top: dragPosition.y, |
| 90 | + width: isDragging ? boundingClientRect.width : undefined, |
| 91 | + }} |
| 92 | + > |
| 93 | + <Header |
| 94 | + expanded={isExpanded} |
| 95 | + expandable={hasContent} |
| 96 | + hovered={isHeaderHovered} |
| 97 | + {...props} |
| 98 | + /> |
| 99 | + </div> |
| 100 | + )} |
| 101 | + <div |
| 102 | + className={resolveClassNames( |
| 103 | + "sortable-list-group-content pl-1 bg-white shadow-inner border-b border-b-gray-300", |
| 104 | + { |
| 105 | + hidden: !isExpanded, |
| 106 | + } |
| 107 | + )} |
| 108 | + style={props.contentStyle} |
| 109 | + > |
| 110 | + {hasContent ? props.children : props.contentWhenEmpty} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + {isHovered && sortableListContext.hoveredArea === HoveredArea.BOTTOM && <SortableListDropIndicator />} |
| 114 | + </> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +type HeaderProps = { |
| 119 | + title: React.ReactNode; |
| 120 | + expanded: boolean; |
| 121 | + expandable: boolean; |
| 122 | + hovered: boolean; |
| 123 | + onToggleExpanded?: () => void; |
| 124 | + icon?: React.ReactNode; |
| 125 | + startAdornment?: React.ReactNode; |
| 126 | + endAdornment?: React.ReactNode; |
| 127 | + headerStyle?: React.CSSProperties; |
| 128 | +}; |
| 129 | + |
| 130 | +function Header(props: HeaderProps): React.ReactNode { |
| 131 | + return ( |
| 132 | + <div |
| 133 | + className={resolveClassNames( |
| 134 | + "sortable-list-item-header flex w-full items-center gap-1 h-8 text-sm border-b border-b-gray-400 px-2", |
| 135 | + { |
| 136 | + "!bg-blue-300": props.hovered, |
| 137 | + "bg-slate-300": !props.hovered, |
| 138 | + } |
| 139 | + )} |
| 140 | + style={props.headerStyle} |
| 141 | + > |
| 142 | + <div className={resolveClassNames("sortable-list-element-indicator hover:cursor-grab")}> |
| 143 | + <DragIndicator fontSize="inherit" className="pointer-events-none" /> |
| 144 | + </div> |
| 145 | + {props.expandable && ( |
| 146 | + <div |
| 147 | + className="hover:cursor-pointer hover:text-blue-800 p-0.5 rounded" |
| 148 | + onClick={props.onToggleExpanded} |
| 149 | + title={props.expanded ? "Hide children" : "Show children"} |
| 150 | + > |
| 151 | + {props.expanded ? <ExpandLess fontSize="inherit" /> : <ExpandMore fontSize="inherit" />} |
| 152 | + </div> |
| 153 | + )} |
| 154 | + <div className="flex items-center gap-2 flex-grow"> |
| 155 | + {props.startAdornment} |
| 156 | + <div className="flex-grow font-bold">{props.title}</div> |
| 157 | + {props.endAdornment} |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +} |
0 commit comments