Skip to content

Commit

Permalink
fix(datepicker): server-side rendering error for disabled input (#10249)
Browse files Browse the repository at this point in the history
Fixes disabled datepicker inputs throwing an error when rendering on the server.

Fixes #10248.
  • Loading branch information
crisbeto authored and jelbourn committed Mar 9, 2018
1 parent 889a9f2 commit af4fc9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/lib/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,19 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
get disabled(): boolean { return !!this._disabled; }
set disabled(value: boolean) {
const newValue = coerceBooleanProperty(value);
const element = this._elementRef.nativeElement;

if (this._disabled !== newValue) {
this._disabled = newValue;
this._disabledChange.emit(newValue);
}

if (newValue) {
// We need to null check the `blur` method, because it's undefined during SSR.
if (newValue && element.blur) {
// Normally, native input elements automatically blur if they turn disabled. This behavior
// is problematic, because it would mean that it triggers another change detection cycle,
// which then causes a changed after checked error if the input element was focused before.
this._elementRef.nativeElement.blur();
element.blur();
}
}
private _disabled: boolean;
Expand Down
8 changes: 8 additions & 0 deletions src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ <h2>Datepicker</h2>
<mat-datepicker #birthday></mat-datepicker>
</mat-form-field>

<h2>Disabled datepicker</h2>

<mat-form-field>
<input type="text" disabled matInput [matDatepicker]="departureDate" placeholder="Departure date">
<mat-datepicker-toggle matSuffix [for]="departureDate"></mat-datepicker-toggle>
<mat-datepicker #departureDate></mat-datepicker>
</mat-form-field>

<h2>Grid list</h2>

<mat-grid-list cols="4">
Expand Down

0 comments on commit af4fc9b

Please sign in to comment.