dragNDropContext.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. closestCenter,
  3. DndContext,
  4. KeyboardSensor,
  5. PointerSensor,
  6. useSensor,
  7. useSensors,
  8. } from '@dnd-kit/core';
  9. import {
  10. SortableContext,
  11. sortableKeyboardCoordinates,
  12. verticalListSortingStrategy,
  13. } from '@dnd-kit/sortable';
  14. import {type Column, useDragNDropColumns} from '../hooks/useDragNDropColumns';
  15. interface DragNDropContextProps {
  16. children: (props: {
  17. deleteColumnAtIndex: (i: number) => void;
  18. editableColumns: Column[];
  19. insertColumn: () => void;
  20. updateColumnAtIndex: (i: number, column: string) => void;
  21. }) => React.ReactNode;
  22. columns: string[];
  23. setColumns: (columns: string[]) => void;
  24. }
  25. export function DragNDropContext({columns, setColumns, children}: DragNDropContextProps) {
  26. const {
  27. editableColumns,
  28. insertColumn,
  29. updateColumnAtIndex,
  30. deleteColumnAtIndex,
  31. onDragEnd,
  32. } = useDragNDropColumns({columns, setColumns});
  33. const sensors = useSensors(
  34. useSensor(PointerSensor),
  35. useSensor(KeyboardSensor, {
  36. coordinateGetter: sortableKeyboardCoordinates,
  37. })
  38. );
  39. return (
  40. <DndContext
  41. sensors={sensors}
  42. collisionDetection={closestCenter}
  43. onDragEnd={onDragEnd}
  44. >
  45. <SortableContext items={editableColumns} strategy={verticalListSortingStrategy}>
  46. {children({
  47. editableColumns,
  48. insertColumn,
  49. updateColumnAtIndex,
  50. deleteColumnAtIndex,
  51. })}
  52. </SortableContext>
  53. </DndContext>
  54. );
  55. }