@@ -34,6 +34,7 @@ import {
34
34
import { DateAdapter , MD_DATE_FORMATS , MdDateFormats } from '@angular/material/core' ;
35
35
import { MdFormField } from '@angular/material/form-field' ;
36
36
import { Subscription } from 'rxjs/Subscription' ;
37
+ import { coerceDateProperty } from './coerce-date-property' ;
37
38
import { MdDatepicker } from './datepicker' ;
38
39
import { createMissingDateImplError } from './datepicker-errors' ;
39
40
@@ -74,8 +75,8 @@ export class MdDatepickerInputEvent<D> {
74
75
host : {
75
76
'[attr.aria-haspopup]' : 'true' ,
76
77
'[attr.aria-owns]' : '(_datepicker?.opened && _datepicker.id) || null' ,
77
- '[attr.min]' : 'min ? _dateAdapter.toISODateString (min) : null' ,
78
- '[attr.max]' : 'max ? _dateAdapter.toISODateString (max) : null' ,
78
+ '[attr.min]' : 'min ? _dateAdapter.toIsoDateString (min) : null' ,
79
+ '[attr.max]' : 'max ? _dateAdapter.toIsoDateString (max) : null' ,
79
80
'[disabled]' : 'disabled' ,
80
81
'(input)' : '_onInput($event.target.value)' ,
81
82
'(change)' : '_onChange()' ,
@@ -122,10 +123,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
122
123
return this . _value ;
123
124
}
124
125
set value ( value : D | null ) {
125
- value = this . _coerceDateProperty ( value ) ;
126
- if ( value != null && ! this . _dateAdapter . isDateInstance ( value ) ) {
127
- throw Error ( 'Datepicker: value not recognized as a date object by DateAdapter.' ) ;
128
- }
126
+ value = coerceDateProperty ( this . _dateAdapter , value ) ;
129
127
this . _lastValueValid = ! value || this . _dateAdapter . isValid ( value ) ;
130
128
value = this . _getValidDateOrNull ( value ) ;
131
129
@@ -143,7 +141,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
143
141
@Input ( )
144
142
get min ( ) : D | null { return this . _min ; }
145
143
set min ( value : D | null ) {
146
- this . _min = this . _coerceDateProperty ( value ) ;
144
+ this . _min = coerceDateProperty ( this . _dateAdapter , value ) ;
147
145
this . _validatorOnChange ( ) ;
148
146
}
149
147
private _min : D | null ;
@@ -152,7 +150,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
152
150
@Input ( )
153
151
get max ( ) : D | null { return this . _max ; }
154
152
set max ( value : D | null ) {
155
- this . _max = this . _coerceDateProperty ( value ) ;
153
+ this . _max = coerceDateProperty ( this . _dateAdapter , value ) ;
156
154
this . _validatorOnChange ( ) ;
157
155
}
158
156
private _max : D | null ;
@@ -200,23 +198,23 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
200
198
201
199
/** The form control validator for the min date. */
202
200
private _minValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
203
- const controlValue = this . _coerceDateProperty ( control . value ) ;
201
+ const controlValue = coerceDateProperty ( this . _dateAdapter , control . value ) ;
204
202
return ( ! this . min || ! controlValue ||
205
203
this . _dateAdapter . compareDate ( this . min , controlValue ) <= 0 ) ?
206
- null : { 'mdDatepickerMin' : { 'min' : this . min , 'actual' : control . value } } ;
204
+ null : { 'mdDatepickerMin' : { 'min' : this . min , 'actual' : controlValue } } ;
207
205
}
208
206
209
207
/** The form control validator for the max date. */
210
208
private _maxValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
211
- const controlValue = this . _coerceDateProperty ( control . value ) ;
209
+ const controlValue = coerceDateProperty ( this . _dateAdapter , control . value ) ;
212
210
return ( ! this . max || ! controlValue ||
213
211
this . _dateAdapter . compareDate ( this . max , controlValue ) >= 0 ) ?
214
- null : { 'mdDatepickerMax' : { 'max' : this . max , 'actual' : control . value } } ;
212
+ null : { 'mdDatepickerMax' : { 'max' : this . max , 'actual' : controlValue } } ;
215
213
}
216
214
217
215
/** The form control validator for the date filter. */
218
216
private _filterValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
219
- const controlValue = this . _coerceDateProperty ( control . value ) ;
217
+ const controlValue = coerceDateProperty ( this . _dateAdapter , control . value ) ;
220
218
return ! this . _dateFilter || ! controlValue || this . _dateFilter ( controlValue ) ?
221
219
null : { 'mdDatepickerFilter' : true } ;
222
220
}
@@ -332,16 +330,4 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
332
330
private _getValidDateOrNull ( obj : any ) : D | null {
333
331
return ( this . _dateAdapter . isDateInstance ( obj ) && this . _dateAdapter . isValid ( obj ) ) ? obj : null ;
334
332
}
335
-
336
- /**
337
- * Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
338
- * ISO 8601 string, returns the original vlaue.
339
- */
340
- private _coerceDateProperty ( value : any ) : any {
341
- if ( typeof value === 'string' ) {
342
- const d = this . _dateAdapter . fromISODateString ( value ) ;
343
- return d || value ;
344
- }
345
- return value ;
346
- }
347
333
}
0 commit comments