import { DragDropContext, Draggable, Droppable, DropResult, } from '@hello-pangea/dnd'; import React from 'react'; type DraggableListProps = { items: T[]; onOrderChange: (items: T[]) => void; renderItem: (item: T, index: number) => React.ReactNode; isDisabled?: boolean; }; /** * Implementa la lógica del drag and drop. * * Se puede "deshabilitar", y al hacer algún cambio en el orden, * llama a una función (del componente padre) que actualiza la lista de elementos. */ const DraggableList = ({ items, onOrderChange, renderItem, isDisabled = false, }: DraggableListProps) => { const handleDragEnd = (result: DropResult) => { if (!result.destination) { return; } const updatedItems = Array.from(items); const [movedItem] = updatedItems.splice(result.source.index, 1); updatedItems.splice(result.destination.index, 0, movedItem); onOrderChange(updatedItems); }; return ( {provided => (
{items.map((item, index) => ( {provided => (
{renderItem(item, index)}
)}
))} {provided.placeholder}
)}
); }; export default DraggableList;