@@ -394,7 +394,7 @@ export const DatePickerMixin = (subclass) =>
394394 if ( this . autoOpenDisabled ) {
395395 const parsedDate = this . _getParsedDate ( ) ;
396396 if ( this . _isValidDate ( parsedDate ) ) {
397- this . _selectedDate = parsedDate ;
397+ this . _selectDate ( parsedDate ) ;
398398 }
399399 }
400400
@@ -462,6 +462,20 @@ export const DatePickerMixin = (subclass) =>
462462 this . _overlayContent . addEventListener ( 'close' , this . _close . bind ( this ) ) ;
463463 this . _overlayContent . addEventListener ( 'focus-input' , this . _focusAndSelect . bind ( this ) ) ;
464464
465+ // User confirmed selected date by clicking the calendar.
466+ this . _overlayContent . addEventListener ( 'date-tap' , ( e ) => {
467+ this . __userConfirmedDate = true ;
468+
469+ this . _selectDate ( e . detail . date ) ;
470+ } ) ;
471+
472+ // User confirmed selected date by pressing Enter or Today.
473+ this . _overlayContent . addEventListener ( 'date-selected' , ( e ) => {
474+ this . __userConfirmedDate = true ;
475+
476+ this . _selectDate ( e . detail . date ) ;
477+ } ) ;
478+
465479 // Keep focus attribute in focusElement for styling
466480 this . _overlayContent . addEventListener ( 'focus' , ( ) => {
467481 this . _setFocused ( true ) ;
@@ -508,6 +522,24 @@ export const DatePickerMixin = (subclass) =>
508522 return inputValid && minMaxValid && inputValidity ;
509523 }
510524
525+ /**
526+ * Select date on user interaction and set the flag
527+ * to fire change event if necessary.
528+ *
529+ * @param {Date } dateToSelect
530+ * @protected
531+ */
532+ _selectDate ( dateToSelect ) {
533+ const value = this . _formatISO ( dateToSelect ) ;
534+
535+ // Only set flag if the value will change.
536+ if ( this . value !== value ) {
537+ this . __dispatchChange = true ;
538+ }
539+
540+ this . _selectedDate = dateToSelect ;
541+ }
542+
511543 /** @private */
512544 _close ( e ) {
513545 if ( e ) {
@@ -609,9 +641,6 @@ export const DatePickerMixin = (subclass) =>
609641 if ( selectedDate === undefined || formatDate === undefined ) {
610642 return ;
611643 }
612- if ( this . __userInputOccurred ) {
613- this . __dispatchChange = true ;
614- }
615644 const value = this . _formatISO ( selectedDate ) ;
616645
617646 this . __keepInputValue || this . _applyInputValue ( selectedDate ) ;
@@ -620,8 +649,6 @@ export const DatePickerMixin = (subclass) =>
620649 this . validate ( ) ;
621650 this . value = value ;
622651 }
623- this . __userInputOccurred = false ;
624- this . __dispatchChange = false ;
625652 this . _ignoreFocusedDateChange = true ;
626653 this . _focusedDate = selectedDate ;
627654 this . _ignoreFocusedDateChange = false ;
@@ -632,7 +659,6 @@ export const DatePickerMixin = (subclass) =>
632659 if ( focusedDate === undefined || formatDate === undefined ) {
633660 return ;
634661 }
635- this . __userInputOccurred = true ;
636662 if ( ! this . _ignoreFocusedDateChange && ! this . _noInput ) {
637663 this . _applyInputValue ( focusedDate ) ;
638664 }
@@ -751,14 +777,15 @@ export const DatePickerMixin = (subclass) =>
751777 const parsedDate = this . _getParsedDate ( inputValue ) ;
752778
753779 if ( this . _isValidDate ( parsedDate ) ) {
754- this . _selectedDate = parsedDate ;
780+ this . _selectDate ( parsedDate ) ;
755781 } else {
756782 this . __keepInputValue = true ;
783+ this . _selectDate ( null ) ;
757784 this . _selectedDate = null ;
758785 this . __keepInputValue = false ;
759786 }
760787 } else if ( this . _focusedDate ) {
761- this . _selectedDate = this . _focusedDate ;
788+ this . _selectDate ( this . _focusedDate ) ;
762789 }
763790 this . _ignoreFocusedDateChange = false ;
764791 }
@@ -774,7 +801,12 @@ export const DatePickerMixin = (subclass) =>
774801 this . _touchPrevented = [ ] ;
775802 }
776803
777- this . _selectParsedOrFocusedDate ( ) ;
804+ // No need to select date on close if it was confirmed by the user.
805+ if ( this . __userConfirmedDate ) {
806+ this . __userConfirmedDate = false ;
807+ } else {
808+ this . _selectParsedOrFocusedDate ( ) ;
809+ }
778810
779811 if ( this . _nativeInput && this . _nativeInput . selectionStart ) {
780812 this . _nativeInput . selectionStart = this . _nativeInput . selectionEnd ;
@@ -903,7 +935,7 @@ export const DatePickerMixin = (subclass) =>
903935 const isValidDate = this . _isValidDate ( parsedDate ) ;
904936 if ( this . opened ) {
905937 if ( this . _overlayInitialized && this . _overlayContent . focusedDate && isValidDate ) {
906- this . _selectedDate = this . _overlayContent . focusedDate ;
938+ this . _selectDate ( this . _overlayContent . focusedDate ) ;
907939 }
908940 this . close ( ) ;
909941 } else if ( ! isValidDate && this . inputElement . value !== '' ) {
@@ -926,7 +958,7 @@ export const DatePickerMixin = (subclass) =>
926958 } else if ( this . autoOpenDisabled ) {
927959 // Do not restore selected date if Esc was pressed after clearing input field
928960 if ( this . inputElement . value === '' ) {
929- this . _selectedDate = null ;
961+ this . _selectDate ( null ) ;
930962 }
931963 this . _applyInputValue ( this . _selectedDate ) ;
932964 } else {
0 commit comments