Skip to content

Commit ef519a1

Browse files
committed
refactor(multiple): remove coercion members
* Replaces all usages of the `ngAcceptInputType` members with type declarations directly on the setters. * Replaces the lint rule that was checking that the correct coercion members were added with a rule that doesn't allow new coercion members to be added.
1 parent a52da04 commit ef519a1

File tree

152 files changed

+468
-1551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+468
-1551
lines changed

CODING_STANDARDS.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,13 @@ For example:
274274
```ts
275275
@Input() disabled: boolean;
276276
get disabled(): boolean { return this._disabled; }
277-
set disabled(v: boolean) { this._disabled = coerceBooleanProperty(v); }
277+
set disabled(v: BooleanInput) { this._disabled = coerceBooleanProperty(v); }
278278
private _disabled = false;
279-
280-
...
281-
282-
static ngAcceptInputType_value: BooleanInput;
283279
```
284280
The above code allows users to set `disabled` similar to how it can be set on native inputs:
285281
```html
286282
<component disabled></component>
287283
```
288-
Even though an empty string is technically what is being provided as the value of `disabled`,
289-
`ngAcceptInputType` allows the mismatched type to be provided and `coerceBooleanProperty`
290-
interprets the given value (an empty string) to the correct type & value, which in this case would
291-
be `true`.
292284

293285
#### Expose native inputs
294286
Native inputs used in components should be exposed to developers through `ng-content`. This allows

src/cdk-experimental/combobox/combobox.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
7070
get disabled(): boolean {
7171
return this._disabled;
7272
}
73-
set disabled(value: boolean) {
73+
set disabled(value: BooleanInput) {
7474
this._disabled = coerceBooleanProperty(value);
7575
}
7676
private _disabled: boolean = false;
@@ -79,7 +79,7 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
7979
get openActions(): OpenAction[] {
8080
return this._openActions;
8181
}
82-
set openActions(action: OpenAction[]) {
82+
set openActions(action: OpenActionInput) {
8383
this._openActions = this._coerceOpenActionProperty(action);
8484
}
8585
private _openActions: OpenAction[] = ['click'];
@@ -89,7 +89,7 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
8989
get autoSetText(): boolean {
9090
return this._autoSetText;
9191
}
92-
set autoSetText(value: boolean) {
92+
set autoSetText(value: BooleanInput) {
9393
this._autoSetText = coerceBooleanProperty(value);
9494
}
9595
private _autoSetText: boolean = true;
@@ -287,18 +287,14 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
287287
return this._panelContent;
288288
}
289289

290-
private _coerceOpenActionProperty(input: string | OpenAction[]): OpenAction[] {
290+
private _coerceOpenActionProperty(input: OpenActionInput): OpenAction[] {
291291
let actions = typeof input === 'string' ? input.trim().split(/[ ,]+/) : input;
292292
if (
293293
(typeof ngDevMode === 'undefined' || ngDevMode) &&
294-
actions.some(a => allowedOpenActions.indexOf(a) === -1)
294+
actions?.some(a => allowedOpenActions.indexOf(a) === -1)
295295
) {
296296
throw Error(`${input} is not a support open action for CdkCombobox`);
297297
}
298298
return actions as OpenAction[];
299299
}
300-
301-
static ngAcceptInputType_openActions: OpenActionInput;
302-
static ngAcceptInputType_autoSetText: OpenActionInput;
303-
static ngAcceptInputType_disabled: BooleanInput;
304300
}

src/cdk-experimental/listbox/listbox.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
7474
get selected(): boolean {
7575
return this._selected;
7676
}
77-
set selected(value: boolean) {
77+
set selected(value: BooleanInput) {
7878
if (!this._disabled) {
7979
this._selected = coerceBooleanProperty(value);
8080
}
@@ -84,7 +84,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
8484
get disabled(): boolean {
8585
return this._disabled;
8686
}
87-
set disabled(value: boolean) {
87+
set disabled(value: BooleanInput) {
8888
this._disabled = coerceBooleanProperty(value);
8989
}
9090

@@ -198,9 +198,6 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
198198
setInactiveStyles() {
199199
this._active = false;
200200
}
201-
202-
static ngAcceptInputType_selected: BooleanInput;
203-
static ngAcceptInputType_disabled: BooleanInput;
204201
}
205202

206203
@Directive({
@@ -261,16 +258,17 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
261258
get multiple(): boolean {
262259
return this._multiple;
263260
}
264-
set multiple(value: boolean) {
265-
this._updateSelectionOnMultiSelectionChange(value);
266-
this._multiple = coerceBooleanProperty(value);
261+
set multiple(value: BooleanInput) {
262+
const coercedValue = coerceBooleanProperty(value);
263+
this._updateSelectionOnMultiSelectionChange(coercedValue);
264+
this._multiple = coercedValue;
267265
}
268266

269267
@Input()
270268
get disabled(): boolean {
271269
return this._disabled;
272270
}
273-
set disabled(value: boolean) {
271+
set disabled(value: BooleanInput) {
274272
this._disabled = coerceBooleanProperty(value);
275273
}
276274

@@ -279,7 +277,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
279277
get useActiveDescendant(): boolean {
280278
return this._useActiveDescendant;
281279
}
282-
set useActiveDescendant(shouldUseActiveDescendant: boolean) {
280+
set useActiveDescendant(shouldUseActiveDescendant: BooleanInput) {
283281
this._useActiveDescendant = coerceBooleanProperty(shouldUseActiveDescendant);
284282
}
285283

@@ -288,7 +286,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
288286
get autoFocus(): boolean {
289287
return this._autoFocus;
290288
}
291-
set autoFocus(shouldAutoFocus: boolean) {
289+
set autoFocus(shouldAutoFocus: BooleanInput) {
292290
this._autoFocus = coerceBooleanProperty(shouldAutoFocus);
293291
}
294292

@@ -553,11 +551,6 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
553551
}
554552
}
555553
}
556-
557-
static ngAcceptInputType_disabled: BooleanInput;
558-
static ngAcceptInputType_multiple: BooleanInput;
559-
static ngAcceptInputType_useActiveDescendant: BooleanInput;
560-
static ngAcceptInputType_autoFocus: BooleanInput;
561554
}
562555

563556
/** Change event that is being fired whenever the selected state of an option changes. */

src/cdk-experimental/menu/context-menu.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ export class CdkContextMenuTrigger implements OnDestroy {
114114

115115
/** Whether the context menu should be disabled. */
116116
@Input('cdkContextMenuDisabled')
117-
get disabled() {
117+
get disabled(): boolean {
118118
return this._disabled;
119119
}
120-
set disabled(value: boolean) {
120+
set disabled(value: BooleanInput) {
121121
this._disabled = coerceBooleanProperty(value);
122122
}
123123
private _disabled = false;
@@ -326,6 +326,4 @@ export class CdkContextMenuTrigger implements OnDestroy {
326326
this._menuPanel._menuStack = null;
327327
}
328328
}
329-
330-
static ngAcceptInputType_disabled: BooleanInput;
331329
}

src/cdk-experimental/menu/menu-item-selectable.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export abstract class CdkMenuItemSelectable extends CdkMenuItem {
2525

2626
/** Whether the element is checked */
2727
@Input()
28-
get checked() {
28+
get checked(): boolean {
2929
return this._checked;
3030
}
31-
set checked(value: boolean) {
31+
set checked(value: BooleanInput) {
3232
this._checked = coerceBooleanProperty(value);
3333
}
3434
private _checked = false;
@@ -45,6 +45,4 @@ export abstract class CdkMenuItemSelectable extends CdkMenuItem {
4545
this.toggled.next(this);
4646
}
4747
}
48-
49-
static ngAcceptInputType_checked: BooleanInput;
5048
}

src/cdk-experimental/menu/menu-item.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, Toggler,
6666
get disabled(): boolean {
6767
return this._disabled;
6868
}
69-
set disabled(value: boolean) {
69+
set disabled(value: BooleanInput) {
7070
this._disabled = coerceBooleanProperty(value);
7171
}
7272
private _disabled = false;
@@ -259,6 +259,4 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, Toggler,
259259
ngOnDestroy() {
260260
this._destroyed.next();
261261
}
262-
263-
static ngAcceptInputType_disabled: BooleanInput;
264262
}

src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
472472
get minBufferPx(): number {
473473
return this._minBufferPx;
474474
}
475-
set minBufferPx(value: number) {
475+
set minBufferPx(value: NumberInput) {
476476
this._minBufferPx = coerceNumberProperty(value);
477477
}
478478
_minBufferPx = 100;
@@ -487,7 +487,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
487487
get maxBufferPx(): number {
488488
return this._maxBufferPx;
489489
}
490-
set maxBufferPx(value: number) {
490+
set maxBufferPx(value: NumberInput) {
491491
this._maxBufferPx = coerceNumberProperty(value);
492492
}
493493
_maxBufferPx = 200;
@@ -498,7 +498,4 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
498498
ngOnChanges() {
499499
this._scrollStrategy.updateBufferSize(this.minBufferPx, this.maxBufferPx);
500500
}
501-
502-
static ngAcceptInputType_minBufferPx: NumberInput;
503-
static ngAcceptInputType_maxBufferPx: NumberInput;
504501
}

src/cdk-experimental/selection/row-selection.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ export class CdkRowSelection<T> {
3232
get index(): number | undefined {
3333
return this._index;
3434
}
35-
set index(index: number | undefined) {
35+
set index(index: NumberInput) {
3636
this._index = coerceNumberProperty(index);
3737
}
3838
protected _index?: number;
3939

4040
constructor(readonly _selection: CdkSelection<T>) {}
41-
42-
static ngAcceptInputType_index: NumberInput;
4341
}

src/cdk-experimental/selection/selection-toggle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class CdkSelectionToggle<T> implements OnDestroy, OnInit {
3737
get index(): number | undefined {
3838
return this._index;
3939
}
40-
set index(index: number | undefined) {
40+
set index(index: NumberInput) {
4141
this._index = coerceNumberProperty(index);
4242
}
4343
protected _index?: number;
@@ -96,6 +96,4 @@ export class CdkSelectionToggle<T> implements OnDestroy, OnInit {
9696
private _isSelected(): boolean {
9797
return this._selection.isSelected(this.value, this.index);
9898
}
99-
100-
static ngAcceptInputType_index: NumberInput;
10199
}

src/cdk-experimental/selection/selection.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
5454
get multiple(): boolean {
5555
return this._multiple;
5656
}
57-
set multiple(multiple: boolean) {
57+
set multiple(multiple: BooleanInput) {
5858
this._multiple = coerceBooleanProperty(multiple);
5959
}
6060
protected _multiple: boolean;
@@ -217,8 +217,6 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
217217
}
218218

219219
selectAllState: SelectAllState = 'none';
220-
221-
static ngAcceptInputType_multiple: BooleanInput;
222220
}
223221

224222
type SelectAllState = 'all' | 'none' | 'partial';

src/cdk/a11y/focus-trap/focus-trap.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
417417
get enabled(): boolean {
418418
return this.focusTrap.enabled;
419419
}
420-
set enabled(value: boolean) {
420+
set enabled(value: BooleanInput) {
421421
this.focusTrap.enabled = coerceBooleanProperty(value);
422422
}
423423

@@ -429,7 +429,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
429429
get autoCapture(): boolean {
430430
return this._autoCapture;
431431
}
432-
set autoCapture(value: boolean) {
432+
set autoCapture(value: BooleanInput) {
433433
this._autoCapture = coerceBooleanProperty(value);
434434
}
435435
private _autoCapture: boolean;
@@ -488,7 +488,4 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
488488
this._previouslyFocusedElement = _getFocusedElementPierceShadowDom();
489489
this.focusTrap.focusInitialElementWhenReady();
490490
}
491-
492-
static ngAcceptInputType_enabled: BooleanInput;
493-
static ngAcceptInputType_autoCapture: BooleanInput;
494491
}

src/cdk/accordion/accordion-item.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class CdkAccordionItem implements OnDestroy {
6363
get expanded(): boolean {
6464
return this._expanded;
6565
}
66-
set expanded(expanded: boolean) {
66+
set expanded(expanded: BooleanInput) {
6767
expanded = coerceBooleanProperty(expanded);
6868

6969
// Only emit events and update the internal value if the value changes.
@@ -95,7 +95,7 @@ export class CdkAccordionItem implements OnDestroy {
9595
get disabled(): boolean {
9696
return this._disabled;
9797
}
98-
set disabled(disabled: boolean) {
98+
set disabled(disabled: BooleanInput) {
9999
this._disabled = coerceBooleanProperty(disabled);
100100
}
101101
private _disabled = false;
@@ -166,7 +166,4 @@ export class CdkAccordionItem implements OnDestroy {
166166
}
167167
});
168168
}
169-
170-
static ngAcceptInputType_expanded: BooleanInput;
171-
static ngAcceptInputType_disabled: BooleanInput;
172169
}

src/cdk/accordion/accordion.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class CdkAccordion implements OnDestroy, OnChanges {
4343
get multi(): boolean {
4444
return this._multi;
4545
}
46-
set multi(multi: boolean) {
46+
set multi(multi: BooleanInput) {
4747
this._multi = coerceBooleanProperty(multi);
4848
}
4949
private _multi: boolean = false;
@@ -68,6 +68,4 @@ export class CdkAccordion implements OnDestroy, OnChanges {
6868
this._stateChanges.complete();
6969
this._openCloseAllActions.complete();
7070
}
71-
72-
static ngAcceptInputType_multi: BooleanInput;
7371
}

src/cdk/coercion/coercion.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MyButton {
2525
// It also allows for a string to be passed like `<my-button disabled="true"></my-button>`.
2626
@Input()
2727
get disabled() { return this._disabled; }
28-
set disabled(value: any) {
28+
set disabled(value: BooleanInput) {
2929
this._disabled = coerceBooleanProperty(value);
3030
}
3131
private _disabled = false;
@@ -37,7 +37,7 @@ class MyButton {
3737
// parsed to a number.
3838
@Input()
3939
get greetDelay() { return this._greetDelay; }
40-
set greetDelay(value: any) {
40+
set greetDelay(value: NumberInput) {
4141
this._greetDelay = coerceNumberProperty(value, 0);
4242
}
4343
private _greetDelay = 0;
@@ -51,9 +51,5 @@ class MyButton {
5151
getElement(elementOrRef: ElementRef<HTMLElement> | HTMLElement): HTMLElement {
5252
return coerceElement(elementOrRef);
5353
}
54-
55-
// Required so that the template type checker can infer the type of the coerced inputs.
56-
static ngAcceptInputType_disabled: BooleanInput;
57-
static ngAcceptInputType_greetDelay: NumberInput;
5854
}
5955
```

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class CdkDragHandle implements OnDestroy {
4848
get disabled(): boolean {
4949
return this._disabled;
5050
}
51-
set disabled(value: boolean) {
51+
set disabled(value: BooleanInput) {
5252
this._disabled = coerceBooleanProperty(value);
5353
this._stateChanges.next(this);
5454
}
@@ -68,6 +68,4 @@ export class CdkDragHandle implements OnDestroy {
6868
ngOnDestroy() {
6969
this._stateChanges.complete();
7070
}
71-
72-
static ngAcceptInputType_disabled: BooleanInput;
7371
}

0 commit comments

Comments
 (0)