Skip to content

Commit 6422dfc

Browse files
committed
Use MatDateSelectionModel to model the selected value in MatDatepickerInput
1 parent c40a0fd commit 6422dfc

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/material/core/datetime/date-selection-model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export abstract class MatDateSelectionModel<D> {
1919
this._valueChangesSubject.complete();
2020
}
2121

22-
abstract add(date: D | null): void;
22+
abstract setDate(date: D | null): void;
2323
abstract isComplete(): boolean;
2424
abstract isSame(other: MatDateSelectionModel<D>): boolean;
2525
abstract isValid(): boolean;
@@ -41,7 +41,7 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
4141
this._date = date === undefined ? null : date;
4242
}
4343

44-
add(date: D | null) {
44+
setDate(date: D | null) {
4545
this._date = date;
4646
this._valueChangesSubject.next();
4747
}
@@ -91,7 +91,7 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
9191
* beginning. If the beginning is set, it will set it to the end.
9292
* If add is called on a complete selection, it will empty the selection and set it as the start.
9393
*/
94-
add(date: D | null): void {
94+
setDate(date: D | null): void {
9595
if (this._start == null) {
9696
this._start = date;
9797
} else if (this._end == null) {

src/material/datepicker/datepicker-input.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import {
2929
ValidatorFn,
3030
Validators,
3131
} from '@angular/forms';
32-
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats, ThemePalette} from '@angular/material/core';
32+
import {
33+
DateAdapter,
34+
MAT_DATE_FORMATS,
35+
MatSingleDateSelectionModel,
36+
MatDateFormats,
37+
ThemePalette,
38+
} from '@angular/material/core';
3339
import {MatFormField} from '@angular/material/form-field';
3440
import {MAT_INPUT_VALUE_ACCESSOR} from '@angular/material/input';
3541
import {Subscription} from 'rxjs';
@@ -123,20 +129,22 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
123129

124130
/** The value of the input. */
125131
@Input()
126-
get value(): D | null { return this._value; }
132+
get value(): D | null { return this._selection.asDate(); }
127133
set value(value: D | null) {
128-
value = this._dateAdapter.deserialize(value);
129-
this._lastValueValid = !value || this._dateAdapter.isValid(value);
130-
value = this._getValidDateOrNull(value);
131-
const oldDate = this.value;
132-
this._value = value;
133-
this._formatValue(value);
134-
135-
if (!this._dateAdapter.sameDate(oldDate, value)) {
136-
this._valueChange.emit(value);
134+
value = this._dateAdapter.deserialize(value);
135+
const oldDate = this._selection.asDate();
136+
const isDifferent = !this._dateAdapter.sameDate(oldDate, value);
137+
this._selection.setDate(value);
138+
139+
this._lastValueValid = this._selection.isValid();
140+
141+
this._formatValue(this._selection.asDate());
142+
143+
if (isDifferent) {
144+
this._valueChange.emit(this.value);
137145
}
138146
}
139-
private _value: D | null;
147+
private _selection: MatSingleDateSelectionModel<D>;
140148

141149
/** The minimum valid date. */
142150
@Input()
@@ -251,6 +259,8 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
251259
throw createMissingDateImplError('MAT_DATE_FORMATS');
252260
}
253261

262+
this._selection = new MatSingleDateSelectionModel(this._dateAdapter, null);
263+
254264
// Update the displayed date when the locale changes.
255265
this._localeSubscription = _dateAdapter.localeChanges.subscribe(() => {
256266
this.value = this.value;
@@ -324,13 +334,13 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
324334
this._lastValueValid = !date || this._dateAdapter.isValid(date);
325335
date = this._getValidDateOrNull(date);
326336

327-
if (!this._dateAdapter.sameDate(date, this._value)) {
328-
this._value = date;
337+
if (!this._dateAdapter.sameDate(date, this._selection.asDate())) {
338+
this._selection.setDate(date);
329339
this._cvaOnChange(date);
330340
this._valueChange.emit(date);
331341
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
332-
} else {
333-
this._validatorOnChange();
342+
} else {
343+
this._validatorOnChange();
334344
}
335345
}
336346

0 commit comments

Comments
 (0)