Skip to content

Commit 94e76de

Browse files
crisbetovivian-hu-zz
authored andcommittedDec 12, 2018
feat(drag-drop): allow entire group of drop lists to be disabled (#14179)
Makes it easier to disable dragging under an entire group of drop lists.
1 parent 220e388 commit 94e76de

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed
 

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

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

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

19231947
});
@@ -2743,7 +2767,7 @@ class ConnectedDropZones implements AfterViewInit {
27432767
}
27442768
`],
27452769
template: `
2746-
<div cdkDropListGroup>
2770+
<div cdkDropListGroup [cdkDropListGroupDisabled]="groupDisabled">
27472771
<div
27482772
cdkDropList
27492773
[cdkDropListData]="todo"
@@ -2760,7 +2784,9 @@ class ConnectedDropZones implements AfterViewInit {
27602784
</div>
27612785
`
27622786
})
2763-
class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {}
2787+
class ConnectedDropZonesViaGroupDirective extends ConnectedDropZones {
2788+
groupDisabled = false;
2789+
}
27642790

27652791

27662792
@Component({

‎src/cdk/drag-drop/directives/drop-list-group.ts

+10-1
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/directives/drop-list.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
9696

9797
/** Whether starting a dragging sequence from this container is disabled. */
9898
@Input('cdkDropListDisabled')
99-
get disabled(): boolean { return this._dropListRef.disabled; }
99+
get disabled(): boolean {
100+
return this._disabled || (!!this._group && this._group.disabled);
101+
}
100102
set disabled(value: boolean) {
101-
this._dropListRef.disabled = coerceBooleanProperty(value);
103+
this._disabled = coerceBooleanProperty(value);
102104
}
105+
private _disabled = false;
103106

104107
/**
105108
* Function that is used to determine whether an item

‎tools/public_api_guard/cdk/drag-drop.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export interface CdkDropListContainer<T = any> {
156156

157157
export declare class CdkDropListGroup<T> implements OnDestroy {
158158
readonly _items: Set<T>;
159+
disabled: boolean;
159160
ngOnDestroy(): void;
160161
}
161162

0 commit comments

Comments
 (0)
Please sign in to comment.