Skip to content

Commit

Permalink
refactor(multiple): remove coercion members (#24055)
Browse files Browse the repository at this point in the history
* 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.

(cherry picked from commit 24180d1)
  • Loading branch information
crisbeto authored and amysorto committed Dec 14, 2021
1 parent 655b41a commit 7310914
Show file tree
Hide file tree
Showing 152 changed files with 469 additions and 1,552 deletions.
10 changes: 1 addition & 9 deletions CODING_STANDARDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,13 @@ For example:
```ts
@Input() disabled: boolean;
get disabled(): boolean { return this._disabled; }
set disabled(v: boolean) { this._disabled = coerceBooleanProperty(v); }
set disabled(v: BooleanInput) { this._disabled = coerceBooleanProperty(v); }
private _disabled = false;

...

static ngAcceptInputType_value: BooleanInput;
```
The above code allows users to set `disabled` similar to how it can be set on native inputs:
```html
<component disabled></component>
```
Even though an empty string is technically what is being provided as the value of `disabled`,
`ngAcceptInputType` allows the mismatched type to be provided and `coerceBooleanProperty`
interprets the given value (an empty string) to the correct type & value, which in this case would
be `true`.

#### Expose native inputs
Native inputs used in components should be exposed to developers through `ng-content`. This allows
Expand Down
14 changes: 5 additions & 9 deletions src/cdk-experimental/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
}
private _disabled: boolean = false;
Expand All @@ -79,7 +79,7 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
get openActions(): OpenAction[] {
return this._openActions;
}
set openActions(action: OpenAction[]) {
set openActions(action: OpenActionInput) {
this._openActions = this._coerceOpenActionProperty(action);
}
private _openActions: OpenAction[] = ['click'];
Expand All @@ -89,7 +89,7 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
get autoSetText(): boolean {
return this._autoSetText;
}
set autoSetText(value: boolean) {
set autoSetText(value: BooleanInput) {
this._autoSetText = coerceBooleanProperty(value);
}
private _autoSetText: boolean = true;
Expand Down Expand Up @@ -287,18 +287,14 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
return this._panelContent;
}

private _coerceOpenActionProperty(input: string | OpenAction[]): OpenAction[] {
private _coerceOpenActionProperty(input: OpenActionInput): OpenAction[] {
let actions = typeof input === 'string' ? input.trim().split(/[ ,]+/) : input;
if (
(typeof ngDevMode === 'undefined' || ngDevMode) &&
actions.some(a => allowedOpenActions.indexOf(a) === -1)
actions?.some(a => allowedOpenActions.indexOf(a) === -1)
) {
throw Error(`${input} is not a support open action for CdkCombobox`);
}
return actions as OpenAction[];
}

static ngAcceptInputType_openActions: OpenActionInput;
static ngAcceptInputType_autoSetText: OpenActionInput;
static ngAcceptInputType_disabled: BooleanInput;
}
25 changes: 9 additions & 16 deletions src/cdk-experimental/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
get selected(): boolean {
return this._selected;
}
set selected(value: boolean) {
set selected(value: BooleanInput) {
if (!this._disabled) {
this._selected = coerceBooleanProperty(value);
}
Expand All @@ -84,7 +84,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
}

Expand Down Expand Up @@ -198,9 +198,6 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
setInactiveStyles() {
this._active = false;
}

static ngAcceptInputType_selected: BooleanInput;
static ngAcceptInputType_disabled: BooleanInput;
}

@Directive({
Expand Down Expand Up @@ -261,16 +258,17 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
get multiple(): boolean {
return this._multiple;
}
set multiple(value: boolean) {
this._updateSelectionOnMultiSelectionChange(value);
this._multiple = coerceBooleanProperty(value);
set multiple(value: BooleanInput) {
const coercedValue = coerceBooleanProperty(value);
this._updateSelectionOnMultiSelectionChange(coercedValue);
this._multiple = coercedValue;
}

@Input()
get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
}

Expand All @@ -279,7 +277,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
get useActiveDescendant(): boolean {
return this._useActiveDescendant;
}
set useActiveDescendant(shouldUseActiveDescendant: boolean) {
set useActiveDescendant(shouldUseActiveDescendant: BooleanInput) {
this._useActiveDescendant = coerceBooleanProperty(shouldUseActiveDescendant);
}

Expand All @@ -288,7 +286,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
get autoFocus(): boolean {
return this._autoFocus;
}
set autoFocus(shouldAutoFocus: boolean) {
set autoFocus(shouldAutoFocus: BooleanInput) {
this._autoFocus = coerceBooleanProperty(shouldAutoFocus);
}

Expand Down Expand Up @@ -553,11 +551,6 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
}
}
}

static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_multiple: BooleanInput;
static ngAcceptInputType_useActiveDescendant: BooleanInput;
static ngAcceptInputType_autoFocus: BooleanInput;
}

/** Change event that is being fired whenever the selected state of an option changes. */
Expand Down
6 changes: 2 additions & 4 deletions src/cdk-experimental/menu/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export class CdkContextMenuTrigger implements OnDestroy {

/** Whether the context menu should be disabled. */
@Input('cdkContextMenuDisabled')
get disabled() {
get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
}
private _disabled = false;
Expand Down Expand Up @@ -326,6 +326,4 @@ export class CdkContextMenuTrigger implements OnDestroy {
this._menuPanel._menuStack = null;
}
}

static ngAcceptInputType_disabled: BooleanInput;
}
6 changes: 2 additions & 4 deletions src/cdk-experimental/menu/menu-item-selectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export abstract class CdkMenuItemSelectable extends CdkMenuItem {

/** Whether the element is checked */
@Input()
get checked() {
get checked(): boolean {
return this._checked;
}
set checked(value: boolean) {
set checked(value: BooleanInput) {
this._checked = coerceBooleanProperty(value);
}
private _checked = false;
Expand All @@ -45,6 +45,4 @@ export abstract class CdkMenuItemSelectable extends CdkMenuItem {
this.toggled.next(this);
}
}

static ngAcceptInputType_checked: BooleanInput;
}
4 changes: 1 addition & 3 deletions src/cdk-experimental/menu/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, Toggler,
get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
}
private _disabled = false;
Expand Down Expand Up @@ -259,6 +259,4 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, Toggler,
ngOnDestroy() {
this._destroyed.next();
}

static ngAcceptInputType_disabled: BooleanInput;
}
7 changes: 2 additions & 5 deletions src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
get minBufferPx(): number {
return this._minBufferPx;
}
set minBufferPx(value: number) {
set minBufferPx(value: NumberInput) {
this._minBufferPx = coerceNumberProperty(value);
}
_minBufferPx = 100;
Expand All @@ -487,7 +487,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
get maxBufferPx(): number {
return this._maxBufferPx;
}
set maxBufferPx(value: number) {
set maxBufferPx(value: NumberInput) {
this._maxBufferPx = coerceNumberProperty(value);
}
_maxBufferPx = 200;
Expand All @@ -498,7 +498,4 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
ngOnChanges() {
this._scrollStrategy.updateBufferSize(this.minBufferPx, this.maxBufferPx);
}

static ngAcceptInputType_minBufferPx: NumberInput;
static ngAcceptInputType_maxBufferPx: NumberInput;
}
4 changes: 1 addition & 3 deletions src/cdk-experimental/selection/row-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ export class CdkRowSelection<T> {
get index(): number | undefined {
return this._index;
}
set index(index: number | undefined) {
set index(index: NumberInput) {
this._index = coerceNumberProperty(index);
}
protected _index?: number;

constructor(readonly _selection: CdkSelection<T>) {}

static ngAcceptInputType_index: NumberInput;
}
4 changes: 1 addition & 3 deletions src/cdk-experimental/selection/selection-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class CdkSelectionToggle<T> implements OnDestroy, OnInit {
get index(): number | undefined {
return this._index;
}
set index(index: number | undefined) {
set index(index: NumberInput) {
this._index = coerceNumberProperty(index);
}
protected _index?: number;
Expand Down Expand Up @@ -96,6 +96,4 @@ export class CdkSelectionToggle<T> implements OnDestroy, OnInit {
private _isSelected(): boolean {
return this._selection.isSelected(this.value, this.index);
}

static ngAcceptInputType_index: NumberInput;
}
4 changes: 1 addition & 3 deletions src/cdk-experimental/selection/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
get multiple(): boolean {
return this._multiple;
}
set multiple(multiple: boolean) {
set multiple(multiple: BooleanInput) {
this._multiple = coerceBooleanProperty(multiple);
}
protected _multiple: boolean;
Expand Down Expand Up @@ -217,8 +217,6 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
}

selectAllState: SelectAllState = 'none';

static ngAcceptInputType_multiple: BooleanInput;
}

type SelectAllState = 'all' | 'none' | 'partial';
Expand Down
7 changes: 2 additions & 5 deletions src/cdk/a11y/focus-trap/focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
get enabled(): boolean {
return this.focusTrap.enabled;
}
set enabled(value: boolean) {
set enabled(value: BooleanInput) {
this.focusTrap.enabled = coerceBooleanProperty(value);
}

Expand All @@ -429,7 +429,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
get autoCapture(): boolean {
return this._autoCapture;
}
set autoCapture(value: boolean) {
set autoCapture(value: BooleanInput) {
this._autoCapture = coerceBooleanProperty(value);
}
private _autoCapture: boolean;
Expand Down Expand Up @@ -488,7 +488,4 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
this._previouslyFocusedElement = _getFocusedElementPierceShadowDom();
this.focusTrap.focusInitialElementWhenReady();
}

static ngAcceptInputType_enabled: BooleanInput;
static ngAcceptInputType_autoCapture: BooleanInput;
}
7 changes: 2 additions & 5 deletions src/cdk/accordion/accordion-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class CdkAccordionItem implements OnDestroy {
get expanded(): boolean {
return this._expanded;
}
set expanded(expanded: boolean) {
set expanded(expanded: BooleanInput) {
expanded = coerceBooleanProperty(expanded);

// Only emit events and update the internal value if the value changes.
Expand Down Expand Up @@ -95,7 +95,7 @@ export class CdkAccordionItem implements OnDestroy {
get disabled(): boolean {
return this._disabled;
}
set disabled(disabled: boolean) {
set disabled(disabled: BooleanInput) {
this._disabled = coerceBooleanProperty(disabled);
}
private _disabled = false;
Expand Down Expand Up @@ -166,7 +166,4 @@ export class CdkAccordionItem implements OnDestroy {
}
});
}

static ngAcceptInputType_expanded: BooleanInput;
static ngAcceptInputType_disabled: BooleanInput;
}
4 changes: 1 addition & 3 deletions src/cdk/accordion/accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CdkAccordion implements OnDestroy, OnChanges {
get multi(): boolean {
return this._multi;
}
set multi(multi: boolean) {
set multi(multi: BooleanInput) {
this._multi = coerceBooleanProperty(multi);
}
private _multi: boolean = false;
Expand All @@ -68,6 +68,4 @@ export class CdkAccordion implements OnDestroy, OnChanges {
this._stateChanges.complete();
this._openCloseAllActions.complete();
}

static ngAcceptInputType_multi: BooleanInput;
}
8 changes: 2 additions & 6 deletions src/cdk/coercion/coercion.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MyButton {
// It also allows for a string to be passed like `<my-button disabled="true"></my-button>`.
@Input()
get disabled() { return this._disabled; }
set disabled(value: any) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
}
private _disabled = false;
Expand All @@ -37,7 +37,7 @@ class MyButton {
// parsed to a number.
@Input()
get greetDelay() { return this._greetDelay; }
set greetDelay(value: any) {
set greetDelay(value: NumberInput) {
this._greetDelay = coerceNumberProperty(value, 0);
}
private _greetDelay = 0;
Expand All @@ -51,9 +51,5 @@ class MyButton {
getElement(elementOrRef: ElementRef<HTMLElement> | HTMLElement): HTMLElement {
return coerceElement(elementOrRef);
}

// Required so that the template type checker can infer the type of the coerced inputs.
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_greetDelay: NumberInput;
}
```
4 changes: 1 addition & 3 deletions src/cdk/drag-drop/directives/drag-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class CdkDragHandle implements OnDestroy {
get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
set disabled(value: BooleanInput) {
this._disabled = coerceBooleanProperty(value);
this._stateChanges.next(this);
}
Expand All @@ -68,6 +68,4 @@ export class CdkDragHandle implements OnDestroy {
ngOnDestroy() {
this._stateChanges.complete();
}

static ngAcceptInputType_disabled: BooleanInput;
}

0 comments on commit 7310914

Please sign in to comment.