@@ -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,25 +129,33 @@ 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 . _value ? this . _value . asDate ( ) : null ; }
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
-
134
+ value = this . _dateAdapter . deserialize ( value ) ;
135
+ const oldDate = this . _value . asDate ( ) ;
136
+ let isDifferent = false ;
135
137
if ( ! this . _dateAdapter . sameDate ( oldDate , value ) ) {
136
- this . _valueChange . emit ( value ) ;
138
+ isDifferent = true ;
139
+ }
140
+ this . _value . add ( value ) ;
141
+
142
+ this . _lastValueValid = ! this . _value || this . _value . isValid ( ) ;
143
+
144
+ if ( this . _value ) {
145
+ this . _formatValue ( this . _value . asDate ( ) ) ;
146
+ }
147
+
148
+ if ( isDifferent ) {
149
+ this . _valueChange . emit ( this . value ) ;
137
150
}
138
151
}
139
- private _value : D | null ;
152
+ private _value : MatSingleDateSelectionModel < D > ;
140
153
141
154
/** The minimum valid date. */
142
155
@Input ( )
143
156
get min ( ) : D | null { return this . _min ; }
144
157
set min ( value : D | null ) {
158
+ value = this . _dateAdapter . deserialize ( value ) ;
145
159
this . _min = this . _getValidDateOrNull ( this . _dateAdapter . deserialize ( value ) ) ;
146
160
this . _validatorOnChange ( ) ;
147
161
}
@@ -226,9 +240,9 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
226
240
227
241
/** The form control validator for the date filter. */
228
242
private _filterValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
229
- const controlValue = this . _getValidDateOrNull ( this . _dateAdapter . deserialize ( control . value ) ) ;
230
- return ! this . _dateFilter || ! controlValue || this . _dateFilter ( controlValue ) ?
231
- null : { 'matDatepickerFilter' : true } ;
243
+ const controlValueSelection = new MatSingleDateSelectionModel ( this . _dateAdapter , control . value ) ;
244
+ return ! this . _dateFilter || ! controlValueSelection . isValid ( ) ||
245
+ this . _dateFilter ( controlValueSelection . asDate ( ) ) ? null : { 'matDatepickerFilter' : true } ;
232
246
}
233
247
234
248
/** The combined form control validator for this input. */
@@ -255,6 +269,8 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
255
269
this . _localeSubscription = _dateAdapter . localeChanges . subscribe ( ( ) => {
256
270
this . value = this . value ;
257
271
} ) ;
272
+
273
+ this . _value = new MatSingleDateSelectionModel ( this . _dateAdapter , null ) ;
258
274
}
259
275
260
276
ngOnDestroy ( ) {
@@ -320,14 +336,14 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
320
336
}
321
337
322
338
_onInput ( value : string ) {
323
- let date = this . _dateAdapter . parse ( value , this . _dateFormats . parse . dateInput ) ;
324
- this . _lastValueValid = ! date || this . _dateAdapter . isValid ( date ) ;
325
- date = this . _getValidDateOrNull ( date ) ;
326
-
327
- if ( ! this . _dateAdapter . sameDate ( date , this . _value ) ) {
328
- this . _value = date ;
329
- this . _cvaOnChange ( date ) ;
330
- this . _valueChange . emit ( date ) ;
339
+ const dateSelection = new MatSingleDateSelectionModel ( this . _dateAdapter ,
340
+ this . _dateAdapter . parse ( value , this . _dateFormats . parse . dateInput ) ) ;
341
+ this . _lastValueValid = dateSelection . isValid ( ) ;
342
+
343
+ if ( ! dateSelection . isSame ( this . _value ) ) {
344
+ this . _value = dateSelection ;
345
+ this . _cvaOnChange ( dateSelection . asDate ( ) ) ;
346
+ this . _valueChange . emit ( dateSelection . asDate ( ) ) ;
331
347
this . dateInput . emit ( new MatDatepickerInputEvent ( this , this . _elementRef . nativeElement ) ) ;
332
348
} else {
333
349
this . _validatorOnChange ( ) ;
0 commit comments