Skip to content

Commit d8d350a

Browse files
committed
refactor(cdk/drag-drop): switch to input transforms
Switches inputs in cdk/drag-drop to use transforms instead of getters/setters.
1 parent e058009 commit d8d350a

File tree

7 files changed

+50
-65
lines changed

7 files changed

+50
-65
lines changed

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

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

9-
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
109
import {
1110
Directive,
1211
ElementRef,
@@ -16,6 +15,7 @@ import {
1615
OnDestroy,
1716
Optional,
1817
SkipSelf,
18+
booleanAttribute,
1919
} from '@angular/core';
2020
import {Subject} from 'rxjs';
2121
import {CDK_DRAG_PARENT} from '../drag-parent';
@@ -45,12 +45,12 @@ export class CdkDragHandle implements OnDestroy {
4545
readonly _stateChanges = new Subject<CdkDragHandle>();
4646

4747
/** Whether starting to drag through this handle is disabled. */
48-
@Input('cdkDragHandleDisabled')
48+
@Input({alias: 'cdkDragHandleDisabled', transform: booleanAttribute})
4949
get disabled(): boolean {
5050
return this._disabled;
5151
}
52-
set disabled(value: BooleanInput) {
53-
this._disabled = coerceBooleanProperty(value);
52+
set disabled(value: boolean) {
53+
this._disabled = value;
5454
this._stateChanges.next(this);
5555
}
5656
private _disabled = false;

src/cdk/drag-drop/directives/drag-preview.ts

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

9-
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
10-
import {Directive, InjectionToken, Input, TemplateRef} from '@angular/core';
9+
import {Directive, InjectionToken, Input, TemplateRef, booleanAttribute} from '@angular/core';
1110

1211
/**
1312
* Injection token that can be used to reference instances of `CdkDragPreview`. It serves as
@@ -30,14 +29,7 @@ export class CdkDragPreview<T = any> {
3029
@Input() data: T;
3130

3231
/** Whether the preview should preserve the same size as the item that is being dragged. */
33-
@Input()
34-
get matchSize(): boolean {
35-
return this._matchSize;
36-
}
37-
set matchSize(value: BooleanInput) {
38-
this._matchSize = coerceBooleanProperty(value);
39-
}
40-
private _matchSize = false;
32+
@Input({transform: booleanAttribute}) matchSize: boolean = false;
4133

4234
constructor(public templateRef: TemplateRef<T>) {}
4335
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ import {
2929
ChangeDetectorRef,
3030
Self,
3131
InjectionToken,
32+
booleanAttribute,
3233
} from '@angular/core';
33-
import {
34-
coerceBooleanProperty,
35-
coerceNumberProperty,
36-
coerceElement,
37-
BooleanInput,
38-
} from '@angular/cdk/coercion';
34+
import {coerceElement, coerceNumberProperty} from '@angular/cdk/coercion';
3935
import {Observable, Observer, Subject, merge} from 'rxjs';
4036
import {startWith, take, map, takeUntil, switchMap, tap} from 'rxjs/operators';
4137
import type {
@@ -128,12 +124,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
128124
@Input('cdkDragFreeDragPosition') freeDragPosition: Point;
129125

130126
/** Whether starting to drag this element is disabled. */
131-
@Input('cdkDragDisabled')
127+
@Input({alias: 'cdkDragDisabled', transform: booleanAttribute})
132128
get disabled(): boolean {
133129
return this._disabled || (this.dropContainer && this.dropContainer.disabled);
134130
}
135-
set disabled(value: BooleanInput) {
136-
this._disabled = coerceBooleanProperty(value);
131+
set disabled(value: boolean) {
132+
this._disabled = value;
137133
this._dragRef.disabled = this._disabled;
138134
}
139135
private _disabled: boolean;

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

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

9-
import {Directive, OnDestroy, Input, InjectionToken} from '@angular/core';
10-
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
9+
import {Directive, OnDestroy, Input, InjectionToken, booleanAttribute} from '@angular/core';
1110

1211
/**
1312
* Injection token that can be used to reference instances of `CdkDropListGroup`. It serves as
@@ -35,14 +34,8 @@ export class CdkDropListGroup<T> implements OnDestroy {
3534
readonly _items = new Set<T>();
3635

3736
/** Whether starting a dragging sequence from inside this group is disabled. */
38-
@Input('cdkDropListGroupDisabled')
39-
get disabled(): boolean {
40-
return this._disabled;
41-
}
42-
set disabled(value: BooleanInput) {
43-
this._disabled = coerceBooleanProperty(value);
44-
}
45-
private _disabled = false;
37+
@Input({alias: 'cdkDropListGroupDisabled', transform: booleanAttribute})
38+
disabled: boolean = false;
4639

4740
ngOnDestroy() {
4841
this._items.clear();

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

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

9-
import {
10-
BooleanInput,
11-
coerceArray,
12-
coerceNumberProperty,
13-
coerceBooleanProperty,
14-
NumberInput,
15-
} from '@angular/cdk/coercion';
9+
import {NumberInput, coerceArray, coerceNumberProperty} from '@angular/cdk/coercion';
1610
import {
1711
ElementRef,
1812
EventEmitter,
@@ -24,6 +18,7 @@ import {
2418
ChangeDetectorRef,
2519
SkipSelf,
2620
Inject,
21+
booleanAttribute,
2722
} from '@angular/core';
2823
import {Directionality} from '@angular/cdk/bidi';
2924
import {ScrollDispatcher} from '@angular/cdk/scrolling';
@@ -96,22 +91,22 @@ export class CdkDropList<T = any> implements OnDestroy {
9691
@Input('cdkDropListLockAxis') lockAxis: DragAxis;
9792

9893
/** Whether starting a dragging sequence from this container is disabled. */
99-
@Input('cdkDropListDisabled')
94+
@Input({alias: 'cdkDropListDisabled', transform: booleanAttribute})
10095
get disabled(): boolean {
10196
return this._disabled || (!!this._group && this._group.disabled);
10297
}
103-
set disabled(value: BooleanInput) {
98+
set disabled(value: boolean) {
10499
// Usually we sync the directive and ref state right before dragging starts, in order to have
105100
// a single point of failure and to avoid having to use setters for everything. `disabled` is
106101
// a special case, because it can prevent the `beforeStarted` event from firing, which can lock
107102
// the user in a disabled state, so we also need to sync it as it's being set.
108-
this._dropListRef.disabled = this._disabled = coerceBooleanProperty(value);
103+
this._dropListRef.disabled = this._disabled = value;
109104
}
110105
private _disabled: boolean;
111106

112107
/** Whether sorting within this drop list is disabled. */
113-
@Input('cdkDropListSortingDisabled')
114-
sortingDisabled: BooleanInput;
108+
@Input({alias: 'cdkDropListSortingDisabled', transform: booleanAttribute})
109+
sortingDisabled: boolean;
115110

116111
/**
117112
* Function that is used to determine whether an item
@@ -125,8 +120,8 @@ export class CdkDropList<T = any> implements OnDestroy {
125120
sortPredicate: (index: number, drag: CdkDrag, drop: CdkDropList) => boolean = () => true;
126121

127122
/** Whether to auto-scroll the view when the user moves their pointer close to the edges. */
128-
@Input('cdkDropListAutoScrollDisabled')
129-
autoScrollDisabled: BooleanInput;
123+
@Input({alias: 'cdkDropListAutoScrollDisabled', transform: booleanAttribute})
124+
autoScrollDisabled: boolean;
130125

131126
/** Number of pixels to scroll for each frame when auto-scrolling an element. */
132127
@Input('cdkDropListAutoScrollStep')
@@ -302,8 +297,8 @@ export class CdkDropList<T = any> implements OnDestroy {
302297

303298
ref.disabled = this.disabled;
304299
ref.lockAxis = this.lockAxis;
305-
ref.sortingDisabled = coerceBooleanProperty(this.sortingDisabled);
306-
ref.autoScrollDisabled = coerceBooleanProperty(this.autoScrollDisabled);
300+
ref.sortingDisabled = this.sortingDisabled;
301+
ref.autoScrollDisabled = this.autoScrollDisabled;
307302
ref.autoScrollStep = coerceNumberProperty(this.autoScrollStep, 2);
308303
ref
309304
.connectedTo(siblings.filter(drop => drop && drop !== this).map(list => list._dropListRef))

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
_getEventTarget,
1515
_getShadowRoot,
1616
} from '@angular/cdk/platform';
17-
import {coerceBooleanProperty, coerceElement} from '@angular/cdk/coercion';
17+
import {coerceElement} from '@angular/cdk/coercion';
1818
import {isFakeMousedownFromScreenReader, isFakeTouchstartFromScreenReader} from '@angular/cdk/a11y';
1919
import {Subscription, Subject, Observable} from 'rxjs';
2020
import type {DropListRef} from './drop-list-ref';
@@ -287,12 +287,10 @@ export class DragRef<T = any> {
287287
return this._disabled || !!(this._dropContainer && this._dropContainer.disabled);
288288
}
289289
set disabled(value: boolean) {
290-
const newValue = coerceBooleanProperty(value);
291-
292-
if (newValue !== this._disabled) {
293-
this._disabled = newValue;
290+
if (value !== this._disabled) {
291+
this._disabled = value;
294292
this._toggleNativeDragInteractions();
295-
this._handles.forEach(handle => toggleNativeDragInteractions(handle, newValue));
293+
this._handles.forEach(handle => toggleNativeDragInteractions(handle, value));
296294
}
297295
}
298296
private _disabled = false;

tools/public_api_guard/cdk/drag-drop.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
```ts
66

77
import { AfterViewInit } from '@angular/core';
8-
import { BooleanInput } from '@angular/cdk/coercion';
98
import { ChangeDetectorRef } from '@angular/core';
109
import { Direction } from '@angular/cdk/bidi';
1110
import { Directionality } from '@angular/cdk/bidi';
@@ -58,7 +57,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
5857
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
5958
data: T;
6059
get disabled(): boolean;
61-
set disabled(value: BooleanInput);
60+
set disabled(value: boolean);
6261
_dragRef: DragRef<CdkDrag<T>>;
6362
dragStartDelay: DragStartDelay;
6463
dropContainer: CdkDropList;
@@ -75,6 +74,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
7574
lockAxis: DragAxis;
7675
readonly moved: Observable<CdkDragMove<T>>;
7776
// (undocumented)
77+
static ngAcceptInputType_disabled: unknown;
78+
// (undocumented)
7879
ngAfterViewInit(): void;
7980
// (undocumented)
8081
ngOnChanges(changes: SimpleChanges): void;
@@ -145,10 +146,12 @@ export interface CdkDragExit<T = any, I = T> {
145146
export class CdkDragHandle implements OnDestroy {
146147
constructor(element: ElementRef<HTMLElement>, parentDrag?: any);
147148
get disabled(): boolean;
148-
set disabled(value: BooleanInput);
149+
set disabled(value: boolean);
149150
// (undocumented)
150151
element: ElementRef<HTMLElement>;
151152
// (undocumented)
153+
static ngAcceptInputType_disabled: unknown;
154+
// (undocumented)
152155
ngOnDestroy(): void;
153156
_parentDrag: {} | undefined;
154157
readonly _stateChanges: Subject<CdkDragHandle>;
@@ -192,8 +195,9 @@ export class CdkDragPlaceholder<T = any> {
192195
export class CdkDragPreview<T = any> {
193196
constructor(templateRef: TemplateRef<T>);
194197
data: T;
195-
get matchSize(): boolean;
196-
set matchSize(value: BooleanInput);
198+
matchSize: boolean;
199+
// (undocumented)
200+
static ngAcceptInputType_matchSize: unknown;
197201
// (undocumented)
198202
templateRef: TemplateRef<T>;
199203
// (undocumented)
@@ -227,12 +231,12 @@ export class CdkDropList<T = any> implements OnDestroy {
227231
constructor(
228232
element: ElementRef<HTMLElement>, dragDrop: DragDrop, _changeDetectorRef: ChangeDetectorRef, _scrollDispatcher: ScrollDispatcher, _dir?: Directionality | undefined, _group?: CdkDropListGroup<CdkDropList<any>> | undefined, config?: DragDropConfig);
229233
addItem(item: CdkDrag): void;
230-
autoScrollDisabled: BooleanInput;
234+
autoScrollDisabled: boolean;
231235
autoScrollStep: NumberInput;
232236
connectedTo: (CdkDropList | string)[] | CdkDropList | string;
233237
data: T;
234238
get disabled(): boolean;
235-
set disabled(value: BooleanInput);
239+
set disabled(value: boolean);
236240
_dropListRef: DropListRef<CdkDropList<T>>;
237241
readonly dropped: EventEmitter<CdkDragDrop<T, any>>;
238242
element: ElementRef<HTMLElement>;
@@ -243,11 +247,17 @@ export class CdkDropList<T = any> implements OnDestroy {
243247
id: string;
244248
lockAxis: DragAxis;
245249
// (undocumented)
250+
static ngAcceptInputType_autoScrollDisabled: unknown;
251+
// (undocumented)
252+
static ngAcceptInputType_disabled: unknown;
253+
// (undocumented)
254+
static ngAcceptInputType_sortingDisabled: unknown;
255+
// (undocumented)
246256
ngOnDestroy(): void;
247257
orientation: DropListOrientation;
248258
removeItem(item: CdkDrag): void;
249259
readonly sorted: EventEmitter<CdkDragSortEvent<T>>;
250-
sortingDisabled: BooleanInput;
260+
sortingDisabled: boolean;
251261
sortPredicate: (index: number, drag: CdkDrag, drop: CdkDropList) => boolean;
252262
// (undocumented)
253263
static ɵdir: i0.ɵɵDirectiveDeclaration<CdkDropList<any>, "[cdkDropList], cdk-drop-list", ["cdkDropList"], { "connectedTo": { "alias": "cdkDropListConnectedTo"; "required": false; }; "data": { "alias": "cdkDropListData"; "required": false; }; "orientation": { "alias": "cdkDropListOrientation"; "required": false; }; "id": { "alias": "id"; "required": false; }; "lockAxis": { "alias": "cdkDropListLockAxis"; "required": false; }; "disabled": { "alias": "cdkDropListDisabled"; "required": false; }; "sortingDisabled": { "alias": "cdkDropListSortingDisabled"; "required": false; }; "enterPredicate": { "alias": "cdkDropListEnterPredicate"; "required": false; }; "sortPredicate": { "alias": "cdkDropListSortPredicate"; "required": false; }; "autoScrollDisabled": { "alias": "cdkDropListAutoScrollDisabled"; "required": false; }; "autoScrollStep": { "alias": "cdkDropListAutoScrollStep"; "required": false; }; }, { "dropped": "cdkDropListDropped"; "entered": "cdkDropListEntered"; "exited": "cdkDropListExited"; "sorted": "cdkDropListSorted"; }, never, never, true, never>;
@@ -257,10 +267,11 @@ export class CdkDropList<T = any> implements OnDestroy {
257267

258268
// @public
259269
export class CdkDropListGroup<T> implements OnDestroy {
260-
get disabled(): boolean;
261-
set disabled(value: BooleanInput);
270+
disabled: boolean;
262271
readonly _items: Set<T>;
263272
// (undocumented)
273+
static ngAcceptInputType_disabled: unknown;
274+
// (undocumented)
264275
ngOnDestroy(): void;
265276
// (undocumented)
266277
static ɵdir: i0.ɵɵDirectiveDeclaration<CdkDropListGroup<any>, "[cdkDropListGroup]", ["cdkDropListGroup"], { "disabled": { "alias": "cdkDropListGroupDisabled"; "required": false; }; }, {}, never, never, true, never>;

0 commit comments

Comments
 (0)