@@ -29,7 +29,13 @@ import {
29
29
ValidatorFn ,
30
30
Validators ,
31
31
} 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' ;
33
39
import { MatFormField } from '@angular/material/form-field' ;
34
40
import { MAT_INPUT_VALUE_ACCESSOR } from '@angular/material/input' ;
35
41
import { Subscription } from 'rxjs' ;
@@ -123,20 +129,22 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
123
129
124
130
/** The value of the input. */
125
131
@Input ( )
126
- get value ( ) : D | null { return this . _value ; }
132
+ get value ( ) : D | null { return this . _selection . asDate ( ) ; }
127
133
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 ) ;
137
145
}
138
146
}
139
- private _value : D | null ;
147
+ private _selection : MatSingleDateSelectionModel < D > ;
140
148
141
149
/** The minimum valid date. */
142
150
@Input ( )
@@ -251,6 +259,8 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
251
259
throw createMissingDateImplError ( 'MAT_DATE_FORMATS' ) ;
252
260
}
253
261
262
+ this . _selection = new MatSingleDateSelectionModel ( this . _dateAdapter , null ) ;
263
+
254
264
// Update the displayed date when the locale changes.
255
265
this . _localeSubscription = _dateAdapter . localeChanges . subscribe ( ( ) => {
256
266
this . value = this . value ;
@@ -324,13 +334,13 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
324
334
this . _lastValueValid = ! date || this . _dateAdapter . isValid ( date ) ;
325
335
date = this . _getValidDateOrNull ( date ) ;
326
336
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 ) ;
329
339
this . _cvaOnChange ( date ) ;
330
340
this . _valueChange . emit ( date ) ;
331
341
this . dateInput . emit ( new MatDatepickerInputEvent ( this , this . _elementRef . nativeElement ) ) ;
332
342
} else {
333
- this . _validatorOnChange ( ) ;
343
+ this . _validatorOnChange ( ) ;
334
344
}
335
345
}
336
346
0 commit comments