Skip to content

Commit 6dbd9d4

Browse files
committed
fix(drag-drop): not picking up initial disabled state of handle
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.
1 parent 3ec531b commit 6dbd9d4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-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
@@ -1021,6 +1021,18 @@ describe('CdkDrag', () => {
10211021
expect(dragElement.style.transform).toBeFalsy();
10221022
}));
10231023

1024+
it('should not be able to drag the element if the handle is disabled before init',
1025+
fakeAsync(() => {
1026+
const fixture = createComponent(StandaloneDraggableWithPreDisabledHandle);
1027+
fixture.detectChanges();
1028+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
1029+
const handle = fixture.componentInstance.handleElement.nativeElement;
1030+
1031+
expect(dragElement.style.transform).toBeFalsy();
1032+
dragElementViaMouse(fixture, handle, 50, 100);
1033+
expect(dragElement.style.transform).toBeFalsy();
1034+
}));
1035+
10241036
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
10251037
const fixture = createComponent(StandaloneDraggableWithHandle);
10261038
fixture.detectChanges();
@@ -4052,6 +4064,25 @@ class StandaloneDraggableWithHandle {
40524064
@ViewChild(CdkDragHandle, {static: false}) handleInstance: CdkDragHandle;
40534065
}
40544066

4067+
@Component({
4068+
template: `
4069+
<div #dragElement cdkDrag
4070+
style="width: 100px; height: 100px; background: red; position: relative">
4071+
<div
4072+
#handleElement
4073+
cdkDragHandle
4074+
[cdkDragHandleDisabled]="disableHandle"
4075+
style="width: 10px; height: 10px; background: green;"></div>
4076+
</div>
4077+
`
4078+
})
4079+
class StandaloneDraggableWithPreDisabledHandle {
4080+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
4081+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
4082+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
4083+
disableHandle = true;
4084+
}
4085+
40554086
@Component({
40564087
template: `
40574088
<div #dragElement cdkDrag

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
257257
}),
258258
// Listen if the state of any of the handles changes.
259259
switchMap((handles: QueryList<CdkDragHandle>) => {
260-
return merge(...handles.map(item => item._stateChanges));
260+
return merge(...handles.map(item => item._stateChanges.pipe(startWith(item))));
261261
}),
262262
takeUntil(this._destroyed)
263263
).subscribe(handleInstance => {

0 commit comments

Comments
 (0)