Using an array of objects with SortableContext #869
-
Hello, I have a sortable list of images, uploaded by the user, at the moment comprising an array of data:image base64s. I would like to be able to use an array of objects with SortableContext as I need the file object to upload to AWS S3 after the images have been sorted This is my current code, using dataOne - I would like to be able to use dataTwo: const dataOne = ['data:image/png;base64..1..', 'data:image/png;base64..2..', 'data:image/png;base64..3..', 'data:image/png;base64..4..', 'data:image/png;base64..5..']
const dataTwo = [
{ id: '1', file: '/image-1.png', src: 'data:image/png;base64..1..' },
{ id: '2', file: '/image-2.png', src: 'data:image/png;base64..2..' },
{ id: '3', file: '/image-3.png', src: 'data:image/png;base64..3..' },
{ id: '4', file: '/image-4.png', src: 'data:image/png;base64..4..' },
{ id: '5', file: '/image-5.png', src: 'data:image/png;base64..5..' },
]
const UploadGallery = () => {
const [previewImages, setPreviewImages] = useState(dataOne)
const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor))
return (
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
onDragCancel={handleDragCancel}>
<SortableContext items={previewImages} strategy={rectSortingStrategy}>
<div className='grid grid-cols-4 gap-5'>
{previewImages.map((url, index) => (
<SortablePhoto key={url} url={url} index={index} />
))}
</div>
</SortableContext>
</DndContext>
)
function handleDragStart(event) {
setActiveId(event.active.id)
}
function handleDragEnd(event) {
const { active, over } = event
if (active.id !== over.id) {
setPreviewImages((previewImages) => {
const oldIndex = previewImages.indexOf(active.id)
const newIndex = previewImages.indexOf(over.id)
return arrayMove(previewImages, oldIndex, newIndex)
})
}
setActiveId(null)
}
function handleDragCancel() {
setActiveId(null)
}
} I have tried doing something like: <SortableContext items={previewImages.map((image) => image.id)} strategy={rectSortingStrategy}>
<div className='grid grid-cols-4 gap-5'>
{previewImages.map((image, index) => (
<SortablePhoto key={image.src} url={image.src} index={index} />
))}
</div>
</SortableContext> This is the SortablePhoto component: export const SortablePhoto = (props) => {
const sortable = useSortable({ id: props.url })
const { attributes, listeners, isDragging, setNodeRef, transform, transition } = sortable
const style = {
transform: CSS.Transform.toString(transform),
transition,
}
return <Photo ref={setNodeRef} style={style} {...props} {...attributes} {...listeners} />
} Many thanks for any help or advice. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
You can definitely use arrays of objects for the items in SortableContext. You can see that being done here: https://codesandbox.io/s/22x1ix?file=/src/App.tsx. If you could make a quick codesandbox mocking/replicating the problem I would be happy to try to help debug. |
Beta Was this translation helpful? Give feedback.
You can definitely use arrays of objects for the items in SortableContext. You can see that being done here: https://codesandbox.io/s/22x1ix?file=/src/App.tsx. If you could make a quick codesandbox mocking/replicating the problem I would be happy to try to help debug.