Skip to content

Commit 8bbb116

Browse files
crisbetoandrewseguin
authored andcommitted
fix(drag-drop): handle delay coming in as a string (#15425)
Since the `cdkDragStartDelay` is an input, it can be passed in as `cdkDragStartDelay="1000"` which doesn't go through type checking and is translated into a string which ends up breaking the drag sequence. These changes make sure that things work even if it comes in as a string.
1 parent e8827e2 commit 8bbb116

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,36 @@ describe('CdkDrag', () => {
758758
'Expected element to be dragged after all the time has passed.');
759759
}));
760760

761+
it('should handle the drag delay as a string', fakeAsync(() => {
762+
// We can't use Jasmine's `clock` because Zone.js interferes with it.
763+
spyOn(Date, 'now').and.callFake(() => currentTime);
764+
let currentTime = 0;
765+
766+
const fixture = createComponent(StandaloneDraggable);
767+
fixture.componentInstance.dragStartDelay = '1000';
768+
fixture.detectChanges();
769+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
770+
771+
expect(dragElement.style.transform).toBeFalsy('Expected element not to be moved by default.');
772+
773+
startDraggingViaMouse(fixture, dragElement);
774+
currentTime += 750;
775+
dispatchMouseEvent(document, 'mousemove', 50, 100);
776+
fixture.detectChanges();
777+
778+
expect(dragElement.style.transform)
779+
.toBeFalsy('Expected element not to be moved if the drag timeout has not passed.');
780+
781+
// The first `mousemove` here starts the sequence and the second one moves the element.
782+
currentTime += 500;
783+
dispatchMouseEvent(document, 'mousemove', 50, 100);
784+
dispatchMouseEvent(document, 'mousemove', 50, 100);
785+
fixture.detectChanges();
786+
787+
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)',
788+
'Expected element to be dragged after all the time has passed.');
789+
}));
790+
761791
});
762792

763793
describe('draggable with a handle', () => {
@@ -3172,7 +3202,7 @@ class StandaloneDraggable {
31723202
endedSpy = jasmine.createSpy('ended spy');
31733203
releasedSpy = jasmine.createSpy('released spy');
31743204
boundarySelector: string;
3175-
dragStartDelay: number;
3205+
dragStartDelay: number | string;
31763206
constrainPosition: (point: Point) => Point;
31773207
}
31783208

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
SimpleChanges,
3131
ChangeDetectorRef,
3232
} from '@angular/core';
33-
import {coerceBooleanProperty} from '@angular/cdk/coercion';
33+
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
3434
import {Observable, Observer, Subject, merge} from 'rxjs';
3535
import {startWith, take, map, takeUntil, switchMap, tap} from 'rxjs/operators';
3636
import {DragDropRegistry} from '../drag-drop-registry';
@@ -317,7 +317,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
317317

318318
ref.disabled = this.disabled;
319319
ref.lockAxis = this.lockAxis;
320-
ref.dragStartDelay = this.dragStartDelay;
320+
ref.dragStartDelay = coerceNumberProperty(this.dragStartDelay);
321321
ref.constrainPosition = this.constrainPosition;
322322
ref
323323
.withBoundaryElement(this._getBoundaryElement())

0 commit comments

Comments
 (0)