Skip to content

Commit 0760454

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.
1 parent d9cf175 commit 0760454

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
@@ -1224,6 +1224,18 @@ describe('CdkDrag', () => {
12241224
expect(dragElement.style.transform).toBeFalsy();
12251225
}));
12261226

1227+
it('should not be able to drag the element if the handle is disabled before init',
1228+
fakeAsync(() => {
1229+
const fixture = createComponent(StandaloneDraggableWithPreDisabledHandle);
1230+
fixture.detectChanges();
1231+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
1232+
const handle = fixture.componentInstance.handleElement.nativeElement;
1233+
1234+
expect(dragElement.style.transform).toBeFalsy();
1235+
dragElementViaMouse(fixture, handle, 50, 100);
1236+
expect(dragElement.style.transform).toBeFalsy();
1237+
}));
1238+
12271239
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
12281240
const fixture = createComponent(StandaloneDraggableWithHandle);
12291241
fixture.detectChanges();
@@ -4709,6 +4721,25 @@ class StandaloneDraggableWithHandle {
47094721
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
47104722
}
47114723

4724+
@Component({
4725+
template: `
4726+
<div #dragElement cdkDrag
4727+
style="width: 100px; height: 100px; background: red; position: relative">
4728+
<div
4729+
#handleElement
4730+
cdkDragHandle
4731+
[cdkDragHandleDisabled]="disableHandle"
4732+
style="width: 10px; height: 10px; background: green;"></div>
4733+
</div>
4734+
`
4735+
})
4736+
class StandaloneDraggableWithPreDisabledHandle {
4737+
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
4738+
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
4739+
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
4740+
disableHandle = true;
4741+
}
4742+
47124743
@Component({
47134744
template: `
47144745
<div #dragElement cdkDrag

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ 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)) as Observable<CdkDragHandle>;
260+
return merge(...handles.map(item => {
261+
return item._stateChanges.pipe(startWith(item));
262+
})) as Observable<CdkDragHandle>;
261263
}),
262264
takeUntil(this._destroyed)
263265
).subscribe(handleInstance => {

0 commit comments

Comments
 (0)