Skip to content

Commit 9fe32c0

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): only call enterPredicate when pointer is inside drop list (#17310)
Fixes the `enterPredicate` being called for each connected container, even if the pointer is outside of it. Fixes #17266.
1 parent 493c32d commit 9fe32c0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,6 +3518,33 @@ describe('CdkDrag', () => {
35183518
expect(spy).toHaveBeenCalledWith(dragItem, dropInstances[1]);
35193519
}));
35203520

3521+
it('should not call the `enterPredicate` if the pointer is not over the container',
3522+
fakeAsync(() => {
3523+
const fixture = createComponent(ConnectedDropZones);
3524+
fixture.detectChanges();
3525+
3526+
const dropInstances = fixture.componentInstance.dropInstances.toArray();
3527+
const spy = jasmine.createSpy('enterPredicate spy').and.returnValue(true);
3528+
const groups = fixture.componentInstance.groupedDragItems.slice();
3529+
const dragElement = groups[0][1].element.nativeElement;
3530+
const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();
3531+
3532+
dropInstances[1].enterPredicate = spy;
3533+
fixture.detectChanges();
3534+
3535+
startDraggingViaMouse(fixture, dragElement);
3536+
3537+
dispatchMouseEvent(document, 'mousemove', targetRect.left - 1, targetRect.top - 1);
3538+
fixture.detectChanges();
3539+
3540+
expect(spy).not.toHaveBeenCalled();
3541+
3542+
dispatchMouseEvent(document, 'mousemove', targetRect.left + 1, targetRect.top + 1);
3543+
fixture.detectChanges();
3544+
3545+
expect(spy).toHaveBeenCalledTimes(1);
3546+
}));
3547+
35213548
it('should be able to start dragging after an item has been transferred', fakeAsync(() => {
35223549
const fixture = createComponent(ConnectedDropZones);
35233550
fixture.detectChanges();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ export class DropListRef<T = any> {
782782
* @param y Position of the item along the Y axis.
783783
*/
784784
_canReceive(item: DragRef, x: number, y: number): boolean {
785-
if (!this.enterPredicate(item, this) || !isInsideClientRect(this._clientRect, x, y)) {
785+
if (!isInsideClientRect(this._clientRect, x, y) || !this.enterPredicate(item, this)) {
786786
return false;
787787
}
788788

0 commit comments

Comments
 (0)