Files
FastGPT/packages/web/components/common/DndDrag/index.tsx
heheer 06a8a5e23d fix: simple mode variables dnd (#3767)
* fix: simple mode variables dnd

* optimize dnd drag
2025-02-12 14:36:04 +08:00

63 lines
1.7 KiB
TypeScript

import { Box, Tbody } from '@chakra-ui/react';
import React, { ReactElement, ReactNode, useState } from 'react';
import {
DragDropContext,
Droppable,
DraggableChildrenFn,
DragStart,
DropResult,
DroppableProvided,
DroppableStateSnapshot
} from 'react-beautiful-dnd';
export * from 'react-beautiful-dnd';
type Props<T = any> = {
onDragEndCb: (result: T[]) => void;
renderClone?: DraggableChildrenFn;
children: ({
provided,
snapshot,
draggingItemHeight
}: {
provided: DroppableProvided;
snapshot: DroppableStateSnapshot;
draggingItemHeight: number;
}) => ReactElement<HTMLElement, string>;
dataList: T[];
};
function DndDrag<T>({ children, renderClone, onDragEndCb, dataList }: Props<T>) {
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 (
<DragDropContext onDragStart={onDragStart} onDragEnd={onDragEnd}>
<Droppable droppableId="droppable" renderClone={renderClone}>
{(provided, snapshot) => children({ provided, snapshot, draggingItemHeight })}
</Droppable>
</DragDropContext>
);
}
export default React.memo(DndDrag) as <T>(props: Props<T>) => React.ReactElement;