Skip to content

fix(drag-drop): not dropping immediately for failed drag after a successful one #13097

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

Merged
merged 1 commit into from
Sep 13, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,39 @@ describe('CdkDrag', () => {
.toBeFalsy('Expected preview to be removed from the DOM if the transition timed out');
}));

it('should reset immediately when failed drag happens after a successful one', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();

const itemInstance = fixture.componentInstance.dragItems.toArray()[1];
const item = itemInstance.element.nativeElement;
const spy = jasmine.createSpy('dropped spy');
const subscription = itemInstance.dropped.asObservable().subscribe(spy);

// Do an initial drag and drop sequence.
dragElementViaMouse(fixture, item, 50, 50);
tick(0); // Important to tick with 0 since we don't want to flush any pending timeouts.

expect(spy).toHaveBeenCalledTimes(1);

// Start another drag.
dispatchMouseEvent(item, 'mousedown');
fixture.detectChanges();

// Add a duration since the tests won't include one.
const preview = document.querySelector('.cdk-drag-preview')! as HTMLElement;
preview.style.transitionDuration = '500ms';

// Dispatch the mouseup immediately to simulate the user not moving the element.
dispatchMouseEvent(document, 'mouseup');
fixture.detectChanges();
tick(0); // Important to tick with 0 since we don't want to flush any pending timeouts.

expect(spy).toHaveBeenCalledTimes(2);

subscription.unsubscribe();
}));

it('should not wait for transition that are not on the `transform` property', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/drag-drop/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
private _activeTransform: Point = {x: 0, y: 0};

/** Whether the element has moved since the user started dragging it. */
private _hasMoved = false;
private _hasMoved: boolean;

/** Drop container in which the CdkDrag resided when dragging began. */
private _initialContainer: CdkDropContainer;
Expand Down Expand Up @@ -284,6 +284,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {

const endedOrDestroyed = merge(this.ended, this._destroyed);

this._hasMoved = false;
this._dragDropRegistry.pointerMove
.pipe(takeUntil(endedOrDestroyed))
.subscribe(this._pointerMove);
Expand Down