dnd-kit icon indicating copy to clipboard operation
dnd-kit copied to clipboard

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

Open GradySain opened this issue 3 years ago • 5 comments

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:

  • User can drag from array of objects (on left) into drop zone.
  • While on the left, objects do not have a "sortID" property. They are not sortable on the left.
  • Once they are dropped, these objects are assigned a "sortID" property.
  • Need to keep the objects on the left (user can drop them multiple times into the drop zone).
  • Once in the drop zone, the items need to be sortable.
  • Using SortableContext

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 avatar Mar 07 '23 14:03 GradySain

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:

  • go to Droppable.js
  • insert this code
  • const itemIds = useMemo(() => props.droppedItems.map((item) => item.sortID), [props.droppedItems]);
  • this creates an array of ints from the props objects' sortID properties
  • then change SortableContext to
  • <SortableContext items={itemIds} strategy={rectSortingStrategy}>

GradySain avatar Mar 07 '23 16:03 GradySain

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 avatar Aug 15 '23 00:08 fullnelson14

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

fullnelson14 avatar Aug 15 '23 00:08 fullnelson14

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>

saltcod avatar Oct 01 '23 07:10 saltcod

This issue was opened against a previous version of dnd-kit. The library has since undergone a major rewrite.

It has been rebuilt from the ground up with support for multiple frameworks (React, Vue, Svelte, Solid, and vanilla JavaScript), improved performance, and a new API.

  • Documentation: https://dndkit.com
  • Migration guide: https://dndkit.com/react/migration
  • GitHub: https://github.com/clauderic/dnd-kit

If this issue is still relevant to the latest version, please open a new issue.

clauderic avatar Feb 16 '26 02:02 clauderic