@@ -87,7 +87,7 @@ export class MatDatepickerInputEvent<D> {
87
87
'[disabled]' : 'disabled' ,
88
88
'(input)' : '_onInput($event.target.value)' ,
89
89
'(change)' : '_onChange()' ,
90
- '(blur)' : '_onTouched ()' ,
90
+ '(blur)' : '_onBlur ()' ,
91
91
'(keydown)' : '_onKeydown($event)' ,
92
92
} ,
93
93
exportAs : 'matDatepickerInput' ,
@@ -123,10 +123,10 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
123
123
value = this . _dateAdapter . deserialize ( value ) ;
124
124
this . _lastValueValid = ! value || this . _dateAdapter . isValid ( value ) ;
125
125
value = this . _getValidDateOrNull ( value ) ;
126
- let oldDate = this . value ;
126
+ const oldDate = this . value ;
127
127
this . _value = value ;
128
- this . _elementRef . nativeElement . value =
129
- value ? this . _dateAdapter . format ( value , this . _dateFormats . display . dateInput ) : '' ;
128
+ this . _formatValue ( value ) ;
129
+
130
130
if ( ! this . _dateAdapter . sameDate ( oldDate , value ) ) {
131
131
this . _valueChange . emit ( value ) ;
132
132
}
@@ -343,6 +343,22 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
343
343
return this . _formField ? this . _formField . color : undefined ;
344
344
}
345
345
346
+ /** Handles blur events on the input. */
347
+ _onBlur ( ) {
348
+ // Reformat the input only if we have a valid value.
349
+ if ( this . value ) {
350
+ this . _formatValue ( this . value ) ;
351
+ }
352
+
353
+ this . _onTouched ( ) ;
354
+ }
355
+
356
+ /** Formats a value and sets it on the input element. */
357
+ private _formatValue ( value : D | null ) {
358
+ this . _elementRef . nativeElement . value =
359
+ value ? this . _dateAdapter . format ( value , this . _dateFormats . display . dateInput ) : '' ;
360
+ }
361
+
346
362
/**
347
363
* @param obj The object to check.
348
364
* @returns The given object if it is both a date instance and valid, otherwise null.
0 commit comments