Skip to content

Commit fa944b7

Browse files
crisbetovivian-hu-zz
authored andcommitted
fix(drag-drop): ignore enter predicate when returning item to its initial container (#13972)
Something I noticed while putting together an example for the `enterPredicate` a while ago. Currently we allow an item to be returned to its initial container, even if the initial container isn't connected to the new one, however we don't do the same for the `enterPredicate`. This means that the user can get into the situation where they drag an item out, but then they're not allowed to return it, which seems weird. These changes will ignore the `enterPredicate`, if an item is being returned to the initial container.
1 parent 20f8924 commit fa944b7

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,46 @@ describe('CdkDrag', () => {
16091609
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
16101610
}));
16111611

1612+
it('should be able to move the element over a new container and return it to the initial ' +
1613+
'one, even if it no longer matches the enterPredicate', fakeAsync(() => {
1614+
const fixture = createComponent(ConnectedDropZones);
1615+
fixture.detectChanges();
1616+
1617+
const groups = fixture.componentInstance.groupedDragItems;
1618+
const dropZones = fixture.componentInstance.dropInstances.map(d => d.element.nativeElement);
1619+
const item = groups[0][1];
1620+
const initialRect = item.element.nativeElement.getBoundingClientRect();
1621+
const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();
1622+
1623+
fixture.componentInstance.dropInstances.first.enterPredicate = () => false;
1624+
fixture.detectChanges();
1625+
1626+
startDraggingViaMouse(fixture, item.element.nativeElement);
1627+
1628+
const placeholder = dropZones[0].querySelector('.cdk-drag-placeholder')!;
1629+
1630+
expect(placeholder).toBeTruthy();
1631+
expect(dropZones[0].contains(placeholder))
1632+
.toBe(true, 'Expected placeholder to be inside the first container.');
1633+
1634+
dispatchMouseEvent(document, 'mousemove', targetRect.left + 1, targetRect.top + 1);
1635+
fixture.detectChanges();
1636+
1637+
expect(dropZones[1].contains(placeholder))
1638+
.toBe(true, 'Expected placeholder to be inside second container.');
1639+
1640+
dispatchMouseEvent(document, 'mousemove', initialRect.left + 1, initialRect.top + 1);
1641+
fixture.detectChanges();
1642+
1643+
expect(dropZones[0].contains(placeholder))
1644+
.toBe(true, 'Expected placeholder to be back inside first container.');
1645+
1646+
dispatchMouseEvent(document, 'mouseup');
1647+
fixture.detectChanges();
1648+
1649+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
1650+
}));
1651+
16121652
it('should transfer the DOM element from one drop zone to another', fakeAsync(() => {
16131653
const fixture = createComponent(ConnectedDropZones);
16141654
fixture.detectChanges();

src/cdk/drag-drop/drag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
521521
// This handles the case where two containers are connected one way and the user tries to
522522
// undo dragging an item into a new container.
523523
if (!newContainer && this.dropContainer !== this._initialContainer &&
524-
this._initialContainer._canReturnItem(this, x, y)) {
524+
this._initialContainer._canReturnItem(x, y)) {
525525
newContainer = this._initialContainer;
526526
}
527527

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface CdkDropListContainer<T = any> {
6060
_draggables: QueryList<CdkDrag>;
6161
_getSiblingContainerFromPosition(item: CdkDrag, x: number, y: number):
6262
CdkDropListContainer | null;
63-
_canReturnItem(item: CdkDrag, x: number, y: number): boolean;
63+
_canReturnItem(x: number, y: number): boolean;
6464
}
6565

6666
/**

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,11 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
356356
/**
357357
* Checks whether an item that started in this container can be returned to it,
358358
* after it was moved out into another container.
359-
* @param item Item that is being checked.
360359
* @param x Position of the item along the X axis.
361360
* @param y Position of the item along the Y axis.
362361
*/
363-
_canReturnItem(item: CdkDrag, x: number, y: number): boolean {
364-
return isInsideClientRect(this._positionCache.self, x, y) && this.enterPredicate(item, this);
362+
_canReturnItem(x: number, y: number): boolean {
363+
return isInsideClientRect(this._positionCache.self, x, y);
365364
}
366365

367366
/** Refreshes the position cache of the items and sibling containers. */

0 commit comments

Comments
 (0)