Skip to content

Commit 51e8c9c

Browse files
committed
refactor: clean up datepicker base class abstract methods (#18278)
I had to do some hacky workarounds in order, because I couldn't figure out how to deal with an abstract class that was being passed into a mixin. These changes clean up the workarounds.
1 parent 80d747f commit 51e8c9c

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ export const MAT_DATE_RANGE_INPUT_PARENT =
6060
* Base class for the individual inputs that can be projected inside a `mat-date-range-input`.
6161
*/
6262
@Directive()
63-
class MatDateRangeInputPartBase<D>
63+
abstract class MatDateRangeInputPartBase<D>
6464
extends MatDatepickerInputBase<DateRange<D>, D> implements OnInit, DoCheck {
6565

6666
/** @docs-private */
6767
ngControl: NgControl;
6868

6969
/** @docs-private */
70-
updateErrorState: () => void;
70+
abstract updateErrorState(): void;
71+
72+
protected abstract _validator: ValidatorFn | null;
73+
protected abstract _assignValueToModel(value: D | null): void;
74+
protected abstract _getValueFromModel(modelValue: DateRange<D>): D | null;
7175

7276
constructor(
7377
@Inject(MAT_DATE_RANGE_INPUT_PARENT) public _rangeInput: MatDateRangeInputParent,
@@ -129,17 +133,12 @@ class MatDateRangeInputPartBase<D>
129133
protected _openPopup(): void {
130134
this._rangeInput._openDatepicker();
131135
}
132-
133-
// Dummy property implementations since we can't pass an abstract class
134-
// into a mixin. These are overridden by the individual input classes.
135-
protected _validator: ValidatorFn | null;
136-
protected _assignValueToModel: (value: D | null) => void;
137-
protected _getValueFromModel: (modelValue: DateRange<D>) => D | null;
138136
}
139137

140138
const _MatDateRangeInputBase:
141139
CanUpdateErrorStateCtor & typeof MatDateRangeInputPartBase =
142-
mixinErrorState(MatDateRangeInputPartBase);
140+
// Needs to be `as any`, because the base class is abstract.
141+
mixinErrorState(MatDateRangeInputPartBase as any);
143142

144143
/** Input for entering the start date in a `mat-date-range-input`. */
145144
@Directive({
@@ -170,8 +169,12 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D>
170169
implements CanUpdateErrorState {
171170
// TODO(crisbeto): start-range-specific validators should go here.
172171
protected _validator = Validators.compose([this._parseValidator]);
173-
protected _getValueFromModel = (modelValue: DateRange<D>) => modelValue.start;
174-
protected _assignValueToModel = (value: D | null) => {
172+
173+
protected _getValueFromModel(modelValue: DateRange<D>) {
174+
return modelValue.start;
175+
}
176+
177+
protected _assignValueToModel(value: D | null) {
175178
if (this._model) {
176179
this._model.updateSelection(new DateRange(value, this._model.selection.end), this);
177180
}
@@ -216,8 +219,12 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D>
216219
export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
217220
// TODO(crisbeto): end-range-specific validators should go here.
218221
protected _validator = Validators.compose([this._parseValidator]);
219-
protected _getValueFromModel = (modelValue: DateRange<D>) => modelValue.end;
220-
protected _assignValueToModel = (value: D | null) => {
222+
223+
protected _getValueFromModel(modelValue: DateRange<D>) {
224+
return modelValue.end;
225+
}
226+
227+
protected _assignValueToModel(value: D | null) {
221228
if (this._model) {
222229
this._model.updateSelection(new DateRange(this._model.selection.start, value), this);
223230
}

src/material/datepicker/datepicker-input.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {BooleanInput} from '@angular/cdk/coercion';
109
import {
1110
Directive,
1211
ElementRef,
@@ -183,5 +182,4 @@ export class MatDatepickerInput<D> extends MatDatepickerInputBase<D | null, D> {
183182
// Accept `any` to avoid conflicts with other directives on `<input>` that
184183
// may accept different types.
185184
static ngAcceptInputType_value: any;
186-
static ngAcceptInputType_disabled: BooleanInput;
187185
}

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ export declare class MatDatepickerInput<D> extends MatDatepickerInputBase<D | nu
184184
protected _openPopup(): void;
185185
getConnectedOverlayOrigin(): ElementRef;
186186
getPopupConnectionElementRef(): ElementRef;
187-
static ngAcceptInputType_disabled: BooleanInput;
188187
static ngAcceptInputType_value: any;
189188
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatDatepickerInput<any>, "input[matDatepicker]", ["matDatepickerInput"], { "matDatepicker": "matDatepicker"; "min": "min"; "max": "max"; "matDatepickerFilter": "matDatepickerFilter"; }, {}, never>;
190189
static ɵfac: i0.ɵɵFactoryDef<MatDatepickerInput<any>>;
@@ -281,9 +280,9 @@ export declare class MatDateRangeInput<D> implements MatFormFieldControl<DateRan
281280
}
282281

283282
export declare class MatEndDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
284-
protected _assignValueToModel: (value: D | null) => void;
285-
protected _getValueFromModel: (modelValue: DateRange<D>) => D | null;
286283
protected _validator: ValidatorFn | null;
284+
protected _assignValueToModel(value: D | null): void;
285+
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
287286
static ngAcceptInputType_disabled: BooleanInput;
288287
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatEndDate<any>, "input[matEndDate]", never, {}, {}, never>;
289288
static ɵfac: i0.ɵɵFactoryDef<MatEndDate<any>>;
@@ -356,9 +355,9 @@ export declare class MatMultiYearView<D> implements AfterContentInit, OnDestroy
356355
}
357356

358357
export declare class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpdateErrorState {
359-
protected _assignValueToModel: (value: D | null) => void;
360-
protected _getValueFromModel: (modelValue: DateRange<D>) => D | null;
361358
protected _validator: ValidatorFn | null;
359+
protected _assignValueToModel(value: D | null): void;
360+
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
362361
getMirrorValue(): string;
363362
static ngAcceptInputType_disabled: BooleanInput;
364363
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatStartDate<any>, "input[matStartDate]", never, {}, {}, never>;

0 commit comments

Comments
 (0)