Skip to content

Commit 0415c1f

Browse files
jaguimammalerba
authored andcommitted
Use MatDateSelectionModel to model the selected value in MatDatepickerInput (#17497)
1 parent 1908ae1 commit 0415c1f

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
7070
asDate(): D | null {
7171
return this.isValid() ? this._date : null;
7272
}
73+
74+
setDate(date: D | null) {
75+
this._date = date;
76+
this._valueChangesSubject.next();
77+
}
7378
}
7479

7580
/**

src/material/datepicker/datepicker-input.ts

Lines changed: 24 additions & 14 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,8 +334,8 @@ 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));

tools/public_api_guard/material/core.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ export declare class MatSingleDateSelectionModel<D> extends MatDateSelectionMode
372372
isComplete(): boolean;
373373
isSame(other: MatDateSelectionModel<D>): boolean;
374374
isValid(): boolean;
375+
setDate(date: D | null): void;
375376
}
376377

377378
export declare const JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SEP = 8, OCT = 9, NOV = 10, DEC = 11;

0 commit comments

Comments
 (0)