@@ -301,6 +301,11 @@ export class Datepicker extends Component<DatepickerOptions> {
301
301
this . gotoDate ( new Date ( ) ) ;
302
302
}
303
303
this . isOpen = false ;
304
+
305
+ // HTML5 input date field support
306
+ if ( this . el . type == 'date' ) {
307
+ this . el . classList . add ( 'datepicker-date-input' ) ;
308
+ }
304
309
}
305
310
306
311
static get defaults ( ) {
@@ -417,6 +422,10 @@ export class Datepicker extends Component<DatepickerOptions> {
417
422
if ( typeof format === 'function' ) return format ( this . date ) ;
418
423
if ( ! Datepicker . _isDate ( this . date ) ) return '' ;
419
424
// String Format
425
+ return this . formatDate ( format ) ;
426
+ }
427
+
428
+ formatDate ( format ) {
420
429
const formatArray = format . split ( / ( d { 1 , 4 } | m { 1 , 4 } | y { 4 } | y y | ! .) / g) ;
421
430
const formattedDate = formatArray
422
431
. map ( label => this . formats [ label ] ? this . formats [ label ] ( ) : label )
@@ -458,11 +467,23 @@ export class Datepicker extends Component<DatepickerOptions> {
458
467
}
459
468
}
460
469
470
+ /**
471
+ * Sets the data-date attribute on the date input field
472
+ */
473
+ setDataDate ( ) {
474
+ this . el . setAttribute ( 'data-date' , this . toString ( ) )
475
+ }
476
+
461
477
/**
462
478
* Sets current date as the input value.
463
479
*/
464
480
setInputValue ( ) {
465
- this . el . value = this . toString ( ) ;
481
+ if ( this . el . type == 'date' ) {
482
+ this . setDataDate ( )
483
+ this . el . value = this . formatDate ( 'yyyy-mm-dd' )
484
+ } else {
485
+ this . el . value = this . toString ( ) ;
486
+ }
466
487
this . el . dispatchEvent ( new CustomEvent ( 'change' , { bubbles :true , cancelable :true , composed :true , detail : { firedBy : this } } ) ) ;
467
488
}
468
489
@@ -925,7 +946,11 @@ export class Datepicker extends Component<DatepickerOptions> {
925
946
this . calendarEl . removeEventListener ( 'click' , this . _handleCalendarClick ) ;
926
947
}
927
948
928
- _handleInputClick = ( ) => {
949
+ _handleInputClick = ( e ) => {
950
+ // Prevents default browser datepicker modal rendering
951
+ if ( this . el . type == 'date' ) {
952
+ e . preventDefault ( )
953
+ }
929
954
this . open ( ) ;
930
955
}
931
956
@@ -1008,7 +1033,12 @@ export class Datepicker extends Component<DatepickerOptions> {
1008
1033
else {
1009
1034
date = new Date ( Date . parse ( this . el . value ) ) ;
1010
1035
}
1011
- if ( Datepicker . _isDate ( date ) ) this . setDate ( date ) ;
1036
+ if ( Datepicker . _isDate ( date ) ) {
1037
+ this . setDate ( date ) ;
1038
+ if ( this . el . type == 'date' ) {
1039
+ this . setDataDate ( ) ;
1040
+ }
1041
+ }
1012
1042
}
1013
1043
1014
1044
renderDayName ( opts , day , abbr : boolean = false ) {
0 commit comments