import { Box, Tbody } from '@chakra-ui/react'; import React, { ReactNode, useState } from 'react'; import { DragDropContext, Droppable, DraggableChildrenFn, DragStart, DropResult, DroppableProvided, DroppableStateSnapshot } from 'react-beautiful-dnd'; export * from 'react-beautiful-dnd'; type Props = { onDragEndCb: (result: T[]) => void; renderClone?: DraggableChildrenFn; children: | ((provided: DroppableProvided, snapshot: DroppableStateSnapshot) => ReactNode) | ReactNode; dataList: T[]; isTable?: boolean; zoom?: number; }; function DndDrag({ children, renderClone, onDragEndCb, dataList, isTable = false, zoom = 1 }: Props) { const [draggingItemHeight, setDraggingItemHeight] = useState(0); const onDragStart = (start: DragStart) => { const draggingNode = document.querySelector(`[data-rbd-draggable-id="${start.draggableId}"]`); setDraggingItemHeight(draggingNode?.getBoundingClientRect().height || 0); }; const onDragEnd = (result: DropResult) => { if (!result.destination) { return; } setDraggingItemHeight(0); const startIndex = result.source.index; const endIndex = result.destination.index; const list = Array.from(dataList); const [removed] = list.splice(startIndex, 1); list.splice(endIndex, 0, removed); onDragEndCb(list); }; return ( {(provided, snapshot) => { return isTable ? ( {typeof children !== 'function' && children} {snapshot.isDraggingOver && } ) : ( {typeof children === 'function' && children(provided, snapshot)} {snapshot.isDraggingOver && } ); }} ); } export default React.memo(DndDrag) as (props: Props) => React.ReactElement;