Skip to content

Commit af4fc9b

Browse files
crisbetojelbourn
authored andcommitted
fix(datepicker): server-side rendering error for disabled input (#10249)
Fixes disabled datepicker inputs throwing an error when rendering on the server. Fixes #10248.
1 parent 889a9f2 commit af4fc9b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/lib/datepicker/datepicker-input.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,19 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
156156
get disabled(): boolean { return !!this._disabled; }
157157
set disabled(value: boolean) {
158158
const newValue = coerceBooleanProperty(value);
159+
const element = this._elementRef.nativeElement;
159160

160161
if (this._disabled !== newValue) {
161162
this._disabled = newValue;
162163
this._disabledChange.emit(newValue);
163164
}
164165

165-
if (newValue) {
166+
// We need to null check the `blur` method, because it's undefined during SSR.
167+
if (newValue && element.blur) {
166168
// Normally, native input elements automatically blur if they turn disabled. This behavior
167169
// is problematic, because it would mean that it triggers another change detection cycle,
168170
// which then causes a changed after checked error if the input element was focused before.
169-
this._elementRef.nativeElement.blur();
171+
element.blur();
170172
}
171173
}
172174
private _disabled: boolean;

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ <h2>Datepicker</h2>
8989
<mat-datepicker #birthday></mat-datepicker>
9090
</mat-form-field>
9191

92+
<h2>Disabled datepicker</h2>
93+
94+
<mat-form-field>
95+
<input type="text" disabled matInput [matDatepicker]="departureDate" placeholder="Departure date">
96+
<mat-datepicker-toggle matSuffix [for]="departureDate"></mat-datepicker-toggle>
97+
<mat-datepicker #departureDate></mat-datepicker>
98+
</mat-form-field>
99+
92100
<h2>Grid list</h2>
93101

94102
<mat-grid-list cols="4">

0 commit comments

Comments
 (0)