Skip to content

Commit 6110525

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 63be232 commit 6110525

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,31 @@ describe('CdkDrag', () => {
19161916
dispatchEvent(document, endEvent);
19171917
fixture.detectChanges();
19181918
}).not.toThrow();
1919+
}));
1920+
1921+
it('should not move the item if the group is disabled', fakeAsync(() => {
1922+
const fixture = createComponent(ConnectedDropZonesViaGroupDirective);
1923+
fixture.detectChanges();
1924+
const dragItems = fixture.componentInstance.groupedDragItems[0];
1925+
1926+
fixture.componentInstance.groupDisabled = true;
1927+
fixture.detectChanges();
1928+
1929+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1930+
.toEqual(['Zero', 'One', 'Two', 'Three']);
1931+
1932+
const firstItem = dragItems[0];
1933+
const thirdItemRect = dragItems[2].element.nativeElement.getBoundingClientRect();
19191934

1935+
dragElementViaMouse(fixture, firstItem.element.nativeElement,
1936+
thirdItemRect.right + 1, thirdItemRect.top + 1);
1937+
flush();
1938+
fixture.detectChanges();
1939+
1940+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
1941+
1942+
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim()))
1943+
.toEqual(['Zero', 'One', 'Two', 'Three']);
19201944
}));
19211945

19221946
});
@@ -2742,7 +2766,7 @@ class ConnectedDropZones implements AfterViewInit {
27422766
}
27432767
`],
27442768
template: `
2745-
<div cdkDropListGroup>
2769+
<div cdkDropListGroup [cdkDropListGroupDisabled]="groupDisabled">
27462770
<div
27472771
cdkDropList
27482772
[cdkDropListData]="todo"
@@ -2759,7 +2783,9 @@ class ConnectedDropZones implements AfterViewInit {
27592783
</div>
27602784
`
27612785
})
2762-
class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {}
2786+
class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {
2787+
groupDisabled = false;
2788+
}
27632789

27642790

27652791
@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`
@@ -22,6 +23,14 @@ export class CdkDropListGroup<T> implements OnDestroy {
2223
/** Drop lists registered inside the group. */
2324
readonly _items = new Set<T>();
2425

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

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

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

126126
/** Whether starting a dragging sequence from this container is disabled. */
127127
@Input('cdkDropListDisabled')
128-
get disabled(): boolean { return this._disabled; }
128+
get disabled(): boolean {
129+
return this._disabled || (!!this._group && this._group.disabled);
130+
}
129131
set disabled(value: boolean) {
130132
this._disabled = coerceBooleanProperty(value);
131133
}

0 commit comments

Comments
 (0)