Skip to content

Commit a7ebc7a

Browse files
authored
fix(datepicker): error in IE/Edge for static disabled binding (#18202)
IE/Edge can throw an error for a datepicker input which has a static `disabled` binding. The error is thrown, because static bindings in Ivy are invoked before the element is in the DOM which browsers seem to have an assertion against.
1 parent 619f7f8 commit a7ebc7a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/material/datepicker/datepicker-input.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
OnDestroy,
1919
Optional,
2020
Output,
21+
AfterViewInit,
2122
} from '@angular/core';
2223
import {
2324
AbstractControl,
@@ -91,7 +92,12 @@ export class MatDatepickerInputEvent<D> {
9192
},
9293
exportAs: 'matDatepickerInput',
9394
})
94-
export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, Validator {
95+
export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, AfterViewInit,
96+
Validator {
97+
98+
/** Whether the component has been initialized. */
99+
private _isInitialized: boolean;
100+
95101
/** The datepicker that this input is associated with. */
96102
@Input()
97103
set matDatepicker(value: MatDatepicker<D>) {
@@ -169,7 +175,10 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
169175
}
170176

171177
// We need to null check the `blur` method, because it's undefined during SSR.
172-
if (newValue && element.blur) {
178+
// In Ivy static bindings are invoked earlier, before the element is attached to the DOM.
179+
// This can cause an error to be thrown in some browsers (IE/Edge) which assert that the
180+
// element has been inserted.
181+
if (newValue && this._isInitialized && element.blur) {
173182
// Normally, native input elements automatically blur if they turn disabled. This behavior
174183
// is problematic, because it would mean that it triggers another change detection cycle,
175184
// which then causes a changed after checked error if the input element was focused before.
@@ -257,6 +266,10 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
257266
});
258267
}
259268

269+
ngAfterViewInit() {
270+
this._isInitialized = true;
271+
}
272+
260273
ngOnDestroy() {
261274
this._datepickerSubscription.unsubscribe();
262275
this._localeSubscription.unsubscribe();

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export declare class MatDatepickerContent<D> extends _MatDatepickerContentMixinB
170170
static ɵfac: i0.ɵɵFactoryDef<MatDatepickerContent<any>, never>;
171171
}
172172

173-
export declare class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, Validator {
173+
export declare class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, AfterViewInit, Validator {
174174
_dateAdapter: DateAdapter<D>;
175175
_dateFilter: (date: D | null) => boolean;
176176
_datepicker: MatDatepicker<D>;
@@ -197,6 +197,7 @@ export declare class MatDatepickerInput<D> implements ControlValueAccessor, OnDe
197197
_onKeydown(event: KeyboardEvent): void;
198198
getConnectedOverlayOrigin(): ElementRef;
199199
getPopupConnectionElementRef(): ElementRef;
200+
ngAfterViewInit(): void;
200201
ngOnDestroy(): void;
201202
registerOnChange(fn: (value: any) => void): void;
202203
registerOnTouched(fn: () => void): void;

0 commit comments

Comments
 (0)