Skip to content

Commit 3b28d33

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): not picking up initial disabled state of handle (#16643)
Fixes the `CdkDrag` directive not picking up the `disabled` state of a handle, if it was set before `CdkDrag` subscribed to its `_stateChanges` stream. Fixes #16642. (cherry picked from commit 0760454)
1 parent 75a7681 commit 3b28d33

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,18 @@ describe('CdkDrag', () => {
11971197
expect(dragElement.style.transform).toBeFalsy();
11981198
}));
11991199

1200+
it('should not be able to drag the element if the handle is disabled before init',
1201+
fakeAsync(() => {
1202+
const fixture = createComponent(StandaloneDraggableWithPreDisabledHandle);
1203+
fixture.detectChanges();
1204+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
1205+
const handle = fixture.componentInstance.handleElement.nativeElement;
1206+
1207+
expect(dragElement.style.transform).toBeFalsy();
1208+
dragElementViaMouse(fixture, handle, 50, 100);
1209+
expect(dragElement.style.transform).toBeFalsy();
1210+
}));
1211+
12001212
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
12011213
const fixture = createComponent(StandaloneDraggableWithHandle);
12021214
fixture.detectChanges();
@@ -4590,6 +4602,25 @@ class StandaloneDraggableWithHandle {
45904602
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
45914603
}
45924604

4605+
@Component({
4606+
template: `
4607+
<div #dragElement cdkDrag
4608+
style="width: 100px; height: 100px; background: red; position: relative">
4609+
<div
4610+
#handleElement
4611+
cdkDragHandle
4612+
[cdkDragHandleDisabled]="disableHandle"
4613+
style="width: 10px; height: 10px; background: green;"></div>
4614+
</div>
4615+
`
4616+
})
4617+
class StandaloneDraggableWithPreDisabledHandle {
4618+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
4619+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
4620+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
4621+
disableHandle = true;
4622+
}
4623+
45934624
@Component({
45944625
template: `
45954626
<div #dragElement cdkDrag

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
256256
}),
257257
// Listen if the state of any of the handles changes.
258258
switchMap((handles: QueryList<CdkDragHandle>) => {
259-
return merge(...handles.map(item => item._stateChanges)) as Observable<CdkDragHandle>;
259+
return merge(...handles.map(item => {
260+
return item._stateChanges.pipe(startWith(item));
261+
})) as Observable<CdkDragHandle>;
260262
}),
261263
takeUntil(this._destroyed)
262264
).subscribe(handleInstance => {

0 commit comments

Comments
 (0)