Skip to content

Commit 24180d1

Browse files
authored
refactor(multiple): remove coercion members (#24055)
* 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 6b79ea4 commit 24180d1

File tree

152 files changed

+469
-1552
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

+469
-1552
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';

0 commit comments

Comments
 (0)