import { useEffect, useState } from 'react';
import { DragDropContext, Draggable, Droppable } from '@hello-pangea/dnd';
export function List({array, children}) {
return (
{array.map((elem, i) => (
-
{children(elem, i)}
))}
);
}
export function ReorderList({droppableId, array, setArray, children}) {
const [localArray, setLocalArray] = useState(array);
useEffect(() => {
setLocalArray(array);
}, [array])
function reorder(list, startIndex, endIndex) {
const result = Array.from(list);
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
return result;
};
function onDragEnd(result) {
if (!result.destination) return;
if (result.destination.index === result.source.index) return;
const newArray = reorder(array, result.source.index, result.destination.index);
setLocalArray(newArray);
setArray(newArray);
}
return (
{provided => (
{localArray.map((elem, i) => (
-
{provided => (
)}
))}
{provided.placeholder}
)}
);
}