Skip to content

Commit 858af25

Browse files
committed
feat(drag-drop): add the ability to disable dragging
Adds inputs to `cdkDrag`, `cdkDropList` and `cdkDragHandle` that allows for dragging to be disabled through those specific elements. Fixes #13651.
1 parent efeefd1 commit 858af25

File tree

9 files changed

+184
-4
lines changed

9 files changed

+184
-4
lines changed

src/cdk/drag-drop/drag-drop.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,11 @@ the element that is moved as the user is dragging. This is useful for cases like
125125
draggable.
126126

127127
<!-- example(cdk-drag-drop-root-element) -->
128+
129+
### Disable dragging
130+
If you want to disable dragging for a particular drag item, you can do so by setting the
131+
`cdkDragDisabled` input on a `cdkDrag` item. Furthermore, you can disable an entire list
132+
using the `cdkDropListDisabled` input on a `cdkDropList` or a particular handle via
133+
`cdkDragHandleDisabled` on `cdkDragHandle`.
134+
135+
<!-- example(cdk-drag-drop-disabled) -->

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, Inject, Optional} from '@angular/core';
9+
import {Directive, ElementRef, Inject, Optional, Input} from '@angular/core';
10+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1011
import {CDK_DRAG_PARENT} from './drag-parent';
1112
import {toggleNativeDragInteractions} from './drag-styling';
1213

@@ -21,6 +22,14 @@ export class CdkDragHandle {
2122
/** Closest parent draggable instance. */
2223
_parentDrag: {} | undefined;
2324

25+
/** Whether starting to drag through this handle is disabled. */
26+
@Input('cdkDragHandleDisabled')
27+
get disabled(): boolean { return this._disabled; }
28+
set disabled(value: boolean) {
29+
this._disabled = coerceBooleanProperty(value);
30+
}
31+
private _disabled = false;
32+
2433
constructor(
2534
public element: ElementRef<HTMLElement>,
2635
@Inject(CDK_DRAG_PARENT) @Optional() parentDrag?: any) {

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,18 @@ describe('CdkDrag', () => {
412412
expect(dragElement.style.transform).toBeFalsy();
413413
}));
414414

415+
it('should not be able to drag the element if dragging is disabled', fakeAsync(() => {
416+
const fixture = createComponent(StandaloneDraggable);
417+
fixture.detectChanges();
418+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
419+
420+
fixture.componentInstance.dragInstance.disabled = true;
421+
422+
expect(dragElement.style.transform).toBeFalsy();
423+
dragElementViaMouse(fixture, dragElement, 50, 100);
424+
expect(dragElement.style.transform).toBeFalsy();
425+
}));
426+
415427
});
416428

417429
describe('draggable with a handle', () => {
@@ -436,6 +448,32 @@ describe('CdkDrag', () => {
436448
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
437449
}));
438450

451+
it('should not be able to drag the element if the handle is disabled', fakeAsync(() => {
452+
const fixture = createComponent(StandaloneDraggableWithHandle);
453+
fixture.detectChanges();
454+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
455+
const handle = fixture.componentInstance.handleElement.nativeElement;
456+
457+
fixture.componentInstance.handleInstance.disabled = true;
458+
459+
expect(dragElement.style.transform).toBeFalsy();
460+
dragElementViaMouse(fixture, handle, 50, 100);
461+
expect(dragElement.style.transform).toBeFalsy();
462+
}));
463+
464+
it('should not be able to drag using the handle if the element is disabled', fakeAsync(() => {
465+
const fixture = createComponent(StandaloneDraggableWithHandle);
466+
fixture.detectChanges();
467+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
468+
const handle = fixture.componentInstance.handleElement.nativeElement;
469+
470+
fixture.componentInstance.dragInstance.disabled = true;
471+
472+
expect(dragElement.style.transform).toBeFalsy();
473+
dragElementViaMouse(fixture, handle, 50, 100);
474+
expect(dragElement.style.transform).toBeFalsy();
475+
}));
476+
439477
it('should be able to use a handle that was added after init', fakeAsync(() => {
440478
const fixture = createComponent(StandaloneDraggableWithDelayedHandle);
441479

@@ -1329,6 +1367,30 @@ describe('CdkDrag', () => {
13291367
expect(thirdItem.style.transform).toBeFalsy();
13301368
}));
13311369

1370+
it('should not move the item if the list is disabled', fakeAsync(() => {
1371+
const fixture = createComponent(DraggableInDropZone);
1372+
fixture.detectChanges();
1373+
const dragItems = fixture.componentInstance.dragItems;
1374+
1375+
fixture.componentInstance.dropInstance.disabled = true;
1376+
1377+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1378+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1379+
1380+
const firstItem = dragItems.first;
1381+
const thirdItemRect = dragItems.toArray()[2].element.nativeElement.getBoundingClientRect();
1382+
1383+
dragElementViaMouse(fixture, firstItem.element.nativeElement,
1384+
thirdItemRect.right + 1, thirdItemRect.top + 1);
1385+
flush();
1386+
fixture.detectChanges();
1387+
1388+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
1389+
1390+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1391+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1392+
}));
1393+
13321394
});
13331395

13341396
describe('in a connected drop container', () => {
@@ -1744,6 +1806,8 @@ class StandaloneDraggable {
17441806
class StandaloneDraggableWithHandle {
17451807
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
17461808
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
1809+
@ViewChild(CdkDrag) dragInstance: CdkDrag;
1810+
@ViewChild(CdkDragHandle) handleInstance: CdkDragHandle;
17471811
}
17481812

17491813
@Component({

src/cdk/drag-drop/drag.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ViewContainerRef,
3030
} from '@angular/core';
3131
import {supportsPassiveEventListeners} from '@angular/cdk/platform';
32+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
3233
import {Observable, Subject, Subscription, Observer} from 'rxjs';
3334
import {take} from 'rxjs/operators';
3435
import {DragDropRegistry} from './drag-drop-registry';
@@ -198,6 +199,16 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
198199
*/
199200
@Input('cdkDragRootElement') rootElementSelector: string;
200201

202+
/** Whether starting to drag this element is disabled. */
203+
@Input('cdkDragDisabled')
204+
get disabled(): boolean {
205+
return this._disabled || (this.dropContainer && this.dropContainer.disabled);
206+
}
207+
set disabled(value: boolean) {
208+
this._disabled = coerceBooleanProperty(value);
209+
}
210+
private _disabled = false;
211+
201212
/** Emits when the user starts dragging the item. */
202213
@Output('cdkDragStarted') started: EventEmitter<CdkDragStart> = new EventEmitter<CdkDragStart>();
203214

@@ -314,10 +325,10 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
314325
return !!target && (target === element || element.contains(target as HTMLElement));
315326
});
316327

317-
if (targetHandle) {
328+
if (targetHandle && !targetHandle.disabled && !this.disabled) {
318329
this._initializeDragSequence(targetHandle.element.nativeElement, event);
319330
}
320-
} else {
331+
} else if (!this.disabled) {
321332
this._initializeDragSequence(this._rootElement, event);
322333
}
323334
}

src/cdk/drag-drop/drop-list-container.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export interface CdkDropListContainer<T = any> {
2626
/** Locks the position of the draggable elements inside the container along the specified axis. */
2727
lockAxis: 'x' | 'y';
2828

29+
/** Whether starting a dragging sequence from this container is disabled. */
30+
disabled: boolean;
31+
2932
/** Starts dragging an item. */
3033
start(): void;
3134

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {coerceArray} from '@angular/cdk/coercion';
9+
import {coerceArray, coerceBooleanProperty} from '@angular/cdk/coercion';
1010
import {
1111
ContentChildren,
1212
ElementRef,
@@ -77,6 +77,14 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
7777
/** Locks the position of the draggable elements inside the container along the specified axis. */
7878
@Input('cdkDropListLockAxis') lockAxis: 'x' | 'y';
7979

80+
/** Whether starting a dragging sequence from this container is disabled. */
81+
@Input('cdkDropListDisabled')
82+
get disabled(): boolean { return this._disabled; }
83+
set disabled(value: boolean) {
84+
this._disabled = coerceBooleanProperty(value);
85+
}
86+
private _disabled = false;
87+
8088
/**
8189
* Function that is used to determine whether an item
8290
* is allowed to be moved into a drop container.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.example-list {
2+
width: 500px;
3+
max-width: 100%;
4+
border: solid 1px #ccc;
5+
min-height: 60px;
6+
display: block;
7+
background: white;
8+
border-radius: 4px;
9+
overflow: hidden;
10+
}
11+
12+
.example-box {
13+
padding: 20px 10px;
14+
border-bottom: solid 1px #ccc;
15+
color: rgba(0, 0, 0, 0.87);
16+
display: flex;
17+
flex-direction: row;
18+
align-items: center;
19+
justify-content: space-between;
20+
box-sizing: border-box;
21+
cursor: move;
22+
background: white;
23+
font-size: 14px;
24+
}
25+
26+
.cdk-drag-preview {
27+
box-sizing: border-box;
28+
border-radius: 4px;
29+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
30+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
31+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
32+
}
33+
34+
.cdk-drag-placeholder {
35+
opacity: 0;
36+
}
37+
38+
.cdk-drag-animating {
39+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
40+
}
41+
42+
.example-box:last-child {
43+
border: none;
44+
}
45+
46+
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
47+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
48+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
2+
<div
3+
class="example-box"
4+
*ngFor="let item of items"
5+
cdkDrag
6+
[cdkDragDisabled]="item.disabled">{{item.value}}</div>
7+
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Component} from '@angular/core';
2+
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
3+
4+
/**
5+
* @title Drag&Drop disabled
6+
*/
7+
@Component({
8+
selector: 'cdk-drag-drop-disabled-example',
9+
templateUrl: 'cdk-drag-drop-disabled-example.html',
10+
styleUrls: ['cdk-drag-drop-disabled-example.css'],
11+
})
12+
export class CdkDragDropDisabledExample {
13+
items = [
14+
{value: 'I can be dragged', disabled: false},
15+
{value: 'I cannot be dragged', disabled: true},
16+
{value: 'I can also be dragged', disabled: false}
17+
];
18+
19+
drop(event: CdkDragDrop<string[]>) {
20+
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
21+
}
22+
}

0 commit comments

Comments
 (0)