Skip to content

fix(drag-drop): not picking up initial disabled state of handle #16643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,18 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBeFalsy();
}));

it('should not be able to drag the element if the handle is disabled before init',
fakeAsync(() => {
const fixture = createComponent(StandaloneDraggableWithPreDisabledHandle);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;
const handle = fixture.componentInstance.handleElement.nativeElement;

expect(dragElement.style.transform).toBeFalsy();
dragElementViaMouse(fixture, handle, 50, 100);
expect(dragElement.style.transform).toBeFalsy();
}));

it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggableWithHandle);
fixture.detectChanges();
Expand Down Expand Up @@ -4481,6 +4493,25 @@ class StandaloneDraggableWithHandle {
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
}

@Component({
template: `
<div #dragElement cdkDrag
style="width: 100px; height: 100px; background: red; position: relative">
<div
#handleElement
cdkDragHandle
[cdkDragHandleDisabled]="disableHandle"
style="width: 10px; height: 10px; background: green;"></div>
</div>
`
})
class StandaloneDraggableWithPreDisabledHandle {
@ViewChild('dragElement', {static: false}) dragElement: ElementRef<HTMLElement>;
@ViewChild('handleElement', {static: false}) handleElement: ElementRef<HTMLElement>;
@ViewChild(CdkDrag, {static: false}) dragInstance: CdkDrag;
disableHandle = true;
}

@Component({
template: `
<div #dragElement cdkDrag
Expand Down
4 changes: 3 additions & 1 deletion src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
}),
// Listen if the state of any of the handles changes.
switchMap((handles: QueryList<CdkDragHandle>) => {
return merge(...handles.map(item => item._stateChanges)) as Observable<CdkDragHandle>;
return merge(...handles.map(item => {
return item._stateChanges.pipe(startWith(item));
})) as Observable<CdkDragHandle>;
}),
takeUntil(this._destroyed)
).subscribe(handleInstance => {
Expand Down