import React, { useState } from 'react'; import type { DragEndEvent } from '@dnd-kit/core'; import { DndContext, PointerSensor, useSensor, useSensors } from '@dnd-kit/core'; import { restrictToVerticalAxis } from '@dnd-kit/modifiers'; import { arrayMove, SortableContext, useSortable, verticalListSortingStrategy, } from '@dnd-kit/sortable'; import { CSS } from '@dnd-kit/utilities'; import { Table } from 'antd'; import type { ColumnsType } from 'antd/es/table'; interface DataType { key: string; name: string; age: number; address: string; } const columns: ColumnsType = [ { title: 'Name', dataIndex: 'name', }, { title: 'Age', dataIndex: 'age', }, { title: 'Address', dataIndex: 'address', }, ]; interface RowProps extends React.HTMLAttributes { 'data-row-key': string; } const Row = (props: RowProps) => { const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: props['data-row-key'], }); const style: React.CSSProperties = { ...props.style, transform: CSS.Translate.toString(transform), transition, cursor: 'move', ...(isDragging ? { position: 'relative', zIndex: 9999 } : {}), }; return ; }; const App: React.FC = () => { const [dataSource, setDataSource] = useState([ { key: '1', name: 'John Brown', age: 32, address: 'Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text', }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', }, ]); const sensors = useSensors( useSensor(PointerSensor, { activationConstraint: { // https://docs.dndkit.com/api-documentation/sensors/pointer#activation-constraints distance: 1, }, }), ); const onDragEnd = ({ active, over }: DragEndEvent) => { if (active.id !== over?.id) { setDataSource((prev) => { const activeIndex = prev.findIndex((i) => i.key === active.id); const overIndex = prev.findIndex((i) => i.key === over?.id); return arrayMove(prev, activeIndex, overIndex); }); } }; return ( i.key)} strategy={verticalListSortingStrategy} > ); }; export default App;