@@ -17,6 +17,7 @@ import { DateMetadataController } from './vaadin-date-metadata-controller.js';
1717import {
1818 dateAllowed ,
1919 dateEquals ,
20+ dateSelectable ,
2021 extractDateParts ,
2122 formatISODate ,
2223 getAdjustedYear ,
@@ -232,11 +233,16 @@ export const DatePickerMixin = (subclass) =>
232233 *
233234 * `disabled` from the metadata is combined with `min`, `max` and `isDateDisabled`: a date is
234235 * disabled if it is out of the min/max range, or `isDateDisabled` returns `true`, or its
235- * metadata marks it disabled. That decides what the calendar renders as disabled and what can
236- * be selected; the field's validity is not yet checked against the provider .
236+ * metadata marks it disabled. That decides what the calendar renders as disabled, what can be
237+ * selected, and whether the field is valid .
237238 *
238- * Keep a stable reference to the function; assigning a new one clears the cache and re-fetches
239- * every visible range.
239+ * A value is checked against the provider even if the overlay is never opened, which loads the
240+ * month holding it. Until that month answers the value is valid, and it is re-validated once
241+ * the answer arrives, so `checkValidity()` can report a value as valid and then invalid.
242+ *
243+ * Keep a stable reference to the function. Assigning a new function clears the cache and
244+ * re-fetches every visible range. To re-fetch while keeping the same function, because the
245+ * data behind it changed, call `clearCache()`.
240246 *
241247 * @type {DatePickerDateMetadataProvider | null | undefined }
242248 */
@@ -308,7 +314,7 @@ export const DatePickerMixin = (subclass) =>
308314 }
309315
310316 static get constraints ( ) {
311- return [ ...super . constraints , 'min' , 'max' ] ;
317+ return [ ...super . constraints , 'min' , 'max' , 'dateMetadataProvider' ] ;
312318 }
313319
314320 constructor ( ) {
@@ -489,9 +495,7 @@ export const DatePickerMixin = (subclass) =>
489495
490496 if ( props . has ( 'dateMetadataProvider' ) ) {
491497 this . _dateMetadataController . setProvider ( this . dateMetadataProvider ) ;
492- if ( this . opened ) {
493- this . _overlayContent ?. loadVisibleDateMetadata ( ) ;
494- }
498+ this . __reloadDateMetadata ( ) ;
495499 }
496500
497501 if ( props . has ( 'showWeekNumbers' ) || props . has ( '__effectiveI18n' ) ) {
@@ -536,6 +540,28 @@ export const DatePickerMixin = (subclass) =>
536540 this . $ . overlay . close ( ) ;
537541 }
538542
543+ /**
544+ * Clears the `dateMetadataProvider` cache and reloads the date metadata.
545+ */
546+ clearCache ( ) {
547+ this . _dateMetadataController . clearCache ( ) ;
548+ this . __reloadDateMetadata ( ) ;
549+ }
550+
551+ /**
552+ * Asks for what the dropped cache was holding: the months the overlay is showing, and the month
553+ * of the value being validated. Requested from here rather than from the controller's
554+ * notification, which would turn a provider that keeps failing into an endless retry, since a
555+ * failed month is dropped and so becomes missing again.
556+ * @private
557+ */
558+ __reloadDateMetadata ( ) {
559+ if ( this . opened ) {
560+ this . _overlayContent ?. loadVisibleDateMetadata ( ) ;
561+ }
562+ this . __ensureSelectedDateLoaded ( ) ;
563+ }
564+
539565 /** @private */
540566 __ensureContent ( ) {
541567 if ( this . _overlayContent ) {
@@ -628,7 +654,14 @@ export const DatePickerMixin = (subclass) =>
628654 const inputValue = this . _inputElementValue ;
629655 const inputValid = ! inputValue || ( ! ! this . _selectedDate && inputValue === this . __formatDate ( this . _selectedDate ) ) ;
630656 const isDateValid =
631- ! this . _selectedDate || dateAllowed ( this . _selectedDate , this . _minDate , this . _maxDate , this . isDateDisabled ) ;
657+ ! this . _selectedDate ||
658+ dateSelectable (
659+ this . _selectedDate ,
660+ this . _minDate ,
661+ this . _maxDate ,
662+ this . isDateDisabled ,
663+ this . _dateMetadataController ,
664+ ) ;
632665
633666 let inputValidity = true ;
634667 if ( this . inputElement && this . inputElement . checkValidity ) {
@@ -638,18 +671,45 @@ export const DatePickerMixin = (subclass) =>
638671 return inputValid && isDateValid && inputValidity ;
639672 }
640673
674+ /**
675+ * Asks the controller for the month holding the selected date, so a value that was set or typed
676+ * without ever opening the overlay is still checked against the provider. Validation is re-run
677+ * from the host callback once the month resolves.
678+ * @private
679+ */
680+ __ensureSelectedDateLoaded ( ) {
681+ const controller = this . _dateMetadataController ;
682+ const awaiting = ! ! ( controller ?. provider && this . _selectedDate && ! controller . isMonthLoaded ( this . _selectedDate ) ) ;
683+ // Always assigned, so clearing the value or removing the provider while a request is in
684+ // flight disarms the pending re-validation, and a later answer for some other month does not
685+ // re-validate a value that never waited for it.
686+ this . __awaitingProviderValidation = awaiting ;
687+ if ( awaiting ) {
688+ controller . ensureRangeLoaded ( this . _selectedDate , this . _selectedDate ) ;
689+ }
690+ }
691+
641692 /**
642693 * Called by the date metadata controller, one microtask after its state changed
643694 * and coalesced, so this never writes reactive state from inside an update. The
644695 * rendered months refresh on their own because they subscribe to the controller.
645696 * @private
646697 */
647698 __onDateMetadataChanged ( ) {
648- if ( ! this . _overlayContent ) {
649- return ;
699+ const controller = this . _dateMetadataController ;
700+
701+ // Only the open overlay has a spinner to update and a today button to re-evaluate.
702+ if ( this . _overlayContent ) {
703+ this . _overlayContent . loading = controller . isLoading ( ) ;
704+ this . _overlayContent . updateTodayButton ( ) ;
705+ }
706+
707+ // Runs whether or not the overlay was ever opened, which is the case this exists for: a value
708+ // set or typed with the overlay closed is reported invalid as soon as its month answers.
709+ if ( this . __awaitingProviderValidation && this . _selectedDate && controller . isMonthLoaded ( this . _selectedDate ) ) {
710+ this . __awaitingProviderValidation = false ;
711+ this . _requestValidation ( ) ;
650712 }
651- this . _overlayContent . loading = this . _dateMetadataController . isLoading ( ) ;
652- this . _overlayContent . updateTodayButton ( ) ;
653713 }
654714
655715 /**
@@ -839,6 +899,8 @@ export const DatePickerMixin = (subclass) =>
839899 this . _ignoreFocusedDateChange = true ;
840900 this . _focusedDate = selectedDate ;
841901 this . _ignoreFocusedDateChange = false ;
902+
903+ this . __ensureSelectedDateLoaded ( ) ;
842904 }
843905
844906 /** @private */
0 commit comments