Skip to content

Commit 6318389

Browse files
mmalerbawagnermaciel
authored andcommitted
fix(material/datepicker): lifecycle hooks not being called (#20700)
* fix(material/datepicker): lifecycle hooks not being called Lifecycle hooks are not being called in some cases due to a weird interaction between extending TS mixins, running in view engine, and transpiling the code with tsickle. * test(material/datepicker): fix api guard tests (cherry picked from commit b59e4a1)
1 parent 9332230 commit 6318389

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/material/datepicker/date-range-input-parts.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ const _MatDateRangeInputBase:
197197
{provide: NG_VALIDATORS, useExisting: MatStartDate, multi: true}
198198
]
199199
})
200-
export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
200+
export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements
201+
CanUpdateErrorState, DoCheck, OnInit {
201202
/** Validator that checks that the start date isn't after the end date. */
202203
private _startValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
203204
const start = this._dateAdapter.getValidDateOrNull(
@@ -225,6 +226,26 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpd
225226
dateAdapter, dateFormats);
226227
}
227228

229+
ngOnInit() {
230+
// Normally this happens automatically, but it seems to break if not added explicitly when all
231+
// of the criteria below are met:
232+
// 1) The class extends a TS mixin.
233+
// 2) The application is running in ViewEngine.
234+
// 3) The application is being transpiled through tsickle.
235+
// This can be removed once google3 is completely migrated to Ivy.
236+
super.ngOnInit();
237+
}
238+
239+
ngDoCheck() {
240+
// Normally this happens automatically, but it seems to break if not added explicitly when all
241+
// of the criteria below are met:
242+
// 1) The class extends a TS mixin.
243+
// 2) The application is running in ViewEngine.
244+
// 3) The application is being transpiled through tsickle.
245+
// This can be removed once google3 is completely migrated to Ivy.
246+
super.ngDoCheck();
247+
}
248+
228249
protected _validator = Validators.compose([...super._getValidators(), this._startValidator]);
229250

230251
protected _getValueFromModel(modelValue: DateRange<D>) {
@@ -282,7 +303,8 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpd
282303
{provide: NG_VALIDATORS, useExisting: MatEndDate, multi: true}
283304
]
284305
})
285-
export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
306+
export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements
307+
CanUpdateErrorState, DoCheck, OnInit {
286308
/** Validator that checks that the end date isn't before the start date. */
287309
private _endValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
288310
const end = this._dateAdapter.getValidDateOrNull(this._dateAdapter.deserialize(control.value));
@@ -309,6 +331,26 @@ export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements CanUpdat
309331
dateAdapter, dateFormats);
310332
}
311333

334+
ngOnInit() {
335+
// Normally this happens automatically, but it seems to break if not added explicitly when all
336+
// of the criteria below are met:
337+
// 1) The class extends a TS mixin.
338+
// 2) The application is running in ViewEngine.
339+
// 3) The application is being transpiled through tsickle.
340+
// This can be removed once google3 is completely migrated to Ivy.
341+
super.ngOnInit();
342+
}
343+
344+
ngDoCheck() {
345+
// Normally this happens automatically, but it seems to break if not added explicitly when all
346+
// of the criteria below are met:
347+
// 1) The class extends a TS mixin.
348+
// 2) The application is running in ViewEngine.
349+
// 3) The application is being transpiled through tsickle.
350+
// This can be removed once google3 is completely migrated to Ivy.
351+
super.ngDoCheck();
352+
}
353+
312354
protected _validator = Validators.compose([...super._getValidators(), this._endValidator]);
313355

314356
protected _getValueFromModel(modelValue: DateRange<D>) {

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,15 @@ export declare abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSe
367367
static ɵfac: i0.ɵɵFactoryDef<MatDateSelectionModel<any, any>, never>;
368368
}
369369

370-
export declare class MatEndDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
370+
export declare class MatEndDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState, DoCheck, OnInit {
371371
protected _canEmitChangeEvent: (event: DateSelectionModelChange<DateRange<D>>) => boolean;
372372
protected _validator: ValidatorFn | null;
373373
constructor(rangeInput: MatDateRangeInputParent<D>, elementRef: ElementRef<HTMLInputElement>, defaultErrorStateMatcher: ErrorStateMatcher, injector: Injector, parentForm: NgForm, parentFormGroup: FormGroupDirective, dateAdapter: DateAdapter<D>, dateFormats: MatDateFormats);
374374
protected _assignValueToModel(value: D | null): void;
375375
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
376376
_onKeydown(event: KeyboardEvent): void;
377+
ngDoCheck(): void;
378+
ngOnInit(): void;
377379
static ngAcceptInputType_disabled: BooleanInput;
378380
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatEndDate<any>, "input[matEndDate]", never, {}, {}, never>;
379381
static ɵfac: i0.ɵɵFactoryDef<MatEndDate<any>, [null, null, null, null, { optional: true; }, { optional: true; }, { optional: true; }, { optional: true; }]>;
@@ -474,14 +476,16 @@ export declare class MatSingleDateSelectionModel<D> extends MatDateSelectionMode
474476
static ɵprov: i0.ɵɵInjectableDef<MatSingleDateSelectionModel<any>>;
475477
}
476478

477-
export declare class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
479+
export declare class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState, DoCheck, OnInit {
478480
protected _canEmitChangeEvent: (event: DateSelectionModelChange<DateRange<D>>) => boolean;
479481
protected _validator: ValidatorFn | null;
480482
constructor(rangeInput: MatDateRangeInputParent<D>, elementRef: ElementRef<HTMLInputElement>, defaultErrorStateMatcher: ErrorStateMatcher, injector: Injector, parentForm: NgForm, parentFormGroup: FormGroupDirective, dateAdapter: DateAdapter<D>, dateFormats: MatDateFormats);
481483
protected _assignValueToModel(value: D | null): void;
482484
protected _formatValue(value: D | null): void;
483485
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
484486
getMirrorValue(): string;
487+
ngDoCheck(): void;
488+
ngOnInit(): void;
485489
static ngAcceptInputType_disabled: BooleanInput;
486490
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatStartDate<any>, "input[matStartDate]", never, {}, {}, never>;
487491
static ɵfac: i0.ɵɵFactoryDef<MatStartDate<any>, [null, null, null, null, { optional: true; }, { optional: true; }, { optional: true; }, { optional: true; }]>;

0 commit comments

Comments
 (0)