daisy
1 year ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
12 additions and
3 deletions
-
components/table/demo/drag-sorting.tsx
|
|
@ -1,9 +1,9 @@ |
|
|
|
import type { DragEndEvent } from '@dnd-kit/core'; |
|
|
|
import { DndContext } from '@dnd-kit/core'; |
|
|
|
import { DndContext, PointerSensor, useSensor, useSensors } from '@dnd-kit/core'; |
|
|
|
import { restrictToVerticalAxis } from '@dnd-kit/modifiers'; |
|
|
|
import { |
|
|
|
arrayMove, |
|
|
|
SortableContext, |
|
|
|
arrayMove, |
|
|
|
useSortable, |
|
|
|
verticalListSortingStrategy, |
|
|
|
} from '@dnd-kit/sortable'; |
|
|
@ -77,6 +77,15 @@ const App: React.FC = () => { |
|
|
|
}, |
|
|
|
]); |
|
|
|
|
|
|
|
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) => { |
|
|
@ -88,7 +97,7 @@ const App: React.FC = () => { |
|
|
|
}; |
|
|
|
|
|
|
|
return ( |
|
|
|
<DndContext modifiers={[restrictToVerticalAxis]} onDragEnd={onDragEnd}> |
|
|
|
<DndContext sensors={sensors} modifiers={[restrictToVerticalAxis]} onDragEnd={onDragEnd}> |
|
|
|
<SortableContext |
|
|
|
// rowKey array
|
|
|
|
items={dataSource.map((i) => i.key)} |
|
|
|