Advertisement
iko1133

Untitled

Nov 29th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import { useState } from "react";
  2. import SingleForm from "../molecules/SingleForm";
  3. import { Plus } from "../../assets/icons/Plus";
  4. import { Label } from "../ui/label";
  5. import {
  6. SortableContext,
  7. sortableKeyboardCoordinates,
  8. verticalListSortingStrategy,
  9. arrayMove,
  10. } from "@dnd-kit/sortable";
  11. import {
  12. DndContext,
  13. KeyboardSensor,
  14. PointerSensor,
  15. useSensor,
  16. useSensors,
  17. DragOverlay,
  18. closestCenter,
  19. } from "@dnd-kit/core";
  20.  
  21. const ShipmentForm = () => {
  22. const [forms, setForms] = useState<string[]>(["1"]);
  23. const [activeId, setActiveId] = useState(null);
  24.  
  25. const sensors = useSensors(
  26. useSensor(PointerSensor),
  27. useSensor(KeyboardSensor, {
  28. coordinateGetter: sortableKeyboardCoordinates,
  29. })
  30. );
  31.  
  32. const addForm = () =>
  33. setForms((prev) => [...prev, (prev.length + 1).toString()]);
  34.  
  35. const onRemoveForm = (id: string) =>
  36. setForms((prev) => prev.filter((item) => item !== id));
  37.  
  38. function handleDragStart(event: any) {
  39. const { active } = event;
  40.  
  41. setActiveId(active.id);
  42. }
  43.  
  44. function handleDragEnd(event: any) {
  45. const { active, over } = event;
  46.  
  47. if (active.id !== over.id) {
  48. setForms((items) => {
  49. const oldIndex = items.indexOf(active.id);
  50. const newIndex = items.indexOf(over.id);
  51.  
  52. return arrayMove(items, oldIndex, newIndex);
  53. });
  54. }
  55.  
  56. setActiveId(null);
  57. }
  58.  
  59. return (
  60. <DndContext
  61. sensors={sensors}
  62. collisionDetection={closestCenter}
  63. onDragStart={handleDragStart}
  64. onDragEnd={handleDragEnd}
  65. >
  66. <SortableContext items={forms} strategy={verticalListSortingStrategy}>
  67. <div className="overflow-y-scroll h-full pb-[100px]">
  68. {forms.map((item, index) => (
  69. <SingleForm
  70. key={item}
  71. id={item}
  72. hasArrow={index !== 0}
  73. onRemove={onRemoveForm}
  74. removeEnabled={forms.length > 1}
  75. />
  76. ))}
  77.  
  78. <div className="flex flex-row items-center mt-4" onClick={addForm}>
  79. <Plus />
  80. <Label style={{ color: "#06B9FC", marginLeft: 8 }}>
  81. Додај нови претовар
  82. </Label>
  83. </div>
  84. </div>
  85. </SortableContext>
  86. {/* <DragOverlay>
  87. {activeId ? <SingleForm id={activeId} /> : null}
  88. </DragOverlay> */}
  89. </DndContext>
  90. );
  91. };
  92.  
  93. export default ShipmentForm;
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement