@vaadin/date-picker 25.3.0-dev.3a3c2d7d2a → 25.3.0

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.
@@ -4,6 +4,79 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
 
7
+ /**
8
+ * Create a date at midnight in local time. Unlike `new Date(year, month, day)`,
9
+ * this supports years below 100, which the constructor maps into the 20th
10
+ * century. The month is assigned before the day so that the initial day of month
11
+ * (1) always exists in the target month.
12
+ *
13
+ * @param {number} year
14
+ * @param {number} month Zero-based month, may be out of range to shift the year
15
+ * @param {number} day May be `0` to select the last day of the previous month
16
+ * @return {Date}
17
+ */
18
+ export function createDate(year, month, day) {
19
+ const date = new Date(0, 0); // Wrong date (1900-01-01), but with midnight in local time
20
+ date.setFullYear(year);
21
+ date.setMonth(month);
22
+ date.setDate(day);
23
+ return date;
24
+ }
25
+
26
+ /**
27
+ * Get the first day of the month the given date is in.
28
+ *
29
+ * @param {!Date} date
30
+ * @return {Date}
31
+ */
32
+ export function firstOfMonth(date) {
33
+ return createDate(date.getFullYear(), date.getMonth(), 1);
34
+ }
35
+
36
+ /**
37
+ * Get the last day of the month the given date is in.
38
+ *
39
+ * @param {!Date} date
40
+ * @return {Date}
41
+ */
42
+ export function lastOfMonth(date) {
43
+ return createDate(date.getFullYear(), date.getMonth() + 1, 0);
44
+ }
45
+
46
+ /**
47
+ * Get the index of a month, counted from January of year 0. Reduces a month to a single
48
+ * integer, so a lookup builds no key and two months are adjacent when their indexes are.
49
+ *
50
+ * @param {number} year
51
+ * @param {number} month Zero-based month
52
+ * @return {number}
53
+ */
54
+ export function monthIndexOf(year, month) {
55
+ return year * 12 + month;
56
+ }
57
+
58
+ /**
59
+ * Get the index of the month the given date is in.
60
+ *
61
+ * @param {!Date} date
62
+ * @return {number}
63
+ */
64
+ export function monthIndex(date) {
65
+ return monthIndexOf(date.getFullYear(), date.getMonth());
66
+ }
67
+
68
+ /**
69
+ * Get the first day of the month with the given index, inverting `monthIndexOf`. Counting from
70
+ * January of year 0 also inverts negative indexes, since `createDate` normalizes a month outside
71
+ * 0-11 into the year.
72
+ *
73
+ * @param {number} index
74
+ * @return {Date}
75
+ */
76
+ export function monthDate(index) {
77
+ return createDate(0, index, 1);
78
+ }
79
+
7
80
  /**
8
81
  * Get ISO 8601 week number for the given date.
9
82
  *
@@ -106,6 +179,22 @@ export function dateAllowed(date, min, max, isDateDisabled) {
106
179
  return (!min || date >= min) && (!max || date <= max) && !dateIsDisabled;
107
180
  }
108
181
 
182
+ /**
183
+ * Check if the given date can be selected: allowed by `dateAllowed` and not reported as disabled
184
+ * by the date metadata controller. This is narrower than `dateAllowed`, which decides what can be
185
+ * focused: a disabled date is still focusable, it just cannot be selected.
186
+ *
187
+ * @param {!Date} date The date to check
188
+ * @param {Date | null} min Range start
189
+ * @param {Date | null} max Range end
190
+ * @param {function(!DatePickerDate): boolean} isDateDisabled Callback to check if the date is disabled
191
+ * @param {DateMetadataController | null} [controller] The date metadata controller
192
+ * @return {boolean} True if the date can be selected
193
+ */
194
+ export function dateSelectable(date, min, max, isDateDisabled, controller) {
195
+ return dateAllowed(date, min, max, isDateDisabled) && !controller?.isDateDisabled(date);
196
+ }
197
+
109
198
  /**
110
199
  * Get closest date from array of dates.
111
200
  *
@@ -171,51 +260,66 @@ export function getAdjustedYear(referenceDate, year, month = 0, day = 1) {
171
260
  return adjustedYear;
172
261
  }
173
262
 
263
+ const ISO_DATE = /^([-+]\d{1,6}|\d{2,4})-(\d{1,2})-(\d{1,2})$/u;
264
+
265
+ // The parts of a date string in a format the parsers accept, as written.
266
+ function parseParts(str) {
267
+ // Parsing with RegExp to ensure correct format
268
+ const parts = ISO_DATE.exec(str);
269
+ if (!parts) {
270
+ return undefined;
271
+ }
272
+
273
+ return { year: parseInt(parts[1], 10), month: parseInt(parts[2], 10) - 1, day: parseInt(parts[3], 10) };
274
+ }
275
+
174
276
  /**
175
277
  * Parse date string of one of the following date formats:
176
278
  * - ISO 8601 `"YYYY-MM-DD"`
177
- * - 6-digit extended ISO 8601 `"+YYYYYY-MM-DD"`, `"-YYYYYY-MM-DD"`
279
+ * - Extended ISO 8601 with a signed year, e.g. `"+012026-MM-DD"` or `"-0001-MM-DD"`
280
+ *
281
+ * A date that does not exist, such as `"2026-02-30"`, is not parsed. Building it would carry the
282
+ * surplus into the next month or year and answer with a date that was never asked for.
283
+ *
178
284
  * @param {!string} str Date string to parse
179
285
  * @return {Date} Parsed date in system timezone
180
286
  */
181
287
  export function parseDate(str) {
182
- // Parsing with RegExp to ensure correct format
183
- const parts = /^([-+]\d{1}|\d{2,4}|[-+]\d{6})-(\d{1,2})-(\d{1,2})$/u.exec(str);
288
+ const parts = parseParts(str);
184
289
  if (!parts) {
185
290
  return undefined;
186
291
  }
187
292
 
188
- const date = new Date(0, 0); // Wrong date (1900-01-01), but with midnight in local time
189
- date.setFullYear(parseInt(parts[1], 10));
190
- date.setMonth(parseInt(parts[2], 10) - 1);
191
- date.setDate(parseInt(parts[3], 10));
192
- return date;
293
+ const date = createDate(parts.year, parts.month, parts.day);
294
+
295
+ return date.getMonth() === parts.month && date.getDate() === parts.day ? date : undefined;
193
296
  }
194
297
 
195
298
  /**
196
299
  * Parse date string of one of the following date formats:
197
300
  * - ISO 8601 `"YYYY-MM-DD"`
198
- * - 6-digit extended ISO 8601 `"+YYYYYY-MM-DD"`, `"-YYYYYY-MM-DD"`
301
+ * - Extended ISO 8601 with a signed year, e.g. `"+012026-MM-DD"` or `"-0001-MM-DD"`
199
302
  *
200
303
  * Uses UTC date components to allow handling date instances independently of
201
304
  * the system time-zone.
202
305
  *
306
+ * A date that does not exist, such as `"2026-02-30"`, is not parsed, as in `parseDate`.
307
+ *
203
308
  * @param {!string} str Date string to parse
204
309
  * @return {Date} Parsed date in UTC timezone
205
310
  */
206
311
  export function parseUTCDate(str) {
207
- // Parsing with RegExp to ensure correct format
208
- const parts = /^([-+]\d{1}|\d{2,4}|[-+]\d{6})-(\d{1,2})-(\d{1,2})$/u.exec(str);
312
+ const parts = parseParts(str);
209
313
  if (!parts) {
210
314
  return undefined;
211
315
  }
212
316
 
213
317
  const date = new Date(Date.UTC(0, 0)); // Wrong date (1900-01-01), but with midnight in UTC
214
- date.setUTCFullYear(parseInt(parts[1], 10));
215
- date.setUTCMonth(parseInt(parts[2], 10) - 1);
216
- date.setUTCDate(parseInt(parts[3], 10));
318
+ date.setUTCFullYear(parts.year);
319
+ date.setUTCMonth(parts.month);
320
+ date.setUTCDate(parts.day);
217
321
 
218
- return date;
322
+ return date.getUTCMonth() === parts.month && date.getUTCDate() === parts.day ? date : undefined;
219
323
  }
220
324
 
221
325
  function formatISODateBase(dateParts) {
@@ -18,17 +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
+ */
21
25
  export interface DatePickerDateRange {
22
26
  /**
23
- * The first date of the range (inclusive).
27
+ * The first date of the range (inclusive), as an ISO 8601 date.
24
28
  */
25
- start: DatePickerDate;
29
+ start: string;
26
30
  /**
27
- * The last date of the range (inclusive).
31
+ * The last date of the range (inclusive), as an ISO 8601 date.
28
32
  */
29
- end: DatePickerDate;
33
+ end: string;
30
34
  }
31
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
+
32
64
  export interface DatePickerI18n {
33
65
  /**
34
66
  * An array with the full names of months starting
@@ -58,6 +90,11 @@ export interface DatePickerI18n {
58
90
  * Translation of the Cancel button text.
59
91
  */
60
92
  cancel?: string;
93
+ /**
94
+ * Accessible name of the overlay content, announced by screen readers when
95
+ * the overlay opens.
96
+ */
97
+ dialogAccessibleName?: string;
61
98
  /**
62
99
  * Used for adjusting the year value when parsing dates with short years.
63
100
  * The year values between 0 and 99 are evaluated and adjusted.
@@ -185,6 +222,10 @@ export declare class DatePickerMixinClass {
185
222
  * // Translation of the Cancel button text.
186
223
  * cancel: 'Cancel',
187
224
  *
225
+ * // Accessible name of the overlay content, announced by screen readers
226
+ * // when the overlay opens.
227
+ * dialogAccessibleName: 'Calendar',
228
+ *
188
229
  * // Used for adjusting the year value when parsing dates with short years.
189
230
  * // The year values between 0 and 99 are evaluated and adjusted.
190
231
  * // Example: for a referenceDate of 1970-10-30;
@@ -244,25 +285,51 @@ export declare class DatePickerMixinClass {
244
285
  * A function to be used to determine whether the user can select a given date.
245
286
  * Receives a `DatePickerDate` object of the date to be selected and should return a
246
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.
247
292
  */
248
293
  isDateDisabled: (date: DatePickerDate) => boolean;
249
294
 
250
295
  /**
251
- * A batch function that is consulted for a range of dates that the calendar is about to
252
- * render. It receives a `DatePickerDateRange` object and returns, or resolves with, an array
253
- * of `DatePickerDate` objects that should be disabled within that range.
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.
254
321
  *
255
- * Unlike `isDateDisabled`, which is called once per date, this function is called for a range
256
- * of dates at a time, and again as the calendar renders further dates. The size of the range
257
- * is decided by the calendar and may span multiple months. It may return a `Promise`, in which
258
- * case the affected dates render in a non-selectable pending state until it resolves.
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.
259
325
  *
260
- * Dates disabled by this function are combined with `min`, `max` and `isDateDisabled`.
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.
261
328
  *
262
- * Keep a stable reference to the function. Assigning a new function resets the internal cache
263
- * and re-fetches every visible range.
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.
264
331
  */
265
- disabledDatesProvider: (range: DatePickerDateRange) => DatePickerDate[] | Promise<DatePickerDate[]>;
332
+ dateMetadataProvider: DatePickerDateMetadataProvider | null | undefined;
266
333
 
267
334
  /**
268
335
  * Opens the dropdown.
@@ -273,4 +340,9 @@ export declare class DatePickerMixinClass {
273
340
  * Closes the dropdown.
274
341
  */
275
342
  close(): void;
343
+
344
+ /**
345
+ * Clears the `dateMetadataProvider` cache and reloads the date metadata.
346
+ */
347
+ clearCache(): void;
276
348
  }