Skip to content

Commit dc4e451

Browse files
committed
fix(material/datepicker): resolve typing issue when extending datepicker input in legacy app (#25988)
In the switch to MDC we kept the reference to `MatFormField` in the datepicker input's constructor which now has a slightly different public API, causing a compilation error for apps that were passing in the legacy form field. These changes replace the reference with an interface that contains only the common members that the datepicker cares about. Fixes #25977. (cherry picked from commit a38e08a)
1 parent 261820e commit dc4e451

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
OnChanges,
2323
SimpleChanges,
2424
} from '@angular/core';
25-
import {MatFormFieldControl, MatFormField, MAT_FORM_FIELD} from '@angular/material/form-field';
25+
import {MatFormFieldControl, MAT_FORM_FIELD} from '@angular/material/form-field';
2626
import {ThemePalette, DateAdapter} from '@angular/material/core';
2727
import {NgControl, ControlContainer} from '@angular/forms';
2828
import {Subject, merge, Subscription} from 'rxjs';
@@ -36,7 +36,7 @@ import {
3636
} from './date-range-input-parts';
3737
import {MatDatepickerControl, MatDatepickerPanel} from './datepicker-base';
3838
import {createMissingDateImplError} from './datepicker-errors';
39-
import {DateFilterFn, dateInputsHaveChanged} from './datepicker-input-base';
39+
import {DateFilterFn, dateInputsHaveChanged, _MatFormFieldPartial} from './datepicker-input-base';
4040
import {MatDateRangePickerInput} from './date-range-picker';
4141
import {DateRange, MatDateSelectionModel} from './date-selection-model';
4242

@@ -255,7 +255,7 @@ export class MatDateRangeInput<D>
255255
private _elementRef: ElementRef<HTMLElement>,
256256
@Optional() @Self() control: ControlContainer,
257257
@Optional() private _dateAdapter: DateAdapter<D>,
258-
@Optional() @Inject(MAT_FORM_FIELD) private _formField?: MatFormField,
258+
@Optional() @Inject(MAT_FORM_FIELD) private _formField?: _MatFormFieldPartial,
259259
) {
260260
if (!_dateAdapter && (typeof ngDevMode === 'undefined' || ngDevMode)) {
261261
throw createMissingDateImplError('DateAdapter');

src/material/datepicker/datepicker-input-base.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
Validator,
2929
ValidatorFn,
3030
} from '@angular/forms';
31-
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats} from '@angular/material/core';
31+
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats, ThemePalette} from '@angular/material/core';
3232
import {Subscription, Subject} from 'rxjs';
3333
import {createMissingDateImplError} from './datepicker-errors';
3434
import {
@@ -59,6 +59,20 @@ export class MatDatepickerInputEvent<D, S = unknown> {
5959
/** Function that can be used to filter out dates from a calendar. */
6060
export type DateFilterFn<D> = (date: D | null) => boolean;
6161

62+
/**
63+
* Partial representation of `MatFormField` that is used for backwards-compatibility
64+
* between the legacy and non-legacy variants.
65+
*/
66+
export interface _MatFormFieldPartial {
67+
getConnectedOverlayOrigin(): ElementRef;
68+
getLabelId(): string | null;
69+
color: ThemePalette;
70+
_elementRef: ElementRef;
71+
_shouldLabelFloat(): boolean;
72+
_hasFloatingLabel(): boolean;
73+
_labelId: string;
74+
}
75+
6276
/** Base class for datepicker inputs. */
6377
@Directive()
6478
export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection<S>>

src/material/datepicker/datepicker-input.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import {Directive, ElementRef, forwardRef, Inject, Input, OnDestroy, Optional} from '@angular/core';
1010
import {NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidatorFn, Validators} from '@angular/forms';
1111
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats, ThemePalette} from '@angular/material/core';
12-
import {MatFormField, MAT_FORM_FIELD} from '@angular/material/form-field';
12+
import {MAT_FORM_FIELD} from '@angular/material/form-field';
1313
import {MAT_INPUT_VALUE_ACCESSOR} from '@angular/material/input';
1414
import {Subscription} from 'rxjs';
15-
import {MatDatepickerInputBase, DateFilterFn} from './datepicker-input-base';
15+
import {MatDatepickerInputBase, DateFilterFn, _MatFormFieldPartial} from './datepicker-input-base';
1616
import {MatDatepickerControl, MatDatepickerPanel} from './datepicker-base';
1717
import {DateSelectionModelChange} from './date-selection-model';
1818

@@ -124,7 +124,7 @@ export class MatDatepickerInput<D>
124124
elementRef: ElementRef<HTMLInputElement>,
125125
@Optional() dateAdapter: DateAdapter<D>,
126126
@Optional() @Inject(MAT_DATE_FORMATS) dateFormats: MatDateFormats,
127-
@Optional() @Inject(MAT_FORM_FIELD) private _formField?: MatFormField,
127+
@Optional() @Inject(MAT_FORM_FIELD) private _formField?: _MatFormFieldPartial,
128128
) {
129129
super(elementRef, dateAdapter, dateFormats);
130130
this._validator = Validators.compose(super._getValidators());

tools/public_api_guard/material/datepicker.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import { InjectionToken } from '@angular/core';
4040
import { Injector } from '@angular/core';
4141
import { MatButton } from '@angular/material/button';
4242
import { MatDateFormats } from '@angular/material/core';
43-
import { MatFormField } from '@angular/material/form-field';
4443
import { MatFormFieldControl } from '@angular/material/form-field';
4544
import { NgControl } from '@angular/forms';
4645
import { NgForm } from '@angular/forms';
@@ -427,7 +426,7 @@ export interface MatDatepickerControl<D> {
427426

428427
// @public
429428
export class MatDatepickerInput<D> extends MatDatepickerInputBase<D | null, D> implements MatDatepickerControl<D | null>, OnDestroy {
430-
constructor(elementRef: ElementRef<HTMLInputElement>, dateAdapter: DateAdapter<D>, dateFormats: MatDateFormats, _formField?: MatFormField | undefined);
429+
constructor(elementRef: ElementRef<HTMLInputElement>, dateAdapter: DateAdapter<D>, dateFormats: MatDateFormats, _formField?: _MatFormFieldPartial | undefined);
431430
// (undocumented)
432431
protected _assignValueToModel(value: D | null): void;
433432
get dateFilter(): DateFilterFn<D | null>;
@@ -557,7 +556,7 @@ export class MatDatepickerToggleIcon {
557556

558557
// @public (undocumented)
559558
export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>, MatDatepickerControl<D>, MatDateRangeInputParent<D>, MatDateRangePickerInput<D>, AfterContentInit, OnChanges, OnDestroy {
560-
constructor(_changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef<HTMLElement>, control: ControlContainer, _dateAdapter: DateAdapter<D>, _formField?: MatFormField | undefined);
559+
constructor(_changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef<HTMLElement>, control: ControlContainer, _dateAdapter: DateAdapter<D>, _formField?: _MatFormFieldPartial | undefined);
561560
_ariaDescribedBy: string | null;
562561
comparisonEnd: D | null;
563562
comparisonStart: D | null;

0 commit comments

Comments
 (0)