clauderic / dnd-kit

The modern, lightweight, performant, accessible and extensible drag & drop toolkit for React.
http://dndkit.com
MIT License
12.87k stars 640 forks source link

Drag, drop, sort - sorting is not animating, but results of onDragEnd produce correct results #1058

Open GradySain opened 1 year ago

GradySain commented 1 year ago

Hello,

Here is a link to my issue - set up in StackBlitz.

https://stackblitz.com/edit/react-dnitfq?file=src/components/App.js

I am trying to build a fairly complex (for me) scenario:

The problem- the sortable elements do not animate when they are being dragged. But once they are dropped, their order is correct.

I think I am doing something wrong (either a small error, or a very big misunderstanding of how to set this up). Any help would be greatly appreciated :)

GradySain commented 1 year ago

Okay I think I have a fix.

Found a similar issue addressed here: https://github.com/clauderic/dnd-kit/issues/183

I was using an array of objects as the items in SortableContext, as opposed to an array of ints.

For anyone that wants to fix the stack blitz link above and learn how the fix works:

fullnelson14 commented 1 year ago

I've got the same issue. I am doing everything in that was suggested above, but no dice. The <SortableContext> is getting an array of strings, but still no animations. Any other pointers?

fullnelson14 commented 1 year ago

Ah! Nevermind! The <SortableContext> needs INTs! Not strings! At least that is what fixed it for me.

saltcod commented 1 year ago

Doesn't need ints but does need an array of unique ids. So instead of passing an array of todos, for example, you need to pass an array of todo ids.

const [items, setItems] = useState(todos.map((todo) => todo.id))

<SortableContext items={items} strategy={verticalListSortingStrategy} >
   {items.map((item) => (
        <SortableItem key={item} id={item} />
    ))}
</SortableContext>