Skip to content

refactor(multiple): remove coercion members #23606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion integration/ts-compat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ write_file(
# List of TypeScript packages that we want to run the compatibility test against.
# The list contains NPM module names that resolve to the desired TypeScript version.
typescript_version_packages = [
"typescript-4.2",
"typescript",
]

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
"tslint": "^6.1.3",
"tsutils": "^3.21.0",
"typescript": "~4.3.2",
"typescript-4.2": "npm:[email protected]",
"vrsource-tslint-rules": "6.0.0",
"yaml": "^1.10.0"
},
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 @@ -64,22 +64,22 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {

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

@Input()
get openActions(): OpenAction[] {
return this._openActions;
}
set openActions(action: OpenAction[]) {
set openActions(action: OpenActionInput) {
this._openActions = this._coerceOpenActionProperty(action);
}
private _openActions: OpenAction[] = ['click'];

/** Whether the textContent is automatically updated upon change of the combobox value. */
@Input()
get autoSetText(): boolean { return this._autoSetText; }
set autoSetText(value: boolean) { this._autoSetText = coerceBooleanProperty(value); }
set autoSetText(value: BooleanInput) { this._autoSetText = coerceBooleanProperty(value); }
private _autoSetText: boolean = true;

@Output('comboboxPanelOpened') readonly opened: EventEmitter<void> = new EventEmitter<void>();
Expand Down Expand Up @@ -276,16 +276,12 @@ 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 || 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 @@ -67,7 +67,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 @@ -77,7 +77,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 @@ -191,9 +191,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 @@ -256,16 +253,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 @@ -274,7 +272,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 @@ -283,7 +281,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 @@ -547,11 +545,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 @@ -324,6 +324,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 @@ -61,7 +61,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 @@ -276,6 +276,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 @@ -447,7 +447,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
*/
@Input()
get minBufferPx(): number { return this._minBufferPx; }
set minBufferPx(value: number) { this._minBufferPx = coerceNumberProperty(value); }
set minBufferPx(value: NumberInput) { this._minBufferPx = coerceNumberProperty(value); }
_minBufferPx = 100;

/**
Expand All @@ -458,7 +458,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges {
*/
@Input()
get maxBufferPx(): number { return this._maxBufferPx; }
set maxBufferPx(value: number) { this._maxBufferPx = coerceNumberProperty(value); }
set maxBufferPx(value: NumberInput) { this._maxBufferPx = coerceNumberProperty(value); }
_maxBufferPx = 200;

/** The scroll strategy used by this directive. */
Expand All @@ -467,7 +467,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 @@ -30,10 +30,8 @@ export class CdkRowSelection<T> {

@Input('cdkRowSelectionIndex')
get index(): number|undefined { return this._index; }
set index(index: number|undefined) { this._index = coerceNumberProperty(index); }
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 @@ -43,7 +43,7 @@ export class CdkSelectionToggle<T> implements OnDestroy, OnInit {
/** The index of the value in the list. Required when used with `trackBy` */
@Input('cdkSelectionToggleIndex')
get index(): number|undefined { return this._index; }
set index(index: number|undefined) { this._index = coerceNumberProperty(index); }
set index(index: NumberInput) { this._index = coerceNumberProperty(index); }
protected _index?: number;

/** The checked state of the selection toggle */
Expand Down Expand Up @@ -98,6 +98,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 @@ -214,8 +214,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 @@ -395,15 +395,15 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
/** Whether the focus trap is active. */
@Input('cdkTrapFocus')
get enabled(): boolean { return this.focusTrap.enabled; }
set enabled(value: boolean) { this.focusTrap.enabled = coerceBooleanProperty(value); }
set enabled(value: BooleanInput) { this.focusTrap.enabled = coerceBooleanProperty(value); }

/**
* Whether the directive should automatically move focus into the trapped region upon
* initialization and return focus to the previous activeElement upon destruction.
*/
@Input('cdkTrapFocusAutoCapture')
get autoCapture(): boolean { return this._autoCapture; }
set autoCapture(value: boolean) { this._autoCapture = coerceBooleanProperty(value); }
set autoCapture(value: BooleanInput) { this._autoCapture = coerceBooleanProperty(value); }
private _autoCapture: boolean;

constructor(
Expand Down Expand Up @@ -455,7 +455,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 @@ -61,7 +61,7 @@ export class CdkAccordionItem implements OnDestroy {
/** Whether the AccordionItem is expanded. */
@Input()
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 @@ -91,7 +91,7 @@ export class CdkAccordionItem implements OnDestroy {
/** Whether the AccordionItem is disabled. */
@Input()
get disabled(): boolean { return this._disabled; }
set disabled(disabled: boolean) { this._disabled = coerceBooleanProperty(disabled); }
set disabled(disabled: BooleanInput) { this._disabled = coerceBooleanProperty(disabled); }
private _disabled = false;

/** Unregister function for _expansionDispatcher. */
Expand Down Expand Up @@ -153,7 +153,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 @@ -41,7 +41,7 @@ export class CdkAccordion implements OnDestroy, OnChanges {
/** Whether the accordion should allow multiple expanded accordion items simultaneously. */
@Input()
get multi(): boolean { return this._multi; }
set multi(multi: boolean) { this._multi = coerceBooleanProperty(multi); }
set multi(multi: BooleanInput) { this._multi = coerceBooleanProperty(multi); }
private _multi: boolean = false;

/** Opens all enabled accordion items in an accordion where multi is enabled. */
Expand All @@ -64,6 +64,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;
}
```
Loading