Skip to content

Commit 05f8dfb

Browse files
committed
fix(cdk/drag-drop): last item not returned at initial index when sorting is disabled
Fixes a regression caused by #19116 where dragging the last item into another container and returning it back to the initial one would insert the item into the first index of the list, rather than the last one. The issue was caused by the fact that we relied on the item to always be inserted at the end of the list of there is no position reference, but the condition was superceded by the condition that inserts it at the beginning of the list. Fixes #23865.
1 parent e5b4dd9 commit 05f8dfb

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,6 +5972,63 @@ describe('CdkDrag', () => {
59725972
}),
59735973
);
59745974

5975+
it('should return the last item to initial position when dragging back into a container with disabled sorting', fakeAsync(() => {
5976+
const fixture = createComponent(ConnectedDropZones);
5977+
fixture.detectChanges();
5978+
5979+
const groups = fixture.componentInstance.groupedDragItems;
5980+
const dropZones = fixture.componentInstance.dropInstances.map(d => d.element.nativeElement);
5981+
const lastIndex = groups[0].length - 1;
5982+
const lastItem = groups[0][lastIndex];
5983+
const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();
5984+
5985+
fixture.componentInstance.dropInstances.first.sortingDisabled = true;
5986+
startDraggingViaMouse(fixture, lastItem.element.nativeElement);
5987+
5988+
const placeholder = dropZones[0].querySelector('.cdk-drag-placeholder')!;
5989+
5990+
expect(placeholder).toBeTruthy();
5991+
expect(dropZones[0].contains(placeholder))
5992+
.withContext('Expected placeholder to be inside the first container.')
5993+
.toBe(true);
5994+
expect(getElementIndexByPosition(placeholder, 'top'))
5995+
.withContext('Expected placeholder to be at item index.')
5996+
.toBe(lastIndex);
5997+
5998+
dispatchMouseEvent(document, 'mousemove', targetRect.left + 1, targetRect.top + 1);
5999+
fixture.detectChanges();
6000+
6001+
expect(dropZones[1].contains(placeholder))
6002+
.withContext('Expected placeholder to be inside second container.')
6003+
.toBe(true);
6004+
expect(getElementIndexByPosition(placeholder, 'top'))
6005+
.withContext('Expected placeholder to be at the target index.')
6006+
.toBe(3);
6007+
6008+
const firstInitialSiblingRect = groups[0][0].element.nativeElement.getBoundingClientRect();
6009+
6010+
// Return the item to an index that is different from the initial one.
6011+
dispatchMouseEvent(
6012+
document,
6013+
'mousemove',
6014+
firstInitialSiblingRect.left,
6015+
firstInitialSiblingRect.top,
6016+
);
6017+
fixture.detectChanges();
6018+
6019+
expect(dropZones[0].contains(placeholder))
6020+
.withContext('Expected placeholder to be back inside first container.')
6021+
.toBe(true);
6022+
expect(getElementIndexByPosition(placeholder, 'top'))
6023+
.withContext('Expected placeholder to be back at the initial index.')
6024+
.toBe(lastIndex);
6025+
6026+
dispatchMouseEvent(document, 'mouseup');
6027+
fixture.detectChanges();
6028+
6029+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
6030+
}));
6031+
59756032
it(
59766033
'should toggle a class when dragging an item inside a wrapper component component ' +
59776034
'with OnPush change detection',

src/cdk/drag-drop/drop-list-ref.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ export class DropListRef<T = any> {
299299
newPositionReference = activeDraggables[newIndex + 1];
300300
}
301301

302+
// If we didn't find a new position reference, it means that either the item didn't start off
303+
// in this container, or that the item requested to be inserted at the end of the list.
304+
if (
305+
!newPositionReference &&
306+
(newIndex == null || newIndex === -1 || newIndex < activeDraggables.length - 1) &&
307+
this._shouldEnterAsFirstChild(pointerX, pointerY)
308+
) {
309+
newPositionReference = activeDraggables[0];
310+
}
311+
302312
// Since the item may be in the `activeDraggables` already (e.g. if the user dragged it
303313
// into another container and back again), we have to ensure that it isn't duplicated.
304314
if (currentIndex > -1) {
@@ -311,10 +321,6 @@ export class DropListRef<T = any> {
311321
const element = newPositionReference.getRootElement();
312322
element.parentElement!.insertBefore(placeholder, element);
313323
activeDraggables.splice(newIndex, 0, item);
314-
} else if (this._shouldEnterAsFirstChild(pointerX, pointerY)) {
315-
const reference = activeDraggables[0].getRootElement();
316-
reference.parentNode!.insertBefore(placeholder, reference);
317-
activeDraggables.unshift(item);
318324
} else {
319325
coerceElement(this.element).appendChild(placeholder);
320326
activeDraggables.push(item);

0 commit comments

Comments
 (0)