Skip to content

Commit 0e02605

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 0f6cd05 commit 0e02605

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
@@ -1152,6 +1152,18 @@ describe('CdkDrag', () => {
11521152
expect(dragElement.style.transform).toBeFalsy();
11531153
}));
11541154

1155+
it('should not be able to drag the element if the handle is disabled before init',
1156+
fakeAsync(() => {
1157+
const fixture = createComponent(StandaloneDraggableWithPreDisabledHandle);
1158+
fixture.detectChanges();
1159+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
1160+
const handle = fixture.componentInstance.handleElement.nativeElement;
1161+
1162+
expect(dragElement.style.transform).toBeFalsy();
1163+
dragElementViaMouse(fixture, handle, 50, 100);
1164+
expect(dragElement.style.transform).toBeFalsy();
1165+
}));
1166+
11551167
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
11561168
const fixture = createComponent(StandaloneDraggableWithHandle);
11571169
fixture.detectChanges();
@@ -4481,6 +4493,25 @@ class StandaloneDraggableWithHandle {
44814493
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
44824494
}
44834495

4496+
@Component({
4497+
template: `
4498+
<div #dragElement cdkDrag
4499+
style="width: 100px; height: 100px; background: red; position: relative">
4500+
<div
4501+
#handleElement
4502+
cdkDragHandle
4503+
[cdkDragHandleDisabled]="disableHandle"
4504+
style="width: 10px; height: 10px; background: green;"></div>
4505+
</div>
4506+
`
4507+
})
4508+
class StandaloneDraggableWithPreDisabledHandle {
4509+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
4510+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
4511+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
4512+
disableHandle = true;
4513+
}
4514+
44844515
@Component({
44854516
template: `
44864517
<div #dragElement cdkDrag

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
251251
}),
252252
// Listen if the state of any of the handles changes.
253253
switchMap((handles: QueryList<CdkDragHandle>) => {
254-
return merge(...handles.map(item => item._stateChanges)) as Observable<CdkDragHandle>;
254+
return merge(...handles.map(item => {
255+
return item._stateChanges.pipe(startWith(item));
256+
})) as Observable<CdkDragHandle>;
255257
}),
256258
takeUntil(this._destroyed)
257259
).subscribe(handleInstance => {

0 commit comments

Comments
 (0)