|
| 1 | +import { ContainedList, ContainedListItem } from "@carbon/react" |
| 2 | + |
| 3 | +import { Draggable } from "@carbon/react/icons" |
| 4 | +import { ContainedListProps } from "@carbon/react/lib/components/ContainedList/ContainedList" |
| 5 | +import { Identifier } from "dnd-core" |
| 6 | +import { Children, useEffect, useRef, useState } from "react" |
| 7 | +import { DropTargetMonitor, useDrag, useDrop } from "react-dnd" |
| 8 | + |
| 9 | +const DragTypes = { |
| 10 | + ROW: "list-row", |
| 11 | +} |
| 12 | + |
| 13 | +interface DragItem { |
| 14 | + index: number |
| 15 | + id: string |
| 16 | +} |
| 17 | + |
| 18 | +// "ContainedListItem" prop type not exported by Carbon |
| 19 | +// See https://react.carbondesignsystem.com/?path=/docs/components-containedlist--overview#component-api |
| 20 | +interface DraggableListItemProps { |
| 21 | + id: string |
| 22 | + [key: string]: unknown |
| 23 | +} |
| 24 | + |
| 25 | +interface ListItemWrapperProps { |
| 26 | + id: string |
| 27 | + index: number |
| 28 | + move: (dragIdx: number, hoverIdx: number) => void |
| 29 | + onDrop: () => void |
| 30 | + renderListItem: React.ReactNode |
| 31 | +} |
| 32 | + |
| 33 | +type DraggableListProps = ContainedListProps & { |
| 34 | + handleDrop?: (ids: string[]) => void |
| 35 | +} |
| 36 | + |
| 37 | +interface HoverInput { |
| 38 | + item: DragItem |
| 39 | + monitor: DropTargetMonitor<DragItem, void> |
| 40 | + hoverIdx: number |
| 41 | + move: (dragIdx: number, hoverIdx: number) => void |
| 42 | + ref: React.RefObject<HTMLDivElement> |
| 43 | +} |
| 44 | + |
| 45 | +type ListElementsType = React.ReactElement<DraggableListItemProps>[] |
| 46 | + |
| 47 | +const handleHover = ({ item, monitor, hoverIdx, move, ref }: HoverInput) => { |
| 48 | + if (!ref.current) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + const dragIdx = item.index |
| 53 | + |
| 54 | + // Don't replace items with themselves |
| 55 | + if (dragIdx === hoverIdx) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const hoverBoundingRect = ref.current?.getBoundingClientRect() |
| 60 | + |
| 61 | + // Get vertical middle |
| 62 | + const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2 |
| 63 | + |
| 64 | + // Determine mouse position |
| 65 | + const clientOffset = monitor.getClientOffset() |
| 66 | + |
| 67 | + // Get pixels to the top |
| 68 | + const hoverClientY = clientOffset!.y - hoverBoundingRect.top |
| 69 | + |
| 70 | + // Only perform the move when the mouse has crossed half of the items height |
| 71 | + |
| 72 | + // Dragging downwards |
| 73 | + if (dragIdx < hoverIdx && hoverClientY < hoverMiddleY) { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // Dragging upwards |
| 78 | + if (dragIdx > hoverIdx && hoverClientY > hoverMiddleY) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // Apply the reordering |
| 83 | + move(dragIdx, hoverIdx) |
| 84 | + |
| 85 | + item.index = hoverIdx |
| 86 | +} |
| 87 | + |
| 88 | +const ListItemWrapper = ({ |
| 89 | + id, |
| 90 | + index, |
| 91 | + move, |
| 92 | + onDrop, |
| 93 | + renderListItem, |
| 94 | +}: ListItemWrapperProps) => { |
| 95 | + const ref = useRef<HTMLDivElement>(null) |
| 96 | + const [{ handlerId }, drop] = useDrop< |
| 97 | + DragItem, |
| 98 | + void, |
| 99 | + { handlerId: Identifier | null } |
| 100 | + >({ |
| 101 | + accept: DragTypes.ROW, |
| 102 | + collect: (monitor) => ({ |
| 103 | + handlerId: monitor.getHandlerId(), |
| 104 | + }), |
| 105 | + drop: () => onDrop && onDrop(), |
| 106 | + hover: (item: DragItem, monitor) => |
| 107 | + handleHover({ item, monitor, hoverIdx: index, move, ref }), |
| 108 | + }) |
| 109 | + |
| 110 | + const [{ isDragging }, drag] = useDrag({ |
| 111 | + type: DragTypes.ROW, |
| 112 | + item: () => ({ |
| 113 | + id, |
| 114 | + index, |
| 115 | + }), |
| 116 | + collect: (monitor) => ({ |
| 117 | + isDragging: monitor.isDragging(), |
| 118 | + }), |
| 119 | + }) |
| 120 | + |
| 121 | + drag(drop(ref)) |
| 122 | + |
| 123 | + const opacity = isDragging ? 0 : 1 |
| 124 | + return ( |
| 125 | + <div ref={ref} style={{ opacity }} data-handler-id={handlerId}> |
| 126 | + {renderListItem} |
| 127 | + </div> |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +// Drag and drop code adapted from https://react-dnd.github.io/react-dnd/examples/sortable/simple |
| 132 | +export const DraggableListItem = (props: DraggableListItemProps) => ( |
| 133 | + <ContainedListItem {...props} renderIcon={Draggable} /> |
| 134 | +) |
| 135 | + |
| 136 | +export const DraggableList = ({ handleDrop, ...props }: DraggableListProps) => { |
| 137 | + const [rows, setRows] = useState( |
| 138 | + () => Children.toArray(props.children) as ListElementsType |
| 139 | + ) |
| 140 | + |
| 141 | + useEffect( |
| 142 | + () => setRows(Children.toArray(props.children) as ListElementsType), |
| 143 | + [props.children] |
| 144 | + ) |
| 145 | + |
| 146 | + const moveRow = (dragIdx: number, hoverIdx: number) => |
| 147 | + setRows((prev) => { |
| 148 | + const items = [...prev] |
| 149 | + const item = items.splice(dragIdx, 1)[0] |
| 150 | + items.splice(hoverIdx, 0, item) |
| 151 | + return items |
| 152 | + }) |
| 153 | + |
| 154 | + return ( |
| 155 | + <ContainedList {...props}> |
| 156 | + {rows.map((row, idx) => ( |
| 157 | + <ListItemWrapper |
| 158 | + key={row.props.id} |
| 159 | + id={row.props.id} |
| 160 | + index={idx} |
| 161 | + move={moveRow} |
| 162 | + onDrop={() => handleDrop && handleDrop(rows.map((r) => r.props.id))} |
| 163 | + renderListItem={row} |
| 164 | + ></ListItemWrapper> |
| 165 | + ))} |
| 166 | + </ContainedList> |
| 167 | + ) |
| 168 | +} |
0 commit comments