@vaadin/date-picker 25.2.7 → 25.3.0-alpha10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/date-picker",
3
- "version": "25.2.7",
3
+ "version": "25.3.0-alpha10",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -35,21 +35,21 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@open-wc/dedupe-mixin": "^1.3.0",
38
- "@vaadin/a11y-base": "~25.2.7",
39
- "@vaadin/button": "~25.2.7",
40
- "@vaadin/component-base": "~25.2.7",
41
- "@vaadin/field-base": "~25.2.7",
42
- "@vaadin/input-container": "~25.2.7",
43
- "@vaadin/overlay": "~25.2.7",
44
- "@vaadin/vaadin-themable-mixin": "~25.2.7",
38
+ "@vaadin/a11y-base": "25.3.0-alpha10",
39
+ "@vaadin/button": "25.3.0-alpha10",
40
+ "@vaadin/component-base": "25.3.0-alpha10",
41
+ "@vaadin/field-base": "25.3.0-alpha10",
42
+ "@vaadin/input-container": "25.3.0-alpha10",
43
+ "@vaadin/overlay": "25.3.0-alpha10",
44
+ "@vaadin/vaadin-themable-mixin": "25.3.0-alpha10",
45
45
  "lit": "^3.0.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@vaadin/aura": "~25.2.7",
49
- "@vaadin/chai-plugins": "~25.2.7",
50
- "@vaadin/test-runner-commands": "~25.2.7",
48
+ "@vaadin/aura": "25.3.0-alpha10",
49
+ "@vaadin/chai-plugins": "25.3.0-alpha10",
50
+ "@vaadin/test-runner-commands": "25.3.0-alpha10",
51
51
  "@vaadin/testing-helpers": "^2.0.0",
52
- "@vaadin/vaadin-lumo-styles": "~25.2.7",
52
+ "@vaadin/vaadin-lumo-styles": "25.3.0-alpha10",
53
53
  "sinon": "^22.0.0"
54
54
  },
55
55
  "customElements": "custom-elements.json",
@@ -57,5 +57,5 @@
57
57
  "web-types.json",
58
58
  "web-types.lit.json"
59
59
  ],
60
- "gitHead": "14be1af674f3968cce68c6eb4742340694c4a949"
60
+ "gitHead": "f2833abdf9b613fa0d0ed216830e3f4de87b7dac"
61
61
  }
@@ -17,6 +17,7 @@ export const overlayContentStyles = css`
17
17
  height: 100%;
18
18
  outline: none;
19
19
  overflow: hidden;
20
+ position: relative;
20
21
  }
21
22
 
22
23
  :host([desktop]) {
@@ -49,6 +50,14 @@ export const overlayContentStyles = css`
49
50
  display: none !important;
50
51
  }
51
52
 
53
+ [part='loader'] {
54
+ position: absolute;
55
+ z-index: 1;
56
+ inset-block-start: var(--vaadin-date-picker-month-header-font-size, 0.9375rem);
57
+ inset-inline: 0;
58
+ margin-inline: auto;
59
+ }
60
+
52
61
  ::slotted([slot='months']) {
53
62
  --vaadin-infinite-scroller-item-height: round(
54
63
  var(--vaadin-date-picker-month-header-font-size, 0.9375rem) + 0.75rem +
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2026 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { ReactiveController, ReactiveControllerHost, ReactiveElement } from 'lit';
7
+ import type { DatePickerDateMetadata, DatePickerDateMetadataProvider } from './vaadin-date-picker-mixin.js';
8
+
9
+ /**
10
+ * A reactive controller that resolves the metadata (currently the disabled state)
11
+ * for the dates shown by the date-picker's `dateMetadataProvider`.
12
+ *
13
+ * The provider is called for a range of months and may return an array
14
+ * synchronously or a `Promise`, so results from a server (Flow) or a remote
15
+ * availability service can be awaited. Each returned entry is a `DatePickerDate`
16
+ * extended with metadata fields, e.g. `{ year, month, day, disabled: true }`.
17
+ *
18
+ * `ARCHITECTURE.md` in this package records the reasoning behind the request,
19
+ * caching, notification and failure behavior.
20
+ */
21
+ export class DateMetadataController implements ReactiveController {
22
+ /**
23
+ * The controller host element.
24
+ */
25
+ host: HTMLElement & ReactiveControllerHost;
26
+
27
+ /**
28
+ * The provider function, or `null` when none is set.
29
+ */
30
+ provider: DatePickerDateMetadataProvider | null;
31
+
32
+ constructor(host: HTMLElement & ReactiveControllerHost, onChange?: () => void);
33
+
34
+ hostConnected(): void;
35
+
36
+ /**
37
+ * Registers an element to be re-rendered whenever the resolved metadata or the
38
+ * loading state changes. The element must render from its bindings, and must not be
39
+ * the one whose own observer triggers a load. It stays registered for the
40
+ * controller's lifetime.
41
+ */
42
+ subscribe(element: ReactiveElement): void;
43
+
44
+ /**
45
+ * Whether any month range is currently being fetched.
46
+ */
47
+ isLoading(): boolean;
48
+
49
+ /**
50
+ * Sets the provider function and clears the cache. Passing the same provider again
51
+ * is a no-op, so callers should keep a stable reference.
52
+ */
53
+ setProvider(provider: DatePickerDateMetadataProvider | null | undefined): void;
54
+
55
+ /**
56
+ * Clears the cache and invalidates any in-flight requests.
57
+ */
58
+ clearCache(): void;
59
+
60
+ /**
61
+ * Whether the provider has answered for the month containing the given date.
62
+ * A month whose request failed is not loaded and will be requested again.
63
+ */
64
+ isMonthLoaded(date: Date | null | undefined): boolean;
65
+
66
+ /**
67
+ * Whether the month containing the given date is currently being fetched. A month
68
+ * that has not been asked about is not pending, so this reports the same state as
69
+ * `isLoading()` does for the whole cache.
70
+ */
71
+ isMonthPending(date: Date | null | undefined): boolean;
72
+
73
+ /**
74
+ * The metadata resolved for the given date, or `undefined` when the date has
75
+ * no metadata or its month has not been resolved yet. Returns the entry the
76
+ * provider supplied, which the caller must not modify.
77
+ */
78
+ getMetadata(date: Date | null | undefined): DatePickerDateMetadata | undefined;
79
+
80
+ /**
81
+ * Whether the given date is disabled by its metadata.
82
+ */
83
+ isDateDisabled(date: Date | null | undefined): boolean;
84
+
85
+ /**
86
+ * Ensures the provider has been consulted for the inclusive range between the
87
+ * given dates, rounded out to whole blocks of months. Months already loaded or in
88
+ * flight are skipped, and the ones left over are requested with a single call.
89
+ *
90
+ * Each call that finds a missing month issues its own request, so a caller that
91
+ * loads on scroll should debounce.
92
+ */
93
+ ensureRangeLoaded(startDate: Date | null | undefined, endDate: Date | null | undefined): void;
94
+ }
@@ -0,0 +1,276 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2026 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { microTask } from '@vaadin/component-base/src/async.js';
7
+ import { Debouncer } from '@vaadin/component-base/src/debounce.js';
8
+ import { issueWarning } from '@vaadin/component-base/src/warnings.js';
9
+ import {
10
+ createDate,
11
+ extractDateParts,
12
+ lastOfMonth,
13
+ monthDate,
14
+ monthIndex,
15
+ monthIndexOf,
16
+ } from './vaadin-date-picker-helper.js';
17
+
18
+ // Counted from January of year 0, so a block is one calendar year.
19
+ const BLOCK_MONTHS = 12;
20
+
21
+ function blockStart(month) {
22
+ return Math.floor(month / BLOCK_MONTHS) * BLOCK_MONTHS;
23
+ }
24
+
25
+ const PENDING_MONTH = Object.freeze({ pending: true });
26
+
27
+ function isValidEntry(entry) {
28
+ if (!entry || !Number.isInteger(entry.year) || !Number.isInteger(entry.month) || !Number.isInteger(entry.day)) {
29
+ return false;
30
+ }
31
+ const date = createDate(entry.year, entry.month, entry.day);
32
+ return date.getFullYear() === entry.year && date.getMonth() === entry.month && date.getDate() === entry.day;
33
+ }
34
+
35
+ function groupEntriesByMonth(months, entries) {
36
+ const result = new Map(months.map((month) => [month, new Map()]));
37
+
38
+ if (Array.isArray(entries)) {
39
+ entries.forEach((entry) => {
40
+ if (isValidEntry(entry)) {
41
+ result.get(monthIndexOf(entry.year, entry.month))?.set(entry.day, entry);
42
+ } else {
43
+ issueWarning('Ignored `dateMetadataProvider` entries with an invalid year, month (0-11) or day.');
44
+ }
45
+ });
46
+ } else if (entries != null) {
47
+ issueWarning('Expected `dateMetadataProvider` to return an array of date metadata objects.');
48
+ }
49
+
50
+ return result;
51
+ }
52
+
53
+ /**
54
+ * A reactive controller that resolves the metadata (currently the disabled state)
55
+ * for the dates shown by the date-picker's `dateMetadataProvider`.
56
+ *
57
+ * The provider is called for a range of months and may return an array
58
+ * synchronously or a `Promise`, so results from a server (Flow) or a remote
59
+ * availability service can be awaited. Each returned entry is a `DatePickerDate`
60
+ * extended with metadata fields, e.g. `{ year, month, day, disabled: true }`.
61
+ *
62
+ * `ARCHITECTURE.md` in this package records the reasoning behind the request,
63
+ * caching, notification and failure behavior.
64
+ */
65
+ export class DateMetadataController {
66
+ /**
67
+ * The controller host element.
68
+ * @type {import('lit').ReactiveControllerHost & HTMLElement}
69
+ */
70
+ host;
71
+
72
+ /**
73
+ * The provider function, or `null` when none is set.
74
+ * @type {Function | null}
75
+ */
76
+ provider = null;
77
+
78
+ /** @type {(() => void) | undefined} */
79
+ #onChange;
80
+
81
+ /**
82
+ * What is known about each month, keyed by month index: a shared marker while its request is in
83
+ * flight, or a record holding the resolved entries by day. A month is absent until it is loaded,
84
+ * and absent again if its request failed.
85
+ * @type {Map<number, { pending: boolean, entries?: Map<number, object> }>}
86
+ */
87
+ #months = new Map();
88
+
89
+ /** @type {Set<import('lit').ReactiveElement>} */
90
+ #subscribers = new Set();
91
+
92
+ #requestId = 0;
93
+
94
+ /** @type {import('@vaadin/component-base/src/debounce.js').Debouncer} */
95
+ #notifyDebouncer;
96
+
97
+ constructor(host, onChange) {
98
+ this.host = host;
99
+ this.#onChange = onChange;
100
+ }
101
+
102
+ hostConnected() {
103
+ this.#notify();
104
+ }
105
+
106
+ /**
107
+ * Registers an element to be re-rendered whenever the resolved metadata or the
108
+ * loading state changes. The element must render from its bindings, and must not be
109
+ * the one whose own observer triggers a load. It stays registered for the
110
+ * controller's lifetime.
111
+ *
112
+ * @param {import('lit').ReactiveElement} element
113
+ */
114
+ subscribe(element) {
115
+ this.#subscribers.add(element);
116
+ }
117
+
118
+ /**
119
+ * Whether any month range is currently being fetched.
120
+ * @return {boolean}
121
+ */
122
+ isLoading() {
123
+ for (const { pending } of this.#months.values()) {
124
+ if (pending) {
125
+ return true;
126
+ }
127
+ }
128
+ return false;
129
+ }
130
+
131
+ /**
132
+ * Sets the provider function and clears the cache. Passing the same provider again
133
+ * is a no-op, so callers should keep a stable reference.
134
+ *
135
+ * @param {Function | null | undefined} provider
136
+ */
137
+ setProvider(provider) {
138
+ const next = provider ?? null;
139
+ if (this.provider === next) {
140
+ return;
141
+ }
142
+ this.provider = next;
143
+ this.clearCache();
144
+ }
145
+
146
+ /**
147
+ * Clears the cache and invalidates any in-flight requests.
148
+ */
149
+ clearCache() {
150
+ this.#months.clear();
151
+ this.#requestId += 1;
152
+ this.#notify();
153
+ }
154
+
155
+ /**
156
+ * Whether the provider has answered for the month containing the given date.
157
+ * A month whose request failed is not loaded and will be requested again.
158
+ * @param {Date | null | undefined} date
159
+ * @return {boolean}
160
+ */
161
+ isMonthLoaded(date) {
162
+ return !!this.#resolvedMonth(date);
163
+ }
164
+
165
+ /**
166
+ * Whether the month containing the given date is currently being fetched. A month
167
+ * that has not been asked about is not pending, so this reports the same state as
168
+ * `isLoading()` does for the whole cache.
169
+ * @param {Date | null | undefined} date
170
+ * @return {boolean}
171
+ */
172
+ isMonthPending(date) {
173
+ return !!date && !!this.#months.get(monthIndex(date))?.pending;
174
+ }
175
+
176
+ /**
177
+ * The metadata resolved for the given date, or `undefined` when the date has
178
+ * no metadata or its month has not been resolved yet. Returns the entry the
179
+ * provider supplied, which the caller must not modify.
180
+ * @param {Date | null | undefined} date
181
+ * @return {object | undefined}
182
+ */
183
+ getMetadata(date) {
184
+ return this.#resolvedMonth(date)?.entries.get(date.getDate());
185
+ }
186
+
187
+ /**
188
+ * Whether the given date is disabled by its metadata.
189
+ * @param {Date | null | undefined} date
190
+ * @return {boolean}
191
+ */
192
+ isDateDisabled(date) {
193
+ return !!this.getMetadata(date)?.disabled;
194
+ }
195
+
196
+ /**
197
+ * Ensures the provider has been consulted for the inclusive range between the
198
+ * given dates, rounded out to whole blocks of months. Months already loaded or in
199
+ * flight are skipped, and the ones left over are requested with a single call.
200
+ *
201
+ * Each call that finds a missing month issues its own request, so a caller that
202
+ * loads on scroll should debounce.
203
+ *
204
+ * @param {Date | null | undefined} startDate
205
+ * @param {Date | null | undefined} endDate
206
+ */
207
+ ensureRangeLoaded(startDate, endDate) {
208
+ if (!this.provider || !startDate || !endDate) {
209
+ return;
210
+ }
211
+
212
+ const first = blockStart(monthIndex(startDate));
213
+ const last = blockStart(monthIndex(endDate)) + BLOCK_MONTHS - 1;
214
+
215
+ const months = [];
216
+ for (let month = first; month <= last; month++) {
217
+ if (!this.#months.has(month)) {
218
+ months.push(month);
219
+ }
220
+ }
221
+
222
+ if (months.length > 0) {
223
+ this.#loadMonths(months);
224
+ }
225
+ }
226
+
227
+ #resolvedMonth(date) {
228
+ const month = date && this.#months.get(monthIndex(date));
229
+ return month && !month.pending ? month : undefined;
230
+ }
231
+
232
+ async #loadMonths(months) {
233
+ const requestId = this.#requestId;
234
+ months.forEach((month) => this.#months.set(month, PENDING_MONTH));
235
+ this.#notify();
236
+
237
+ const range = {
238
+ start: extractDateParts(monthDate(months[0])),
239
+ end: extractDateParts(lastOfMonth(monthDate(months.at(-1)))),
240
+ };
241
+
242
+ let entries;
243
+ try {
244
+ const data = await this.provider(range);
245
+ entries = groupEntriesByMonth(months, data);
246
+ } catch (error) {
247
+ console.error(error);
248
+ }
249
+
250
+ if (requestId !== this.#requestId) {
251
+ return;
252
+ }
253
+
254
+ months.forEach((month) => {
255
+ if (entries) {
256
+ this.#months.set(month, { pending: false, entries: entries.get(month) });
257
+ } else {
258
+ this.#months.delete(month);
259
+ }
260
+ });
261
+
262
+ this.#notify();
263
+ }
264
+
265
+ #notify() {
266
+ this.#subscribers.forEach((element) => element.requestUpdate());
267
+
268
+ if (this.#onChange) {
269
+ this.#notifyDebouncer = Debouncer.debounce(this.#notifyDebouncer, microTask, () => {
270
+ if (this.host.isConnected) {
271
+ this.#onChange();
272
+ }
273
+ });
274
+ }
275
+ }
276
+ }
@@ -3,6 +3,7 @@
3
3
  * Copyright (c) 2016 - 2026 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
+ import type { DateMetadataController } from './vaadin-date-metadata-controller.js';
6
7
  import type { DatePickerDate } from './vaadin-date-picker-mixin.js';
7
8
 
8
9
  /**
@@ -31,6 +32,21 @@ declare function dateAllowed(
31
32
  isDateDisabled: (date: DatePickerDate) => boolean | null,
32
33
  ): boolean;
33
34
 
35
+ /**
36
+ * Check if the given date can be selected: allowed by `dateAllowed` and not reported as disabled
37
+ * by the date metadata controller. This is narrower than `dateAllowed`, which decides what can be
38
+ * focused: a disabled date is still focusable, it just cannot be selected.
39
+ *
40
+ * @returns True if the date can be selected
41
+ */
42
+ declare function dateSelectable(
43
+ date: Date,
44
+ min: Date | null,
45
+ max: Date | null,
46
+ isDateDisabled: (date: DatePickerDate) => boolean | null,
47
+ controller?: DateMetadataController | null,
48
+ ): boolean;
49
+
34
50
  /**
35
51
  * Get closest date from array of dates.
36
52
  *
@@ -66,6 +82,7 @@ export {
66
82
  getISOWeekNumber,
67
83
  dateEquals,
68
84
  dateAllowed,
85
+ dateSelectable,
69
86
  getClosestDate,
70
87
  extractDateParts,
71
88
  dateAfterXMonths,
@@ -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
  *
@@ -185,11 +274,7 @@ export function parseDate(str) {
185
274
  return undefined;
186
275
  }
187
276
 
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;
277
+ return createDate(parseInt(parts[1], 10), parseInt(parts[2], 10) - 1, parseInt(parts[3], 10));
193
278
  }
194
279
 
195
280
  /**