Skip to content

Commit

Permalink
Optimistic sorting bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Nov 3, 2024
1 parent b5e5962 commit 7ef9864
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fix-optimistic-sorting-bugs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/dom': patch
---

Fixed bugs with the `OptimisticSortingPlugin` when sorting across different groups.
12 changes: 11 additions & 1 deletion packages/dom/src/sortable/OptimisticSortingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ export class OptimisticSortingPlugin extends Plugin<DragDropManager> {

// Wait for the renderer to handle the event before attempting to optimistically update
manager.renderer.rendering.then(() => {
const newInstances = getSortableInstances();

for (const [group, sortableInstances] of instances.entries()) {
const entries = Array.from(sortableInstances).entries();

for (const [index, sortable] of entries) {
if (sortable.index !== index || sortable.group !== group) {
if (
sortable.index !== index ||
sortable.group !== group ||
!newInstances.get(group)?.has(sortable)
) {
// At least one index or group was changed so we should abort optimistic updates
return;
}
Expand All @@ -88,6 +94,10 @@ export class OptimisticSortingPlugin extends Plugin<DragDropManager> {
return;
}

if (!sameGroup && target.id === source.sortable.group) {
return;
}

const orderedSourceSortables = sort(sourceInstances);
const orderedTargetSortables = sameGroup
? orderedSourceSortables
Expand Down

0 comments on commit 7ef9864

Please sign in to comment.