Skip to content

Commit b56aa0f

Browse files
committed
feat(drag-drop): allow entire group of drop lists to be disabled
Makes it easier to disable dragging under an entire group of drop lists.
1 parent 6d7f417 commit b56aa0f

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ describe('CdkDrag', () => {
493493
expect(dragElement.style.touchAction)
494494
.not.toEqual('none', 'should not disable touchAction on when there is a drag handle');
495495
});
496+
496497
it('should be able to reset a freely-dragged item to its initial position', fakeAsync(() => {
497498
const fixture = createComponent(StandaloneDraggable);
498499
fixture.detectChanges();
@@ -1760,6 +1761,31 @@ describe('CdkDrag', () => {
17601761
.toEqual(['Zero', 'One', 'Two', 'Three']);
17611762
}));
17621763

1764+
it('should not move the item if the group is disabled', fakeAsync(() => {
1765+
const fixture = createComponent(ConnectedDropZonesViaGroupDirective);
1766+
fixture.detectChanges();
1767+
const dragItems = fixture.componentInstance.groupedDragItems[0];
1768+
1769+
fixture.componentInstance.groupDisabled = true;
1770+
fixture.detectChanges();
1771+
1772+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1773+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1774+
1775+
const firstItem = dragItems[0];
1776+
const thirdItemRect = dragItems[2].element.nativeElement.getBoundingClientRect();
1777+
1778+
dragElementViaMouse(fixture, firstItem.element.nativeElement,
1779+
thirdItemRect.right + 1, thirdItemRect.top + 1);
1780+
flush();
1781+
fixture.detectChanges();
1782+
1783+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
1784+
1785+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1786+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1787+
}));
1788+
17631789
});
17641790

17651791
describe('in a connected drop container', () => {
@@ -2501,7 +2527,7 @@ class ConnectedDropZones implements AfterViewInit {
25012527
}
25022528
`],
25032529
template: `
2504-
<div cdkDropListGroup>
2530+
<div cdkDropListGroup [cdkDropListGroupDisabled]="groupDisabled">
25052531
<div
25062532
cdkDropList
25072533
[cdkDropListData]="todo"
@@ -2518,7 +2544,9 @@ class ConnectedDropZones implements AfterViewInit {
25182544
</div>
25192545
`
25202546
})
2521-
class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {}
2547+
class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {
2548+
groupDisabled = false;
2549+
}
25222550

25232551

25242552
@Component({

src/cdk/drag-drop/drop-list-group.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, OnDestroy} from '@angular/core';
9+
import {Directive, OnDestroy, Input} from '@angular/core';
10+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1011

1112
/**
1213
* Declaratively connects sibling `cdkDropList` instances together. All of the `cdkDropList`
@@ -21,6 +22,14 @@ export class CdkDropListGroup<T> implements OnDestroy {
2122
/** Drop lists registered inside the group. */
2223
readonly _items = new Set<T>();
2324

25+
/** Whether starting a dragging sequence from inside this group is disabled. */
26+
@Input('cdkDropListGroupDisabled')
27+
get disabled(): boolean { return this._disabled; }
28+
set disabled(value: boolean) {
29+
this._disabled = coerceBooleanProperty(value);
30+
}
31+
private _disabled = false;
32+
2433
ngOnDestroy() {
2534
this._items.clear();
2635
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
118118

119119
/** Whether starting a dragging sequence from this container is disabled. */
120120
@Input('cdkDropListDisabled')
121-
get disabled(): boolean { return this._disabled; }
121+
get disabled(): boolean {
122+
return this._disabled || (!!this._group && this._group.disabled);
123+
}
122124
set disabled(value: boolean) {
123125
this._disabled = coerceBooleanProperty(value);
124126
}

0 commit comments

Comments
 (0)