@vaadin/date-picker 25.3.0-alpha1 → 25.3.0-alpha11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,49 @@ export interface DatePickerDate {
18
18
  year: number;
19
19
  }
20
20
 
21
+ /**
22
+ * A range of dates that `dateMetadataProvider` is asked about.
23
+ * It can span several months and always covers whole months.
24
+ */
25
+ export interface DatePickerDateRange {
26
+ /**
27
+ * The first date of the range (inclusive), as an ISO 8601 date.
28
+ */
29
+ start: string;
30
+ /**
31
+ * The last date of the range (inclusive), as an ISO 8601 date.
32
+ */
33
+ end: string;
34
+ }
35
+
36
+ /**
37
+ * Metadata for a single date, returned by `dateMetadataProvider`.
38
+ */
39
+ export interface DatePickerDateMetadata {
40
+ /**
41
+ * The date the metadata applies to in ISO 8601 format.
42
+ */
43
+ date: string;
44
+ /**
45
+ * Whether the date cannot be selected.
46
+ */
47
+ disabled?: boolean;
48
+ /**
49
+ * Part names to add to the date, so a theme can style it with `::part()`. A single name, or
50
+ * several separated by spaces. Do not use built-in names like `disabled` and `selected`.
51
+ */
52
+ part?: string;
53
+ }
54
+
55
+ /**
56
+ * A function called with the range of dates the calendar is about to render, returning
57
+ * the metadata for the dates in that range. It can return a `Promise` to load the metadata
58
+ * asynchronously, and `null` or `undefined` when no date in the range has metadata.
59
+ */
60
+ export type DatePickerDateMetadataProvider = (
61
+ range: DatePickerDateRange,
62
+ ) => DatePickerDateMetadata[] | Promise<DatePickerDateMetadata[] | null | undefined> | null | undefined;
63
+
21
64
  export interface DatePickerI18n {
22
65
  /**
23
66
  * An array with the full names of months starting
@@ -47,6 +90,11 @@ export interface DatePickerI18n {
47
90
  * Translation of the Cancel button text.
48
91
  */
49
92
  cancel?: string;
93
+ /**
94
+ * Accessible name of the overlay content, announced by screen readers when
95
+ * the overlay opens.
96
+ */
97
+ dialogAccessibleName?: string;
50
98
  /**
51
99
  * Used for adjusting the year value when parsing dates with short years.
52
100
  * The year values between 0 and 99 are evaluated and adjusted.
@@ -174,6 +222,10 @@ export declare class DatePickerMixinClass {
174
222
  * // Translation of the Cancel button text.
175
223
  * cancel: 'Cancel',
176
224
  *
225
+ * // Accessible name of the overlay content, announced by screen readers
226
+ * // when the overlay opens.
227
+ * dialogAccessibleName: 'Calendar',
228
+ *
177
229
  * // Used for adjusting the year value when parsing dates with short years.
178
230
  * // The year values between 0 and 99 are evaluated and adjusted.
179
231
  * // Example: for a referenceDate of 1970-10-30;
@@ -233,9 +285,52 @@ export declare class DatePickerMixinClass {
233
285
  * A function to be used to determine whether the user can select a given date.
234
286
  * Receives a `DatePickerDate` object of the date to be selected and should return a
235
287
  * boolean.
288
+ *
289
+ * The function is called once per date and has to answer synchronously. Use
290
+ * `dateMetadataProvider` when the answer has to be loaded first, or when dates also need
291
+ * custom part names. A date is disabled when either of the two disables it.
236
292
  */
237
293
  isDateDisabled: (date: DatePickerDate) => boolean;
238
294
 
295
+ /**
296
+ * A function that provides metadata for the dates the calendar is about to render: whether they
297
+ * are disabled, and CSS `part` names for styling from outside using the `::part()` selector.
298
+ * Unlike `isDateDisabled`, which is called once per date, the metadata provider is called for
299
+ * a range of dates at a time, and again as the calendar renders further dates.
300
+ *
301
+ * It receives a `DatePickerDateRange` and returns an array of `DatePickerDateMetadata` objects
302
+ * for the dates in that range that have metadata. It can return a `Promise` to load the metadata
303
+ * asynchronously, and `null` or `undefined` when no date in the range has metadata.
304
+ *
305
+ * The returned array has the following structure:
306
+ *
307
+ * ```js
308
+ * [
309
+ * // The date is an ISO 8601 string.
310
+ * { date: '2026-01-01', disabled: true },
311
+ *
312
+ * // Adds a custom part name to the date.
313
+ * { date: '2026-01-02', part: 'busy' },
314
+ * ]
315
+ * ```
316
+ *
317
+ * A date is disabled if its metadata marks it disabled, or `isDateDisabled` returns `true`, or
318
+ * it is outside `min` and `max`. Disabled dates are not selectable, and typing a disabled date in
319
+ * the field makes it invalid. The provider does not affect which date is focused when opening the
320
+ * overlay. Use `initialPosition` property to provide a selectable date.
321
+ *
322
+ * While a returned `Promise` is pending, the dates it covers are not disabled yet and render with
323
+ * the `loading` part. If the function throws or rejects, corresponding dates are requested again
324
+ * the next time the user navigates.
325
+ *
326
+ * The provider is used for validation also when the overlay is closed. Date is considered valid
327
+ * while the provider is pending, and is re-validated again after the metadata is loaded.
328
+ *
329
+ * Keep a stable reference to the function: assigning a new one clears the cache and re-fetches
330
+ * visible range. Call `clearCache()` to re-fetch when the data behind the same function changed.
331
+ */
332
+ dateMetadataProvider: DatePickerDateMetadataProvider | null | undefined;
333
+
239
334
  /**
240
335
  * Opens the dropdown.
241
336
  */
@@ -245,4 +340,9 @@ export declare class DatePickerMixinClass {
245
340
  * Closes the dropdown.
246
341
  */
247
342
  close(): void;
343
+
344
+ /**
345
+ * Clears the `dateMetadataProvider` cache and reloads the date metadata.
346
+ */
347
+ clearCache(): void;
248
348
  }
@@ -8,13 +8,16 @@ import { DelegateFocusMixin } from '@vaadin/a11y-base/src/delegate-focus-mixin.j
8
8
  import { isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
9
9
  import { KeyboardMixin } from '@vaadin/a11y-base/src/keyboard-mixin.js';
10
10
  import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
11
+ import { setOrRemoveAttribute } from '@vaadin/component-base/src/dom-utils.js';
11
12
  import { I18nMixin } from '@vaadin/component-base/src/i18n-mixin.js';
12
13
  import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
13
14
  import { InputConstraintsMixin } from '@vaadin/field-base/src/input-constraints-mixin.js';
14
15
  import { VirtualKeyboardController } from '@vaadin/field-base/src/virtual-keyboard-controller.js';
16
+ import { DateMetadataController } from './vaadin-date-metadata-controller.js';
15
17
  import {
16
18
  dateAllowed,
17
19
  dateEquals,
20
+ dateSelectable,
18
21
  extractDateParts,
19
22
  formatISODate,
20
23
  getAdjustedYear,
@@ -42,6 +45,7 @@ export const datePickerI18nDefaults = Object.freeze({
42
45
  firstDayOfWeek: 0,
43
46
  today: 'Today',
44
47
  cancel: 'Cancel',
48
+ dialogAccessibleName: 'Calendar',
45
49
  referenceDate: '',
46
50
  formatDate(d) {
47
51
  const yearStr = String(d.year).replace(/\d+/u, (y) => '0000'.substr(y.length) + y);
@@ -59,7 +63,7 @@ export const datePickerI18nDefaults = Object.freeze({
59
63
  date = parseInt(parts[1]);
60
64
  year = parseInt(parts[2]);
61
65
  if (parts[2].length < 3 && year >= 0) {
62
- const usedReferenceDate = this.referenceDate ? parseDate(this.referenceDate) : new Date();
66
+ const usedReferenceDate = parseDate(this.referenceDate) || new Date();
63
67
  year = getAdjustedYear(usedReferenceDate, year, month, date);
64
68
  }
65
69
  } else if (parts.length === 2) {
@@ -202,12 +206,59 @@ export const DatePickerMixin = (subclass) =>
202
206
  * Receives a `DatePickerDate` object of the date to be selected and should return a
203
207
  * boolean.
204
208
  *
209
+ * The function is called once per date and has to answer synchronously. Use
210
+ * `dateMetadataProvider` when the answer has to be loaded first, or when dates also need
211
+ * custom part names. A date is disabled when either of the two disables it.
212
+ *
205
213
  * @type {function(DatePickerDate): boolean | undefined}
206
214
  */
207
215
  isDateDisabled: {
208
216
  type: Function,
209
217
  },
210
218
 
219
+ /**
220
+ * A function that provides metadata for the dates the calendar is about to render: whether they
221
+ * are disabled, and CSS `part` names for styling from outside using the `::part()` selector.
222
+ * Unlike `isDateDisabled`, which is called once per date, the metadata provider is called for
223
+ * a range of dates at a time, and again as the calendar renders further dates.
224
+ *
225
+ * It receives a `DatePickerDateRange` and returns an array of `DatePickerDateMetadata` objects
226
+ * for the dates in that range that have metadata. It can return a `Promise` to load the metadata
227
+ * asynchronously, and `null` or `undefined` when no date in the range has metadata.
228
+ *
229
+ * The returned array has the following structure:
230
+ *
231
+ * ```js
232
+ * [
233
+ * // The date is an ISO 8601 string.
234
+ * { date: '2026-01-01', disabled: true },
235
+ *
236
+ * // Adds a custom part name to the date.
237
+ * { date: '2026-01-02', part: 'busy' },
238
+ * ]
239
+ * ```
240
+ *
241
+ * A date is disabled if its metadata marks it disabled, or `isDateDisabled` returns `true`, or
242
+ * it is outside `min` and `max`. Disabled dates are not selectable, and typing a disabled date in
243
+ * the field makes it invalid. The provider does not affect which date is focused when opening the
244
+ * overlay. Use `initialPosition` property to provide a selectable date.
245
+ *
246
+ * While a returned `Promise` is pending, the dates it covers are not disabled yet and render with
247
+ * the `loading` part. If the function throws or rejects, corresponding dates are requested again
248
+ * the next time the user navigates.
249
+ *
250
+ * The provider is used for validation also when the overlay is closed. Date is considered valid
251
+ * while the provider is pending, and is re-validated again after the metadata is loaded.
252
+ *
253
+ * Keep a stable reference to the function: assigning a new one clears the cache and re-fetches
254
+ * visible range. Call `clearCache()` to re-fetch when the data behind the same function changed.
255
+ *
256
+ * @type {DatePickerDateMetadataProvider | null | undefined}
257
+ */
258
+ dateMetadataProvider: {
259
+ type: Function,
260
+ },
261
+
211
262
  /**
212
263
  * The earliest date that can be selected. All earlier dates will be disabled.
213
264
  * @type {Date | undefined}
@@ -272,7 +323,7 @@ export const DatePickerMixin = (subclass) =>
272
323
  }
273
324
 
274
325
  static get constraints() {
275
- return [...super.constraints, 'min', 'max'];
326
+ return [...super.constraints, 'min', 'max', 'dateMetadataProvider'];
276
327
  }
277
328
 
278
329
  constructor() {
@@ -280,6 +331,9 @@ export const DatePickerMixin = (subclass) =>
280
331
 
281
332
  this._boundOnClick = this._onClick.bind(this);
282
333
  this._boundOnScroll = this._onScroll.bind(this);
334
+
335
+ this._dateMetadataController = new DateMetadataController(this, () => this.__onDateMetadataChanged());
336
+ this.addController(this._dateMetadataController);
283
337
  }
284
338
 
285
339
  /**
@@ -322,6 +376,10 @@ export const DatePickerMixin = (subclass) =>
322
376
  * // Translation of the Cancel button text.
323
377
  * cancel: 'Cancel',
324
378
  *
379
+ * // Accessible name of the overlay content, announced by screen readers
380
+ * // when the overlay opens.
381
+ * dialogAccessibleName: 'Calendar',
382
+ *
325
383
  * // Used for adjusting the year value when parsing dates with short years.
326
384
  * // The year values between 0 and 99 are evaluated and adjusted.
327
385
  * // Example: for a referenceDate of 1970-10-30;
@@ -444,6 +502,11 @@ export const DatePickerMixin = (subclass) =>
444
502
  updated(props) {
445
503
  super.updated(props);
446
504
 
505
+ if (props.has('dateMetadataProvider')) {
506
+ this._dateMetadataController.setProvider(this.dateMetadataProvider);
507
+ this.__reloadDateMetadata();
508
+ }
509
+
447
510
  if (props.has('showWeekNumbers') || props.has('__effectiveI18n')) {
448
511
  // Currently only supported for locales that start the week on Monday.
449
512
  this.toggleAttribute('week-numbers', this.showWeekNumbers && this.__effectiveI18n.firstDayOfWeek === 1);
@@ -486,6 +549,28 @@ export const DatePickerMixin = (subclass) =>
486
549
  this.$.overlay.close();
487
550
  }
488
551
 
552
+ /**
553
+ * Clears the `dateMetadataProvider` cache and reloads the date metadata.
554
+ */
555
+ clearCache() {
556
+ this._dateMetadataController.clearCache();
557
+ this.__reloadDateMetadata();
558
+ }
559
+
560
+ /**
561
+ * Asks for what the dropped cache was holding: the months the overlay is showing, and the month
562
+ * of the value being validated. Requested from here rather than from the controller's
563
+ * notification, which would turn a provider that keeps failing into an endless retry, since a
564
+ * failed month is dropped and so becomes missing again.
565
+ * @private
566
+ */
567
+ __reloadDateMetadata() {
568
+ if (this.opened) {
569
+ this._overlayContent?.loadVisibleDateMetadata();
570
+ }
571
+ this.__ensureSelectedDateLoaded();
572
+ }
573
+
489
574
  /** @private */
490
575
  __ensureContent() {
491
576
  if (this._overlayContent) {
@@ -578,7 +663,14 @@ export const DatePickerMixin = (subclass) =>
578
663
  const inputValue = this._inputElementValue;
579
664
  const inputValid = !inputValue || (!!this._selectedDate && inputValue === this.__formatDate(this._selectedDate));
580
665
  const isDateValid =
581
- !this._selectedDate || dateAllowed(this._selectedDate, this._minDate, this._maxDate, this.isDateDisabled);
666
+ !this._selectedDate ||
667
+ dateSelectable(
668
+ this._selectedDate,
669
+ this._minDate,
670
+ this._maxDate,
671
+ this.isDateDisabled,
672
+ this._dateMetadataController,
673
+ );
582
674
 
583
675
  let inputValidity = true;
584
676
  if (this.inputElement && this.inputElement.checkValidity) {
@@ -588,6 +680,47 @@ export const DatePickerMixin = (subclass) =>
588
680
  return inputValid && isDateValid && inputValidity;
589
681
  }
590
682
 
683
+ /**
684
+ * Asks the controller for the month holding the selected date, so a value that was set or typed
685
+ * without ever opening the overlay is still checked against the provider. Validation is re-run
686
+ * from the host callback once the month resolves.
687
+ * @private
688
+ */
689
+ __ensureSelectedDateLoaded() {
690
+ const controller = this._dateMetadataController;
691
+ const awaiting = !!(controller?.provider && this._selectedDate && !controller.isMonthLoaded(this._selectedDate));
692
+ // Always assigned, so clearing the value or removing the provider while a request is in
693
+ // flight disarms the pending re-validation, and a later answer for some other month does not
694
+ // re-validate a value that never waited for it.
695
+ this.__awaitingProviderValidation = awaiting;
696
+ if (awaiting) {
697
+ controller.ensureRangeLoaded(this._selectedDate, this._selectedDate);
698
+ }
699
+ }
700
+
701
+ /**
702
+ * Called by the date metadata controller, one microtask after its state changed
703
+ * and coalesced, so this never writes reactive state from inside an update. The
704
+ * rendered months refresh on their own because they subscribe to the controller.
705
+ * @private
706
+ */
707
+ __onDateMetadataChanged() {
708
+ const controller = this._dateMetadataController;
709
+
710
+ // Only the open overlay has a spinner to update and a today button to re-evaluate.
711
+ if (this._overlayContent) {
712
+ this._overlayContent.loading = controller.isLoading();
713
+ this._overlayContent.updateTodayButton();
714
+ }
715
+
716
+ // Runs whether or not the overlay was ever opened, which is the case this exists for: a value
717
+ // set or typed with the overlay closed is reported invalid as soon as its month answers.
718
+ if (this.__awaitingProviderValidation && this._selectedDate && controller.isMonthLoaded(this._selectedDate)) {
719
+ this.__awaitingProviderValidation = false;
720
+ this._requestValidation();
721
+ }
722
+ }
723
+
591
724
  /**
592
725
  * Override method inherited from `FocusMixin`
593
726
  * to not call `_setFocused(true)` when focus
@@ -775,6 +908,8 @@ export const DatePickerMixin = (subclass) =>
775
908
  this._ignoreFocusedDateChange = true;
776
909
  this._focusedDate = selectedDate;
777
910
  this._ignoreFocusedDateChange = false;
911
+
912
+ this.__ensureSelectedDateLoaded();
778
913
  }
779
914
 
780
915
  /** @private */
@@ -845,6 +980,8 @@ export const DatePickerMixin = (subclass) =>
845
980
  enteredDate,
846
981
  ) {
847
982
  if (overlayContent) {
983
+ // Reuse the date-picker's controller so the overlay shares the same cache.
984
+ overlayContent._dateMetadataController = this._dateMetadataController;
848
985
  overlayContent.i18n = effectiveI18n;
849
986
  overlayContent.label = label;
850
987
  overlayContent.minDate = minDate;
@@ -860,11 +997,7 @@ export const DatePickerMixin = (subclass) =>
860
997
  /** @private */
861
998
  __updateOverlayContentTheme(overlayContent, theme) {
862
999
  if (overlayContent) {
863
- if (theme) {
864
- overlayContent.setAttribute('theme', theme);
865
- } else {
866
- overlayContent.removeAttribute('theme');
867
- }
1000
+ setOrRemoveAttribute(overlayContent, 'theme', theme);
868
1001
  }
869
1002
  }
870
1003
 
@@ -964,6 +1097,8 @@ export const DatePickerMixin = (subclass) =>
964
1097
 
965
1098
  /** @protected */
966
1099
  _onOverlayClosed() {
1100
+ this._overlayContent?.cancelLoadVisibleDateMetadata();
1101
+
967
1102
  // Reset `aria-hidden` state.
968
1103
  if (this.__showOthers) {
969
1104
  this.__showOthers();