Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/drag-drop): error if dragged item is destroyed as a result of the entered event #22904

Merged
merged 1 commit into from Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Expand Up @@ -5447,6 +5447,28 @@ describe('CdkDrag', () => {
expect(itemEnterEvent).toEqual(expectedEvent);
}));

it('should not throw if dragging was interrupted as a result of the entered event',
fakeAsync(() => {
const fixture = createComponent(ConnectedDropZones);
fixture.detectChanges();

const groups = fixture.componentInstance.groupedDragItems;
const item = groups[0][1];
const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();

fixture.componentInstance.enteredSpy.and.callFake(() => {
fixture.componentInstance.todo = [];
fixture.detectChanges();
});

expect(() => {
dragElementViaMouse(
fixture, item.element.nativeElement, targetRect.left + 1, targetRect.top + 1);
flush();
fixture.detectChanges();
}).not.toThrow();
}));

it('should be able to drop into a new container after scrolling into view', fakeAsync(() => {
const fixture = createComponent(ConnectedDropZones);
fixture.detectChanges();
Expand Down
13 changes: 8 additions & 5 deletions src/cdk/drag-drop/drag-ref.ts
Expand Up @@ -151,7 +151,7 @@ export class DragRef<T = any> {
* Whether the dragging sequence has been started. Doesn't
* necessarily mean that the element has been moved.
*/
private _hasStartedDragging: boolean;
private _hasStartedDragging = false;

/** Whether the element has moved since the user started dragging it. */
private _hasMoved: boolean;
Expand Down Expand Up @@ -982,10 +982,13 @@ export class DragRef<T = any> {
});
}

this._dropContainer!._startScrollingIfNecessary(rawX, rawY);
this._dropContainer!._sortItem(this, x, y, this._pointerDirectionDelta);
this._applyPreviewTransform(
x - this._pickupPositionInElement.x, y - this._pickupPositionInElement.y);
// Dragging may have been interrupted as a result of the events above.
if (this.isDragging()) {
this._dropContainer!._startScrollingIfNecessary(rawX, rawY);
this._dropContainer!._sortItem(this, x, y, this._pointerDirectionDelta);
this._applyPreviewTransform(
x - this._pickupPositionInElement.x, y - this._pickupPositionInElement.y);
}
}

/**
Expand Down