Skip to content

fix(checkbox): unknown property warning with Ivy during server-side rendering #17485

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

Merged
merged 1 commit into from
Nov 14, 2019
Merged
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
1 change: 0 additions & 1 deletion src/material-experimental/mdc-checkbox/checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
[attr.value]="value"
[checked]="checked"
[disabled]="disabled"
[indeterminate]="indeterminate"
[id]="inputId"
[required]="required"
[tabIndex]="tabIndex"
Expand Down
17 changes: 17 additions & 0 deletions src/material-experimental/mdc-checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
}
set indeterminate(indeterminate) {
this._indeterminate = coerceBooleanProperty(indeterminate);
this._syncIndeterminate(this._indeterminate);
}
private _indeterminate = false;

Expand Down Expand Up @@ -257,6 +258,7 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
}

ngAfterViewInit() {
this._syncIndeterminate(this._indeterminate);
this._checkboxFoundation.init();
}

Expand Down Expand Up @@ -370,6 +372,21 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
this._changeDetectorRef.markForCheck();
}

/**
* Syncs the indeterminate value with the checkbox DOM node.
*
* We sync `indeterminate` directly on the DOM node, because in Ivy the check for whether a
* property is supported on an element boils down to `if (propName in element)`. Domino's
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we file an issue (or send a PR) for Domino and add a TODO here referencing that?

(not sure if there's more to it than adding to the input attrs object)

* HTMLInputElement doesn't have an `indeterminate` property so Ivy will warn during
* server-side rendering.
*/
private _syncIndeterminate(value: boolean) {
const nativeCheckbox = this._nativeCheckbox;
if (nativeCheckbox) {
nativeCheckbox.nativeElement.indeterminate = value;
}
}

static ngAcceptInputType_checked: boolean | string | null | undefined;
static ngAcceptInputType_indeterminate: boolean | string | null | undefined;
static ngAcceptInputType_disabled: boolean | string | null | undefined;
Expand Down
1 change: 0 additions & 1 deletion src/material/checkbox/checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[disabled]="disabled"
[attr.name]="name"
[tabIndex]="tabIndex"
[indeterminate]="indeterminate"
[attr.aria-label]="ariaLabel || null"
[attr.aria-labelledby]="ariaLabelledby"
[attr.aria-checked]="_getAriaChecked()"
Expand Down
28 changes: 26 additions & 2 deletions src/material/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Output,
ViewChild,
ViewEncapsulation,
AfterViewInit,
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {
Expand Down Expand Up @@ -131,7 +132,7 @@ const _MatCheckboxMixinBase:
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAccessor,
AfterViewChecked, OnDestroy, CanColor, CanDisable, HasTabIndex, CanDisableRipple,
AfterViewInit, AfterViewChecked, OnDestroy, CanColor, CanDisable, HasTabIndex, CanDisableRipple,
FocusableOption {

/**
Expand Down Expand Up @@ -235,6 +236,10 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
this._clickAction = this._clickAction || this._options.clickAction;
}

ngAfterViewInit() {
this._syncIndeterminate(this._indeterminate);
}

// TODO: Delete next major revision.
ngAfterViewChecked() {}

Expand Down Expand Up @@ -281,7 +286,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
get indeterminate(): boolean { return this._indeterminate; }
set indeterminate(value: boolean) {
const changed = value != this._indeterminate;
this._indeterminate = value;
this._indeterminate = coerceBooleanProperty(value);

if (changed) {
if (this._indeterminate) {
Expand All @@ -292,6 +297,8 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
}
this.indeterminateChange.emit(this._indeterminate);
}

this._syncIndeterminate(this._indeterminate);
}
private _indeterminate: boolean = false;

Expand Down Expand Up @@ -470,7 +477,24 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
return `mat-checkbox-anim-${animSuffix}`;
}

/**
* Syncs the indeterminate value with the checkbox DOM node.
*
* We sync `indeterminate` directly on the DOM node, because in Ivy the check for whether a
* property is supported on an element boils down to `if (propName in element)`. Domino's
* HTMLInputElement doesn't have an `indeterminate` property so Ivy will warn during
* server-side rendering.
*/
private _syncIndeterminate(value: boolean) {
const nativeCheckbox = this._inputElement;

if (nativeCheckbox) {
nativeCheckbox.nativeElement.indeterminate = value;
}
}

static ngAcceptInputType_disabled: boolean | string | null | undefined;
static ngAcceptInputType_required: boolean | string | null | undefined;
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
static ngAcceptInputType_indeterminate: boolean | string | null | undefined;
}
4 changes: 3 additions & 1 deletion tools/public_api_guard/material/checkbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export declare function MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY(): MatCheckboxDefau

export declare const MAT_CHECKBOX_REQUIRED_VALIDATOR: Provider;

export declare class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAccessor, AfterViewChecked, OnDestroy, CanColor, CanDisable, HasTabIndex, CanDisableRipple, FocusableOption {
export declare class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAccessor, AfterViewInit, AfterViewChecked, OnDestroy, CanColor, CanDisable, HasTabIndex, CanDisableRipple, FocusableOption {
_animationMode?: string | undefined;
_inputElement: ElementRef<HTMLInputElement>;
_onTouched: () => any;
Expand All @@ -40,6 +40,7 @@ export declare class MatCheckbox extends _MatCheckboxMixinBase implements Contro
_onLabelTextChange(): void;
focus(origin?: FocusOrigin, options?: FocusOptions): void;
ngAfterViewChecked(): void;
ngAfterViewInit(): void;
ngOnDestroy(): void;
registerOnChange(fn: (value: any) => void): void;
registerOnTouched(fn: any): void;
Expand All @@ -48,6 +49,7 @@ export declare class MatCheckbox extends _MatCheckboxMixinBase implements Contro
writeValue(value: any): void;
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
static ngAcceptInputType_disabled: boolean | string | null | undefined;
static ngAcceptInputType_indeterminate: boolean | string | null | undefined;
static ngAcceptInputType_required: boolean | string | null | undefined;
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatCheckbox, "mat-checkbox", ["matCheckbox"], { 'disableRipple': "disableRipple", 'color': "color", 'tabIndex': "tabIndex", 'ariaLabel': "aria-label", 'ariaLabelledby': "aria-labelledby", 'id': "id", 'required': "required", 'labelPosition': "labelPosition", 'name': "name", 'value': "value", 'checked': "checked", 'disabled': "disabled", 'indeterminate': "indeterminate" }, { 'change': "change", 'indeterminateChange': "indeterminateChange" }, never>;
static ɵfac: i0.ɵɵFactoryDef<MatCheckbox>;
Expand Down