@klinking/colander 0.0.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.
- package/dist/formats/date.cjs +49 -0
- package/dist/formats/date.d.cts +45 -0
- package/dist/formats/date.d.mts +45 -0
- package/dist/formats/date.mjs +2 -0
- package/dist/formats/object.cjs +49 -0
- package/dist/formats/object.d.cts +45 -0
- package/dist/formats/object.d.mts +45 -0
- package/dist/formats/object.mjs +2 -0
- package/dist/formats/plain-date-time.cjs +49 -0
- package/dist/formats/plain-date-time.d.cts +45 -0
- package/dist/formats/plain-date-time.d.mts +45 -0
- package/dist/formats/plain-date-time.mjs +2 -0
- package/dist/formats/plain-date.cjs +49 -0
- package/dist/formats/plain-date.d.cts +45 -0
- package/dist/formats/plain-date.d.mts +45 -0
- package/dist/formats/plain-date.mjs +2 -0
- package/dist/formats/plain-month-day.cjs +49 -0
- package/dist/formats/plain-month-day.d.cts +45 -0
- package/dist/formats/plain-month-day.d.mts +45 -0
- package/dist/formats/plain-month-day.mjs +2 -0
- package/dist/formats/plain-year-month.cjs +49 -0
- package/dist/formats/plain-year-month.d.cts +45 -0
- package/dist/formats/plain-year-month.d.mts +45 -0
- package/dist/formats/plain-year-month.mjs +2 -0
- package/dist/formats/zoned-date-time.cjs +49 -0
- package/dist/formats/zoned-date-time.d.cts +45 -0
- package/dist/formats/zoned-date-time.d.mts +45 -0
- package/dist/formats/zoned-date-time.mjs +2 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
- package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
- package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
- package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
- package/package.json +89 -0
- package/src/calendar-context.ts +22 -0
- package/src/calendar-provider.test.tsx +123 -0
- package/src/calendar-provider.tsx +538 -0
- package/src/calendar-types.ts +212 -0
- package/src/compute-weeks-in-window.test.ts +83 -0
- package/src/compute-weeks-in-window.ts +40 -0
- package/src/context.ts +34 -0
- package/src/day-cell.tsx +635 -0
- package/src/drag-handle.tsx +137 -0
- package/src/formats/date.ts +110 -0
- package/src/formats/formats.test.ts +18 -0
- package/src/formats/object.ts +110 -0
- package/src/formats/plain-date-time.ts +110 -0
- package/src/formats/plain-date.ts +110 -0
- package/src/formats/plain-month-day.ts +110 -0
- package/src/formats/plain-year-month.ts +110 -0
- package/src/formats/zoned-date-time.ts +110 -0
- package/src/grid-header.tsx +141 -0
- package/src/grid.event-perf.test.tsx +282 -0
- package/src/grid.perf.test.tsx +467 -0
- package/src/grid.tsx +608 -0
- package/src/index.ts +116 -0
- package/src/keyboard.test.ts +498 -0
- package/src/keyboard.ts +142 -0
- package/src/month-separator.test.tsx +183 -0
- package/src/month-separator.tsx +238 -0
- package/src/month-view-context.ts +26 -0
- package/src/month-view-types.ts +89 -0
- package/src/month-view.test.tsx +1433 -0
- package/src/month-view.tsx +373 -0
- package/src/navigation.tsx +350 -0
- package/src/overflow.test.ts +124 -0
- package/src/overflow.ts +233 -0
- package/src/range-preview.tsx +90 -0
- package/src/resolve-first-week.test.ts +132 -0
- package/src/resolve-first-week.ts +145 -0
- package/src/root-selection.ts +621 -0
- package/src/selected-range.tsx +163 -0
- package/src/temporal-polyfill.test.ts +317 -0
- package/src/temporal-polyfill.ts +566 -0
- package/src/test-temporal.ts +17 -0
- package/src/types.ts +814 -0
- package/src/utils.bench.ts +183 -0
- package/src/utils.test.ts +983 -0
- package/src/utils.ts +493 -0
- package/src/view-context.ts +28 -0
- package/src/week-number.tsx +118 -0
- package/src/weeks-grid.tsx +265 -0
- package/src/weeks-keyboard.test.ts +222 -0
- package/src/weeks-keyboard.ts +156 -0
- package/src/weeks-navigation.tsx +209 -0
- package/src/weeks-view-context.ts +26 -0
- package/src/weeks-view-state.ts +421 -0
- package/src/weeks-view-types.ts +102 -0
- package/src/weeks-view.test.tsx +479 -0
- package/src/weeks-view.tsx +127 -0
|
@@ -0,0 +1,4556 @@
|
|
|
1
|
+
import { Children, createContext, forwardRef, isValidElement, memo, useCallback, useContext, useEffect, useId, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
4
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
5
|
+
//#region src/calendar-context.ts
|
|
6
|
+
/** @internal React context carrying stable CalendarProvider values (callbacks, config). */
|
|
7
|
+
const CalendarStableContext = createContext(null);
|
|
8
|
+
/** @internal React context carrying volatile CalendarProvider state. */
|
|
9
|
+
const CalendarStateContext = createContext(null);
|
|
10
|
+
/** Returns the stable (callbacks/config) part of CalendarProvider context. */
|
|
11
|
+
function useCalendarStable() {
|
|
12
|
+
const ctx = useContext(CalendarStableContext);
|
|
13
|
+
if (!ctx) throw new Error("useCalendarStable must be used within CalendarProvider");
|
|
14
|
+
return ctx;
|
|
15
|
+
}
|
|
16
|
+
/** Returns the volatile state part of CalendarProvider context. */
|
|
17
|
+
function useCalendarState() {
|
|
18
|
+
const ctx = useContext(CalendarStateContext);
|
|
19
|
+
if (!ctx) throw new Error("useCalendarState must be used within CalendarProvider");
|
|
20
|
+
return ctx;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/utils.ts
|
|
24
|
+
/**
|
|
25
|
+
* Returns the default calendar system for a given locale (e.g. `"gregory"` for `"en-US"`).
|
|
26
|
+
*
|
|
27
|
+
* @param locale - A BCP 47 locale string. Falls back to the runtime default when empty.
|
|
28
|
+
*/
|
|
29
|
+
function calendarForLocale(locale) {
|
|
30
|
+
return new Intl.DateTimeFormat(locale || void 0).resolvedOptions().calendar;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Resolves a {@link TemporalNamespace} instance.
|
|
34
|
+
*
|
|
35
|
+
* Returns `provided` when given, otherwise checks `globalThis.Temporal`.
|
|
36
|
+
* Throws if neither is available.
|
|
37
|
+
*
|
|
38
|
+
* @param provided - An explicit Temporal polyfill or namespace.
|
|
39
|
+
*/
|
|
40
|
+
function resolveTemporal(provided) {
|
|
41
|
+
if (provided) return provided;
|
|
42
|
+
if (typeof globalThis !== "undefined" && globalThis.Temporal) return globalThis.Temporal;
|
|
43
|
+
throw new Error("DatePicker: Temporal is not available. Pass a Temporal polyfill via the `temporal` prop, or use a browser that supports the Temporal API natively.");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns the IANA time zone identifier of the host environment (e.g. `"America/New_York"`).
|
|
47
|
+
*
|
|
48
|
+
* @param T - Temporal namespace.
|
|
49
|
+
*/
|
|
50
|
+
function getSystemTimeZone(T) {
|
|
51
|
+
return T.Now.timeZoneId();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Converts any {@link DateValueObject} into a `Temporal.ZonedDateTime`.
|
|
55
|
+
*
|
|
56
|
+
* Handles all supported value formats (`PlainDate`, `PlainDateTime`,
|
|
57
|
+
* `PlainMonthDay`, `PlainTime`, `PlainYearMonth`, `ZonedDateTime`,
|
|
58
|
+
* `object`, and `Date`). For partial types (`PlainMonthDay`, `PlainTime`)
|
|
59
|
+
* the missing components are filled from the current date/time.
|
|
60
|
+
*
|
|
61
|
+
* @param tagged - The tagged date value to convert.
|
|
62
|
+
* @param timeZone - IANA time zone used for the resulting `ZonedDateTime`.
|
|
63
|
+
* @param T - Temporal namespace.
|
|
64
|
+
*/
|
|
65
|
+
function toZonedDateTime(tagged, timeZone, T) {
|
|
66
|
+
const now = T.Now.zonedDateTimeISO(timeZone);
|
|
67
|
+
switch (tagged.format) {
|
|
68
|
+
case "PlainDate": return tagged.value.toZonedDateTime(timeZone);
|
|
69
|
+
case "PlainDateTime": return tagged.value.toZonedDateTime(timeZone);
|
|
70
|
+
case "PlainMonthDay": return tagged.value.toPlainDate({ year: now.year }).toZonedDateTime(timeZone);
|
|
71
|
+
case "PlainTime": return now.toPlainDate().toPlainDateTime(tagged.value).toZonedDateTime(timeZone);
|
|
72
|
+
case "PlainYearMonth": return tagged.value.toPlainDate({ day: 1 }).toZonedDateTime(timeZone);
|
|
73
|
+
case "ZonedDateTime": return tagged.value;
|
|
74
|
+
case "object": {
|
|
75
|
+
const obj = tagged.value;
|
|
76
|
+
return T.PlainDateTime.from({
|
|
77
|
+
year: obj.year ?? now.year,
|
|
78
|
+
month: obj.month ?? now.month,
|
|
79
|
+
day: obj.day ?? now.day,
|
|
80
|
+
hour: obj.hour ?? 0,
|
|
81
|
+
minute: obj.minute ?? 0,
|
|
82
|
+
second: obj.second ?? 0
|
|
83
|
+
}).toZonedDateTime(obj.timeZone ?? timeZone);
|
|
84
|
+
}
|
|
85
|
+
case "Date": {
|
|
86
|
+
const d = tagged.value;
|
|
87
|
+
return T.PlainDateTime.from({
|
|
88
|
+
year: d.getFullYear(),
|
|
89
|
+
month: d.getMonth() + 1,
|
|
90
|
+
day: d.getDate(),
|
|
91
|
+
hour: d.getHours(),
|
|
92
|
+
minute: d.getMinutes(),
|
|
93
|
+
second: d.getSeconds()
|
|
94
|
+
}).toZonedDateTime(timeZone);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Converts a `Temporal.ZonedDateTime` back into a {@link DateValueObject}
|
|
100
|
+
* of the requested format.
|
|
101
|
+
*
|
|
102
|
+
* This is the inverse of {@link toZonedDateTime}.
|
|
103
|
+
*
|
|
104
|
+
* @param zdt - The zoned date-time to convert from.
|
|
105
|
+
* @param format - The target {@link ValueFormat}.
|
|
106
|
+
* @param T - Temporal namespace.
|
|
107
|
+
*/
|
|
108
|
+
function fromZonedDateTime(zdt, format, T) {
|
|
109
|
+
switch (format) {
|
|
110
|
+
case "PlainDate": return {
|
|
111
|
+
format,
|
|
112
|
+
value: zdt.toPlainDate()
|
|
113
|
+
};
|
|
114
|
+
case "PlainDateTime": return {
|
|
115
|
+
format,
|
|
116
|
+
value: zdt.toPlainDateTime()
|
|
117
|
+
};
|
|
118
|
+
case "PlainMonthDay": return {
|
|
119
|
+
format,
|
|
120
|
+
value: T.PlainMonthDay.from({
|
|
121
|
+
month: zdt.month,
|
|
122
|
+
day: zdt.day
|
|
123
|
+
})
|
|
124
|
+
};
|
|
125
|
+
case "PlainTime": return {
|
|
126
|
+
format,
|
|
127
|
+
value: zdt.toPlainTime()
|
|
128
|
+
};
|
|
129
|
+
case "PlainYearMonth": return {
|
|
130
|
+
format,
|
|
131
|
+
value: T.PlainYearMonth.from({
|
|
132
|
+
year: zdt.year,
|
|
133
|
+
month: zdt.month
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
case "ZonedDateTime": return {
|
|
137
|
+
format,
|
|
138
|
+
value: zdt
|
|
139
|
+
};
|
|
140
|
+
case "object": return {
|
|
141
|
+
format,
|
|
142
|
+
value: {
|
|
143
|
+
year: zdt.year,
|
|
144
|
+
month: zdt.month,
|
|
145
|
+
day: zdt.day,
|
|
146
|
+
hour: zdt.hour,
|
|
147
|
+
minute: zdt.minute,
|
|
148
|
+
second: zdt.second,
|
|
149
|
+
timeZone: zdt.timeZoneId
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
case "Date": return {
|
|
153
|
+
format,
|
|
154
|
+
value: new Date(zdt.epochMilliseconds)
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Convenience wrapper around {@link toZonedDateTime} that accepts `undefined`.
|
|
160
|
+
*
|
|
161
|
+
* Returns `undefined` when `selected` is not provided, otherwise converts to
|
|
162
|
+
* a `Temporal.ZonedDateTime`.
|
|
163
|
+
*
|
|
164
|
+
* @param selected - The currently selected value, or `undefined`.
|
|
165
|
+
* @param timeZone - IANA time zone.
|
|
166
|
+
* @param T - Temporal namespace.
|
|
167
|
+
*/
|
|
168
|
+
function selectedToZdt(selected, timeZone, T) {
|
|
169
|
+
if (!selected) return void 0;
|
|
170
|
+
return toZonedDateTime(selected, timeZone, T);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Builds a 2D array of calendar weeks for a given month.
|
|
174
|
+
*
|
|
175
|
+
* Each inner array has exactly `daysInWeek` entries. The grid is padded
|
|
176
|
+
* with days from adjacent months so every week is complete.
|
|
177
|
+
*
|
|
178
|
+
* @param year - Calendar year.
|
|
179
|
+
* @param month - Calendar month (1–12).
|
|
180
|
+
* @param T - Temporal namespace.
|
|
181
|
+
* @param opts - Optional settings for week start day and fixed weeks.
|
|
182
|
+
* @returns An array of 4–6 weeks, each containing `daysInWeek` `PlainDate` values.
|
|
183
|
+
*/
|
|
184
|
+
function getMonthWeeks(year, month, T, opts) {
|
|
185
|
+
const weekStartDay = opts?.weekStartDay ?? 0;
|
|
186
|
+
const fixedWeeks = opts?.fixedWeeks ?? false;
|
|
187
|
+
const firstOfMonth = T.PlainDate.from({
|
|
188
|
+
year,
|
|
189
|
+
month,
|
|
190
|
+
day: 1
|
|
191
|
+
});
|
|
192
|
+
const daysInWeek = firstOfMonth.daysInWeek;
|
|
193
|
+
const daysInMonth = firstOfMonth.daysInMonth;
|
|
194
|
+
const lastOfMonth = T.PlainDate.from({
|
|
195
|
+
year,
|
|
196
|
+
month,
|
|
197
|
+
day: daysInMonth
|
|
198
|
+
});
|
|
199
|
+
const adjustedFirst = (firstOfMonth.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek;
|
|
200
|
+
const gridStart = firstOfMonth.subtract({ days: adjustedFirst });
|
|
201
|
+
const adjustedLast = (lastOfMonth.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek;
|
|
202
|
+
const daysAfter = daysInWeek - 1 - adjustedLast;
|
|
203
|
+
const gridEnd = lastOfMonth.add({ days: daysAfter });
|
|
204
|
+
const weeks = [];
|
|
205
|
+
let current = gridStart;
|
|
206
|
+
while (T.PlainDate.compare(current, gridEnd) <= 0) {
|
|
207
|
+
const week = [];
|
|
208
|
+
for (let i = 0; i < daysInWeek; i++) {
|
|
209
|
+
week.push(current);
|
|
210
|
+
current = current.add({ days: 1 });
|
|
211
|
+
}
|
|
212
|
+
weeks.push(week);
|
|
213
|
+
}
|
|
214
|
+
if (fixedWeeks) while (weeks.length < 6) {
|
|
215
|
+
const week = [];
|
|
216
|
+
for (let i = 0; i < daysInWeek; i++) {
|
|
217
|
+
week.push(current);
|
|
218
|
+
current = current.add({ days: 1 });
|
|
219
|
+
}
|
|
220
|
+
weeks.push(week);
|
|
221
|
+
}
|
|
222
|
+
return weeks;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Converts a `Temporal.ZonedDateTime` to a native JavaScript `Date`.
|
|
226
|
+
*
|
|
227
|
+
* @param zdt - The zoned date-time to convert.
|
|
228
|
+
*/
|
|
229
|
+
function zdtToNativeDate(zdt) {
|
|
230
|
+
return new Date(zdt.epochMilliseconds);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Returns a date falling on the configured week start day, used as an anchor
|
|
234
|
+
* for generating weekday name lists.
|
|
235
|
+
*
|
|
236
|
+
* @param T - Temporal namespace.
|
|
237
|
+
* @param weekStartDay - 0=Sunday (default), 1=Monday, etc.
|
|
238
|
+
*/
|
|
239
|
+
function getReferenceWeekStart(T, weekStartDay = 0) {
|
|
240
|
+
return T.PlainDate.from("2024-01-07").add({ days: weekStartDay });
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Computes the year/month and first day of the month adjacent to `current`.
|
|
244
|
+
*
|
|
245
|
+
* Handles year boundaries (e.g. Dec → Jan, Jan → Dec).
|
|
246
|
+
*
|
|
247
|
+
* @param current - The reference month.
|
|
248
|
+
* @param direction - `"next"` or `"prev"`.
|
|
249
|
+
* @param T - Temporal namespace.
|
|
250
|
+
*/
|
|
251
|
+
function computeAdjacentMonth(current, direction, T) {
|
|
252
|
+
const d = T.PlainDate.from({
|
|
253
|
+
year: current.year,
|
|
254
|
+
month: current.month,
|
|
255
|
+
day: 1
|
|
256
|
+
})[direction === "next" ? "add" : "subtract"]({ months: 1 });
|
|
257
|
+
return {
|
|
258
|
+
year: d.year,
|
|
259
|
+
month: d.month,
|
|
260
|
+
firstDay: d
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Determines the focused date after navigating to a new month.
|
|
265
|
+
*
|
|
266
|
+
* If `currentFocused` is already within `targetMonth`, it is preserved.
|
|
267
|
+
* Otherwise, `firstDay` of the target month is returned.
|
|
268
|
+
*
|
|
269
|
+
* @param currentFocused - The currently focused date.
|
|
270
|
+
* @param targetMonth - The month being navigated to.
|
|
271
|
+
* @param firstDay - The first day of the target month (fallback).
|
|
272
|
+
*/
|
|
273
|
+
function focusedDateForMonth(currentFocused, targetMonth, firstDay) {
|
|
274
|
+
if (currentFocused.year === targetMonth.year && currentFocused.month === targetMonth.month) return currentFocused;
|
|
275
|
+
return firstDay;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Resolves which date should receive focus/tabindex in the calendar grid.
|
|
279
|
+
*
|
|
280
|
+
* Priority when `gridHasFocus` is `true` (keyboard navigation):
|
|
281
|
+
* 1. `focusedDate` if it exists in the grid.
|
|
282
|
+
* 2. `selectedDate` if it exists in the grid.
|
|
283
|
+
* 3. First enabled day of `currentMonth`, or first grid day as last resort.
|
|
284
|
+
*
|
|
285
|
+
* When `gridHasFocus` is `false` (tab-in), `selectedDate` takes priority
|
|
286
|
+
* over `focusedDate`.
|
|
287
|
+
*
|
|
288
|
+
* @param focusedDate - The date currently tracked as focused.
|
|
289
|
+
* @param selectedDate - The currently selected date, if any.
|
|
290
|
+
* @param weeks - The 2D weeks array from {@link getMonthWeeks}.
|
|
291
|
+
* @param currentMonth - The month being displayed.
|
|
292
|
+
* @param isDateDisabled - Predicate for disabled dates.
|
|
293
|
+
* @param T - Temporal namespace.
|
|
294
|
+
* @param gridHasFocus - Whether the grid currently holds DOM focus.
|
|
295
|
+
*/
|
|
296
|
+
function resolveFocusTarget(focusedDate, selectedDate, weeks, currentMonth, isDateDisabled, T, gridHasFocus = true) {
|
|
297
|
+
const allDays = weeks.flat();
|
|
298
|
+
const inGrid = (d) => allDays.some((g) => T.PlainDate.compare(g, d) === 0);
|
|
299
|
+
if (gridHasFocus && inGrid(focusedDate)) return focusedDate;
|
|
300
|
+
if (selectedDate && inGrid(selectedDate)) return selectedDate;
|
|
301
|
+
if (!gridHasFocus && inGrid(focusedDate)) return focusedDate;
|
|
302
|
+
return allDays.find((d) => d.year === currentMonth.year && d.month === currentMonth.month && !isDateDisabled(d)) ?? allDays[0];
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Determines whether a day cell should imperatively receive DOM focus.
|
|
306
|
+
*
|
|
307
|
+
* Only returns `true` when the cell is the focused date **and** the grid
|
|
308
|
+
* currently holds DOM focus (i.e. keyboard navigation is active).
|
|
309
|
+
*
|
|
310
|
+
* @param isFocused - Whether this cell is the logically focused date.
|
|
311
|
+
* @param gridHasFocus - Whether the grid currently holds DOM focus.
|
|
312
|
+
*/
|
|
313
|
+
function shouldMoveDomFocus(isFocused, gridHasFocus) {
|
|
314
|
+
return isFocused && gridHasFocus;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Returns the normalized position of `date` within the inclusive range
|
|
318
|
+
* `[rangeStart, rangeEnd]` as a number from `0` (start) to `1` (end),
|
|
319
|
+
* or `false` if the date is outside the range.
|
|
320
|
+
* A single-day range returns `0` for that date.
|
|
321
|
+
*
|
|
322
|
+
* @param date - The date to test.
|
|
323
|
+
* @param rangeStart - Start of the range (inclusive). When `undefined` and `rangeEnd` is defined, the range collapses to a single-day range at `rangeEnd`.
|
|
324
|
+
* @param rangeEnd - End of the range (inclusive). When `undefined` and `rangeStart` is defined, the range collapses to a single-day range at `rangeStart`. Returns `false` when both boundaries are `undefined`.
|
|
325
|
+
* @param T - Temporal namespace used for date comparison.
|
|
326
|
+
*/
|
|
327
|
+
function isInRange(date, rangeStart, rangeEnd, T) {
|
|
328
|
+
if (!rangeStart && !rangeEnd) return false;
|
|
329
|
+
const effectiveStart = rangeStart ?? rangeEnd;
|
|
330
|
+
const effectiveEnd = rangeEnd ?? rangeStart;
|
|
331
|
+
if (T.PlainDate.compare(date, effectiveStart) < 0 || T.PlainDate.compare(date, effectiveEnd) > 0) return false;
|
|
332
|
+
const totalDays = effectiveEnd.since(effectiveStart).days;
|
|
333
|
+
if (totalDays === 0) return 0;
|
|
334
|
+
return date.since(effectiveStart).days / totalDays;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Computes how a date range intersects with a single calendar week row.
|
|
338
|
+
*
|
|
339
|
+
* Used to position and style the range highlight overlay within a week.
|
|
340
|
+
*
|
|
341
|
+
* @param weekDays - Array of 7 `PlainDate` values for the week (Sun–Sat).
|
|
342
|
+
* @param rangeStart - Start of the selected range, or `undefined`.
|
|
343
|
+
* @param rangeEnd - End of the selected range, or `undefined`.
|
|
344
|
+
* @param T - Temporal namespace.
|
|
345
|
+
* @returns An object with `active` (whether the range overlaps this week),
|
|
346
|
+
* `startIndex` / `endIndex` (0-based column positions), and
|
|
347
|
+
* `extendsBefore` / `extendsAfter` (whether the range continues beyond the week).
|
|
348
|
+
*/
|
|
349
|
+
function computeWeekRangeInfo(weekDays, rangeStart, rangeEnd, T) {
|
|
350
|
+
const inactive = {
|
|
351
|
+
active: false,
|
|
352
|
+
startIndex: 0,
|
|
353
|
+
endIndex: 0,
|
|
354
|
+
extendsBefore: false,
|
|
355
|
+
extendsAfter: false
|
|
356
|
+
};
|
|
357
|
+
if (weekDays.length === 0) return inactive;
|
|
358
|
+
if (!rangeStart && !rangeEnd) return inactive;
|
|
359
|
+
const effectiveStart = rangeStart ?? rangeEnd;
|
|
360
|
+
const effectiveEnd = rangeEnd ?? rangeStart;
|
|
361
|
+
const weekStart = weekDays[0];
|
|
362
|
+
const weekEnd = weekDays[weekDays.length - 1];
|
|
363
|
+
if (T.PlainDate.compare(effectiveEnd, weekStart) < 0 || T.PlainDate.compare(effectiveStart, weekEnd) > 0) return inactive;
|
|
364
|
+
const extendsBefore = T.PlainDate.compare(effectiveStart, weekStart) < 0;
|
|
365
|
+
const extendsAfter = T.PlainDate.compare(effectiveEnd, weekEnd) > 0;
|
|
366
|
+
let startIndex = 0;
|
|
367
|
+
if (!extendsBefore) {
|
|
368
|
+
startIndex = weekDays.findIndex((d) => T.PlainDate.compare(d, effectiveStart) === 0);
|
|
369
|
+
if (startIndex === -1) return inactive;
|
|
370
|
+
}
|
|
371
|
+
let endIndex = weekDays.length - 1;
|
|
372
|
+
if (!extendsAfter) {
|
|
373
|
+
endIndex = weekDays.findIndex((d) => T.PlainDate.compare(d, effectiveEnd) === 0);
|
|
374
|
+
if (endIndex === -1) return inactive;
|
|
375
|
+
}
|
|
376
|
+
return {
|
|
377
|
+
active: true,
|
|
378
|
+
startIndex,
|
|
379
|
+
endIndex,
|
|
380
|
+
extendsBefore,
|
|
381
|
+
extendsAfter
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Generates localized weekday names starting from the configured week start day.
|
|
386
|
+
*
|
|
387
|
+
* @param locale - BCP 47 locale string (e.g. `"en-US"`).
|
|
388
|
+
* @param T - Temporal namespace.
|
|
389
|
+
* @param weekStartDay - 0=Sunday (default), 1=Monday, etc.
|
|
390
|
+
* @returns An array of 7 objects with `long`, `short`, and `narrow` name variants.
|
|
391
|
+
*/
|
|
392
|
+
function getWeekdayNames(locale, T, weekStartDay = 0) {
|
|
393
|
+
const refStart = getReferenceWeekStart(T, weekStartDay);
|
|
394
|
+
const daysInWeek = refStart.daysInWeek;
|
|
395
|
+
const names = [];
|
|
396
|
+
for (let i = 0; i < daysInWeek; i++) {
|
|
397
|
+
const date = refStart.add({ days: i });
|
|
398
|
+
names.push({
|
|
399
|
+
long: date.toLocaleString(locale, { weekday: "long" }),
|
|
400
|
+
short: date.toLocaleString(locale, { weekday: "short" }),
|
|
401
|
+
narrow: date.toLocaleString(locale, { weekday: "narrow" })
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
return names;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Computes the ISO 8601 week number for a given date.
|
|
408
|
+
*
|
|
409
|
+
* The ISO week number is determined by the Thursday of the date's ISO week.
|
|
410
|
+
*
|
|
411
|
+
* @param date - The date to compute the week number for.
|
|
412
|
+
* @param T - Temporal namespace.
|
|
413
|
+
*/
|
|
414
|
+
function getISOWeekNumber(date, T) {
|
|
415
|
+
const thursday = date.add({ days: 4 - date.dayOfWeek });
|
|
416
|
+
const jan4 = T.PlainDate.from({
|
|
417
|
+
year: thursday.year,
|
|
418
|
+
month: 1,
|
|
419
|
+
day: 4
|
|
420
|
+
});
|
|
421
|
+
const jan4Thursday = jan4.add({ days: 4 - jan4.dayOfWeek });
|
|
422
|
+
const daysDiff = thursday.since(jan4Thursday, { largestUnit: "days" }).days;
|
|
423
|
+
return Math.round(daysDiff / 7) + 1;
|
|
424
|
+
}
|
|
425
|
+
//#endregion
|
|
426
|
+
//#region src/root-selection.ts
|
|
427
|
+
/** Construct a DateRange (workaround for TS generic conditional type widening). */
|
|
428
|
+
function mkRange(start, end) {
|
|
429
|
+
return {
|
|
430
|
+
start,
|
|
431
|
+
end
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
function isDateRange(v) {
|
|
435
|
+
return v != null && typeof v === "object" && "start" in v && "end" in v;
|
|
436
|
+
}
|
|
437
|
+
function tagRaw(raw, format) {
|
|
438
|
+
if (raw == null) return void 0;
|
|
439
|
+
return {
|
|
440
|
+
format,
|
|
441
|
+
value: raw
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
/** Resolve a `RangeMode` to a concrete "start" | "end" | "reset" action for inside-range clicks. */
|
|
445
|
+
function resolveInsideAction(mode, date, start, end) {
|
|
446
|
+
if (mode === "adjust-start") return "start";
|
|
447
|
+
if (mode === "adjust-end") return "end";
|
|
448
|
+
if (mode === "reset") return "reset";
|
|
449
|
+
const daysFromStart = Math.abs(date.since(start).days);
|
|
450
|
+
const daysFromEnd = Math.abs(date.since(end).days);
|
|
451
|
+
if (daysFromStart < daysFromEnd) return "start";
|
|
452
|
+
if (daysFromEnd < daysFromStart) return "end";
|
|
453
|
+
return mode === "nearest-start" ? "start" : "end";
|
|
454
|
+
}
|
|
455
|
+
/** Compute what `onSelect(date)` should do, without touching React state. */
|
|
456
|
+
function computeSelectionUpdate(opts) {
|
|
457
|
+
const { date, readOnly, isDateDisabled, isMultiple, isRange, committedDates, committedStart, committedEnd, selectedZdt, rangeMode, allowRangeReversal, sortDates, currentSingleFormatted, currentRangeFormatted, currentMultipleFormatted, resolvedFormat, timeZone, T } = opts;
|
|
458
|
+
if (readOnly) return { skip: true };
|
|
459
|
+
if (isDateDisabled(date)) return { skip: true };
|
|
460
|
+
if (isMultiple) {
|
|
461
|
+
const dates = committedDates.filter((d) => d != null);
|
|
462
|
+
const prevArr = currentMultipleFormatted();
|
|
463
|
+
const idx = dates.findIndex((d) => T.PlainDate.compare(d, date) === 0);
|
|
464
|
+
let newDates;
|
|
465
|
+
if (idx >= 0) newDates = [...dates.slice(0, idx), ...dates.slice(idx + 1)];
|
|
466
|
+
else newDates = sortDates([...dates, date]);
|
|
467
|
+
return {
|
|
468
|
+
newDates,
|
|
469
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
470
|
+
onValueChange?.(newDates.map(plainToFormatValue), {
|
|
471
|
+
date,
|
|
472
|
+
previous: prevArr
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
if (isRange && rangeMode === "start-end") {
|
|
478
|
+
const prevRange = currentRangeFormatted();
|
|
479
|
+
const isPending = committedDates.length === 2 && (committedDates[0] === null || committedDates[1] === null);
|
|
480
|
+
const isComplete = committedDates.length === 2 && committedDates[0] != null && committedDates[1] != null;
|
|
481
|
+
if (committedDates.length === 0) return {
|
|
482
|
+
newDates: [date, null],
|
|
483
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
484
|
+
onValueChange?.(mkRange(plainToFormatValue(date), null), {
|
|
485
|
+
date,
|
|
486
|
+
previous: prevRange
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
if (isPending && committedDates[0] != null && committedDates[1] === null) {
|
|
491
|
+
const start = committedDates[0];
|
|
492
|
+
if (T.PlainDate.compare(date, start) === 0) return {
|
|
493
|
+
newDates: [date, date],
|
|
494
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
495
|
+
onValueChange?.(mkRange(plainToFormatValue(date), plainToFormatValue(date)), {
|
|
496
|
+
date,
|
|
497
|
+
previous: prevRange
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
if (T.PlainDate.compare(date, start) >= 0) return {
|
|
502
|
+
newDates: [start, date],
|
|
503
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
504
|
+
onValueChange?.(mkRange(plainToFormatValue(start), plainToFormatValue(date)), {
|
|
505
|
+
date,
|
|
506
|
+
previous: prevRange
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
if (allowRangeReversal) return {
|
|
511
|
+
newDates: [date, start],
|
|
512
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
513
|
+
onValueChange?.(mkRange(plainToFormatValue(date), plainToFormatValue(start)), {
|
|
514
|
+
date,
|
|
515
|
+
previous: prevRange
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
return {
|
|
520
|
+
newDates: [start, start],
|
|
521
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
522
|
+
onValueChange?.(mkRange(plainToFormatValue(start), plainToFormatValue(start)), {
|
|
523
|
+
date,
|
|
524
|
+
previous: prevRange
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
if (isComplete) {
|
|
530
|
+
const start = committedDates[0];
|
|
531
|
+
const end = committedDates[1];
|
|
532
|
+
const isOnStart = T.PlainDate.compare(date, start) === 0;
|
|
533
|
+
const isOnEnd = T.PlainDate.compare(date, end) === 0;
|
|
534
|
+
if (isOnStart || isOnEnd) return {
|
|
535
|
+
newDates: [],
|
|
536
|
+
fireCallback: (onValueChange) => {
|
|
537
|
+
onValueChange?.(null, {
|
|
538
|
+
date,
|
|
539
|
+
previous: prevRange
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
return {
|
|
544
|
+
newDates: [date, null],
|
|
545
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
546
|
+
onValueChange?.(mkRange(plainToFormatValue(date), null), {
|
|
547
|
+
date,
|
|
548
|
+
previous: prevRange
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
return { skip: true };
|
|
554
|
+
}
|
|
555
|
+
if (isRange && committedStart && committedEnd) {
|
|
556
|
+
const prevRange = currentRangeFormatted();
|
|
557
|
+
const isSingleDay = T.PlainDate.compare(committedStart, committedEnd) === 0;
|
|
558
|
+
if (isSingleDay && T.PlainDate.compare(date, committedStart) === 0) return {
|
|
559
|
+
newDates: [],
|
|
560
|
+
fireCallback: (onValueChange) => {
|
|
561
|
+
onValueChange?.(null, {
|
|
562
|
+
date,
|
|
563
|
+
previous: prevRange
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
if (!isSingleDay) {
|
|
568
|
+
const isOnStart = T.PlainDate.compare(date, committedStart) === 0;
|
|
569
|
+
const isOnEnd = T.PlainDate.compare(date, committedEnd) === 0;
|
|
570
|
+
if (isOnStart || isOnEnd) return {
|
|
571
|
+
newDates: [date, date],
|
|
572
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
573
|
+
onValueChange?.(mkRange(plainToFormatValue(date), plainToFormatValue(date)), {
|
|
574
|
+
date,
|
|
575
|
+
previous: prevRange
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
const beforeStart = T.PlainDate.compare(date, committedStart) < 0;
|
|
581
|
+
const afterEnd = T.PlainDate.compare(date, committedEnd) > 0;
|
|
582
|
+
let newDates;
|
|
583
|
+
if (beforeStart) newDates = [date, committedEnd];
|
|
584
|
+
else if (afterEnd) newDates = [committedStart, date];
|
|
585
|
+
else {
|
|
586
|
+
const action = resolveInsideAction(rangeMode, date, committedStart, committedEnd);
|
|
587
|
+
if (action === "start") newDates = [date, committedEnd];
|
|
588
|
+
else if (action === "end") newDates = [committedStart, date];
|
|
589
|
+
else newDates = [date, date];
|
|
590
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
newDates,
|
|
593
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
594
|
+
onValueChange?.(mkRange(plainToFormatValue(newDates[0]), plainToFormatValue(newDates[1])), {
|
|
595
|
+
date,
|
|
596
|
+
previous: prevRange
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
if (isRange) {
|
|
602
|
+
const prevRange = currentRangeFormatted();
|
|
603
|
+
const newDates = [date, date];
|
|
604
|
+
return {
|
|
605
|
+
newDates,
|
|
606
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
607
|
+
onValueChange?.(mkRange(plainToFormatValue(newDates[0]), plainToFormatValue(newDates[1])), {
|
|
608
|
+
date,
|
|
609
|
+
previous: prevRange
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
const prevSingle = currentSingleFormatted();
|
|
615
|
+
if (committedDates.length === 1 && committedDates[0] != null && T.PlainDate.compare(committedDates[0], date) === 0) return {
|
|
616
|
+
newDates: [],
|
|
617
|
+
fireCallback: (onValueChange) => {
|
|
618
|
+
onValueChange?.(null, {
|
|
619
|
+
date,
|
|
620
|
+
previous: prevSingle
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
return {
|
|
625
|
+
newDates: [date],
|
|
626
|
+
fireCallback: (onValueChange) => {
|
|
627
|
+
const prevTime = selectedZdt ? {
|
|
628
|
+
hour: selectedZdt.hour,
|
|
629
|
+
minute: selectedZdt.minute,
|
|
630
|
+
second: selectedZdt.second
|
|
631
|
+
} : {
|
|
632
|
+
hour: 0,
|
|
633
|
+
minute: 0,
|
|
634
|
+
second: 0
|
|
635
|
+
};
|
|
636
|
+
const newTagged = fromZonedDateTime(date.toPlainDateTime(prevTime).toZonedDateTime(timeZone), resolvedFormat, T);
|
|
637
|
+
onValueChange?.(newTagged.value, {
|
|
638
|
+
date,
|
|
639
|
+
previous: prevSingle
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
/** Compute what `setRange(start, end)` should do, without touching React state. */
|
|
645
|
+
function computeSetRangeUpdate(opts) {
|
|
646
|
+
const { start, end, readOnly, isMultiple, isRange, currentRangeFormatted, currentSingleFormatted, resolvedFormat, timeZone, T } = opts;
|
|
647
|
+
if (readOnly || isMultiple) return { skip: true };
|
|
648
|
+
const [lo, hi] = T.PlainDate.compare(start, end) <= 0 ? [start, end] : [end, start];
|
|
649
|
+
const effectiveEnd = isRange ? hi : lo;
|
|
650
|
+
return {
|
|
651
|
+
newDates: isRange ? [lo, effectiveEnd] : [lo],
|
|
652
|
+
fireCallback: (onValueChange, plainToFormatValue) => {
|
|
653
|
+
if (isRange) {
|
|
654
|
+
const prevRange = currentRangeFormatted();
|
|
655
|
+
onValueChange?.(mkRange(plainToFormatValue(lo), plainToFormatValue(effectiveEnd)), {
|
|
656
|
+
date: void 0,
|
|
657
|
+
previous: prevRange
|
|
658
|
+
});
|
|
659
|
+
} else {
|
|
660
|
+
const prevSingle = currentSingleFormatted();
|
|
661
|
+
const tagged = fromZonedDateTime(lo.toPlainDateTime({
|
|
662
|
+
hour: 0,
|
|
663
|
+
minute: 0,
|
|
664
|
+
second: 0
|
|
665
|
+
}).toZonedDateTime(timeZone), resolvedFormat, T);
|
|
666
|
+
onValueChange?.(tagged.value, {
|
|
667
|
+
date: void 0,
|
|
668
|
+
previous: prevSingle
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
/** Truncate dates when selection mode changes. Returns new dates + fires callback. */
|
|
675
|
+
function truncateDatesForMode(opts) {
|
|
676
|
+
const { prev, maxDatesForMode, selectionMode, plainToFormatValue, onValueChange } = opts;
|
|
677
|
+
const clamped = prev.slice(0, maxDatesForMode);
|
|
678
|
+
if (clamped.length === prev.length) return prev;
|
|
679
|
+
const noDate = { date: void 0 };
|
|
680
|
+
const fmt = (d) => d != null ? plainToFormatValue(d) : null;
|
|
681
|
+
if (selectionMode === "single") {
|
|
682
|
+
const prevVal = prev.length > 0 && prev[0] != null ? plainToFormatValue(prev[0]) : null;
|
|
683
|
+
const newVal = clamped.length > 0 && clamped[0] != null ? plainToFormatValue(clamped[0]) : null;
|
|
684
|
+
onValueChange?.(newVal, {
|
|
685
|
+
...noDate,
|
|
686
|
+
previous: prevVal
|
|
687
|
+
});
|
|
688
|
+
} else if (selectionMode === "range") {
|
|
689
|
+
const prevRange = prev.length >= 2 ? mkRange(fmt(prev[0]), fmt(prev[1])) : null;
|
|
690
|
+
let newRange = null;
|
|
691
|
+
if (clamped.length >= 2) newRange = mkRange(fmt(clamped[0]), fmt(clamped[1]));
|
|
692
|
+
else if (clamped.length === 1) {
|
|
693
|
+
const v = fmt(clamped[0]);
|
|
694
|
+
newRange = mkRange(v, v);
|
|
695
|
+
}
|
|
696
|
+
onValueChange?.(newRange, {
|
|
697
|
+
...noDate,
|
|
698
|
+
previous: prevRange
|
|
699
|
+
});
|
|
700
|
+
} else {
|
|
701
|
+
const prevArr = prev.filter((d) => d != null).map(plainToFormatValue);
|
|
702
|
+
const clampedNonNull = clamped.filter((d) => d != null);
|
|
703
|
+
onValueChange?.(clampedNonNull.map(plainToFormatValue), {
|
|
704
|
+
...noDate,
|
|
705
|
+
previous: prevArr
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
return clamped;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Pure utility that computes the preview range for a hovered date, using the
|
|
712
|
+
* same logic as the internal hook. Useful for deriving the preview in an
|
|
713
|
+
* `onHoveredDateChange` handler without needing a controlled `previewRange`.
|
|
714
|
+
*/
|
|
715
|
+
function computePreviewRange(hoveredDate, currentRange, rangeMode, preventRangeReversal = false, T) {
|
|
716
|
+
const Temporal = T ?? globalThis.Temporal;
|
|
717
|
+
if (!Temporal) throw new Error("computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.");
|
|
718
|
+
const rawStart = currentRange?.start != null ? Temporal.PlainDate.from(currentRange.start) : null;
|
|
719
|
+
const rawEnd = currentRange?.end != null ? Temporal.PlainDate.from(currentRange.end) : null;
|
|
720
|
+
const committedDates = currentRange ? rawStart === null && rawEnd !== null ? [rawEnd, null] : [rawStart, rawEnd] : [];
|
|
721
|
+
const committedStart = committedDates[0] ?? void 0;
|
|
722
|
+
const committedEnd = committedDates[1] ?? void 0;
|
|
723
|
+
const noop = () => [];
|
|
724
|
+
const result = computeSelectionUpdate({
|
|
725
|
+
date: hoveredDate,
|
|
726
|
+
readOnly: false,
|
|
727
|
+
isDateDisabled: () => false,
|
|
728
|
+
isMultiple: false,
|
|
729
|
+
isRange: true,
|
|
730
|
+
committedDates,
|
|
731
|
+
committedStart,
|
|
732
|
+
committedEnd,
|
|
733
|
+
selectedZdt: void 0,
|
|
734
|
+
rangeMode,
|
|
735
|
+
allowRangeReversal: !preventRangeReversal,
|
|
736
|
+
sortDates: (d) => [...d].sort((a, b) => Temporal.PlainDate.compare(a, b)),
|
|
737
|
+
currentSingleFormatted: noop,
|
|
738
|
+
currentRangeFormatted: () => currentRange,
|
|
739
|
+
currentMultipleFormatted: noop,
|
|
740
|
+
resolvedFormat: "PlainDate",
|
|
741
|
+
timeZone: "UTC",
|
|
742
|
+
T: Temporal
|
|
743
|
+
});
|
|
744
|
+
if (result.skip || result.newDates.length === 0) return null;
|
|
745
|
+
const start = result.newDates[0];
|
|
746
|
+
const end = result.newDates.length > 1 ? result.newDates[1] : start;
|
|
747
|
+
if (start == null || end == null) return null;
|
|
748
|
+
return mkRange(start, end);
|
|
749
|
+
}
|
|
750
|
+
//#endregion
|
|
751
|
+
//#region src/calendar-provider.tsx
|
|
752
|
+
/**
|
|
753
|
+
* Provides shared calendar state (selection, config, range preview) to all
|
|
754
|
+
* descendant view components. Does NOT manage focus, navigation, or grid data.
|
|
755
|
+
*/
|
|
756
|
+
function CalendarProvider(props) {
|
|
757
|
+
const { format: formatProp, selectionMode: selectionModeProp, min, max, disabled: disabledProp, readOnly: readOnlyProp, isDateDisabled: isDateDisabledProp, timeZone: timeZoneProp, locale: localeProp, temporal: temporalProp, weekStartDay: weekStartDayProp, children } = props;
|
|
758
|
+
const T = resolveTemporal(temporalProp);
|
|
759
|
+
const locale = localeProp ?? "en-US";
|
|
760
|
+
const timeZone = timeZoneProp ?? getSystemTimeZone(T);
|
|
761
|
+
const weekStartDay = weekStartDayProp ?? 0;
|
|
762
|
+
const resolvedFormat = formatProp ?? "PlainDate";
|
|
763
|
+
const selectionMode = selectionModeProp ?? "single";
|
|
764
|
+
const disabled = disabledProp ?? false;
|
|
765
|
+
const readOnly = readOnlyProp ?? false;
|
|
766
|
+
const rangeParams = selectionMode === "range" ? props : void 0;
|
|
767
|
+
const rangeMode = rangeParams?.rangeMode ?? "nearest-end";
|
|
768
|
+
const allowRangeReversal = !(rangeParams?.preventRangeReversal ?? false);
|
|
769
|
+
const previewRangeProp = rangeParams?.previewRange;
|
|
770
|
+
const onHoveredDateChange = rangeParams?.onHoveredDateChange;
|
|
771
|
+
const isRange = selectionMode === "range";
|
|
772
|
+
const isMultiple = selectionMode === "multiple";
|
|
773
|
+
const valueProp = props.value;
|
|
774
|
+
const defaultValueProp = props.defaultValue;
|
|
775
|
+
const onValueChange = props.onValueChange;
|
|
776
|
+
const singleValue = !isRange && !isMultiple ? valueProp : void 0;
|
|
777
|
+
const singleDefault = !isRange && !isMultiple ? defaultValueProp : void 0;
|
|
778
|
+
const rangeValue = isRange ? valueProp === null ? null : valueProp != null && isDateRange(valueProp) ? valueProp : void 0 : void 0;
|
|
779
|
+
const rangeDefault = isRange && defaultValueProp != null && isDateRange(defaultValueProp) ? defaultValueProp : void 0;
|
|
780
|
+
const multipleValue = isMultiple ? valueProp === null ? null : Array.isArray(valueProp) ? valueProp : void 0 : void 0;
|
|
781
|
+
const multipleDefault = isMultiple && Array.isArray(defaultValueProp) ? defaultValueProp : void 0;
|
|
782
|
+
const taggedValue = useMemo(() => tagRaw(singleValue ?? void 0, resolvedFormat), [singleValue, resolvedFormat]);
|
|
783
|
+
const minValue = useMemo(() => {
|
|
784
|
+
if (min == null) return void 0;
|
|
785
|
+
return toZonedDateTime({
|
|
786
|
+
format: resolvedFormat,
|
|
787
|
+
value: min
|
|
788
|
+
}, timeZone, T).toPlainDate();
|
|
789
|
+
}, [
|
|
790
|
+
min,
|
|
791
|
+
resolvedFormat,
|
|
792
|
+
timeZone,
|
|
793
|
+
T
|
|
794
|
+
]);
|
|
795
|
+
const maxValue = useMemo(() => {
|
|
796
|
+
if (max == null) return void 0;
|
|
797
|
+
return toZonedDateTime({
|
|
798
|
+
format: resolvedFormat,
|
|
799
|
+
value: max
|
|
800
|
+
}, timeZone, T).toPlainDate();
|
|
801
|
+
}, [
|
|
802
|
+
max,
|
|
803
|
+
resolvedFormat,
|
|
804
|
+
timeZone,
|
|
805
|
+
T
|
|
806
|
+
]);
|
|
807
|
+
const isDateDisabled = useCallback((date) => {
|
|
808
|
+
if (disabled) return true;
|
|
809
|
+
if (minValue && T.PlainDate.compare(date, minValue) < 0) return true;
|
|
810
|
+
if (maxValue && T.PlainDate.compare(date, maxValue) > 0) return true;
|
|
811
|
+
return isDateDisabledProp?.(date) ?? false;
|
|
812
|
+
}, [
|
|
813
|
+
disabled,
|
|
814
|
+
minValue,
|
|
815
|
+
maxValue,
|
|
816
|
+
isDateDisabledProp,
|
|
817
|
+
T
|
|
818
|
+
]);
|
|
819
|
+
const rawToPlain = useCallback((raw) => {
|
|
820
|
+
return toZonedDateTime({
|
|
821
|
+
format: resolvedFormat,
|
|
822
|
+
value: raw
|
|
823
|
+
}, timeZone, T).toPlainDate();
|
|
824
|
+
}, [
|
|
825
|
+
resolvedFormat,
|
|
826
|
+
timeZone,
|
|
827
|
+
T
|
|
828
|
+
]);
|
|
829
|
+
const sortDates = useCallback((dates) => [...dates].sort((a, b) => T.PlainDate.compare(a, b)), [T]);
|
|
830
|
+
const maxDatesForMode = isMultiple ? Number.POSITIVE_INFINITY : isRange ? 2 : 1;
|
|
831
|
+
const controlledDates = useMemo(() => {
|
|
832
|
+
if (isMultiple) {
|
|
833
|
+
if (multipleValue === null) return [];
|
|
834
|
+
if (multipleValue) return sortDates(multipleValue.map(rawToPlain));
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
if (isRange) {
|
|
838
|
+
if (rangeValue === null) return [];
|
|
839
|
+
if (rangeValue) {
|
|
840
|
+
const start = rangeValue.start != null ? rawToPlain(rangeValue.start) : null;
|
|
841
|
+
const end = rangeValue.end != null ? rawToPlain(rangeValue.end) : null;
|
|
842
|
+
if (start === null && end === null) return [];
|
|
843
|
+
if (start === null && end !== null) return [end, null];
|
|
844
|
+
return [start, end];
|
|
845
|
+
}
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
if (singleValue === null) return [];
|
|
849
|
+
if (singleValue !== void 0) return [rawToPlain(singleValue)];
|
|
850
|
+
}, [
|
|
851
|
+
isMultiple,
|
|
852
|
+
isRange,
|
|
853
|
+
multipleValue,
|
|
854
|
+
rangeValue,
|
|
855
|
+
singleValue,
|
|
856
|
+
rawToPlain,
|
|
857
|
+
sortDates
|
|
858
|
+
]);
|
|
859
|
+
const [internalDates, setInternalDates] = useState(useMemo(() => {
|
|
860
|
+
if (isMultiple) {
|
|
861
|
+
if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
|
|
862
|
+
return [];
|
|
863
|
+
}
|
|
864
|
+
if (rangeDefault) {
|
|
865
|
+
const start = rangeDefault.start != null ? rawToPlain(rangeDefault.start) : null;
|
|
866
|
+
const end = rangeDefault.end != null ? rawToPlain(rangeDefault.end) : null;
|
|
867
|
+
if (start === null && end === null) return [];
|
|
868
|
+
if (start === null && end !== null) return [end, null];
|
|
869
|
+
return [start, end];
|
|
870
|
+
}
|
|
871
|
+
if (singleDefault != null) return [rawToPlain(singleDefault)];
|
|
872
|
+
return [];
|
|
873
|
+
}, [
|
|
874
|
+
isMultiple,
|
|
875
|
+
multipleDefault,
|
|
876
|
+
rangeDefault,
|
|
877
|
+
singleDefault,
|
|
878
|
+
rawToPlain,
|
|
879
|
+
sortDates
|
|
880
|
+
]));
|
|
881
|
+
const isControlled = controlledDates !== void 0;
|
|
882
|
+
const committedDates = isControlled ? controlledDates : internalDates;
|
|
883
|
+
const rangeStart = !isMultiple && committedDates.length > 0 ? committedDates[0] ?? void 0 : void 0;
|
|
884
|
+
const rangeEnd = !isMultiple && committedDates.length >= 2 ? committedDates[1] ?? void 0 : void 0;
|
|
885
|
+
const committedStart = committedDates[0] ?? void 0;
|
|
886
|
+
const committedEnd = committedDates.length > 1 ? committedDates[committedDates.length - 1] ?? void 0 : committedStart;
|
|
887
|
+
const [hoveredDate, setHoveredDateRaw] = useState(void 0);
|
|
888
|
+
const setHoveredDate = useCallback((date) => {
|
|
889
|
+
onHoveredDateChange?.(date);
|
|
890
|
+
setHoveredDateRaw(date);
|
|
891
|
+
}, [onHoveredDateChange]);
|
|
892
|
+
const plainToFormatValue = useCallback((plain) => {
|
|
893
|
+
return fromZonedDateTime(plain.toPlainDateTime({
|
|
894
|
+
hour: 0,
|
|
895
|
+
minute: 0,
|
|
896
|
+
second: 0
|
|
897
|
+
}).toZonedDateTime(timeZone), resolvedFormat, T).value;
|
|
898
|
+
}, [
|
|
899
|
+
resolvedFormat,
|
|
900
|
+
timeZone,
|
|
901
|
+
T
|
|
902
|
+
]);
|
|
903
|
+
const commitSelection = useCallback((result) => {
|
|
904
|
+
if (result.skip) return;
|
|
905
|
+
if (isControlled) setInternalDates((prev) => prev.length === 0 ? prev : []);
|
|
906
|
+
else setInternalDates(result.newDates);
|
|
907
|
+
result.fireCallback(onValueChange, plainToFormatValue);
|
|
908
|
+
}, [
|
|
909
|
+
isControlled,
|
|
910
|
+
onValueChange,
|
|
911
|
+
plainToFormatValue
|
|
912
|
+
]);
|
|
913
|
+
const currentSingleFormatted = useCallback(() => {
|
|
914
|
+
if (committedDates.length === 0) return null;
|
|
915
|
+
const first = committedDates[0];
|
|
916
|
+
if (first == null) return null;
|
|
917
|
+
return plainToFormatValue(first);
|
|
918
|
+
}, [committedDates, plainToFormatValue]);
|
|
919
|
+
const currentRangeFormatted = useCallback(() => {
|
|
920
|
+
if (!committedStart && !committedEnd) return null;
|
|
921
|
+
return {
|
|
922
|
+
start: committedStart ? plainToFormatValue(committedStart) : null,
|
|
923
|
+
end: committedEnd ? plainToFormatValue(committedEnd) : null
|
|
924
|
+
};
|
|
925
|
+
}, [
|
|
926
|
+
committedStart,
|
|
927
|
+
committedEnd,
|
|
928
|
+
plainToFormatValue
|
|
929
|
+
]);
|
|
930
|
+
const currentMultipleFormatted = useCallback(() => committedDates.filter((d) => d != null).map(plainToFormatValue), [committedDates, plainToFormatValue]);
|
|
931
|
+
const selected = useMemo(() => {
|
|
932
|
+
if (taggedValue) return taggedValue;
|
|
933
|
+
if (committedDates.length === 0) return void 0;
|
|
934
|
+
const first = committedDates[0];
|
|
935
|
+
if (!first) return void 0;
|
|
936
|
+
return fromZonedDateTime(first.toPlainDateTime({
|
|
937
|
+
hour: 0,
|
|
938
|
+
minute: 0,
|
|
939
|
+
second: 0
|
|
940
|
+
}).toZonedDateTime(timeZone), resolvedFormat, T);
|
|
941
|
+
}, [
|
|
942
|
+
taggedValue,
|
|
943
|
+
committedDates,
|
|
944
|
+
timeZone,
|
|
945
|
+
resolvedFormat,
|
|
946
|
+
T
|
|
947
|
+
]);
|
|
948
|
+
const selectedZdt = useMemo(() => selectedToZdt(selected, timeZone, T), [
|
|
949
|
+
selected,
|
|
950
|
+
timeZone,
|
|
951
|
+
T
|
|
952
|
+
]);
|
|
953
|
+
const [previewStart, previewEnd] = useMemo(() => {
|
|
954
|
+
if (previewRangeProp !== void 0) {
|
|
955
|
+
if (!previewRangeProp) return [void 0, void 0];
|
|
956
|
+
const rawToPlainLocal = (raw) => {
|
|
957
|
+
return toZonedDateTime({
|
|
958
|
+
format: resolvedFormat,
|
|
959
|
+
value: raw
|
|
960
|
+
}, timeZone, T).toPlainDate();
|
|
961
|
+
};
|
|
962
|
+
return [previewRangeProp.start != null ? rawToPlainLocal(previewRangeProp.start) : void 0, previewRangeProp.end != null ? rawToPlainLocal(previewRangeProp.end) : void 0];
|
|
963
|
+
}
|
|
964
|
+
if (!hoveredDate || !isRange) return [void 0, void 0];
|
|
965
|
+
const result = computeSelectionUpdate({
|
|
966
|
+
date: hoveredDate,
|
|
967
|
+
readOnly,
|
|
968
|
+
isDateDisabled,
|
|
969
|
+
isMultiple: false,
|
|
970
|
+
isRange: true,
|
|
971
|
+
committedDates,
|
|
972
|
+
committedStart,
|
|
973
|
+
committedEnd,
|
|
974
|
+
selectedZdt: void 0,
|
|
975
|
+
rangeMode,
|
|
976
|
+
allowRangeReversal,
|
|
977
|
+
sortDates,
|
|
978
|
+
currentSingleFormatted,
|
|
979
|
+
currentRangeFormatted,
|
|
980
|
+
currentMultipleFormatted,
|
|
981
|
+
resolvedFormat,
|
|
982
|
+
timeZone,
|
|
983
|
+
T
|
|
984
|
+
});
|
|
985
|
+
if (result.skip || result.newDates.length === 0) return [void 0, void 0];
|
|
986
|
+
const ps = result.newDates[0];
|
|
987
|
+
const pe = result.newDates.length > 1 ? result.newDates[1] : ps;
|
|
988
|
+
if (ps == null || pe == null) return [void 0, void 0];
|
|
989
|
+
return [ps, pe];
|
|
990
|
+
}, [
|
|
991
|
+
previewRangeProp,
|
|
992
|
+
hoveredDate,
|
|
993
|
+
isRange,
|
|
994
|
+
readOnly,
|
|
995
|
+
isDateDisabled,
|
|
996
|
+
committedDates,
|
|
997
|
+
committedStart,
|
|
998
|
+
committedEnd,
|
|
999
|
+
rangeMode,
|
|
1000
|
+
allowRangeReversal,
|
|
1001
|
+
sortDates,
|
|
1002
|
+
currentSingleFormatted,
|
|
1003
|
+
currentRangeFormatted,
|
|
1004
|
+
currentMultipleFormatted,
|
|
1005
|
+
resolvedFormat,
|
|
1006
|
+
timeZone,
|
|
1007
|
+
T
|
|
1008
|
+
]);
|
|
1009
|
+
const prevModeRef = useRef(selectionMode);
|
|
1010
|
+
useEffect(() => {
|
|
1011
|
+
if (prevModeRef.current === selectionMode) return;
|
|
1012
|
+
prevModeRef.current = selectionMode;
|
|
1013
|
+
if (isControlled) return;
|
|
1014
|
+
setInternalDates((prev) => truncateDatesForMode({
|
|
1015
|
+
prev,
|
|
1016
|
+
maxDatesForMode,
|
|
1017
|
+
selectionMode,
|
|
1018
|
+
plainToFormatValue,
|
|
1019
|
+
onValueChange
|
|
1020
|
+
}));
|
|
1021
|
+
}, [
|
|
1022
|
+
selectionMode,
|
|
1023
|
+
maxDatesForMode,
|
|
1024
|
+
onValueChange,
|
|
1025
|
+
plainToFormatValue,
|
|
1026
|
+
isControlled
|
|
1027
|
+
]);
|
|
1028
|
+
useEffect(() => {
|
|
1029
|
+
if (isRange || isMultiple) return;
|
|
1030
|
+
const start = committedStart;
|
|
1031
|
+
if (!start) return;
|
|
1032
|
+
if (minValue && T.PlainDate.compare(start, minValue) < 0 || maxValue && T.PlainDate.compare(start, maxValue) > 0) {
|
|
1033
|
+
const prev = currentSingleFormatted();
|
|
1034
|
+
commitSelection({
|
|
1035
|
+
newDates: [],
|
|
1036
|
+
fireCallback: (onValueChange) => {
|
|
1037
|
+
onValueChange?.(null, {
|
|
1038
|
+
date: void 0,
|
|
1039
|
+
previous: prev
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
1044
|
+
}, [
|
|
1045
|
+
minValue,
|
|
1046
|
+
maxValue,
|
|
1047
|
+
committedStart,
|
|
1048
|
+
commitSelection,
|
|
1049
|
+
currentSingleFormatted,
|
|
1050
|
+
T,
|
|
1051
|
+
isRange,
|
|
1052
|
+
isMultiple
|
|
1053
|
+
]);
|
|
1054
|
+
const onSelect = useCallback((date) => {
|
|
1055
|
+
const result = computeSelectionUpdate({
|
|
1056
|
+
date,
|
|
1057
|
+
readOnly,
|
|
1058
|
+
isDateDisabled,
|
|
1059
|
+
isMultiple,
|
|
1060
|
+
isRange,
|
|
1061
|
+
committedDates,
|
|
1062
|
+
committedStart,
|
|
1063
|
+
committedEnd,
|
|
1064
|
+
selectedZdt,
|
|
1065
|
+
rangeMode,
|
|
1066
|
+
allowRangeReversal,
|
|
1067
|
+
sortDates,
|
|
1068
|
+
currentSingleFormatted,
|
|
1069
|
+
currentRangeFormatted,
|
|
1070
|
+
currentMultipleFormatted,
|
|
1071
|
+
resolvedFormat,
|
|
1072
|
+
timeZone,
|
|
1073
|
+
T
|
|
1074
|
+
});
|
|
1075
|
+
if (result.skip) return;
|
|
1076
|
+
commitSelection(result);
|
|
1077
|
+
if (isRange) setHoveredDateRaw(void 0);
|
|
1078
|
+
}, [
|
|
1079
|
+
readOnly,
|
|
1080
|
+
isMultiple,
|
|
1081
|
+
committedDates,
|
|
1082
|
+
selectedZdt,
|
|
1083
|
+
commitSelection,
|
|
1084
|
+
resolvedFormat,
|
|
1085
|
+
isDateDisabled,
|
|
1086
|
+
timeZone,
|
|
1087
|
+
T,
|
|
1088
|
+
isRange,
|
|
1089
|
+
committedStart,
|
|
1090
|
+
committedEnd,
|
|
1091
|
+
sortDates,
|
|
1092
|
+
rangeMode,
|
|
1093
|
+
allowRangeReversal,
|
|
1094
|
+
currentSingleFormatted,
|
|
1095
|
+
currentRangeFormatted,
|
|
1096
|
+
currentMultipleFormatted
|
|
1097
|
+
]);
|
|
1098
|
+
const setRange = useCallback((start, end) => {
|
|
1099
|
+
const result = computeSetRangeUpdate({
|
|
1100
|
+
start,
|
|
1101
|
+
end,
|
|
1102
|
+
readOnly,
|
|
1103
|
+
isMultiple,
|
|
1104
|
+
isRange,
|
|
1105
|
+
currentRangeFormatted,
|
|
1106
|
+
currentSingleFormatted,
|
|
1107
|
+
resolvedFormat,
|
|
1108
|
+
timeZone,
|
|
1109
|
+
T
|
|
1110
|
+
});
|
|
1111
|
+
if (result.skip) return;
|
|
1112
|
+
commitSelection(result);
|
|
1113
|
+
}, [
|
|
1114
|
+
readOnly,
|
|
1115
|
+
isMultiple,
|
|
1116
|
+
isRange,
|
|
1117
|
+
commitSelection,
|
|
1118
|
+
currentRangeFormatted,
|
|
1119
|
+
currentSingleFormatted,
|
|
1120
|
+
timeZone,
|
|
1121
|
+
resolvedFormat,
|
|
1122
|
+
T
|
|
1123
|
+
]);
|
|
1124
|
+
const nonNullDates = useMemo(() => committedDates.filter((d) => d != null), [committedDates]);
|
|
1125
|
+
const stableCtx = useMemo(() => ({
|
|
1126
|
+
onSelect,
|
|
1127
|
+
setRange,
|
|
1128
|
+
selectionMode,
|
|
1129
|
+
disabled,
|
|
1130
|
+
readOnly,
|
|
1131
|
+
isDateDisabled,
|
|
1132
|
+
minValue,
|
|
1133
|
+
maxValue,
|
|
1134
|
+
timeZone,
|
|
1135
|
+
locale,
|
|
1136
|
+
temporal: T,
|
|
1137
|
+
weekStartDay,
|
|
1138
|
+
rangeMode,
|
|
1139
|
+
preventRangeReversal: !allowRangeReversal,
|
|
1140
|
+
valueFormat: resolvedFormat,
|
|
1141
|
+
setHoveredDate
|
|
1142
|
+
}), [
|
|
1143
|
+
onSelect,
|
|
1144
|
+
setRange,
|
|
1145
|
+
selectionMode,
|
|
1146
|
+
disabled,
|
|
1147
|
+
readOnly,
|
|
1148
|
+
isDateDisabled,
|
|
1149
|
+
minValue,
|
|
1150
|
+
maxValue,
|
|
1151
|
+
timeZone,
|
|
1152
|
+
locale,
|
|
1153
|
+
T,
|
|
1154
|
+
weekStartDay,
|
|
1155
|
+
rangeMode,
|
|
1156
|
+
allowRangeReversal,
|
|
1157
|
+
resolvedFormat,
|
|
1158
|
+
setHoveredDate
|
|
1159
|
+
]);
|
|
1160
|
+
const stateCtx = useMemo(() => ({
|
|
1161
|
+
selected,
|
|
1162
|
+
selectedDates: nonNullDates,
|
|
1163
|
+
rangeStart,
|
|
1164
|
+
rangeEnd,
|
|
1165
|
+
hoveredDate,
|
|
1166
|
+
previewStart,
|
|
1167
|
+
previewEnd
|
|
1168
|
+
}), [
|
|
1169
|
+
selected,
|
|
1170
|
+
nonNullDates,
|
|
1171
|
+
rangeStart,
|
|
1172
|
+
rangeEnd,
|
|
1173
|
+
hoveredDate,
|
|
1174
|
+
previewStart,
|
|
1175
|
+
previewEnd
|
|
1176
|
+
]);
|
|
1177
|
+
return /* @__PURE__ */ jsx(CalendarStableContext.Provider, {
|
|
1178
|
+
value: stableCtx,
|
|
1179
|
+
children: /* @__PURE__ */ jsx(CalendarStateContext.Provider, {
|
|
1180
|
+
value: stateCtx,
|
|
1181
|
+
children
|
|
1182
|
+
})
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
//#endregion
|
|
1186
|
+
//#region src/month-view-context.ts
|
|
1187
|
+
/** @internal React context for MonthView stable values (config + callbacks). */
|
|
1188
|
+
const MonthViewStableContext = createContext(null);
|
|
1189
|
+
/** @internal React context for MonthView volatile state. */
|
|
1190
|
+
const MonthViewStateContext = createContext(null);
|
|
1191
|
+
/** Returns the MonthView stable context (config + callbacks). */
|
|
1192
|
+
function useMonthViewStable() {
|
|
1193
|
+
const ctx = useContext(MonthViewStableContext);
|
|
1194
|
+
if (!ctx) throw new Error("useMonthViewStable must be used within MonthView.Root");
|
|
1195
|
+
return ctx;
|
|
1196
|
+
}
|
|
1197
|
+
/** Returns the MonthView state context (volatile grid data). */
|
|
1198
|
+
function useMonthViewState() {
|
|
1199
|
+
const ctx = useContext(MonthViewStateContext);
|
|
1200
|
+
if (!ctx) throw new Error("useMonthViewState must be used within MonthView.Root");
|
|
1201
|
+
return ctx;
|
|
1202
|
+
}
|
|
1203
|
+
//#endregion
|
|
1204
|
+
//#region src/view-context.ts
|
|
1205
|
+
/** @internal React context for the active view (MonthView or WeeksView). */
|
|
1206
|
+
const ViewContext = createContext(null);
|
|
1207
|
+
/** Returns the nearest view context (MonthView or WeeksView). */
|
|
1208
|
+
function useViewContext() {
|
|
1209
|
+
const ctx = useContext(ViewContext);
|
|
1210
|
+
if (!ctx) throw new Error("useViewContext must be used within MonthView or WeeksView");
|
|
1211
|
+
return ctx;
|
|
1212
|
+
}
|
|
1213
|
+
//#endregion
|
|
1214
|
+
//#region src/month-view.tsx
|
|
1215
|
+
function MonthViewRoot(props) {
|
|
1216
|
+
const { numberOfMonths: numberOfMonthsProp, fixedWeeks: fixedWeeksProp, outsideDays: outsideDaysProp, overflowBehavior: overflowBehaviorProp, month: monthProp, defaultMonth: defaultMonthProp, onMonthChange, children } = props;
|
|
1217
|
+
const numberOfMonths = Math.max(1, Math.min(numberOfMonthsProp ?? 1, 12));
|
|
1218
|
+
const fixedWeeks = fixedWeeksProp ?? false;
|
|
1219
|
+
const outsideDays = outsideDaysProp ?? "enabled";
|
|
1220
|
+
const overflowBehavior = overflowBehaviorProp ?? "unbounded";
|
|
1221
|
+
const calStable = useCalendarStable();
|
|
1222
|
+
const calState = useCalendarState();
|
|
1223
|
+
const T = calStable.temporal;
|
|
1224
|
+
const { timeZone, locale, weekStartDay, isDateDisabled } = calStable;
|
|
1225
|
+
const isMonthControlled = monthProp !== void 0;
|
|
1226
|
+
const [internalMonth, setInternalMonth] = useState(() => {
|
|
1227
|
+
if (monthProp) return {
|
|
1228
|
+
year: monthProp.year,
|
|
1229
|
+
month: monthProp.month
|
|
1230
|
+
};
|
|
1231
|
+
if (defaultMonthProp) return {
|
|
1232
|
+
year: defaultMonthProp.year,
|
|
1233
|
+
month: defaultMonthProp.month
|
|
1234
|
+
};
|
|
1235
|
+
if (calState.selected) {
|
|
1236
|
+
const zdt = toZonedDateTime(calState.selected, timeZone, T);
|
|
1237
|
+
return {
|
|
1238
|
+
year: zdt.year,
|
|
1239
|
+
month: zdt.month
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
const now = T.Now.zonedDateTimeISO(timeZone);
|
|
1243
|
+
return {
|
|
1244
|
+
year: now.year,
|
|
1245
|
+
month: now.month
|
|
1246
|
+
};
|
|
1247
|
+
});
|
|
1248
|
+
const currentMonth = isMonthControlled ? {
|
|
1249
|
+
year: monthProp.year,
|
|
1250
|
+
month: monthProp.month
|
|
1251
|
+
} : internalMonth;
|
|
1252
|
+
const [focusedDate, setFocusedDate] = useState(() => {
|
|
1253
|
+
if (calState.selected) return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
|
|
1254
|
+
return T.Now.plainDateISO();
|
|
1255
|
+
});
|
|
1256
|
+
const [gridHasFocus, setGridHasFocus] = useState(false);
|
|
1257
|
+
const gridFocusedRef = useRef(false);
|
|
1258
|
+
const [gridLabelIds, setGridLabelIds] = useState({});
|
|
1259
|
+
const setGridLabelId = useCallback((monthIndex, id) => {
|
|
1260
|
+
setGridLabelIds((prev) => {
|
|
1261
|
+
if (id === void 0) {
|
|
1262
|
+
if (!(monthIndex in prev)) return prev;
|
|
1263
|
+
const next = { ...prev };
|
|
1264
|
+
delete next[monthIndex];
|
|
1265
|
+
return next;
|
|
1266
|
+
}
|
|
1267
|
+
if (prev[monthIndex] === id) return prev;
|
|
1268
|
+
return {
|
|
1269
|
+
...prev,
|
|
1270
|
+
[monthIndex]: id
|
|
1271
|
+
};
|
|
1272
|
+
});
|
|
1273
|
+
}, []);
|
|
1274
|
+
const navigateToMonth = useCallback((targetYear, targetMonth) => {
|
|
1275
|
+
if (isMonthControlled) return;
|
|
1276
|
+
setInternalMonth((prev) => {
|
|
1277
|
+
for (let i = 0; i < numberOfMonths; i++) {
|
|
1278
|
+
const totalMonths = prev.year * 12 + (prev.month - 1) + i;
|
|
1279
|
+
const y = Math.floor(totalMonths / 12);
|
|
1280
|
+
const m = totalMonths % 12 + 1;
|
|
1281
|
+
if (targetYear === y && targetMonth === m) return prev;
|
|
1282
|
+
}
|
|
1283
|
+
return {
|
|
1284
|
+
year: targetYear,
|
|
1285
|
+
month: targetMonth
|
|
1286
|
+
};
|
|
1287
|
+
});
|
|
1288
|
+
}, [isMonthControlled, numberOfMonths]);
|
|
1289
|
+
useEffect(() => {
|
|
1290
|
+
navigateToMonth(focusedDate.year, focusedDate.month);
|
|
1291
|
+
}, [focusedDate, navigateToMonth]);
|
|
1292
|
+
const goNextMonth = useCallback(() => {
|
|
1293
|
+
if (isMonthControlled) {
|
|
1294
|
+
const { year, month, firstDay } = computeAdjacentMonth({
|
|
1295
|
+
year: monthProp.year,
|
|
1296
|
+
month: monthProp.month
|
|
1297
|
+
}, "next", T);
|
|
1298
|
+
setFocusedDate((prev) => focusedDateForMonth(prev, {
|
|
1299
|
+
year,
|
|
1300
|
+
month
|
|
1301
|
+
}, firstDay));
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
1304
|
+
setInternalMonth((m) => {
|
|
1305
|
+
const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
|
|
1306
|
+
setFocusedDate((prev) => focusedDateForMonth(prev, {
|
|
1307
|
+
year,
|
|
1308
|
+
month
|
|
1309
|
+
}, firstDay));
|
|
1310
|
+
return {
|
|
1311
|
+
year,
|
|
1312
|
+
month
|
|
1313
|
+
};
|
|
1314
|
+
});
|
|
1315
|
+
}, [
|
|
1316
|
+
T,
|
|
1317
|
+
isMonthControlled,
|
|
1318
|
+
monthProp
|
|
1319
|
+
]);
|
|
1320
|
+
const goPrevMonth = useCallback(() => {
|
|
1321
|
+
if (isMonthControlled) {
|
|
1322
|
+
const { year, month, firstDay } = computeAdjacentMonth({
|
|
1323
|
+
year: monthProp.year,
|
|
1324
|
+
month: monthProp.month
|
|
1325
|
+
}, "prev", T);
|
|
1326
|
+
setFocusedDate((prev) => focusedDateForMonth(prev, {
|
|
1327
|
+
year,
|
|
1328
|
+
month
|
|
1329
|
+
}, firstDay));
|
|
1330
|
+
return;
|
|
1331
|
+
}
|
|
1332
|
+
setInternalMonth((m) => {
|
|
1333
|
+
const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
|
|
1334
|
+
setFocusedDate((prev) => focusedDateForMonth(prev, {
|
|
1335
|
+
year,
|
|
1336
|
+
month
|
|
1337
|
+
}, firstDay));
|
|
1338
|
+
return {
|
|
1339
|
+
year,
|
|
1340
|
+
month
|
|
1341
|
+
};
|
|
1342
|
+
});
|
|
1343
|
+
}, [
|
|
1344
|
+
T,
|
|
1345
|
+
isMonthControlled,
|
|
1346
|
+
monthProp
|
|
1347
|
+
]);
|
|
1348
|
+
const allMonths = useMemo(() => {
|
|
1349
|
+
const opts = {
|
|
1350
|
+
weekStartDay,
|
|
1351
|
+
fixedWeeks
|
|
1352
|
+
};
|
|
1353
|
+
const result = [];
|
|
1354
|
+
for (let i = 0; i < numberOfMonths; i++) {
|
|
1355
|
+
const totalMonths = currentMonth.year * 12 + (currentMonth.month - 1) + i;
|
|
1356
|
+
const y = Math.floor(totalMonths / 12);
|
|
1357
|
+
const m = totalMonths % 12 + 1;
|
|
1358
|
+
result.push({
|
|
1359
|
+
year: y,
|
|
1360
|
+
month: m,
|
|
1361
|
+
weeks: getMonthWeeks(y, m, T, opts)
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
return result;
|
|
1365
|
+
}, [
|
|
1366
|
+
currentMonth.year,
|
|
1367
|
+
currentMonth.month,
|
|
1368
|
+
T,
|
|
1369
|
+
weekStartDay,
|
|
1370
|
+
fixedWeeks,
|
|
1371
|
+
numberOfMonths
|
|
1372
|
+
]);
|
|
1373
|
+
const weeks = allMonths[0].weeks;
|
|
1374
|
+
const selectedZdt = useMemo(() => selectedToZdt(calState.selected, timeZone, T), [
|
|
1375
|
+
calState.selected,
|
|
1376
|
+
timeZone,
|
|
1377
|
+
T
|
|
1378
|
+
]);
|
|
1379
|
+
const currentDateTime = useMemo(() => T.PlainDateTime.from({
|
|
1380
|
+
year: currentMonth.year,
|
|
1381
|
+
month: currentMonth.month,
|
|
1382
|
+
day: focusedDate.day,
|
|
1383
|
+
hour: selectedZdt?.hour ?? 0,
|
|
1384
|
+
minute: selectedZdt?.minute ?? 0,
|
|
1385
|
+
second: selectedZdt?.second ?? 0
|
|
1386
|
+
}, { overflow: "constrain" }), [
|
|
1387
|
+
currentMonth,
|
|
1388
|
+
focusedDate.day,
|
|
1389
|
+
selectedZdt,
|
|
1390
|
+
T
|
|
1391
|
+
]);
|
|
1392
|
+
const selectedPlain = selectedZdt?.toPlainDate();
|
|
1393
|
+
const isDateDisabledFn = useCallback((date) => {
|
|
1394
|
+
return isDateDisabled?.(date) ?? false;
|
|
1395
|
+
}, [isDateDisabled]);
|
|
1396
|
+
const tabTargetDate = useMemo(() => resolveFocusTarget(focusedDate, selectedPlain, weeks, currentMonth, isDateDisabledFn, T, gridHasFocus), [
|
|
1397
|
+
focusedDate,
|
|
1398
|
+
selectedPlain,
|
|
1399
|
+
weeks,
|
|
1400
|
+
currentMonth,
|
|
1401
|
+
isDateDisabledFn,
|
|
1402
|
+
T,
|
|
1403
|
+
gridHasFocus
|
|
1404
|
+
]);
|
|
1405
|
+
const localeCalendar = useMemo(() => calendarForLocale(locale), [locale]);
|
|
1406
|
+
const viewingYearMonth = useMemo(() => T.PlainYearMonth.from({
|
|
1407
|
+
year: currentMonth.year,
|
|
1408
|
+
month: currentMonth.month,
|
|
1409
|
+
calendar: localeCalendar
|
|
1410
|
+
}), [
|
|
1411
|
+
currentMonth,
|
|
1412
|
+
T,
|
|
1413
|
+
localeCalendar
|
|
1414
|
+
]);
|
|
1415
|
+
const onMonthChangeRef = useRef(onMonthChange);
|
|
1416
|
+
onMonthChangeRef.current = onMonthChange;
|
|
1417
|
+
const mountedRef = useRef(false);
|
|
1418
|
+
useEffect(() => {
|
|
1419
|
+
if (!mountedRef.current) {
|
|
1420
|
+
mountedRef.current = true;
|
|
1421
|
+
return;
|
|
1422
|
+
}
|
|
1423
|
+
onMonthChangeRef.current?.(viewingYearMonth);
|
|
1424
|
+
}, [viewingYearMonth]);
|
|
1425
|
+
const { selected, selectedDates, rangeStart, rangeEnd } = calState;
|
|
1426
|
+
const rootState = useMemo(() => ({
|
|
1427
|
+
hasSelection: selectedDates.length > 0,
|
|
1428
|
+
selected: selected?.value,
|
|
1429
|
+
selectedDates,
|
|
1430
|
+
rangeStart,
|
|
1431
|
+
rangeEnd,
|
|
1432
|
+
focused: focusedDate,
|
|
1433
|
+
viewing: viewingYearMonth,
|
|
1434
|
+
timeZone,
|
|
1435
|
+
locale,
|
|
1436
|
+
readOnly: calStable.readOnly
|
|
1437
|
+
}), [
|
|
1438
|
+
selectedDates,
|
|
1439
|
+
selected,
|
|
1440
|
+
rangeStart,
|
|
1441
|
+
rangeEnd,
|
|
1442
|
+
focusedDate,
|
|
1443
|
+
viewingYearMonth,
|
|
1444
|
+
timeZone,
|
|
1445
|
+
locale,
|
|
1446
|
+
calStable.readOnly
|
|
1447
|
+
]);
|
|
1448
|
+
const stableCtx = useMemo(() => ({
|
|
1449
|
+
numberOfMonths,
|
|
1450
|
+
fixedWeeks,
|
|
1451
|
+
outsideDays,
|
|
1452
|
+
overflowBehavior,
|
|
1453
|
+
goNextMonth,
|
|
1454
|
+
goPrevMonth,
|
|
1455
|
+
setGridLabelId,
|
|
1456
|
+
gridFocusedRef
|
|
1457
|
+
}), [
|
|
1458
|
+
numberOfMonths,
|
|
1459
|
+
fixedWeeks,
|
|
1460
|
+
outsideDays,
|
|
1461
|
+
overflowBehavior,
|
|
1462
|
+
goNextMonth,
|
|
1463
|
+
goPrevMonth,
|
|
1464
|
+
setGridLabelId
|
|
1465
|
+
]);
|
|
1466
|
+
const stateCtx = useMemo(() => ({
|
|
1467
|
+
currentMonth,
|
|
1468
|
+
weeks,
|
|
1469
|
+
allMonths,
|
|
1470
|
+
currentDateTime,
|
|
1471
|
+
gridLabelIds,
|
|
1472
|
+
rootState
|
|
1473
|
+
}), [
|
|
1474
|
+
currentMonth,
|
|
1475
|
+
weeks,
|
|
1476
|
+
allMonths,
|
|
1477
|
+
currentDateTime,
|
|
1478
|
+
gridLabelIds,
|
|
1479
|
+
rootState
|
|
1480
|
+
]);
|
|
1481
|
+
const viewCtx = useMemo(() => ({
|
|
1482
|
+
viewType: "month",
|
|
1483
|
+
focusedDate,
|
|
1484
|
+
setFocusedDate,
|
|
1485
|
+
tabTargetDate,
|
|
1486
|
+
gridHasFocus,
|
|
1487
|
+
setGridHasFocus
|
|
1488
|
+
}), [
|
|
1489
|
+
focusedDate,
|
|
1490
|
+
tabTargetDate,
|
|
1491
|
+
gridHasFocus
|
|
1492
|
+
]);
|
|
1493
|
+
return /* @__PURE__ */ jsx(MonthViewStableContext.Provider, {
|
|
1494
|
+
value: stableCtx,
|
|
1495
|
+
children: /* @__PURE__ */ jsx(MonthViewStateContext.Provider, {
|
|
1496
|
+
value: stateCtx,
|
|
1497
|
+
children: /* @__PURE__ */ jsx(ViewContext.Provider, {
|
|
1498
|
+
value: viewCtx,
|
|
1499
|
+
children
|
|
1500
|
+
})
|
|
1501
|
+
})
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
function MonthView(props) {
|
|
1505
|
+
const { numberOfMonths, fixedWeeks, outsideDays, overflowBehavior, month, defaultMonth, onMonthChange, children, ...calendarProps } = props;
|
|
1506
|
+
return /* @__PURE__ */ jsx(CalendarProvider, {
|
|
1507
|
+
...calendarProps,
|
|
1508
|
+
children: /* @__PURE__ */ jsx(MonthViewRoot, {
|
|
1509
|
+
numberOfMonths,
|
|
1510
|
+
fixedWeeks,
|
|
1511
|
+
outsideDays,
|
|
1512
|
+
overflowBehavior,
|
|
1513
|
+
month,
|
|
1514
|
+
defaultMonth,
|
|
1515
|
+
onMonthChange,
|
|
1516
|
+
children
|
|
1517
|
+
})
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
MonthView.Root = MonthViewRoot;
|
|
1521
|
+
//#endregion
|
|
1522
|
+
//#region src/context.ts
|
|
1523
|
+
/** @internal Provides the days and index of the current week row to child components. */
|
|
1524
|
+
const WeekDataContext = createContext(null);
|
|
1525
|
+
/** @internal Provides the resolved grid orientation to descendant cells. */
|
|
1526
|
+
const GridContext = createContext({ orientation: "horizontal" });
|
|
1527
|
+
/** @internal Provides the date and column index from a `DayCellTemplate` to its child `DayButton`. */
|
|
1528
|
+
const DayCellDataContext = createContext(null);
|
|
1529
|
+
/** @internal Provides per-grid month data to WeekTemplate and DayCellTemplate. */
|
|
1530
|
+
const GridMonthContext = createContext(null);
|
|
1531
|
+
//#endregion
|
|
1532
|
+
//#region src/grid-header.tsx
|
|
1533
|
+
function useGridHeaderCellState(index) {
|
|
1534
|
+
const { locale, temporal: T, weekStartDay } = useCalendarStable();
|
|
1535
|
+
const { rootState } = useMonthViewState();
|
|
1536
|
+
const weekdayNames = useMemo(() => getWeekdayNames(locale, T, weekStartDay), [
|
|
1537
|
+
locale,
|
|
1538
|
+
T,
|
|
1539
|
+
weekStartDay
|
|
1540
|
+
]);
|
|
1541
|
+
const state = useMemo(() => ({
|
|
1542
|
+
root: rootState,
|
|
1543
|
+
dayOfWeek: index,
|
|
1544
|
+
long: weekdayNames[index].long,
|
|
1545
|
+
short: weekdayNames[index].short,
|
|
1546
|
+
narrow: weekdayNames[index].narrow
|
|
1547
|
+
}), [
|
|
1548
|
+
rootState,
|
|
1549
|
+
index,
|
|
1550
|
+
weekdayNames
|
|
1551
|
+
]);
|
|
1552
|
+
return {
|
|
1553
|
+
state,
|
|
1554
|
+
defaultProps: {
|
|
1555
|
+
scope: "col",
|
|
1556
|
+
abbr: state.long,
|
|
1557
|
+
"aria-label": state.long,
|
|
1558
|
+
children: state.narrow
|
|
1559
|
+
}
|
|
1560
|
+
};
|
|
1561
|
+
}
|
|
1562
|
+
const gridHeaderStateAttributesMapping = { root: () => null };
|
|
1563
|
+
const gridHeaderCellStateAttributesMapping = {
|
|
1564
|
+
root: () => null,
|
|
1565
|
+
dayOfWeek: () => null,
|
|
1566
|
+
long: () => null,
|
|
1567
|
+
short: () => null,
|
|
1568
|
+
narrow: () => null
|
|
1569
|
+
};
|
|
1570
|
+
function GridHeaderCellInstanceFn(props, ref) {
|
|
1571
|
+
const { render, index, ...otherProps } = props;
|
|
1572
|
+
const { state, defaultProps } = useGridHeaderCellState(index);
|
|
1573
|
+
return useRender({
|
|
1574
|
+
defaultTagName: "th",
|
|
1575
|
+
render,
|
|
1576
|
+
ref: ref ? [ref] : [],
|
|
1577
|
+
state,
|
|
1578
|
+
stateAttributesMapping: gridHeaderCellStateAttributesMapping,
|
|
1579
|
+
props: mergeProps(defaultProps, otherProps)
|
|
1580
|
+
});
|
|
1581
|
+
}
|
|
1582
|
+
const GridHeaderCellInstance = forwardRef(GridHeaderCellInstanceFn);
|
|
1583
|
+
function GridHeaderCellFn(props, ref) {
|
|
1584
|
+
const { index: indexProp, ...restProps } = props;
|
|
1585
|
+
const { temporal: T } = useCalendarStable();
|
|
1586
|
+
const Instance = GridHeaderCellInstance;
|
|
1587
|
+
if (indexProp != null) return /* @__PURE__ */ jsx(Instance, {
|
|
1588
|
+
...restProps,
|
|
1589
|
+
ref,
|
|
1590
|
+
index: indexProp
|
|
1591
|
+
});
|
|
1592
|
+
const daysInWeek = getReferenceWeekStart(T).daysInWeek;
|
|
1593
|
+
return /* @__PURE__ */ jsx(Fragment, { children: Array.from({ length: daysInWeek }, (_, i) => /* @__PURE__ */ jsx(Instance, {
|
|
1594
|
+
...restProps,
|
|
1595
|
+
ref,
|
|
1596
|
+
index: i
|
|
1597
|
+
}, i)) });
|
|
1598
|
+
}
|
|
1599
|
+
/**
|
|
1600
|
+
* Renders weekday column headers (`<th>`). When no `index` is provided,
|
|
1601
|
+
* renders all 7 days (Sunday–Saturday). Each cell includes `abbr` and
|
|
1602
|
+
* `aria-label` with the full weekday name.
|
|
1603
|
+
*/
|
|
1604
|
+
const GridHeaderCell = forwardRef(GridHeaderCellFn);
|
|
1605
|
+
function GridHeaderFn(props, ref) {
|
|
1606
|
+
const { render, children, ...otherProps } = props;
|
|
1607
|
+
const { rootState } = useMonthViewState();
|
|
1608
|
+
const state = useMemo(() => ({ root: rootState }), [rootState]);
|
|
1609
|
+
return useRender({
|
|
1610
|
+
defaultTagName: "thead",
|
|
1611
|
+
render,
|
|
1612
|
+
ref: ref ? [ref] : [],
|
|
1613
|
+
state,
|
|
1614
|
+
stateAttributesMapping: gridHeaderStateAttributesMapping,
|
|
1615
|
+
props: mergeProps({ children: /* @__PURE__ */ jsx("tr", { children: children ?? /* @__PURE__ */ jsx(GridHeaderCell, {}) }) }, otherProps)
|
|
1616
|
+
});
|
|
1617
|
+
}
|
|
1618
|
+
/** Table header section (`<thead>`) wrapping a row of {@link GridHeaderCell}s. */
|
|
1619
|
+
const GridHeader = forwardRef(GridHeaderFn);
|
|
1620
|
+
//#endregion
|
|
1621
|
+
//#region src/keyboard.ts
|
|
1622
|
+
/**
|
|
1623
|
+
* Computes the target date when jumping by `months` from `focusedDate`.
|
|
1624
|
+
*
|
|
1625
|
+
* Day-of-month is constrained (e.g. Jan 31 + 1 month → Feb 28/29).
|
|
1626
|
+
*/
|
|
1627
|
+
function computeMonthJumpTarget(focusedDate, months, T) {
|
|
1628
|
+
const totalMonths = focusedDate.year * 12 + (focusedDate.month - 1) + months;
|
|
1629
|
+
const targetYear = Math.floor(totalMonths / 12);
|
|
1630
|
+
const targetMonth = totalMonths % 12 + 1;
|
|
1631
|
+
return T.PlainDate.from({
|
|
1632
|
+
year: targetYear,
|
|
1633
|
+
month: targetMonth,
|
|
1634
|
+
day: focusedDate.day
|
|
1635
|
+
}, { overflow: "constrain" });
|
|
1636
|
+
}
|
|
1637
|
+
function clampToBounds(target, focusedDate, minValue, maxValue, T) {
|
|
1638
|
+
let clamped = target;
|
|
1639
|
+
if (minValue && T.PlainDate.compare(clamped, minValue) < 0) clamped = minValue;
|
|
1640
|
+
if (maxValue && T.PlainDate.compare(clamped, maxValue) > 0) clamped = maxValue;
|
|
1641
|
+
if (T.PlainDate.compare(clamped, focusedDate) === 0) return { action: "none" };
|
|
1642
|
+
return {
|
|
1643
|
+
action: "move",
|
|
1644
|
+
date: clamped
|
|
1645
|
+
};
|
|
1646
|
+
}
|
|
1647
|
+
/**
|
|
1648
|
+
* Pure function that maps a keyboard event to a navigation result.
|
|
1649
|
+
*
|
|
1650
|
+
* Handles Arrow keys (±1 day / ±1 week), Home/End (start/end of week),
|
|
1651
|
+
* PageUp/PageDown (±1 month, ±1 year with Shift), and Enter/Space (select).
|
|
1652
|
+
* Results are clamped to `minValue`/`maxValue` bounds.
|
|
1653
|
+
*/
|
|
1654
|
+
function computeNextFocusDate(input) {
|
|
1655
|
+
const { key, shiftKey, focusedDate, minValue, maxValue, disabled, readOnly, isDateDisabled, T, weekStartDay = 0 } = input;
|
|
1656
|
+
if (disabled) return { action: "none" };
|
|
1657
|
+
const daysInWeek = focusedDate.daysInWeek;
|
|
1658
|
+
let nextDate = null;
|
|
1659
|
+
switch (key) {
|
|
1660
|
+
case "ArrowRight":
|
|
1661
|
+
nextDate = focusedDate.add({ days: 1 });
|
|
1662
|
+
break;
|
|
1663
|
+
case "ArrowLeft":
|
|
1664
|
+
nextDate = focusedDate.subtract({ days: 1 });
|
|
1665
|
+
break;
|
|
1666
|
+
case "ArrowDown":
|
|
1667
|
+
nextDate = focusedDate.add({ weeks: 1 });
|
|
1668
|
+
break;
|
|
1669
|
+
case "ArrowUp":
|
|
1670
|
+
nextDate = focusedDate.subtract({ weeks: 1 });
|
|
1671
|
+
break;
|
|
1672
|
+
case "Home": {
|
|
1673
|
+
const adjustedDow = (focusedDate.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek;
|
|
1674
|
+
nextDate = focusedDate.subtract({ days: adjustedDow });
|
|
1675
|
+
break;
|
|
1676
|
+
}
|
|
1677
|
+
case "End": {
|
|
1678
|
+
const adjustedDow = (focusedDate.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek;
|
|
1679
|
+
nextDate = focusedDate.add({ days: daysInWeek - 1 - adjustedDow });
|
|
1680
|
+
break;
|
|
1681
|
+
}
|
|
1682
|
+
case "PageUp": return clampToBounds(computeMonthJumpTarget(focusedDate, shiftKey ? -12 : -1, T), focusedDate, minValue, maxValue, T);
|
|
1683
|
+
case "PageDown": return clampToBounds(computeMonthJumpTarget(focusedDate, shiftKey ? 12 : 1, T), focusedDate, minValue, maxValue, T);
|
|
1684
|
+
case "Enter":
|
|
1685
|
+
case " ":
|
|
1686
|
+
if (readOnly) return { action: "none" };
|
|
1687
|
+
if (!isDateDisabled?.(focusedDate)) return { action: "select" };
|
|
1688
|
+
return { action: "none" };
|
|
1689
|
+
default: return { action: "none" };
|
|
1690
|
+
}
|
|
1691
|
+
if (nextDate) return clampToBounds(nextDate, focusedDate, minValue, maxValue, T);
|
|
1692
|
+
return { action: "none" };
|
|
1693
|
+
}
|
|
1694
|
+
//#endregion
|
|
1695
|
+
//#region src/month-separator.tsx
|
|
1696
|
+
/** Context providing MonthSeparator data to child components. */
|
|
1697
|
+
const MonthSeparatorDataContext = createContext(null);
|
|
1698
|
+
function useMonthSeparatorData() {
|
|
1699
|
+
const ctx = useContext(MonthSeparatorDataContext);
|
|
1700
|
+
if (!ctx) throw new Error("MonthSeparator child components must be used within a MonthSeparatorDataContext.Provider");
|
|
1701
|
+
return ctx;
|
|
1702
|
+
}
|
|
1703
|
+
const stateAttributesMapping = {
|
|
1704
|
+
month: (v) => ({ "data-month": String(v) }),
|
|
1705
|
+
year: (v) => ({ "data-year": String(v) }),
|
|
1706
|
+
firstOfYear: (v) => v ? { "data-first-of-year": "" } : null,
|
|
1707
|
+
firstVisible: (v) => v ? { "data-first-visible": "" } : null,
|
|
1708
|
+
weeksVisibleBefore: () => null,
|
|
1709
|
+
weeksVisibleAfter: () => null,
|
|
1710
|
+
firstDayColumn: (v) => ({ "data-first-day-column": String(v) }),
|
|
1711
|
+
totalColumns: (v) => ({ "data-total-columns": String(v) }),
|
|
1712
|
+
firstDayVisible: (v) => v ? { "data-first-day-visible": "" } : null,
|
|
1713
|
+
fullWeeksVisibleAfter: (v) => ({ "data-full-weeks-visible-after": String(v) }),
|
|
1714
|
+
gridRowStart: (v) => ({ "data-grid-row-start": String(v) })
|
|
1715
|
+
};
|
|
1716
|
+
function MonthSeparatorMonthFn(props, ref) {
|
|
1717
|
+
const { render, locale = "en-US", format = "long", ...otherProps } = props;
|
|
1718
|
+
const data = useMonthSeparatorData();
|
|
1719
|
+
const monthName = useMemo(() => {
|
|
1720
|
+
const date = new Date(data.year, data.month - 1, 1);
|
|
1721
|
+
return new Intl.DateTimeFormat(locale, { month: format }).format(date);
|
|
1722
|
+
}, [
|
|
1723
|
+
data.year,
|
|
1724
|
+
data.month,
|
|
1725
|
+
locale,
|
|
1726
|
+
format
|
|
1727
|
+
]);
|
|
1728
|
+
return useRender({
|
|
1729
|
+
defaultTagName: "span",
|
|
1730
|
+
render,
|
|
1731
|
+
ref: ref ? [ref] : [],
|
|
1732
|
+
state: data,
|
|
1733
|
+
stateAttributesMapping,
|
|
1734
|
+
props: mergeProps({ children: monthName }, otherProps)
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
const MonthSeparatorMonth = forwardRef(MonthSeparatorMonthFn);
|
|
1738
|
+
function MonthSeparatorYearFn(props, ref) {
|
|
1739
|
+
const { render, ...otherProps } = props;
|
|
1740
|
+
const data = useMonthSeparatorData();
|
|
1741
|
+
return useRender({
|
|
1742
|
+
defaultTagName: "span",
|
|
1743
|
+
render,
|
|
1744
|
+
ref: ref ? [ref] : [],
|
|
1745
|
+
state: data,
|
|
1746
|
+
stateAttributesMapping,
|
|
1747
|
+
props: mergeProps({ children: data.year }, otherProps)
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1750
|
+
const MonthSeparatorYear = forwardRef(MonthSeparatorYearFn);
|
|
1751
|
+
function MonthSeparatorWeekCountFn(props, ref) {
|
|
1752
|
+
const { render, ...otherProps } = props;
|
|
1753
|
+
const data = useMonthSeparatorData();
|
|
1754
|
+
return useRender({
|
|
1755
|
+
defaultTagName: "span",
|
|
1756
|
+
render,
|
|
1757
|
+
ref: ref ? [ref] : [],
|
|
1758
|
+
state: data,
|
|
1759
|
+
stateAttributesMapping,
|
|
1760
|
+
props: mergeProps({ children: data.weeksVisibleAfter }, otherProps)
|
|
1761
|
+
});
|
|
1762
|
+
}
|
|
1763
|
+
const MonthSeparatorWeekCount = forwardRef(MonthSeparatorWeekCountFn);
|
|
1764
|
+
function MonthSeparatorRowFn(props, ref) {
|
|
1765
|
+
const { render, ...otherProps } = props;
|
|
1766
|
+
const data = useMonthSeparatorData();
|
|
1767
|
+
const rendered = useRender({
|
|
1768
|
+
defaultTagName: "tr",
|
|
1769
|
+
render,
|
|
1770
|
+
ref: ref ? [ref] : [],
|
|
1771
|
+
state: data,
|
|
1772
|
+
stateAttributesMapping,
|
|
1773
|
+
props: mergeProps({}, otherProps)
|
|
1774
|
+
});
|
|
1775
|
+
return /* @__PURE__ */ jsx(MonthSeparatorDataContext.Provider, {
|
|
1776
|
+
value: data,
|
|
1777
|
+
children: rendered
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
/** Renders the `<tr>` for a month separator. Provides MonthSeparatorDataContext. */
|
|
1781
|
+
const MonthSeparatorRow = forwardRef(MonthSeparatorRowFn);
|
|
1782
|
+
function MonthSeparatorCellFn(props, ref) {
|
|
1783
|
+
const { render, ...otherProps } = props;
|
|
1784
|
+
const data = useMonthSeparatorData();
|
|
1785
|
+
return useRender({
|
|
1786
|
+
defaultTagName: "td",
|
|
1787
|
+
render,
|
|
1788
|
+
ref: ref ? [ref] : [],
|
|
1789
|
+
state: data,
|
|
1790
|
+
stateAttributesMapping,
|
|
1791
|
+
props: mergeProps({ colSpan: 7 }, otherProps)
|
|
1792
|
+
});
|
|
1793
|
+
}
|
|
1794
|
+
/** Renders the `<td>` inside a MonthSeparatorRow. */
|
|
1795
|
+
const MonthSeparatorCell = forwardRef(MonthSeparatorCellFn);
|
|
1796
|
+
function MonthSeparatorFn(props, ref) {
|
|
1797
|
+
const { render, children, ...otherProps } = props;
|
|
1798
|
+
const data = useMonthSeparatorData();
|
|
1799
|
+
const rendered = useRender({
|
|
1800
|
+
defaultTagName: "tr",
|
|
1801
|
+
render,
|
|
1802
|
+
ref: ref ? [ref] : [],
|
|
1803
|
+
state: data,
|
|
1804
|
+
stateAttributesMapping,
|
|
1805
|
+
props: mergeProps({ children: /* @__PURE__ */ jsx("td", {
|
|
1806
|
+
colSpan: 7,
|
|
1807
|
+
children: children ?? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1808
|
+
/* @__PURE__ */ jsx(MonthSeparatorMonth, {}),
|
|
1809
|
+
" ",
|
|
1810
|
+
/* @__PURE__ */ jsx(MonthSeparatorYear, {})
|
|
1811
|
+
] })
|
|
1812
|
+
}) }, otherProps)
|
|
1813
|
+
});
|
|
1814
|
+
return /* @__PURE__ */ jsx(MonthSeparatorDataContext.Provider, {
|
|
1815
|
+
value: data,
|
|
1816
|
+
children: rendered
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Convenience component: `<tr><td colspan="7">children</td></tr>`.
|
|
1821
|
+
* Combines MonthSeparatorRow + MonthSeparatorCell.
|
|
1822
|
+
* Default children: `<Month /> <Year />`.
|
|
1823
|
+
*/
|
|
1824
|
+
const MonthSeparator = forwardRef(MonthSeparatorFn);
|
|
1825
|
+
//#endregion
|
|
1826
|
+
//#region src/weeks-view-context.ts
|
|
1827
|
+
/** @internal React context for WeeksView stable values (config + callbacks). */
|
|
1828
|
+
const WeeksViewStableContext = createContext(null);
|
|
1829
|
+
/** @internal React context for WeeksView volatile state. */
|
|
1830
|
+
const WeeksViewStateContext = createContext(null);
|
|
1831
|
+
/** Returns the WeeksView stable context (config + callbacks). */
|
|
1832
|
+
function useWeeksViewStable() {
|
|
1833
|
+
const ctx = useContext(WeeksViewStableContext);
|
|
1834
|
+
if (!ctx) throw new Error("useWeeksViewStable must be used within WeeksView.Root");
|
|
1835
|
+
return ctx;
|
|
1836
|
+
}
|
|
1837
|
+
/** Returns the WeeksView state context (volatile grid data). */
|
|
1838
|
+
function useWeeksViewState() {
|
|
1839
|
+
const ctx = useContext(WeeksViewStateContext);
|
|
1840
|
+
if (!ctx) throw new Error("useWeeksViewState must be used within WeeksView.Root");
|
|
1841
|
+
return ctx;
|
|
1842
|
+
}
|
|
1843
|
+
//#endregion
|
|
1844
|
+
//#region src/weeks-grid.tsx
|
|
1845
|
+
/**
|
|
1846
|
+
* @internal WeeksGrid — renders continuous week rows for WeeksView.
|
|
1847
|
+
* Not exported from the public API.
|
|
1848
|
+
*/
|
|
1849
|
+
/**
|
|
1850
|
+
* Expand a WeekDescriptor into the 7 PlainDate values for that week.
|
|
1851
|
+
*/
|
|
1852
|
+
function weekDays(week, _T) {
|
|
1853
|
+
const days = [];
|
|
1854
|
+
let d = week.startDate;
|
|
1855
|
+
for (let i = 0; i < 7; i++) {
|
|
1856
|
+
days.push(d);
|
|
1857
|
+
if (i < 6) d = d.add({ days: 1 });
|
|
1858
|
+
}
|
|
1859
|
+
return days;
|
|
1860
|
+
}
|
|
1861
|
+
/**
|
|
1862
|
+
* Check if a React element is a MonthSeparator (or wraps one).
|
|
1863
|
+
*/
|
|
1864
|
+
function isSeparatorElement(child) {
|
|
1865
|
+
if (!isValidElement(child)) return false;
|
|
1866
|
+
return child.type === MonthSeparator || child.type === MonthSeparatorRow || child.type === MonthSeparator;
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* Renders the `<tbody>` contents for a WeeksView grid.
|
|
1870
|
+
* Iterates over the weeks array and provides WeekDataContext per row.
|
|
1871
|
+
* Renders MonthSeparator children at month boundaries with proper context.
|
|
1872
|
+
*/
|
|
1873
|
+
/**
|
|
1874
|
+
* Compute the 0-based column index of a date within a week row.
|
|
1875
|
+
* If hasWeekNumbers, column 0 is the week number column, and day columns are 1-7.
|
|
1876
|
+
* Otherwise day columns are 0-6.
|
|
1877
|
+
*/
|
|
1878
|
+
function dayColumnIndex(date, weekStartDay, hasWeekNumbers) {
|
|
1879
|
+
const col = (date.dayOfWeek % 7 - weekStartDay + 7) % 7;
|
|
1880
|
+
return hasWeekNumbers ? col + 1 : col;
|
|
1881
|
+
}
|
|
1882
|
+
function WeeksGrid(props) {
|
|
1883
|
+
const { children, className, style, hasWeekNumbers = false } = props;
|
|
1884
|
+
const { temporal: T, weekStartDay } = useCalendarStable();
|
|
1885
|
+
const { weeks } = useWeeksViewState();
|
|
1886
|
+
const separatorTemplate = [];
|
|
1887
|
+
const weekRowChildren = [];
|
|
1888
|
+
Children.forEach(children, (child) => {
|
|
1889
|
+
if (isSeparatorElement(child)) separatorTemplate.push(child);
|
|
1890
|
+
else weekRowChildren.push(child);
|
|
1891
|
+
});
|
|
1892
|
+
const weekDaysArrays = useMemo(() => weeks.map((w) => weekDays(w, T)), [weeks, T]);
|
|
1893
|
+
const totalColumns = hasWeekNumbers ? 8 : 7;
|
|
1894
|
+
const monthBoundaries = useMemo(() => {
|
|
1895
|
+
const boundaries = [];
|
|
1896
|
+
const seenMonths = /* @__PURE__ */ new Set();
|
|
1897
|
+
for (let i = 0; i < weeks.length; i++) {
|
|
1898
|
+
const days = weekDaysArrays[i];
|
|
1899
|
+
const isFirstWeek = i === 0;
|
|
1900
|
+
const monthsInWeek = [];
|
|
1901
|
+
for (const day of days) {
|
|
1902
|
+
const monthKey = `${day.year}-${day.month}`;
|
|
1903
|
+
if (!seenMonths.has(monthKey) && !monthsInWeek.some((m) => m.key === monthKey)) monthsInWeek.push({
|
|
1904
|
+
month: day.month,
|
|
1905
|
+
year: day.year,
|
|
1906
|
+
key: monthKey
|
|
1907
|
+
});
|
|
1908
|
+
}
|
|
1909
|
+
const monthsToShow = isFirstWeek && monthsInWeek.length > 1 ? monthsInWeek.slice(1) : monthsInWeek;
|
|
1910
|
+
for (const m of monthsInWeek) seenMonths.add(m.key);
|
|
1911
|
+
for (const { month: newMonth, year: newYear } of monthsToShow) {
|
|
1912
|
+
const firstOfMonth = T.PlainDate.from({
|
|
1913
|
+
year: newYear,
|
|
1914
|
+
month: newMonth,
|
|
1915
|
+
day: 1
|
|
1916
|
+
});
|
|
1917
|
+
const firstDayCol = dayColumnIndex(firstOfMonth, weekStartDay, hasWeekNumbers);
|
|
1918
|
+
const windowStart = weekDaysArrays[0]?.[0];
|
|
1919
|
+
const windowEnd = weekDaysArrays[weekDaysArrays.length - 1]?.[6];
|
|
1920
|
+
const firstDayVisible = windowStart && windowEnd ? T.PlainDate.compare(firstOfMonth, windowStart) >= 0 && T.PlainDate.compare(firstOfMonth, windowEnd) <= 0 : false;
|
|
1921
|
+
let weeksVisibleBefore = 0;
|
|
1922
|
+
for (let j = i - 1; j >= 0; j--) if (!weekDaysArrays[j].some((d) => d.month === newMonth && d.year === newYear)) weeksVisibleBefore++;
|
|
1923
|
+
else break;
|
|
1924
|
+
let weeksVisibleAfter = 0;
|
|
1925
|
+
for (let j = i; j < weeks.length; j++) if (weekDaysArrays[j].some((d) => d.month === newMonth && d.year === newYear)) weeksVisibleAfter++;
|
|
1926
|
+
else break;
|
|
1927
|
+
const isNewYear = boundaries.length > 0 ? boundaries[boundaries.length - 1].data.year !== newYear : newMonth === 1;
|
|
1928
|
+
boundaries.push({
|
|
1929
|
+
index: i,
|
|
1930
|
+
data: {
|
|
1931
|
+
month: newMonth,
|
|
1932
|
+
year: newYear,
|
|
1933
|
+
firstOfYear: isFirstWeek ? newMonth === 1 : isNewYear,
|
|
1934
|
+
firstVisible: boundaries.length === 0,
|
|
1935
|
+
weeksVisibleBefore,
|
|
1936
|
+
weeksVisibleAfter,
|
|
1937
|
+
firstDayColumn: firstDayCol,
|
|
1938
|
+
totalColumns,
|
|
1939
|
+
firstDayVisible,
|
|
1940
|
+
fullWeeksVisibleAfter: 0,
|
|
1941
|
+
gridRowStart: 0
|
|
1942
|
+
}
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
for (let b = 0; b < boundaries.length; b++) {
|
|
1947
|
+
const current = boundaries[b];
|
|
1948
|
+
const nextIndex = b + 1 < boundaries.length ? boundaries[b + 1].index : weeks.length;
|
|
1949
|
+
current.data.fullWeeksVisibleAfter = nextIndex - current.index;
|
|
1950
|
+
current.data.gridRowStart = current.index + 2;
|
|
1951
|
+
}
|
|
1952
|
+
return boundaries;
|
|
1953
|
+
}, [
|
|
1954
|
+
weeks,
|
|
1955
|
+
weekDaysArrays,
|
|
1956
|
+
weekStartDay,
|
|
1957
|
+
hasWeekNumbers,
|
|
1958
|
+
totalColumns,
|
|
1959
|
+
T
|
|
1960
|
+
]);
|
|
1961
|
+
const boundariesAtIndex = useMemo(() => {
|
|
1962
|
+
const map = /* @__PURE__ */ new Map();
|
|
1963
|
+
for (const b of monthBoundaries) {
|
|
1964
|
+
const existing = map.get(b.index);
|
|
1965
|
+
if (existing) existing.push(b.data);
|
|
1966
|
+
else map.set(b.index, [b.data]);
|
|
1967
|
+
}
|
|
1968
|
+
return map;
|
|
1969
|
+
}, [monthBoundaries]);
|
|
1970
|
+
const rows = [];
|
|
1971
|
+
for (let i = 0; i < weeks.length; i++) {
|
|
1972
|
+
const week = weeks[i];
|
|
1973
|
+
const days = weekDaysArrays[i];
|
|
1974
|
+
const separators = boundariesAtIndex.get(i);
|
|
1975
|
+
if (separators && separatorTemplate.length > 0) for (const separatorData of separators) rows.push(/* @__PURE__ */ jsx(MonthSeparatorDataContext.Provider, {
|
|
1976
|
+
value: separatorData,
|
|
1977
|
+
children: separatorTemplate
|
|
1978
|
+
}, `sep-${separatorData.year}-${separatorData.month}`));
|
|
1979
|
+
const weekGridRow = i + 2;
|
|
1980
|
+
rows.push(/* @__PURE__ */ jsx(WeekDataContext.Provider, {
|
|
1981
|
+
value: {
|
|
1982
|
+
days,
|
|
1983
|
+
weekIndex: week.weekIndex,
|
|
1984
|
+
gridRowIndex: weekGridRow
|
|
1985
|
+
},
|
|
1986
|
+
children: /* @__PURE__ */ jsx(GridMonthContext.Provider, {
|
|
1987
|
+
value: {
|
|
1988
|
+
weeks: [days],
|
|
1989
|
+
year: week.year,
|
|
1990
|
+
month: week.month
|
|
1991
|
+
},
|
|
1992
|
+
children: weekRowChildren
|
|
1993
|
+
})
|
|
1994
|
+
}, week.startDate.toString()));
|
|
1995
|
+
}
|
|
1996
|
+
return /* @__PURE__ */ jsx("tbody", {
|
|
1997
|
+
className,
|
|
1998
|
+
style,
|
|
1999
|
+
children: rows
|
|
2000
|
+
});
|
|
2001
|
+
}
|
|
2002
|
+
//#endregion
|
|
2003
|
+
//#region src/weeks-keyboard.ts
|
|
2004
|
+
const NONE = {
|
|
2005
|
+
action: "none",
|
|
2006
|
+
windowShift: 0,
|
|
2007
|
+
followFocus: false
|
|
2008
|
+
};
|
|
2009
|
+
/**
|
|
2010
|
+
* Pure function that maps a keyboard event to a weeks-view navigation result.
|
|
2011
|
+
*
|
|
2012
|
+
* Handles Arrow keys (±1 day / ±1 week), Home/End (first/last day of window),
|
|
2013
|
+
* PageUp/PageDown (±weekCount weeks, ±1 year with Shift), and Enter/Space (select).
|
|
2014
|
+
* Results are clamped to `minValue`/`maxValue` bounds.
|
|
2015
|
+
*/
|
|
2016
|
+
function computeWeeksKeyNav(input) {
|
|
2017
|
+
const { key, shiftKey, focusedDate, windowStart, weekCount, minValue, maxValue, disabled, readOnly, isDateDisabled, scrollBy, T } = input;
|
|
2018
|
+
if (disabled) return NONE;
|
|
2019
|
+
const windowEnd = windowStart.add({ weeks: weekCount }).subtract({ days: 1 });
|
|
2020
|
+
function isOutsideWindow(date) {
|
|
2021
|
+
return T.PlainDate.compare(date, windowStart) < 0 || T.PlainDate.compare(date, windowEnd) > 0;
|
|
2022
|
+
}
|
|
2023
|
+
function windowShiftFor(target) {
|
|
2024
|
+
if (T.PlainDate.compare(target, windowEnd) > 0) return scrollBy === "row" ? 1 : weekCount;
|
|
2025
|
+
if (T.PlainDate.compare(target, windowStart) < 0) return scrollBy === "row" ? -1 : -weekCount;
|
|
2026
|
+
return 0;
|
|
2027
|
+
}
|
|
2028
|
+
function clampToMove(target, windowShift, followFocus) {
|
|
2029
|
+
let clamped = target;
|
|
2030
|
+
if (minValue && T.PlainDate.compare(clamped, minValue) < 0) clamped = minValue;
|
|
2031
|
+
if (maxValue && T.PlainDate.compare(clamped, maxValue) > 0) clamped = maxValue;
|
|
2032
|
+
if (T.PlainDate.compare(clamped, focusedDate) === 0) return NONE;
|
|
2033
|
+
return {
|
|
2034
|
+
action: "move",
|
|
2035
|
+
date: clamped,
|
|
2036
|
+
windowShift,
|
|
2037
|
+
followFocus
|
|
2038
|
+
};
|
|
2039
|
+
}
|
|
2040
|
+
switch (key) {
|
|
2041
|
+
case "ArrowRight": {
|
|
2042
|
+
const target = focusedDate.add({ days: 1 });
|
|
2043
|
+
return clampToMove(target, isOutsideWindow(target) ? windowShiftFor(target) : 0, false);
|
|
2044
|
+
}
|
|
2045
|
+
case "ArrowLeft": {
|
|
2046
|
+
const target = focusedDate.subtract({ days: 1 });
|
|
2047
|
+
return clampToMove(target, isOutsideWindow(target) ? windowShiftFor(target) : 0, false);
|
|
2048
|
+
}
|
|
2049
|
+
case "ArrowDown": {
|
|
2050
|
+
const target = focusedDate.add({ weeks: 1 });
|
|
2051
|
+
return clampToMove(target, isOutsideWindow(target) ? windowShiftFor(target) : 0, false);
|
|
2052
|
+
}
|
|
2053
|
+
case "ArrowUp": {
|
|
2054
|
+
const target = focusedDate.subtract({ weeks: 1 });
|
|
2055
|
+
return clampToMove(target, isOutsideWindow(target) ? windowShiftFor(target) : 0, false);
|
|
2056
|
+
}
|
|
2057
|
+
case "Home": return clampToMove(windowStart, 0, false);
|
|
2058
|
+
case "End": return clampToMove(windowEnd, 0, false);
|
|
2059
|
+
case "PageDown":
|
|
2060
|
+
if (shiftKey) return clampToMove(focusedDate.add({ years: 1 }), 0, true);
|
|
2061
|
+
return clampToMove(focusedDate.add({ weeks: weekCount }), weekCount, false);
|
|
2062
|
+
case "PageUp":
|
|
2063
|
+
if (shiftKey) return clampToMove(focusedDate.subtract({ years: 1 }), 0, true);
|
|
2064
|
+
return clampToMove(focusedDate.subtract({ weeks: weekCount }), -weekCount, false);
|
|
2065
|
+
case "Enter":
|
|
2066
|
+
case " ":
|
|
2067
|
+
if (readOnly) return NONE;
|
|
2068
|
+
if (!isDateDisabled?.(focusedDate)) return {
|
|
2069
|
+
action: "select",
|
|
2070
|
+
windowShift: 0,
|
|
2071
|
+
followFocus: false
|
|
2072
|
+
};
|
|
2073
|
+
return NONE;
|
|
2074
|
+
default: return NONE;
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
//#endregion
|
|
2078
|
+
//#region src/day-cell.tsx
|
|
2079
|
+
/** Computes derived day cell state from context values. Pure function, no hooks. */
|
|
2080
|
+
function computeDayCellState(date, orientation, columnIndex, rootState, selectedDates, currentDateTime, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd) {
|
|
2081
|
+
const isCurrentMonth = date.year === currentDateTime.year && date.month === currentDateTime.month;
|
|
2082
|
+
if (!isCurrentMonth && outsideDays === "hidden") return {
|
|
2083
|
+
root: rootState,
|
|
2084
|
+
date,
|
|
2085
|
+
columnIndex,
|
|
2086
|
+
orientation,
|
|
2087
|
+
outsideMonth: true,
|
|
2088
|
+
hidden: true,
|
|
2089
|
+
selected: false,
|
|
2090
|
+
today: false,
|
|
2091
|
+
disabled: true,
|
|
2092
|
+
focused: false,
|
|
2093
|
+
rangeStart: false,
|
|
2094
|
+
rangeEnd: false,
|
|
2095
|
+
rangeBoundary: false,
|
|
2096
|
+
inRange: false,
|
|
2097
|
+
rangeIndex: false,
|
|
2098
|
+
rangeLength: false,
|
|
2099
|
+
rangeHasStart: false,
|
|
2100
|
+
rangeHasEnd: false,
|
|
2101
|
+
rangePreviewStart: false,
|
|
2102
|
+
rangePreviewEnd: false,
|
|
2103
|
+
rangePreviewBoundary: false,
|
|
2104
|
+
rangePreviewInRange: false,
|
|
2105
|
+
rangePreviewIndex: false,
|
|
2106
|
+
rangePreviewLength: false,
|
|
2107
|
+
rangePreviewHasStart: false,
|
|
2108
|
+
rangePreviewHasEnd: false,
|
|
2109
|
+
isTabTarget: false
|
|
2110
|
+
};
|
|
2111
|
+
const today = T.Now.plainDateISO();
|
|
2112
|
+
const isToday = T.PlainDate.compare(date, today) === 0;
|
|
2113
|
+
const isRange = selectionMode === "range";
|
|
2114
|
+
let isRangeStart = false;
|
|
2115
|
+
let isRangeEnd = false;
|
|
2116
|
+
let isInRangeDay = false;
|
|
2117
|
+
let rangeIdx = false;
|
|
2118
|
+
let rangeLen = false;
|
|
2119
|
+
let isPreviewRangeStart = false;
|
|
2120
|
+
let isPreviewRangeEnd = false;
|
|
2121
|
+
let isInPreviewRange = false;
|
|
2122
|
+
let previewIdx = false;
|
|
2123
|
+
let previewLen = false;
|
|
2124
|
+
if (isRange) {
|
|
2125
|
+
isRangeStart = rangeStart ? T.PlainDate.compare(date, rangeStart) === 0 : false;
|
|
2126
|
+
isRangeEnd = rangeEnd ? T.PlainDate.compare(date, rangeEnd) === 0 : false;
|
|
2127
|
+
isInRangeDay = isInRange(date, rangeStart, rangeEnd, T) !== false;
|
|
2128
|
+
const effectiveStart = rangeStart ?? rangeEnd;
|
|
2129
|
+
const effectiveEnd = rangeEnd ?? rangeStart;
|
|
2130
|
+
rangeIdx = isInRangeDay && effectiveStart ? date.since(effectiveStart).days : false;
|
|
2131
|
+
rangeLen = isInRangeDay && effectiveStart && effectiveEnd ? effectiveEnd.since(effectiveStart).days + 1 : false;
|
|
2132
|
+
isPreviewRangeStart = previewStart ? T.PlainDate.compare(date, previewStart) === 0 : false;
|
|
2133
|
+
isPreviewRangeEnd = previewEnd ? T.PlainDate.compare(date, previewEnd) === 0 : false;
|
|
2134
|
+
isInPreviewRange = previewStart && previewEnd ? isInRange(date, previewStart, previewEnd, T) !== false : false;
|
|
2135
|
+
previewIdx = isInPreviewRange && previewStart ? date.since(previewStart).days : false;
|
|
2136
|
+
previewLen = isInPreviewRange && previewStart && previewEnd ? previewEnd.since(previewStart).days + 1 : false;
|
|
2137
|
+
}
|
|
2138
|
+
const outsideNonInteractive = !isCurrentMonth && outsideDays !== "enabled";
|
|
2139
|
+
const suppressRange = !isCurrentMonth && (outsideDays === "disabled" || outsideDays === "hidden");
|
|
2140
|
+
if (outsideNonInteractive) return {
|
|
2141
|
+
root: rootState,
|
|
2142
|
+
date,
|
|
2143
|
+
columnIndex,
|
|
2144
|
+
orientation,
|
|
2145
|
+
outsideMonth: true,
|
|
2146
|
+
hidden: false,
|
|
2147
|
+
selected: false,
|
|
2148
|
+
today: isToday,
|
|
2149
|
+
disabled: true,
|
|
2150
|
+
focused: false,
|
|
2151
|
+
rangeStart: suppressRange ? false : isRangeStart,
|
|
2152
|
+
rangeEnd: suppressRange ? false : isRangeEnd,
|
|
2153
|
+
rangeBoundary: suppressRange ? false : isRangeStart || isRangeEnd,
|
|
2154
|
+
inRange: suppressRange ? false : isInRangeDay,
|
|
2155
|
+
rangeIndex: suppressRange ? false : rangeIdx,
|
|
2156
|
+
rangeLength: suppressRange ? false : rangeLen,
|
|
2157
|
+
rangeHasStart: suppressRange ? false : isInRangeDay && rangeStart !== void 0,
|
|
2158
|
+
rangeHasEnd: suppressRange ? false : isInRangeDay && rangeEnd !== void 0,
|
|
2159
|
+
rangePreviewStart: suppressRange ? false : isPreviewRangeStart,
|
|
2160
|
+
rangePreviewEnd: suppressRange ? false : isPreviewRangeEnd,
|
|
2161
|
+
rangePreviewBoundary: suppressRange ? false : isPreviewRangeStart || isPreviewRangeEnd,
|
|
2162
|
+
rangePreviewInRange: suppressRange ? false : isInPreviewRange,
|
|
2163
|
+
rangePreviewIndex: suppressRange ? false : previewIdx,
|
|
2164
|
+
rangePreviewLength: suppressRange ? false : previewLen,
|
|
2165
|
+
rangePreviewHasStart: suppressRange ? false : isInPreviewRange && previewStart !== void 0,
|
|
2166
|
+
rangePreviewHasEnd: suppressRange ? false : isInPreviewRange && previewEnd !== void 0,
|
|
2167
|
+
isTabTarget: false
|
|
2168
|
+
};
|
|
2169
|
+
const isSelected = selectionMode !== "range" ? selectedDates.some((d) => T.PlainDate.compare(d, date) === 0) : false;
|
|
2170
|
+
const isDisabled = disabled || (isDateDisabled?.(date) ?? false);
|
|
2171
|
+
const isFocused = T.PlainDate.compare(date, focusedDate) === 0;
|
|
2172
|
+
const isTabTarget = T.PlainDate.compare(date, tabTargetDate) === 0;
|
|
2173
|
+
return {
|
|
2174
|
+
root: rootState,
|
|
2175
|
+
date,
|
|
2176
|
+
columnIndex,
|
|
2177
|
+
orientation,
|
|
2178
|
+
selected: isSelected,
|
|
2179
|
+
today: isToday,
|
|
2180
|
+
disabled: isDisabled,
|
|
2181
|
+
outsideMonth: !isCurrentMonth,
|
|
2182
|
+
hidden: false,
|
|
2183
|
+
focused: isFocused,
|
|
2184
|
+
rangeStart: isRangeStart,
|
|
2185
|
+
rangeEnd: isRangeEnd,
|
|
2186
|
+
rangeBoundary: isRangeStart || isRangeEnd,
|
|
2187
|
+
inRange: isInRangeDay,
|
|
2188
|
+
rangeIndex: rangeIdx,
|
|
2189
|
+
rangeLength: rangeLen,
|
|
2190
|
+
rangeHasStart: isInRangeDay && rangeStart !== void 0,
|
|
2191
|
+
rangeHasEnd: isInRangeDay && rangeEnd !== void 0,
|
|
2192
|
+
rangePreviewStart: isPreviewRangeStart,
|
|
2193
|
+
rangePreviewEnd: isPreviewRangeEnd,
|
|
2194
|
+
rangePreviewBoundary: isPreviewRangeStart || isPreviewRangeEnd,
|
|
2195
|
+
rangePreviewInRange: isInPreviewRange,
|
|
2196
|
+
rangePreviewIndex: previewIdx,
|
|
2197
|
+
rangePreviewLength: previewLen,
|
|
2198
|
+
rangePreviewHasStart: isInPreviewRange && previewStart !== void 0,
|
|
2199
|
+
rangePreviewHasEnd: isInPreviewRange && previewEnd !== void 0,
|
|
2200
|
+
isTabTarget
|
|
2201
|
+
};
|
|
2202
|
+
}
|
|
2203
|
+
const dayStateAttributesMapping = {
|
|
2204
|
+
root: () => null,
|
|
2205
|
+
date: (v) => v ? { "data-date": v.toString() } : null,
|
|
2206
|
+
columnIndex: () => null,
|
|
2207
|
+
orientation: (v) => v ? { "data-orientation": v } : null,
|
|
2208
|
+
selected: (v) => v ? { "data-selected": "" } : null,
|
|
2209
|
+
today: (v) => v ? { "data-today": "" } : null,
|
|
2210
|
+
disabled: (v) => v ? { "data-disabled": "" } : null,
|
|
2211
|
+
outsideMonth: (v) => v ? { "data-outside-month": "" } : null,
|
|
2212
|
+
hidden: (v) => v ? { "data-hidden": "" } : null,
|
|
2213
|
+
focused: (v) => v ? { "data-focused": "" } : null,
|
|
2214
|
+
rangeStart: (v) => v ? { "data-range-start": "" } : null,
|
|
2215
|
+
rangeEnd: (v) => v ? { "data-range-end": "" } : null,
|
|
2216
|
+
rangeBoundary: (v) => v ? { "data-range-boundary": "" } : null,
|
|
2217
|
+
inRange: (v) => v ? { "data-in-range": "" } : null,
|
|
2218
|
+
rangeIndex: (v) => v !== false ? { "data-range-index": String(v) } : null,
|
|
2219
|
+
rangeLength: (v) => v !== false ? { "data-range-length": String(v) } : null,
|
|
2220
|
+
rangeHasStart: (v) => v ? { "data-range-has-start": "" } : null,
|
|
2221
|
+
rangeHasEnd: (v) => v ? { "data-range-has-end": "" } : null,
|
|
2222
|
+
rangePreviewStart: (v) => v ? { "data-range-preview-start": "" } : null,
|
|
2223
|
+
rangePreviewEnd: (v) => v ? { "data-range-preview-end": "" } : null,
|
|
2224
|
+
rangePreviewBoundary: (v) => v ? { "data-range-preview-boundary": "" } : null,
|
|
2225
|
+
rangePreviewInRange: (v) => v ? { "data-range-preview-in-range": "" } : null,
|
|
2226
|
+
rangePreviewIndex: (v) => v !== false ? { "data-range-preview-index": String(v) } : null,
|
|
2227
|
+
rangePreviewLength: (v) => v !== false ? { "data-range-preview-length": String(v) } : null,
|
|
2228
|
+
rangePreviewHasStart: (v) => v ? { "data-range-preview-has-start": "" } : null,
|
|
2229
|
+
rangePreviewHasEnd: (v) => v ? { "data-range-preview-has-end": "" } : null
|
|
2230
|
+
};
|
|
2231
|
+
function DayCellInstanceFn(props) {
|
|
2232
|
+
const { render, date, columnIndex, children, _derivedState, ...otherProps } = props;
|
|
2233
|
+
const state = _derivedState;
|
|
2234
|
+
const cell = useRender({
|
|
2235
|
+
defaultTagName: "td",
|
|
2236
|
+
render,
|
|
2237
|
+
ref: [],
|
|
2238
|
+
state,
|
|
2239
|
+
stateAttributesMapping: dayStateAttributesMapping,
|
|
2240
|
+
props: mergeProps(state.hidden ? {
|
|
2241
|
+
role: "gridcell",
|
|
2242
|
+
"aria-hidden": true,
|
|
2243
|
+
children: null
|
|
2244
|
+
} : {
|
|
2245
|
+
role: "gridcell",
|
|
2246
|
+
"aria-selected": state.selected || void 0,
|
|
2247
|
+
"aria-disabled": state.disabled || void 0,
|
|
2248
|
+
children: children ?? /* @__PURE__ */ jsx(DayButton, { _derivedState })
|
|
2249
|
+
}, otherProps)
|
|
2250
|
+
});
|
|
2251
|
+
const outsideDisabled = state.hidden || state.outsideMonth && state.disabled;
|
|
2252
|
+
return /* @__PURE__ */ jsx(DayCellDataContext.Provider, {
|
|
2253
|
+
value: {
|
|
2254
|
+
date,
|
|
2255
|
+
columnIndex,
|
|
2256
|
+
outsideDisabled
|
|
2257
|
+
},
|
|
2258
|
+
children: cell
|
|
2259
|
+
});
|
|
2260
|
+
}
|
|
2261
|
+
function dayCellInstancePropsAreEqual(prev, next) {
|
|
2262
|
+
if (prev.date !== next.date || prev.render !== next.render || prev.columnIndex !== next.columnIndex) return false;
|
|
2263
|
+
const a = prev._derivedState;
|
|
2264
|
+
const b = next._derivedState;
|
|
2265
|
+
return a.selected === b.selected && a.today === b.today && a.disabled === b.disabled && a.outsideMonth === b.outsideMonth && a.hidden === b.hidden && a.focused === b.focused && a.rangeStart === b.rangeStart && a.rangeEnd === b.rangeEnd && a.inRange === b.inRange && a.rangeIndex === b.rangeIndex && a.rangeLength === b.rangeLength && a.rangeHasStart === b.rangeHasStart && a.rangeHasEnd === b.rangeHasEnd && a.rangePreviewStart === b.rangePreviewStart && a.rangePreviewEnd === b.rangePreviewEnd && a.rangePreviewBoundary === b.rangePreviewBoundary && a.rangePreviewInRange === b.rangePreviewInRange && a.rangePreviewIndex === b.rangePreviewIndex && a.rangePreviewLength === b.rangePreviewLength && a.rangePreviewHasStart === b.rangePreviewHasStart && a.rangePreviewHasEnd === b.rangePreviewHasEnd && a.isTabTarget === b.isTabTarget;
|
|
2266
|
+
}
|
|
2267
|
+
const DayCellInstance = memo(DayCellInstanceFn, dayCellInstancePropsAreEqual);
|
|
2268
|
+
/**
|
|
2269
|
+
* Renders one `<td role="gridcell">` per day. Exposes data-attributes for
|
|
2270
|
+
* `selected`, `today`, `disabled`, `outside-month`, `focused`,
|
|
2271
|
+
* `range-start`, `range-end`, `range-boundary`, `in-range`, and `date`.
|
|
2272
|
+
*
|
|
2273
|
+
* When used inside a {@link WeekTemplate}, iterates over that week's days.
|
|
2274
|
+
* An explicit `date` prop renders a single cell.
|
|
2275
|
+
*/
|
|
2276
|
+
function DayCellTemplate(props) {
|
|
2277
|
+
const { date: dateProp, ...restProps } = props;
|
|
2278
|
+
const weekData = useContext(WeekDataContext);
|
|
2279
|
+
const { disabled, isDateDisabled, selectionMode, temporal: T, weekStartDay } = useCalendarStable();
|
|
2280
|
+
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
|
|
2281
|
+
const viewCtx = useViewContext();
|
|
2282
|
+
const { focusedDate, tabTargetDate } = viewCtx;
|
|
2283
|
+
const monthStable = useContext(MonthViewStableContext);
|
|
2284
|
+
const { rootState, weeks, currentMonth: currentDateTime } = useMonthViewState();
|
|
2285
|
+
const outsideDays = monthStable?.outsideDays ?? "enabled";
|
|
2286
|
+
const isWeeksView = viewCtx.viewType === "weeks";
|
|
2287
|
+
const cellMonth = isWeeksView ? null : weekData?.gridMonth ?? currentDateTime;
|
|
2288
|
+
const { orientation } = useContext(GridContext);
|
|
2289
|
+
if (dateProp) {
|
|
2290
|
+
const daysInWeek = dateProp.daysInWeek;
|
|
2291
|
+
const derived = computeDayCellState(dateProp, orientation, (dateProp.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, isWeeksView ? {
|
|
2292
|
+
year: dateProp.year,
|
|
2293
|
+
month: dateProp.month
|
|
2294
|
+
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2295
|
+
return /* @__PURE__ */ jsx(DayCellInstance, {
|
|
2296
|
+
...restProps,
|
|
2297
|
+
date: dateProp,
|
|
2298
|
+
_derivedState: derived
|
|
2299
|
+
});
|
|
2300
|
+
}
|
|
2301
|
+
const days = weekData ? weekData.days : weeks.flat();
|
|
2302
|
+
const perWeek = weekData != null;
|
|
2303
|
+
return /* @__PURE__ */ jsx(Fragment, { children: days.map((day, i) => {
|
|
2304
|
+
const daysInWeek = day.daysInWeek;
|
|
2305
|
+
const derived = computeDayCellState(day, orientation, perWeek ? i : (day.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, isWeeksView ? {
|
|
2306
|
+
year: day.year,
|
|
2307
|
+
month: day.month
|
|
2308
|
+
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2309
|
+
return /* @__PURE__ */ jsx(DayCellInstance, {
|
|
2310
|
+
...restProps,
|
|
2311
|
+
date: day,
|
|
2312
|
+
columnIndex: perWeek ? i : void 0,
|
|
2313
|
+
_derivedState: derived
|
|
2314
|
+
}, day.toString());
|
|
2315
|
+
}) });
|
|
2316
|
+
}
|
|
2317
|
+
function DayButtonInnerFn(props, ref) {
|
|
2318
|
+
const { render, date, _derivedState, ...otherProps } = props;
|
|
2319
|
+
const { onSelect, locale, readOnly, setHoveredDate } = useCalendarStable();
|
|
2320
|
+
const { setFocusedDate } = useViewContext();
|
|
2321
|
+
const gridFocusedRef = useContext(MonthViewStableContext).gridFocusedRef;
|
|
2322
|
+
const internalRef = useRef(null);
|
|
2323
|
+
const isHidden = _derivedState?.hidden ?? false;
|
|
2324
|
+
const isFocused = _derivedState?.focused ?? false;
|
|
2325
|
+
const isTabTarget = _derivedState?.isTabTarget ?? false;
|
|
2326
|
+
const isDisabled = _derivedState?.disabled ?? false;
|
|
2327
|
+
useEffect(() => {
|
|
2328
|
+
if (shouldMoveDomFocus(isFocused, gridFocusedRef.current) && internalRef.current) internalRef.current.focus();
|
|
2329
|
+
}, [isFocused, gridFocusedRef]);
|
|
2330
|
+
const state = _derivedState;
|
|
2331
|
+
const defaultProps = isHidden ? {} : {
|
|
2332
|
+
type: "button",
|
|
2333
|
+
tabIndex: isTabTarget ? 0 : -1,
|
|
2334
|
+
disabled: isDisabled,
|
|
2335
|
+
"aria-disabled": readOnly || void 0,
|
|
2336
|
+
"aria-label": date.toLocaleString(locale, {
|
|
2337
|
+
weekday: "long",
|
|
2338
|
+
month: "long",
|
|
2339
|
+
day: "numeric",
|
|
2340
|
+
year: "numeric"
|
|
2341
|
+
}),
|
|
2342
|
+
date: date.toString(),
|
|
2343
|
+
onClick: () => {
|
|
2344
|
+
setFocusedDate(date);
|
|
2345
|
+
onSelect(date);
|
|
2346
|
+
},
|
|
2347
|
+
onPointerEnter: () => {
|
|
2348
|
+
if (!isHidden && !isDisabled) setHoveredDate(date);
|
|
2349
|
+
},
|
|
2350
|
+
children: date.day
|
|
2351
|
+
};
|
|
2352
|
+
return useRender(isHidden ? {} : {
|
|
2353
|
+
defaultTagName: "button",
|
|
2354
|
+
render,
|
|
2355
|
+
ref: ref ? [ref, internalRef] : [internalRef],
|
|
2356
|
+
state,
|
|
2357
|
+
stateAttributesMapping: dayStateAttributesMapping,
|
|
2358
|
+
props: mergeProps(defaultProps, otherProps)
|
|
2359
|
+
});
|
|
2360
|
+
}
|
|
2361
|
+
function dayButtonInnerPropsAreEqual(prev, next) {
|
|
2362
|
+
if (prev.date !== next.date || prev.render !== next.render) return false;
|
|
2363
|
+
const a = prev._derivedState;
|
|
2364
|
+
const b = next._derivedState;
|
|
2365
|
+
if (!a || !b) return a === b;
|
|
2366
|
+
return a.selected === b.selected && a.today === b.today && a.disabled === b.disabled && a.outsideMonth === b.outsideMonth && a.hidden === b.hidden && a.focused === b.focused && a.rangeStart === b.rangeStart && a.rangeEnd === b.rangeEnd && a.inRange === b.inRange && a.rangeIndex === b.rangeIndex && a.rangeLength === b.rangeLength && a.rangeHasStart === b.rangeHasStart && a.rangeHasEnd === b.rangeHasEnd && a.rangePreviewStart === b.rangePreviewStart && a.rangePreviewEnd === b.rangePreviewEnd && a.rangePreviewBoundary === b.rangePreviewBoundary && a.rangePreviewInRange === b.rangePreviewInRange && a.rangePreviewIndex === b.rangePreviewIndex && a.rangePreviewLength === b.rangePreviewLength && a.rangePreviewHasStart === b.rangePreviewHasStart && a.rangePreviewHasEnd === b.rangePreviewHasEnd && a.isTabTarget === b.isTabTarget;
|
|
2367
|
+
}
|
|
2368
|
+
const DayButtonInner = memo(forwardRef(DayButtonInnerFn), dayButtonInnerPropsAreEqual);
|
|
2369
|
+
/**
|
|
2370
|
+
* Interactive `<button>` inside a day cell. Handles click-to-select,
|
|
2371
|
+
* roving tabindex, and imperative focus on keyboard navigation.
|
|
2372
|
+
*
|
|
2373
|
+
* Must be used inside a {@link DayCellTemplate} or given an explicit `date` prop.
|
|
2374
|
+
*/
|
|
2375
|
+
function DayButtonFn(props, ref) {
|
|
2376
|
+
const { date: dateProp, _derivedState, ...restProps } = props;
|
|
2377
|
+
const cellData = useContext(DayCellDataContext);
|
|
2378
|
+
const resolvedDate = dateProp ?? cellData?.date;
|
|
2379
|
+
if (resolvedDate) {
|
|
2380
|
+
if (_derivedState) return /* @__PURE__ */ jsx(DayButtonInner, {
|
|
2381
|
+
...restProps,
|
|
2382
|
+
ref,
|
|
2383
|
+
date: resolvedDate,
|
|
2384
|
+
_derivedState
|
|
2385
|
+
});
|
|
2386
|
+
return /* @__PURE__ */ jsx(DayButtonFallback, {
|
|
2387
|
+
ref,
|
|
2388
|
+
...restProps,
|
|
2389
|
+
date: resolvedDate
|
|
2390
|
+
});
|
|
2391
|
+
}
|
|
2392
|
+
throw new Error("DayButton must be used inside DayCellTemplate or receive an explicit date prop.");
|
|
2393
|
+
}
|
|
2394
|
+
const DayButton = forwardRef(DayButtonFn);
|
|
2395
|
+
/** Fallback for standalone DayButton usage that needs to read context. */
|
|
2396
|
+
function DayButtonFallbackFn(props, ref) {
|
|
2397
|
+
const { date, ...restProps } = props;
|
|
2398
|
+
const { orientation } = useContext(GridContext);
|
|
2399
|
+
const weekData = useContext(WeekDataContext);
|
|
2400
|
+
const { disabled, isDateDisabled, selectionMode, temporal: T, weekStartDay } = useCalendarStable();
|
|
2401
|
+
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
|
|
2402
|
+
const viewCtx = useViewContext();
|
|
2403
|
+
const { focusedDate, tabTargetDate } = viewCtx;
|
|
2404
|
+
const monthStable = useContext(MonthViewStableContext);
|
|
2405
|
+
const { rootState, currentMonth: currentDateTime } = useMonthViewState();
|
|
2406
|
+
const outsideDays = monthStable?.outsideDays ?? "enabled";
|
|
2407
|
+
const cellMonth = viewCtx.viewType === "weeks" ? {
|
|
2408
|
+
year: date.year,
|
|
2409
|
+
month: date.month
|
|
2410
|
+
} : weekData?.gridMonth ?? currentDateTime;
|
|
2411
|
+
const daysInWeek = date.daysInWeek;
|
|
2412
|
+
const derived = computeDayCellState(date, orientation, (date.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2413
|
+
return /* @__PURE__ */ jsx(DayButtonInner, {
|
|
2414
|
+
...restProps,
|
|
2415
|
+
ref,
|
|
2416
|
+
date,
|
|
2417
|
+
_derivedState: derived
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
const DayButtonFallback = forwardRef(DayButtonFallbackFn);
|
|
2421
|
+
//#endregion
|
|
2422
|
+
//#region src/grid.tsx
|
|
2423
|
+
function useGridKeyboard() {
|
|
2424
|
+
const { onSelect, disabled, readOnly, isDateDisabled, minValue, maxValue, temporal: T, weekStartDay } = useCalendarStable();
|
|
2425
|
+
const { focusedDate, setFocusedDate } = useViewContext();
|
|
2426
|
+
return useCallback((e) => {
|
|
2427
|
+
const result = computeNextFocusDate({
|
|
2428
|
+
key: e.key,
|
|
2429
|
+
shiftKey: e.shiftKey,
|
|
2430
|
+
focusedDate,
|
|
2431
|
+
minValue,
|
|
2432
|
+
maxValue,
|
|
2433
|
+
disabled,
|
|
2434
|
+
readOnly,
|
|
2435
|
+
isDateDisabled,
|
|
2436
|
+
T,
|
|
2437
|
+
weekStartDay
|
|
2438
|
+
});
|
|
2439
|
+
if (result.action === "move") {
|
|
2440
|
+
e.preventDefault();
|
|
2441
|
+
setFocusedDate(result.date);
|
|
2442
|
+
} else if (result.action === "select") {
|
|
2443
|
+
e.preventDefault();
|
|
2444
|
+
onSelect(focusedDate);
|
|
2445
|
+
}
|
|
2446
|
+
}, [
|
|
2447
|
+
focusedDate,
|
|
2448
|
+
setFocusedDate,
|
|
2449
|
+
onSelect,
|
|
2450
|
+
disabled,
|
|
2451
|
+
readOnly,
|
|
2452
|
+
isDateDisabled,
|
|
2453
|
+
minValue,
|
|
2454
|
+
maxValue,
|
|
2455
|
+
T,
|
|
2456
|
+
weekStartDay
|
|
2457
|
+
]);
|
|
2458
|
+
}
|
|
2459
|
+
function useWeeksGridKeyboard() {
|
|
2460
|
+
const { onSelect, disabled, readOnly, isDateDisabled, minValue, maxValue, temporal: T, weekStartDay } = useCalendarStable();
|
|
2461
|
+
const { focusedDate, setFocusedDate } = useViewContext();
|
|
2462
|
+
const weeksStable = useWeeksViewStable();
|
|
2463
|
+
const weeksState = useWeeksViewState();
|
|
2464
|
+
return useCallback((e) => {
|
|
2465
|
+
const result = computeWeeksKeyNav({
|
|
2466
|
+
key: e.key,
|
|
2467
|
+
shiftKey: e.shiftKey,
|
|
2468
|
+
focusedDate,
|
|
2469
|
+
windowStart: weeksState.windowInfo.windowStart,
|
|
2470
|
+
weekCount: weeksStable.weekCount,
|
|
2471
|
+
minValue,
|
|
2472
|
+
maxValue,
|
|
2473
|
+
disabled,
|
|
2474
|
+
readOnly,
|
|
2475
|
+
isDateDisabled,
|
|
2476
|
+
scrollBy: weeksStable.scrollBy,
|
|
2477
|
+
T,
|
|
2478
|
+
weekStartDay
|
|
2479
|
+
});
|
|
2480
|
+
if (result.action === "move") {
|
|
2481
|
+
e.preventDefault();
|
|
2482
|
+
setFocusedDate(result.date);
|
|
2483
|
+
if (result.followFocus) weeksStable.scrollToWeek(result.date, { snap: "start" });
|
|
2484
|
+
else if (result.windowShift !== 0) weeksStable.scrollToWeek(result.date, { snap: "nearest" });
|
|
2485
|
+
} else if (result.action === "select") {
|
|
2486
|
+
e.preventDefault();
|
|
2487
|
+
onSelect(focusedDate);
|
|
2488
|
+
}
|
|
2489
|
+
}, [
|
|
2490
|
+
focusedDate,
|
|
2491
|
+
setFocusedDate,
|
|
2492
|
+
onSelect,
|
|
2493
|
+
disabled,
|
|
2494
|
+
readOnly,
|
|
2495
|
+
isDateDisabled,
|
|
2496
|
+
minValue,
|
|
2497
|
+
maxValue,
|
|
2498
|
+
T,
|
|
2499
|
+
weekStartDay,
|
|
2500
|
+
weeksStable,
|
|
2501
|
+
weeksState.windowInfo.windowStart
|
|
2502
|
+
]);
|
|
2503
|
+
}
|
|
2504
|
+
const gridStateAttributesMapping = {
|
|
2505
|
+
root: () => null,
|
|
2506
|
+
month: () => null,
|
|
2507
|
+
year: () => null,
|
|
2508
|
+
orientation: (v) => ({ "data-orientation": v })
|
|
2509
|
+
};
|
|
2510
|
+
/**
|
|
2511
|
+
* Calendar grid container. Renders a `<table>` with `role="grid"` by default.
|
|
2512
|
+
*
|
|
2513
|
+
* Sets CSS custom properties `--calendar-days-per-week` and
|
|
2514
|
+
* `--calendar-weeks-in-month`, and matching `data-*` attributes.
|
|
2515
|
+
* Manages keyboard navigation, grid focus tracking, and
|
|
2516
|
+
* `aria-labelledby` linkage to {@link MonthYearString}.
|
|
2517
|
+
*
|
|
2518
|
+
* Automatically detects the active view type (MonthView or WeeksView) and
|
|
2519
|
+
* delegates to the appropriate internal renderer.
|
|
2520
|
+
*/
|
|
2521
|
+
function GridFn(props, ref) {
|
|
2522
|
+
const monthState = useContext(MonthViewStableContext);
|
|
2523
|
+
const weeksState = useContext(WeeksViewStateContext);
|
|
2524
|
+
if (monthState) return /* @__PURE__ */ jsx(MonthGrid, {
|
|
2525
|
+
ref,
|
|
2526
|
+
...props
|
|
2527
|
+
});
|
|
2528
|
+
if (weeksState) return /* @__PURE__ */ jsx(WeeksViewGrid, {
|
|
2529
|
+
ref,
|
|
2530
|
+
...props
|
|
2531
|
+
});
|
|
2532
|
+
throw new Error("Grid must be used inside MonthView.Root or WeeksView.Root.");
|
|
2533
|
+
}
|
|
2534
|
+
const Grid = forwardRef(GridFn);
|
|
2535
|
+
function MonthGridFn(props, ref) {
|
|
2536
|
+
const { render, mode: _mode, orientation, autoFocus, monthIndex: monthIndexProp, children, ...otherProps } = props;
|
|
2537
|
+
const monthIndex = monthIndexProp ?? 0;
|
|
2538
|
+
const { setHoveredDate } = useCalendarStable();
|
|
2539
|
+
const { setGridHasFocus } = useViewContext();
|
|
2540
|
+
const monthViewStable = useMonthViewStable();
|
|
2541
|
+
const monthViewState = useMonthViewState();
|
|
2542
|
+
const { gridFocusedRef } = monthViewStable;
|
|
2543
|
+
const { currentMonth: currentDateTime, gridLabelIds, rootState, weeks: defaultWeeks, allMonths } = monthViewState;
|
|
2544
|
+
if (monthIndex >= allMonths.length) console.warn(`[DatePicker] Grid monthIndex={${monthIndex}} is out of bounds (numberOfMonths=${allMonths.length}). Falling back to the first month.`);
|
|
2545
|
+
const monthData = allMonths[monthIndex];
|
|
2546
|
+
const gridWeeks = monthData?.weeks ?? defaultWeeks;
|
|
2547
|
+
const handleKeyDown = useGridKeyboard();
|
|
2548
|
+
const gridRef = useRef(null);
|
|
2549
|
+
useEffect(() => {
|
|
2550
|
+
if (autoFocus && gridRef.current) {
|
|
2551
|
+
const target = gridRef.current.querySelector("[tabindex=\"0\"]");
|
|
2552
|
+
if (target) {
|
|
2553
|
+
target.focus();
|
|
2554
|
+
gridFocusedRef.current = true;
|
|
2555
|
+
setGridHasFocus(true);
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
}, [
|
|
2559
|
+
autoFocus,
|
|
2560
|
+
gridFocusedRef,
|
|
2561
|
+
setGridHasFocus
|
|
2562
|
+
]);
|
|
2563
|
+
const resolvedOrientation = orientation ?? "horizontal";
|
|
2564
|
+
const daysPerWeek = gridWeeks[0]?.length ?? 7;
|
|
2565
|
+
const weeksInMonth = gridWeeks.length;
|
|
2566
|
+
const gridMonth = monthData ?? {
|
|
2567
|
+
year: currentDateTime.year,
|
|
2568
|
+
month: currentDateTime.month
|
|
2569
|
+
};
|
|
2570
|
+
const state = useMemo(() => ({
|
|
2571
|
+
root: rootState,
|
|
2572
|
+
month: gridMonth.month,
|
|
2573
|
+
year: gridMonth.year,
|
|
2574
|
+
orientation: resolvedOrientation
|
|
2575
|
+
}), [
|
|
2576
|
+
rootState,
|
|
2577
|
+
gridMonth.month,
|
|
2578
|
+
gridMonth.year,
|
|
2579
|
+
resolvedOrientation
|
|
2580
|
+
]);
|
|
2581
|
+
const defaultProps = {
|
|
2582
|
+
role: "grid",
|
|
2583
|
+
"aria-labelledby": gridLabelIds[monthIndex] || void 0,
|
|
2584
|
+
"aria-label": gridLabelIds[monthIndex] ? void 0 : "Calendar",
|
|
2585
|
+
"data-days-per-week": daysPerWeek,
|
|
2586
|
+
"data-weeks-in-month": weeksInMonth,
|
|
2587
|
+
style: {
|
|
2588
|
+
"--calendar-days-per-week": daysPerWeek,
|
|
2589
|
+
"--calendar-weeks-in-month": weeksInMonth
|
|
2590
|
+
},
|
|
2591
|
+
onKeyDown: handleKeyDown,
|
|
2592
|
+
onFocus: () => {
|
|
2593
|
+
gridFocusedRef.current = true;
|
|
2594
|
+
setGridHasFocus(true);
|
|
2595
|
+
},
|
|
2596
|
+
onBlur: () => {
|
|
2597
|
+
gridFocusedRef.current = false;
|
|
2598
|
+
setGridHasFocus(false);
|
|
2599
|
+
},
|
|
2600
|
+
onPointerLeave: () => {
|
|
2601
|
+
setHoveredDate(void 0);
|
|
2602
|
+
},
|
|
2603
|
+
children: children ?? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(GridHeader, { children: /* @__PURE__ */ jsx(GridHeaderCell, {}) }), /* @__PURE__ */ jsx(GridBody, { children: /* @__PURE__ */ jsx(WeekTemplate, { children: /* @__PURE__ */ jsx(DayCellTemplate, { children: /* @__PURE__ */ jsx(DayButton, {}) }) }) })] })
|
|
2604
|
+
};
|
|
2605
|
+
const el = useRender({
|
|
2606
|
+
defaultTagName: "table",
|
|
2607
|
+
render,
|
|
2608
|
+
ref: ref ? [ref, gridRef] : [gridRef],
|
|
2609
|
+
state,
|
|
2610
|
+
stateAttributesMapping: gridStateAttributesMapping,
|
|
2611
|
+
props: mergeProps(defaultProps, otherProps)
|
|
2612
|
+
});
|
|
2613
|
+
const orientationCtx = useMemo(() => ({ orientation: resolvedOrientation }), [resolvedOrientation]);
|
|
2614
|
+
const monthCtx = useMemo(() => ({
|
|
2615
|
+
weeks: gridWeeks,
|
|
2616
|
+
year: gridMonth.year,
|
|
2617
|
+
month: gridMonth.month
|
|
2618
|
+
}), [
|
|
2619
|
+
gridWeeks,
|
|
2620
|
+
gridMonth.year,
|
|
2621
|
+
gridMonth.month
|
|
2622
|
+
]);
|
|
2623
|
+
return /* @__PURE__ */ jsx(GridMonthContext.Provider, {
|
|
2624
|
+
value: monthCtx,
|
|
2625
|
+
children: /* @__PURE__ */ jsx(GridContext.Provider, {
|
|
2626
|
+
value: orientationCtx,
|
|
2627
|
+
children: el
|
|
2628
|
+
})
|
|
2629
|
+
});
|
|
2630
|
+
}
|
|
2631
|
+
const MonthGrid = forwardRef(MonthGridFn);
|
|
2632
|
+
function WeeksViewGridFn(props, ref) {
|
|
2633
|
+
const { render, mode: _mode, orientation, autoFocus, monthIndex: _monthIndex, children, ...otherProps } = props;
|
|
2634
|
+
const { setHoveredDate } = useCalendarStable();
|
|
2635
|
+
const { setGridHasFocus } = useViewContext();
|
|
2636
|
+
const weeksStable = useWeeksViewStable();
|
|
2637
|
+
const weeksState = useWeeksViewState();
|
|
2638
|
+
const { gridFocusedRef } = weeksStable;
|
|
2639
|
+
const { gridLabelIds, weeks, currentDateTime } = weeksState;
|
|
2640
|
+
const handleKeyDown = useWeeksGridKeyboard();
|
|
2641
|
+
const gridRef = useRef(null);
|
|
2642
|
+
useEffect(() => {
|
|
2643
|
+
if (autoFocus && gridRef.current) {
|
|
2644
|
+
const target = gridRef.current.querySelector("[tabindex=\"0\"]");
|
|
2645
|
+
if (target) {
|
|
2646
|
+
target.focus();
|
|
2647
|
+
gridFocusedRef.current = true;
|
|
2648
|
+
setGridHasFocus(true);
|
|
2649
|
+
}
|
|
2650
|
+
}
|
|
2651
|
+
}, [
|
|
2652
|
+
autoFocus,
|
|
2653
|
+
gridFocusedRef,
|
|
2654
|
+
setGridHasFocus
|
|
2655
|
+
]);
|
|
2656
|
+
const resolvedOrientation = orientation ?? "horizontal";
|
|
2657
|
+
const weekCount = weeks.length;
|
|
2658
|
+
const firstWeek = weeks[0];
|
|
2659
|
+
const gridMonth = firstWeek ? {
|
|
2660
|
+
month: firstWeek.month,
|
|
2661
|
+
year: firstWeek.year
|
|
2662
|
+
} : {
|
|
2663
|
+
month: 1,
|
|
2664
|
+
year: 1970
|
|
2665
|
+
};
|
|
2666
|
+
const state = useMemo(() => ({
|
|
2667
|
+
root: {},
|
|
2668
|
+
month: gridMonth.month,
|
|
2669
|
+
year: gridMonth.year,
|
|
2670
|
+
orientation: resolvedOrientation
|
|
2671
|
+
}), [
|
|
2672
|
+
gridMonth.month,
|
|
2673
|
+
gridMonth.year,
|
|
2674
|
+
resolvedOrientation
|
|
2675
|
+
]);
|
|
2676
|
+
const weekRowChildren = children ?? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(GridHeader, { children: /* @__PURE__ */ jsx(GridHeaderCell, {}) }), /* @__PURE__ */ jsx(WeeksGrid, { children: /* @__PURE__ */ jsx("tr", { children: /* @__PURE__ */ jsx(DayCellTemplate, { children: /* @__PURE__ */ jsx(DayButton, {}) }) }) })] });
|
|
2677
|
+
const defaultProps = {
|
|
2678
|
+
role: "grid",
|
|
2679
|
+
"aria-labelledby": gridLabelIds[0] || void 0,
|
|
2680
|
+
"aria-label": gridLabelIds[0] ? void 0 : "Calendar",
|
|
2681
|
+
"data-days-per-week": 7,
|
|
2682
|
+
"data-weeks-in-month": weekCount,
|
|
2683
|
+
style: {
|
|
2684
|
+
"--calendar-days-per-week": 7,
|
|
2685
|
+
"--calendar-weeks-in-month": weekCount
|
|
2686
|
+
},
|
|
2687
|
+
onKeyDown: handleKeyDown,
|
|
2688
|
+
onFocus: () => {
|
|
2689
|
+
gridFocusedRef.current = true;
|
|
2690
|
+
setGridHasFocus(true);
|
|
2691
|
+
},
|
|
2692
|
+
onBlur: () => {
|
|
2693
|
+
gridFocusedRef.current = false;
|
|
2694
|
+
setGridHasFocus(false);
|
|
2695
|
+
},
|
|
2696
|
+
onPointerLeave: () => {
|
|
2697
|
+
setHoveredDate(void 0);
|
|
2698
|
+
},
|
|
2699
|
+
children: weekRowChildren
|
|
2700
|
+
};
|
|
2701
|
+
const el = useRender({
|
|
2702
|
+
defaultTagName: "table",
|
|
2703
|
+
render,
|
|
2704
|
+
ref: ref ? [ref, gridRef] : [gridRef],
|
|
2705
|
+
state,
|
|
2706
|
+
stateAttributesMapping: gridStateAttributesMapping,
|
|
2707
|
+
props: mergeProps(defaultProps, otherProps)
|
|
2708
|
+
});
|
|
2709
|
+
const orientationCtx = useMemo(() => ({ orientation: resolvedOrientation }), [resolvedOrientation]);
|
|
2710
|
+
const monthViewStableShim = useMemo(() => ({
|
|
2711
|
+
numberOfMonths: 1,
|
|
2712
|
+
fixedWeeks: false,
|
|
2713
|
+
outsideDays: "enabled",
|
|
2714
|
+
overflowBehavior: "unbounded",
|
|
2715
|
+
goNextMonth: () => {},
|
|
2716
|
+
goPrevMonth: () => {},
|
|
2717
|
+
setGridLabelId: () => {},
|
|
2718
|
+
gridFocusedRef
|
|
2719
|
+
}), [gridFocusedRef]);
|
|
2720
|
+
const monthViewStateShim = useMemo(() => ({
|
|
2721
|
+
currentMonth: {
|
|
2722
|
+
year: currentDateTime.year,
|
|
2723
|
+
month: currentDateTime.month
|
|
2724
|
+
},
|
|
2725
|
+
weeks: [],
|
|
2726
|
+
allMonths: [],
|
|
2727
|
+
currentDateTime,
|
|
2728
|
+
gridLabelIds,
|
|
2729
|
+
rootState: {}
|
|
2730
|
+
}), [currentDateTime, gridLabelIds]);
|
|
2731
|
+
return /* @__PURE__ */ jsx(MonthViewStableContext.Provider, {
|
|
2732
|
+
value: monthViewStableShim,
|
|
2733
|
+
children: /* @__PURE__ */ jsx(MonthViewStateContext.Provider, {
|
|
2734
|
+
value: monthViewStateShim,
|
|
2735
|
+
children: /* @__PURE__ */ jsx(GridContext.Provider, {
|
|
2736
|
+
value: orientationCtx,
|
|
2737
|
+
children: el
|
|
2738
|
+
})
|
|
2739
|
+
})
|
|
2740
|
+
});
|
|
2741
|
+
}
|
|
2742
|
+
const WeeksViewGrid = forwardRef(WeeksViewGridFn);
|
|
2743
|
+
const gridBodyStateAttributesMapping = { root: () => null };
|
|
2744
|
+
/** Table body wrapping the week rows. Renders a `<tbody>` by default. */
|
|
2745
|
+
function GridBodyFn(props, ref) {
|
|
2746
|
+
if (useContext(WeeksViewStateContext)) {
|
|
2747
|
+
const { children, className, style } = props;
|
|
2748
|
+
return /* @__PURE__ */ jsx(WeeksGrid, {
|
|
2749
|
+
className,
|
|
2750
|
+
style,
|
|
2751
|
+
children
|
|
2752
|
+
});
|
|
2753
|
+
}
|
|
2754
|
+
return /* @__PURE__ */ jsx(MonthGridBody, {
|
|
2755
|
+
ref,
|
|
2756
|
+
...props
|
|
2757
|
+
});
|
|
2758
|
+
}
|
|
2759
|
+
const GridBody = forwardRef(GridBodyFn);
|
|
2760
|
+
function MonthGridBodyFn(props, ref) {
|
|
2761
|
+
const { render, ...otherProps } = props;
|
|
2762
|
+
const { rootState } = useMonthViewState();
|
|
2763
|
+
const state = useMemo(() => ({ root: rootState }), [rootState]);
|
|
2764
|
+
const defaultProps = { children: props.children ?? /* @__PURE__ */ jsx(WeekTemplate, { children: /* @__PURE__ */ jsx(DayCellTemplate, { children: /* @__PURE__ */ jsx(DayButton, {}) }) }) };
|
|
2765
|
+
const { children: _children, ...restOtherProps } = otherProps;
|
|
2766
|
+
return useRender({
|
|
2767
|
+
defaultTagName: "tbody",
|
|
2768
|
+
render,
|
|
2769
|
+
ref: ref ? [ref] : [],
|
|
2770
|
+
state,
|
|
2771
|
+
stateAttributesMapping: gridBodyStateAttributesMapping,
|
|
2772
|
+
props: mergeProps(defaultProps, restOtherProps)
|
|
2773
|
+
});
|
|
2774
|
+
}
|
|
2775
|
+
const MonthGridBody = forwardRef(MonthGridBodyFn);
|
|
2776
|
+
const weekInstanceStateAttributesMapping = {
|
|
2777
|
+
root: () => null,
|
|
2778
|
+
weekIndex: () => null,
|
|
2779
|
+
gridRowIndex: () => null
|
|
2780
|
+
};
|
|
2781
|
+
function WeekInstance(props) {
|
|
2782
|
+
const { render, ...otherProps } = props;
|
|
2783
|
+
const weekData = useContext(WeekDataContext);
|
|
2784
|
+
const { rootState } = useMonthViewState();
|
|
2785
|
+
return useRender({
|
|
2786
|
+
defaultTagName: "tr",
|
|
2787
|
+
render,
|
|
2788
|
+
ref: [],
|
|
2789
|
+
state: useMemo(() => ({
|
|
2790
|
+
root: rootState,
|
|
2791
|
+
weekIndex: weekData.weekIndex,
|
|
2792
|
+
gridRowIndex: weekData.gridRowIndex
|
|
2793
|
+
}), [
|
|
2794
|
+
rootState,
|
|
2795
|
+
weekData.weekIndex,
|
|
2796
|
+
weekData.gridRowIndex
|
|
2797
|
+
]),
|
|
2798
|
+
stateAttributesMapping: weekInstanceStateAttributesMapping,
|
|
2799
|
+
props: mergeProps({}, otherProps)
|
|
2800
|
+
});
|
|
2801
|
+
}
|
|
2802
|
+
/**
|
|
2803
|
+
* Iterates over the weeks in the current month and renders one `<tr>` per week.
|
|
2804
|
+
* Each instance receives its week's days and index via {@link WeekDataContext}.
|
|
2805
|
+
*/
|
|
2806
|
+
function WeekTemplate(props) {
|
|
2807
|
+
const outerWeekData = useContext(WeekDataContext);
|
|
2808
|
+
const gridMonthCtx = useContext(GridMonthContext);
|
|
2809
|
+
const { weeks: defaultWeeks } = useMonthViewState();
|
|
2810
|
+
const weeks = gridMonthCtx?.weeks ?? defaultWeeks;
|
|
2811
|
+
const gridMonth = useMemo(() => gridMonthCtx ? {
|
|
2812
|
+
year: gridMonthCtx.year,
|
|
2813
|
+
month: gridMonthCtx.month
|
|
2814
|
+
} : void 0, [gridMonthCtx]);
|
|
2815
|
+
const Instance = WeekInstance;
|
|
2816
|
+
return /* @__PURE__ */ jsx(Fragment, { children: weeks.map((weekDays, i) => /* @__PURE__ */ jsx(WeekDataContext.Provider, {
|
|
2817
|
+
value: {
|
|
2818
|
+
days: weekDays,
|
|
2819
|
+
weekIndex: i,
|
|
2820
|
+
gridMonth,
|
|
2821
|
+
gridRowIndex: outerWeekData?.gridRowIndex
|
|
2822
|
+
},
|
|
2823
|
+
children: /* @__PURE__ */ jsx(Instance, { ...props })
|
|
2824
|
+
}, weekDays[0].toString())) });
|
|
2825
|
+
}
|
|
2826
|
+
//#endregion
|
|
2827
|
+
//#region src/navigation.tsx
|
|
2828
|
+
const dateStringStateAttributesMapping = {
|
|
2829
|
+
root: () => null,
|
|
2830
|
+
month: () => null,
|
|
2831
|
+
year: () => null,
|
|
2832
|
+
day: () => null
|
|
2833
|
+
};
|
|
2834
|
+
const timeStringStateAttributesMapping = {
|
|
2835
|
+
root: () => null,
|
|
2836
|
+
hour: () => null,
|
|
2837
|
+
minute: () => null,
|
|
2838
|
+
second: () => null
|
|
2839
|
+
};
|
|
2840
|
+
const monthYearStringStateAttributesMapping = {
|
|
2841
|
+
root: () => null,
|
|
2842
|
+
month: () => null,
|
|
2843
|
+
year: () => null
|
|
2844
|
+
};
|
|
2845
|
+
function DateStringFn(props, ref) {
|
|
2846
|
+
const { render, locales, options, ...otherProps } = props;
|
|
2847
|
+
const { locale, temporal: T, timeZone } = useCalendarStable();
|
|
2848
|
+
const { selected } = useCalendarState();
|
|
2849
|
+
const { currentDateTime, rootState } = useMonthViewState();
|
|
2850
|
+
const selectedZdt = selectedToZdt(selected, timeZone, T);
|
|
2851
|
+
const displayDate = selectedZdt ? zdtToNativeDate(selectedZdt) : new Date(currentDateTime.year, currentDateTime.month - 1, 1);
|
|
2852
|
+
const formatted = displayDate.toLocaleDateString(locales ?? locale, options);
|
|
2853
|
+
const month = displayDate.getMonth() + 1;
|
|
2854
|
+
const year = displayDate.getFullYear();
|
|
2855
|
+
const day = displayDate.getDate();
|
|
2856
|
+
const state = useMemo(() => ({
|
|
2857
|
+
root: rootState,
|
|
2858
|
+
month,
|
|
2859
|
+
year,
|
|
2860
|
+
day
|
|
2861
|
+
}), [
|
|
2862
|
+
rootState,
|
|
2863
|
+
month,
|
|
2864
|
+
year,
|
|
2865
|
+
day
|
|
2866
|
+
]);
|
|
2867
|
+
return useRender({
|
|
2868
|
+
defaultTagName: "span",
|
|
2869
|
+
render,
|
|
2870
|
+
ref: ref ? [ref] : [],
|
|
2871
|
+
state,
|
|
2872
|
+
stateAttributesMapping: dateStringStateAttributesMapping,
|
|
2873
|
+
props: mergeProps({
|
|
2874
|
+
children: formatted,
|
|
2875
|
+
"aria-live": "polite"
|
|
2876
|
+
}, otherProps)
|
|
2877
|
+
});
|
|
2878
|
+
}
|
|
2879
|
+
/**
|
|
2880
|
+
* Displays the currently selected (or current) date as localized text.
|
|
2881
|
+
* Renders a `<span>` with `aria-live="polite"`.
|
|
2882
|
+
*/
|
|
2883
|
+
const DateString = forwardRef(DateStringFn);
|
|
2884
|
+
function TimeStringFn(props, ref) {
|
|
2885
|
+
const { render, locales, options, ...otherProps } = props;
|
|
2886
|
+
const { locale, temporal: T, timeZone } = useCalendarStable();
|
|
2887
|
+
const { selected } = useCalendarState();
|
|
2888
|
+
const { rootState } = useMonthViewState();
|
|
2889
|
+
const selZdt = selectedToZdt(selected, timeZone, T);
|
|
2890
|
+
const displayDate = selZdt ? zdtToNativeDate(selZdt) : zdtToNativeDate(T.Now.zonedDateTimeISO(timeZone));
|
|
2891
|
+
const mergedOptions = {
|
|
2892
|
+
timeZone,
|
|
2893
|
+
...options
|
|
2894
|
+
};
|
|
2895
|
+
const formatted = displayDate.toLocaleTimeString(locales ?? locale, mergedOptions);
|
|
2896
|
+
const nowZdt = selZdt ?? T.Now.zonedDateTimeISO(timeZone);
|
|
2897
|
+
const hour = nowZdt.hour;
|
|
2898
|
+
const minute = nowZdt.minute;
|
|
2899
|
+
const second = nowZdt.second;
|
|
2900
|
+
const state = useMemo(() => ({
|
|
2901
|
+
root: rootState,
|
|
2902
|
+
hour,
|
|
2903
|
+
minute,
|
|
2904
|
+
second
|
|
2905
|
+
}), [
|
|
2906
|
+
rootState,
|
|
2907
|
+
hour,
|
|
2908
|
+
minute,
|
|
2909
|
+
second
|
|
2910
|
+
]);
|
|
2911
|
+
return useRender({
|
|
2912
|
+
defaultTagName: "span",
|
|
2913
|
+
render,
|
|
2914
|
+
ref: ref ? [ref] : [],
|
|
2915
|
+
state,
|
|
2916
|
+
stateAttributesMapping: timeStringStateAttributesMapping,
|
|
2917
|
+
props: mergeProps({
|
|
2918
|
+
children: formatted,
|
|
2919
|
+
"aria-live": "polite"
|
|
2920
|
+
}, otherProps)
|
|
2921
|
+
});
|
|
2922
|
+
}
|
|
2923
|
+
/**
|
|
2924
|
+
* Displays the currently selected time as localized text.
|
|
2925
|
+
* Falls back to the current time when nothing is selected.
|
|
2926
|
+
* Renders a `<span>` with `aria-live="polite"`.
|
|
2927
|
+
*/
|
|
2928
|
+
const TimeString = forwardRef(TimeStringFn);
|
|
2929
|
+
function MonthYearStringFn(props, ref) {
|
|
2930
|
+
const { render, locales, options, monthIndex: monthIndexProp, ...otherProps } = props;
|
|
2931
|
+
const monthIndex = monthIndexProp ?? 0;
|
|
2932
|
+
const { locale } = useCalendarStable();
|
|
2933
|
+
const monthViewStable = useMonthViewStable();
|
|
2934
|
+
const { currentMonth: currentDateTime, allMonths, rootState } = useMonthViewState();
|
|
2935
|
+
const { setGridLabelId } = monthViewStable;
|
|
2936
|
+
const id = useId();
|
|
2937
|
+
useEffect(() => {
|
|
2938
|
+
setGridLabelId(monthIndex, id);
|
|
2939
|
+
return () => setGridLabelId(monthIndex, void 0);
|
|
2940
|
+
}, [
|
|
2941
|
+
id,
|
|
2942
|
+
setGridLabelId,
|
|
2943
|
+
monthIndex
|
|
2944
|
+
]);
|
|
2945
|
+
const monthData = allMonths[monthIndex];
|
|
2946
|
+
const displayYear = monthData?.year ?? currentDateTime.year;
|
|
2947
|
+
const displayMonth = monthData?.month ?? currentDateTime.month;
|
|
2948
|
+
const displayDate = new Date(displayYear, displayMonth - 1, 1);
|
|
2949
|
+
const defaultOptions = options ?? {
|
|
2950
|
+
month: "long",
|
|
2951
|
+
year: "numeric"
|
|
2952
|
+
};
|
|
2953
|
+
const formatted = displayDate.toLocaleDateString(locales ?? locale, defaultOptions);
|
|
2954
|
+
const state = useMemo(() => ({
|
|
2955
|
+
root: rootState,
|
|
2956
|
+
month: displayMonth,
|
|
2957
|
+
year: displayYear
|
|
2958
|
+
}), [
|
|
2959
|
+
rootState,
|
|
2960
|
+
displayMonth,
|
|
2961
|
+
displayYear
|
|
2962
|
+
]);
|
|
2963
|
+
return useRender({
|
|
2964
|
+
defaultTagName: "span",
|
|
2965
|
+
render,
|
|
2966
|
+
ref: ref ? [ref] : [],
|
|
2967
|
+
state,
|
|
2968
|
+
stateAttributesMapping: monthYearStringStateAttributesMapping,
|
|
2969
|
+
props: mergeProps({
|
|
2970
|
+
id,
|
|
2971
|
+
children: formatted,
|
|
2972
|
+
"aria-live": "polite"
|
|
2973
|
+
}, otherProps)
|
|
2974
|
+
});
|
|
2975
|
+
}
|
|
2976
|
+
/**
|
|
2977
|
+
* Displays the currently viewed month and year as a live-region heading for the
|
|
2978
|
+
* calendar grid. Defaults to `{ month: "long", year: "numeric" }` (e.g.
|
|
2979
|
+
* "March 2026"). Automatically registers a unique `id` so that `Grid` can
|
|
2980
|
+
* reference it via `aria-labelledby`.
|
|
2981
|
+
*
|
|
2982
|
+
* **Accessibility requirement:** When using a custom `render` function or custom
|
|
2983
|
+
* `options` that change the displayed format, both the month and year **must**
|
|
2984
|
+
* remain accessible to screen readers within this component. This element is
|
|
2985
|
+
* referenced by the calendar grid's `aria-labelledby` and serves as its
|
|
2986
|
+
* accessible name.
|
|
2987
|
+
*/
|
|
2988
|
+
const MonthYearString = forwardRef(MonthYearStringFn);
|
|
2989
|
+
function useNavButton(direction) {
|
|
2990
|
+
const { disabled: globalDisabled, minValue, maxValue, locale, temporal: T } = useCalendarStable();
|
|
2991
|
+
const monthViewStable = useMonthViewStable();
|
|
2992
|
+
const monthViewState = useMonthViewState();
|
|
2993
|
+
const { goNextMonth: goToNextMonth, goPrevMonth: goToPrevMonth, numberOfMonths } = monthViewStable;
|
|
2994
|
+
const { currentMonth: currentDateTime, allMonths, rootState } = monthViewState;
|
|
2995
|
+
const refMonth = direction === "next" ? allMonths[numberOfMonths - 1] ?? currentDateTime : currentDateTime;
|
|
2996
|
+
const destMonth = direction === "prev" ? refMonth.month === 1 ? 12 : refMonth.month - 1 : refMonth.month === 12 ? 1 : refMonth.month + 1;
|
|
2997
|
+
const destYear = direction === "prev" ? refMonth.month === 1 ? refMonth.year - 1 : refMonth.year : refMonth.month === 12 ? refMonth.year + 1 : refMonth.year;
|
|
2998
|
+
const boundValue = direction === "prev" ? minValue : maxValue;
|
|
2999
|
+
const isDisabled = useMemo(() => {
|
|
3000
|
+
if (globalDisabled) return true;
|
|
3001
|
+
if (!boundValue) return false;
|
|
3002
|
+
if (direction === "prev") return destYear < boundValue.year || destYear === boundValue.year && destMonth < boundValue.month;
|
|
3003
|
+
return destYear > boundValue.year || destYear === boundValue.year && destMonth > boundValue.month;
|
|
3004
|
+
}, [
|
|
3005
|
+
globalDisabled,
|
|
3006
|
+
destYear,
|
|
3007
|
+
destMonth,
|
|
3008
|
+
boundValue,
|
|
3009
|
+
direction
|
|
3010
|
+
]);
|
|
3011
|
+
const localeCalendar = useMemo(() => calendarForLocale(locale), [locale]);
|
|
3012
|
+
const target = useMemo(() => T.PlainYearMonth.from({
|
|
3013
|
+
year: destYear,
|
|
3014
|
+
month: destMonth,
|
|
3015
|
+
calendar: localeCalendar
|
|
3016
|
+
}), [
|
|
3017
|
+
destYear,
|
|
3018
|
+
destMonth,
|
|
3019
|
+
T,
|
|
3020
|
+
localeCalendar
|
|
3021
|
+
]);
|
|
3022
|
+
return {
|
|
3023
|
+
state: useMemo(() => ({
|
|
3024
|
+
root: rootState,
|
|
3025
|
+
direction,
|
|
3026
|
+
disabled: isDisabled,
|
|
3027
|
+
target
|
|
3028
|
+
}), [
|
|
3029
|
+
rootState,
|
|
3030
|
+
direction,
|
|
3031
|
+
isDisabled,
|
|
3032
|
+
target
|
|
3033
|
+
]),
|
|
3034
|
+
defaultProps: {
|
|
3035
|
+
type: "button",
|
|
3036
|
+
"aria-label": `Go to ${direction === "prev" ? "previous" : "next"} month`,
|
|
3037
|
+
disabled: isDisabled,
|
|
3038
|
+
onClick: isDisabled ? void 0 : direction === "prev" ? goToPrevMonth : goToNextMonth
|
|
3039
|
+
}
|
|
3040
|
+
};
|
|
3041
|
+
}
|
|
3042
|
+
const navButtonStateAttributesMapping = {
|
|
3043
|
+
root: () => null,
|
|
3044
|
+
direction: (v) => ({ "data-direction": v }),
|
|
3045
|
+
disabled: () => null,
|
|
3046
|
+
target: () => null
|
|
3047
|
+
};
|
|
3048
|
+
function PrevMonthButtonFn(props, ref) {
|
|
3049
|
+
const { render, ...otherProps } = props;
|
|
3050
|
+
const { state, defaultProps } = useNavButton("prev");
|
|
3051
|
+
return useRender({
|
|
3052
|
+
defaultTagName: "button",
|
|
3053
|
+
render,
|
|
3054
|
+
ref: ref ? [ref] : [],
|
|
3055
|
+
state,
|
|
3056
|
+
stateAttributesMapping: navButtonStateAttributesMapping,
|
|
3057
|
+
props: mergeProps(defaultProps, otherProps)
|
|
3058
|
+
});
|
|
3059
|
+
}
|
|
3060
|
+
/**
|
|
3061
|
+
* Button that navigates to the previous month. Automatically disabled
|
|
3062
|
+
* when the previous month falls before `min`. Exposes `data-direction="prev"`.
|
|
3063
|
+
*/
|
|
3064
|
+
const PrevMonthButton = forwardRef(PrevMonthButtonFn);
|
|
3065
|
+
function NextMonthButtonFn(props, ref) {
|
|
3066
|
+
const { render, ...otherProps } = props;
|
|
3067
|
+
const { state, defaultProps } = useNavButton("next");
|
|
3068
|
+
return useRender({
|
|
3069
|
+
defaultTagName: "button",
|
|
3070
|
+
render,
|
|
3071
|
+
ref: ref ? [ref] : [],
|
|
3072
|
+
state,
|
|
3073
|
+
stateAttributesMapping: navButtonStateAttributesMapping,
|
|
3074
|
+
props: mergeProps(defaultProps, otherProps)
|
|
3075
|
+
});
|
|
3076
|
+
}
|
|
3077
|
+
/**
|
|
3078
|
+
* Button that navigates to the next month. Automatically disabled
|
|
3079
|
+
* when the next month falls after `max`. Exposes `data-direction="next"`.
|
|
3080
|
+
*/
|
|
3081
|
+
const NextMonthButton = forwardRef(NextMonthButtonFn);
|
|
3082
|
+
//#endregion
|
|
3083
|
+
//#region src/selected-range.tsx
|
|
3084
|
+
const rangeOverlayStateAttributesMapping = {
|
|
3085
|
+
root: () => null,
|
|
3086
|
+
active: (v) => v ? { "data-active": "" } : null,
|
|
3087
|
+
weekIndex: (v) => ({ "data-week-index": String(v) }),
|
|
3088
|
+
startIndex: (v) => ({ "data-start-index": String(v) }),
|
|
3089
|
+
endIndex: (v) => ({ "data-end-index": String(v) }),
|
|
3090
|
+
startDate: (v) => v ? { "data-start-date": v } : null,
|
|
3091
|
+
endDate: (v) => v ? { "data-end-date": v } : null,
|
|
3092
|
+
extendsBefore: (v) => v ? { "data-extends-before": "" } : null,
|
|
3093
|
+
extendsAfter: (v) => v ? { "data-extends-after": "" } : null,
|
|
3094
|
+
hasStart: (v) => v ? { "data-has-start": "" } : null,
|
|
3095
|
+
hasEnd: (v) => v ? { "data-has-end": "" } : null,
|
|
3096
|
+
orientation: (v) => ({ "data-orientation": v })
|
|
3097
|
+
};
|
|
3098
|
+
function computeClippedRangeInfo(days, rangeStart, rangeEnd, T, outsideDays, gridMonth) {
|
|
3099
|
+
const raw = computeWeekRangeInfo(days, rangeStart, rangeEnd, T);
|
|
3100
|
+
if ((outsideDays === "disabled" || outsideDays === "hidden") && raw.active && gridMonth) {
|
|
3101
|
+
let { startIndex, endIndex, extendsBefore, extendsAfter } = raw;
|
|
3102
|
+
while (startIndex <= endIndex && (days[startIndex].year !== gridMonth.year || days[startIndex].month !== gridMonth.month)) {
|
|
3103
|
+
startIndex++;
|
|
3104
|
+
extendsBefore = true;
|
|
3105
|
+
}
|
|
3106
|
+
while (endIndex >= startIndex && (days[endIndex].year !== gridMonth.year || days[endIndex].month !== gridMonth.month)) {
|
|
3107
|
+
endIndex--;
|
|
3108
|
+
extendsAfter = true;
|
|
3109
|
+
}
|
|
3110
|
+
if (startIndex > endIndex) return {
|
|
3111
|
+
active: false,
|
|
3112
|
+
startIndex: 0,
|
|
3113
|
+
endIndex: 0,
|
|
3114
|
+
extendsBefore: false,
|
|
3115
|
+
extendsAfter: false
|
|
3116
|
+
};
|
|
3117
|
+
return {
|
|
3118
|
+
active: true,
|
|
3119
|
+
startIndex,
|
|
3120
|
+
endIndex,
|
|
3121
|
+
extendsBefore,
|
|
3122
|
+
extendsAfter
|
|
3123
|
+
};
|
|
3124
|
+
}
|
|
3125
|
+
return raw;
|
|
3126
|
+
}
|
|
3127
|
+
function RangeSelectedFn(props, ref) {
|
|
3128
|
+
const { selectionMode } = useCalendarStable();
|
|
3129
|
+
if (selectionMode !== "range") return null;
|
|
3130
|
+
return /* @__PURE__ */ jsx(RangeSelectedInner, {
|
|
3131
|
+
ref,
|
|
3132
|
+
...props
|
|
3133
|
+
});
|
|
3134
|
+
}
|
|
3135
|
+
/**
|
|
3136
|
+
* Visual overlay (`<td role="presentation">`) that highlights the selected
|
|
3137
|
+
* date range within a single week row. Exposes data-attributes for
|
|
3138
|
+
* `active`, `week-index`, `start-index`, `end-index`, `start-date`,
|
|
3139
|
+
* `end-date`, `extends-before`, and `extends-after`.
|
|
3140
|
+
*/
|
|
3141
|
+
const RangeSelected = forwardRef(RangeSelectedFn);
|
|
3142
|
+
function RangeSelectedInnerFn(props, ref) {
|
|
3143
|
+
const { render, ...otherProps } = props;
|
|
3144
|
+
const weekData = useContext(WeekDataContext);
|
|
3145
|
+
const { orientation } = useContext(GridContext);
|
|
3146
|
+
const { temporal: T } = useCalendarStable();
|
|
3147
|
+
const { rangeStart, rangeEnd } = useCalendarState();
|
|
3148
|
+
const monthStable = useContext(MonthViewStableContext);
|
|
3149
|
+
const { rootState } = useMonthViewState();
|
|
3150
|
+
const outsideDays = monthStable?.outsideDays ?? "enabled";
|
|
3151
|
+
const days = weekData?.days ?? [];
|
|
3152
|
+
const gridMonth = weekData?.gridMonth;
|
|
3153
|
+
const info = useMemo(() => computeClippedRangeInfo(days, rangeStart, rangeEnd, T, outsideDays, gridMonth), [
|
|
3154
|
+
days,
|
|
3155
|
+
rangeStart,
|
|
3156
|
+
rangeEnd,
|
|
3157
|
+
T,
|
|
3158
|
+
outsideDays,
|
|
3159
|
+
gridMonth
|
|
3160
|
+
]);
|
|
3161
|
+
const startDate = info.active ? days[info.startIndex].toString() : "";
|
|
3162
|
+
const endDate = info.active ? days[info.endIndex].toString() : "";
|
|
3163
|
+
const weekIndex = weekData?.weekIndex ?? 0;
|
|
3164
|
+
const state = useMemo(() => ({
|
|
3165
|
+
root: rootState,
|
|
3166
|
+
active: info.active,
|
|
3167
|
+
weekIndex,
|
|
3168
|
+
startIndex: info.startIndex,
|
|
3169
|
+
endIndex: info.endIndex,
|
|
3170
|
+
startDate,
|
|
3171
|
+
endDate,
|
|
3172
|
+
extendsBefore: info.extendsBefore,
|
|
3173
|
+
extendsAfter: info.extendsAfter,
|
|
3174
|
+
hasStart: rangeStart !== void 0,
|
|
3175
|
+
hasEnd: rangeEnd !== void 0,
|
|
3176
|
+
orientation
|
|
3177
|
+
}), [
|
|
3178
|
+
rootState,
|
|
3179
|
+
info,
|
|
3180
|
+
weekIndex,
|
|
3181
|
+
startDate,
|
|
3182
|
+
endDate,
|
|
3183
|
+
rangeStart,
|
|
3184
|
+
rangeEnd,
|
|
3185
|
+
orientation
|
|
3186
|
+
]);
|
|
3187
|
+
return useRender({
|
|
3188
|
+
defaultTagName: "td",
|
|
3189
|
+
render,
|
|
3190
|
+
ref: ref ? [ref] : [],
|
|
3191
|
+
state,
|
|
3192
|
+
stateAttributesMapping: rangeOverlayStateAttributesMapping,
|
|
3193
|
+
props: mergeProps({
|
|
3194
|
+
role: "presentation",
|
|
3195
|
+
"aria-hidden": true
|
|
3196
|
+
}, otherProps)
|
|
3197
|
+
});
|
|
3198
|
+
}
|
|
3199
|
+
const RangeSelectedInner = forwardRef(RangeSelectedInnerFn);
|
|
3200
|
+
//#endregion
|
|
3201
|
+
//#region src/range-preview.tsx
|
|
3202
|
+
function RangePreviewFn(props, ref) {
|
|
3203
|
+
const { selectionMode } = useCalendarStable();
|
|
3204
|
+
if (selectionMode !== "range") return null;
|
|
3205
|
+
return /* @__PURE__ */ jsx(RangePreviewInner, {
|
|
3206
|
+
ref,
|
|
3207
|
+
...props
|
|
3208
|
+
});
|
|
3209
|
+
}
|
|
3210
|
+
/**
|
|
3211
|
+
* Visual overlay (`<td role="presentation">`) that highlights the hover
|
|
3212
|
+
* preview range within a single week row. Structurally identical to
|
|
3213
|
+
* {@link RangeSelected} but reads `previewStart`/`previewEnd` instead of
|
|
3214
|
+
* the committed range boundaries.
|
|
3215
|
+
*/
|
|
3216
|
+
const RangePreview = forwardRef(RangePreviewFn);
|
|
3217
|
+
function RangePreviewInnerFn(props, ref) {
|
|
3218
|
+
const { render, ...otherProps } = props;
|
|
3219
|
+
const weekData = useContext(WeekDataContext);
|
|
3220
|
+
const { orientation } = useContext(GridContext);
|
|
3221
|
+
const { temporal: T } = useCalendarStable();
|
|
3222
|
+
const { previewStart, previewEnd } = useCalendarState();
|
|
3223
|
+
const monthStable = useContext(MonthViewStableContext);
|
|
3224
|
+
const { rootState } = useMonthViewState();
|
|
3225
|
+
const outsideDays = monthStable?.outsideDays ?? "enabled";
|
|
3226
|
+
const days = weekData?.days ?? [];
|
|
3227
|
+
const gridMonth = weekData?.gridMonth;
|
|
3228
|
+
const info = useMemo(() => computeClippedRangeInfo(days, previewStart, previewEnd, T, outsideDays, gridMonth), [
|
|
3229
|
+
days,
|
|
3230
|
+
previewStart,
|
|
3231
|
+
previewEnd,
|
|
3232
|
+
T,
|
|
3233
|
+
outsideDays,
|
|
3234
|
+
gridMonth
|
|
3235
|
+
]);
|
|
3236
|
+
const startDate = info.active ? days[info.startIndex].toString() : "";
|
|
3237
|
+
const endDate = info.active ? days[info.endIndex].toString() : "";
|
|
3238
|
+
const weekIndex = weekData?.weekIndex ?? 0;
|
|
3239
|
+
const state = useMemo(() => ({
|
|
3240
|
+
root: rootState,
|
|
3241
|
+
active: info.active,
|
|
3242
|
+
weekIndex,
|
|
3243
|
+
startIndex: info.startIndex,
|
|
3244
|
+
endIndex: info.endIndex,
|
|
3245
|
+
startDate,
|
|
3246
|
+
endDate,
|
|
3247
|
+
extendsBefore: info.extendsBefore,
|
|
3248
|
+
extendsAfter: info.extendsAfter,
|
|
3249
|
+
hasStart: previewStart !== void 0,
|
|
3250
|
+
hasEnd: previewEnd !== void 0,
|
|
3251
|
+
orientation
|
|
3252
|
+
}), [
|
|
3253
|
+
rootState,
|
|
3254
|
+
info,
|
|
3255
|
+
weekIndex,
|
|
3256
|
+
startDate,
|
|
3257
|
+
endDate,
|
|
3258
|
+
previewStart,
|
|
3259
|
+
previewEnd,
|
|
3260
|
+
orientation
|
|
3261
|
+
]);
|
|
3262
|
+
return useRender({
|
|
3263
|
+
defaultTagName: "td",
|
|
3264
|
+
render,
|
|
3265
|
+
ref: ref ? [ref] : [],
|
|
3266
|
+
state,
|
|
3267
|
+
stateAttributesMapping: rangeOverlayStateAttributesMapping,
|
|
3268
|
+
props: mergeProps({
|
|
3269
|
+
role: "presentation",
|
|
3270
|
+
"aria-hidden": true
|
|
3271
|
+
}, otherProps)
|
|
3272
|
+
});
|
|
3273
|
+
}
|
|
3274
|
+
const RangePreviewInner = forwardRef(RangePreviewInnerFn);
|
|
3275
|
+
//#endregion
|
|
3276
|
+
//#region src/drag-handle.tsx
|
|
3277
|
+
const dragHandleStateAttributesMapping = {
|
|
3278
|
+
root: () => null,
|
|
3279
|
+
active: (v) => v ? { "data-active": "" } : null,
|
|
3280
|
+
dragging: (v) => v ? { "data-dragging": "" } : null,
|
|
3281
|
+
edge: (v) => ({ "data-edge": v }),
|
|
3282
|
+
orientation: (v) => ({ "data-orientation": v })
|
|
3283
|
+
};
|
|
3284
|
+
function useDragHandle(edge, { dragging: draggingProp }) {
|
|
3285
|
+
const { temporal: T, setHoveredDate, readOnly, disabled } = useCalendarStable();
|
|
3286
|
+
const { rangeStart, rangeEnd } = useCalendarState();
|
|
3287
|
+
const { rootState } = useMonthViewState();
|
|
3288
|
+
const cellData = useContext(DayCellDataContext);
|
|
3289
|
+
const { orientation } = useContext(GridContext);
|
|
3290
|
+
const date = cellData?.date;
|
|
3291
|
+
const outsideDisabled = cellData?.outsideDisabled ?? false;
|
|
3292
|
+
const isActive = readOnly || disabled || outsideDisabled ? false : edge === "start" ? !!(date && rangeStart && T.PlainDate.compare(date, rangeStart) === 0) : !!(date && rangeEnd && T.PlainDate.compare(date, rangeEnd) === 0);
|
|
3293
|
+
const isDragging = (draggingProp ?? false) && isActive;
|
|
3294
|
+
const handleRef = useRef(null);
|
|
3295
|
+
return {
|
|
3296
|
+
state: useMemo(() => ({
|
|
3297
|
+
root: rootState,
|
|
3298
|
+
active: isActive,
|
|
3299
|
+
dragging: isDragging,
|
|
3300
|
+
edge,
|
|
3301
|
+
orientation
|
|
3302
|
+
}), [
|
|
3303
|
+
rootState,
|
|
3304
|
+
isActive,
|
|
3305
|
+
isDragging,
|
|
3306
|
+
edge,
|
|
3307
|
+
orientation
|
|
3308
|
+
]),
|
|
3309
|
+
stateAttributesMapping: dragHandleStateAttributesMapping,
|
|
3310
|
+
defaultProps: {
|
|
3311
|
+
role: "slider",
|
|
3312
|
+
"aria-roledescription": "drag handle",
|
|
3313
|
+
"aria-label": edge === "start" ? "Range start date" : "Range end date",
|
|
3314
|
+
"aria-valuetext": date?.toString(),
|
|
3315
|
+
"aria-grabbed": isActive ? isDragging : void 0,
|
|
3316
|
+
"aria-hidden": isActive ? void 0 : true,
|
|
3317
|
+
onPointerEnter: () => {
|
|
3318
|
+
if (date && !outsideDisabled) setHoveredDate(date);
|
|
3319
|
+
}
|
|
3320
|
+
},
|
|
3321
|
+
handleRef
|
|
3322
|
+
};
|
|
3323
|
+
}
|
|
3324
|
+
function RangeDragHandleFn(props, ref) {
|
|
3325
|
+
const { render, dragging, edge, ...otherProps } = props;
|
|
3326
|
+
const { state, stateAttributesMapping, defaultProps, handleRef } = useDragHandle(edge, { dragging });
|
|
3327
|
+
const { selectionMode } = useCalendarStable();
|
|
3328
|
+
const { rangeStart: rs, rangeEnd: re } = useCalendarState();
|
|
3329
|
+
return useRender(selectionMode !== "range" || !rs || !re ? {} : {
|
|
3330
|
+
defaultTagName: "span",
|
|
3331
|
+
render,
|
|
3332
|
+
ref: ref ? [ref, handleRef] : [handleRef],
|
|
3333
|
+
state,
|
|
3334
|
+
stateAttributesMapping,
|
|
3335
|
+
props: mergeProps(defaultProps, otherProps)
|
|
3336
|
+
});
|
|
3337
|
+
}
|
|
3338
|
+
/**
|
|
3339
|
+
* Drag handle (`<span>`) rendered at a range boundary. Exposes
|
|
3340
|
+
* `data-active`, `data-dragging`, and `data-edge` attributes.
|
|
3341
|
+
*/
|
|
3342
|
+
const RangeDragHandle = forwardRef(RangeDragHandleFn);
|
|
3343
|
+
function RangeStartDragHandleFn(props, ref) {
|
|
3344
|
+
return /* @__PURE__ */ jsx(RangeDragHandle, {
|
|
3345
|
+
edge: "start",
|
|
3346
|
+
ref,
|
|
3347
|
+
...props
|
|
3348
|
+
});
|
|
3349
|
+
}
|
|
3350
|
+
/** Convenience wrapper for {@link RangeDragHandle} with `edge="start"`. */
|
|
3351
|
+
const RangeStartDragHandle = forwardRef(RangeStartDragHandleFn);
|
|
3352
|
+
function RangeEndDragHandleFn(props, ref) {
|
|
3353
|
+
return /* @__PURE__ */ jsx(RangeDragHandle, {
|
|
3354
|
+
edge: "end",
|
|
3355
|
+
ref,
|
|
3356
|
+
...props
|
|
3357
|
+
});
|
|
3358
|
+
}
|
|
3359
|
+
/** Convenience wrapper for {@link RangeDragHandle} with `edge="end"`. */
|
|
3360
|
+
const RangeEndDragHandle = forwardRef(RangeEndDragHandleFn);
|
|
3361
|
+
//#endregion
|
|
3362
|
+
//#region src/week-number.tsx
|
|
3363
|
+
const weekNumberCellStateAttributesMapping = {
|
|
3364
|
+
root: () => null,
|
|
3365
|
+
weekNumber: (v) => ({ "data-week-number": String(v) })
|
|
3366
|
+
};
|
|
3367
|
+
function WeekNumberCellFn(props, ref) {
|
|
3368
|
+
const { render, ...otherProps } = props;
|
|
3369
|
+
const weekData = useContext(WeekDataContext);
|
|
3370
|
+
const { rootState } = useMonthViewState();
|
|
3371
|
+
const { temporal: T } = useCalendarStable();
|
|
3372
|
+
const days = weekData?.days ?? [];
|
|
3373
|
+
const weekNumber = useMemo(() => {
|
|
3374
|
+
if (days.length === 0) return 0;
|
|
3375
|
+
let thursday = days[0];
|
|
3376
|
+
for (const d of days) if (d.dayOfWeek === 4) {
|
|
3377
|
+
thursday = d;
|
|
3378
|
+
break;
|
|
3379
|
+
}
|
|
3380
|
+
return getISOWeekNumber(thursday, T);
|
|
3381
|
+
}, [days, T]);
|
|
3382
|
+
const state = useMemo(() => ({
|
|
3383
|
+
root: rootState,
|
|
3384
|
+
weekNumber
|
|
3385
|
+
}), [rootState, weekNumber]);
|
|
3386
|
+
const defaultProps = {
|
|
3387
|
+
role: "rowheader",
|
|
3388
|
+
"aria-label": `Week ${weekNumber}`,
|
|
3389
|
+
children: weekNumber
|
|
3390
|
+
};
|
|
3391
|
+
return useRender({
|
|
3392
|
+
defaultTagName: "td",
|
|
3393
|
+
render,
|
|
3394
|
+
ref: ref ? [ref] : [],
|
|
3395
|
+
state,
|
|
3396
|
+
stateAttributesMapping: weekNumberCellStateAttributesMapping,
|
|
3397
|
+
props: mergeProps(defaultProps, otherProps)
|
|
3398
|
+
});
|
|
3399
|
+
}
|
|
3400
|
+
/**
|
|
3401
|
+
* Renders the ISO 8601 week number for a week row. Must be used inside
|
|
3402
|
+
* a {@link WeekTemplate}. Renders a `<td>` with `role="rowheader"`.
|
|
3403
|
+
*/
|
|
3404
|
+
const WeekNumberCell = forwardRef(WeekNumberCellFn);
|
|
3405
|
+
const weekNumberHeaderStateAttributesMapping = { root: () => null };
|
|
3406
|
+
function WeekNumberHeaderFn(props, ref) {
|
|
3407
|
+
const { render, ...otherProps } = props;
|
|
3408
|
+
const { rootState } = useMonthViewState();
|
|
3409
|
+
const state = useMemo(() => ({ root: rootState }), [rootState]);
|
|
3410
|
+
return useRender({
|
|
3411
|
+
defaultTagName: "th",
|
|
3412
|
+
render,
|
|
3413
|
+
ref: ref ? [ref] : [],
|
|
3414
|
+
state,
|
|
3415
|
+
stateAttributesMapping: weekNumberHeaderStateAttributesMapping,
|
|
3416
|
+
props: mergeProps({
|
|
3417
|
+
scope: "col",
|
|
3418
|
+
"aria-label": "Week number",
|
|
3419
|
+
children: "#"
|
|
3420
|
+
}, otherProps)
|
|
3421
|
+
});
|
|
3422
|
+
}
|
|
3423
|
+
/**
|
|
3424
|
+
* Column header for the week number column. Renders a `<th scope="col">`.
|
|
3425
|
+
*/
|
|
3426
|
+
const WeekNumberHeader = forwardRef(WeekNumberHeaderFn);
|
|
3427
|
+
//#endregion
|
|
3428
|
+
//#region src/compute-weeks-in-window.ts
|
|
3429
|
+
/**
|
|
3430
|
+
* Computes an array of week descriptors for a continuous window.
|
|
3431
|
+
* `firstWeek` is snapped back to the nearest `weekStartDay`.
|
|
3432
|
+
*/
|
|
3433
|
+
function computeWeeksInWindow(firstWeek, weekCount, weekStartDay, _T) {
|
|
3434
|
+
const offset = (firstWeek.dayOfWeek % 7 - weekStartDay + 7) % 7;
|
|
3435
|
+
let weekStart = firstWeek.subtract({ days: offset });
|
|
3436
|
+
const weeks = [];
|
|
3437
|
+
for (let i = 0; i < weekCount; i++) {
|
|
3438
|
+
const endDate = weekStart.add({ days: 6 });
|
|
3439
|
+
weeks.push({
|
|
3440
|
+
startDate: weekStart,
|
|
3441
|
+
endDate,
|
|
3442
|
+
weekIndex: i,
|
|
3443
|
+
month: weekStart.month,
|
|
3444
|
+
year: weekStart.year
|
|
3445
|
+
});
|
|
3446
|
+
weekStart = weekStart.add({ weeks: 1 });
|
|
3447
|
+
}
|
|
3448
|
+
return weeks;
|
|
3449
|
+
}
|
|
3450
|
+
//#endregion
|
|
3451
|
+
//#region src/overflow.ts
|
|
3452
|
+
/** Returns true if any day in the 7-day span starting at weekStart falls within [min, max]. */
|
|
3453
|
+
function weekHasValidDay(weekStart, min, max, T) {
|
|
3454
|
+
const weekEnd = weekStart.add({ days: 6 });
|
|
3455
|
+
return T.PlainDate.compare(weekStart, max) <= 0 && T.PlainDate.compare(weekEnd, min) >= 0;
|
|
3456
|
+
}
|
|
3457
|
+
/**
|
|
3458
|
+
* Returns the start of the week (per weekStartDay) that contains the given date.
|
|
3459
|
+
* Uses dayOfWeek % 7 → Sun=0…Sat=6 convention.
|
|
3460
|
+
*/
|
|
3461
|
+
function weekStartContaining(date, weekStartDay, _T) {
|
|
3462
|
+
const daysFromStart = (date.dayOfWeek % 7 - weekStartDay + 7) % 7;
|
|
3463
|
+
return date.subtract({ days: daysFromStart });
|
|
3464
|
+
}
|
|
3465
|
+
/** Find the week start (per weekStartDay) whose 7-day span contains min. */
|
|
3466
|
+
function findFirstValidWeekStart(min, weekStartDay, T) {
|
|
3467
|
+
return weekStartContaining(min, weekStartDay, T);
|
|
3468
|
+
}
|
|
3469
|
+
/** Find the week start (per weekStartDay) whose 7-day span contains max. */
|
|
3470
|
+
function findLastValidWeekStart(max, weekStartDay, T) {
|
|
3471
|
+
return weekStartContaining(max, weekStartDay, T);
|
|
3472
|
+
}
|
|
3473
|
+
/**
|
|
3474
|
+
* Trim leading and trailing fully-disabled weeks from a window.
|
|
3475
|
+
* Returns new { firstWeek, weekCount }.
|
|
3476
|
+
*/
|
|
3477
|
+
function shrinkWindow(firstWeek, weekCount, min, max, T) {
|
|
3478
|
+
let start = 0;
|
|
3479
|
+
let end = weekCount - 1;
|
|
3480
|
+
while (start <= end) {
|
|
3481
|
+
if (weekHasValidDay(firstWeek.add({ days: start * 7 }), min, max, T)) break;
|
|
3482
|
+
start++;
|
|
3483
|
+
}
|
|
3484
|
+
while (end >= start) {
|
|
3485
|
+
if (weekHasValidDay(firstWeek.add({ days: end * 7 }), min, max, T)) break;
|
|
3486
|
+
end--;
|
|
3487
|
+
}
|
|
3488
|
+
if (start > end) return {
|
|
3489
|
+
firstWeek,
|
|
3490
|
+
weekCount
|
|
3491
|
+
};
|
|
3492
|
+
return {
|
|
3493
|
+
firstWeek: firstWeek.add({ days: start * 7 }),
|
|
3494
|
+
weekCount: end - start + 1
|
|
3495
|
+
};
|
|
3496
|
+
}
|
|
3497
|
+
/**
|
|
3498
|
+
* Given a target firstWeek and weekCount, apply overflow behavior and return
|
|
3499
|
+
* the adjusted { firstWeek, weekCount }.
|
|
3500
|
+
*/
|
|
3501
|
+
function applyOverflow({ targetFirstWeek, weekCount, behavior, min, max, weekStartDay, T }) {
|
|
3502
|
+
if (!min || !max || behavior === "unbounded") return {
|
|
3503
|
+
firstWeek: targetFirstWeek,
|
|
3504
|
+
weekCount
|
|
3505
|
+
};
|
|
3506
|
+
if (behavior === "stop") return {
|
|
3507
|
+
firstWeek: targetFirstWeek,
|
|
3508
|
+
weekCount
|
|
3509
|
+
};
|
|
3510
|
+
if (behavior === "stop-shrink") return shrinkWindow(targetFirstWeek, weekCount, min, max, T);
|
|
3511
|
+
if (behavior === "snap" || behavior === "snap-shrink") {
|
|
3512
|
+
const lastValid = findLastValidWeekStart(max, weekStartDay, T);
|
|
3513
|
+
const snappedFirst = lastValid.subtract({ days: (weekCount - 1) * 7 });
|
|
3514
|
+
const targetLastWeek = targetFirstWeek.add({ days: (weekCount - 1) * 7 });
|
|
3515
|
+
let resolvedFirst;
|
|
3516
|
+
if (T.PlainDate.compare(targetLastWeek, lastValid) <= 0) {
|
|
3517
|
+
const firstValid = findFirstValidWeekStart(min, weekStartDay, T);
|
|
3518
|
+
if (T.PlainDate.compare(targetFirstWeek, firstValid) >= 0) resolvedFirst = targetFirstWeek;
|
|
3519
|
+
else resolvedFirst = firstValid;
|
|
3520
|
+
} else resolvedFirst = snappedFirst;
|
|
3521
|
+
if (behavior === "snap-shrink") return shrinkWindow(resolvedFirst, weekCount, min, max, T);
|
|
3522
|
+
return {
|
|
3523
|
+
firstWeek: resolvedFirst,
|
|
3524
|
+
weekCount
|
|
3525
|
+
};
|
|
3526
|
+
}
|
|
3527
|
+
return {
|
|
3528
|
+
firstWeek: targetFirstWeek,
|
|
3529
|
+
weekCount
|
|
3530
|
+
};
|
|
3531
|
+
}
|
|
3532
|
+
/**
|
|
3533
|
+
* Returns whether the navigation button for the given direction should be enabled.
|
|
3534
|
+
*/
|
|
3535
|
+
function canShift({ currentFirstWeek, weekCount, direction, shiftBy, behavior, min, max, weekStartDay, T }) {
|
|
3536
|
+
if (behavior === "unbounded" || !min || !max) return true;
|
|
3537
|
+
const shift = shiftBy ?? weekCount;
|
|
3538
|
+
const targetFirstWeek = currentFirstWeek.add({ days: direction * shift * 7 });
|
|
3539
|
+
if (behavior === "stop" || behavior === "stop-shrink") {
|
|
3540
|
+
for (let i = 0; i < weekCount; i++) if (weekHasValidDay(targetFirstWeek.add({ days: i * 7 }), min, max, T)) return true;
|
|
3541
|
+
return false;
|
|
3542
|
+
}
|
|
3543
|
+
if (behavior === "snap" || behavior === "snap-shrink") {
|
|
3544
|
+
const applied = applyOverflow({
|
|
3545
|
+
targetFirstWeek,
|
|
3546
|
+
weekCount,
|
|
3547
|
+
behavior,
|
|
3548
|
+
min,
|
|
3549
|
+
max,
|
|
3550
|
+
weekStartDay,
|
|
3551
|
+
T
|
|
3552
|
+
});
|
|
3553
|
+
return T.PlainDate.compare(applied.firstWeek, currentFirstWeek) !== 0;
|
|
3554
|
+
}
|
|
3555
|
+
return true;
|
|
3556
|
+
}
|
|
3557
|
+
//#endregion
|
|
3558
|
+
//#region src/resolve-first-week.ts
|
|
3559
|
+
/** Snaps `date` back to the start of its containing week (determined by `weekStartDay`). */
|
|
3560
|
+
function snapToWeekStart(date, weekStartDay) {
|
|
3561
|
+
const offset = (date.dayOfWeek % 7 - weekStartDay + 7) % 7;
|
|
3562
|
+
return date.subtract({ days: offset });
|
|
3563
|
+
}
|
|
3564
|
+
/**
|
|
3565
|
+
* Resolves any {@link FirstWeekSpec} variant to the `PlainDate` of the
|
|
3566
|
+
* containing week's start (snapped to `weekStartDay`).
|
|
3567
|
+
*
|
|
3568
|
+
* @param spec - The week specifier to resolve.
|
|
3569
|
+
* @param weekStartDay - Day the calendar week starts on (`0` = Sunday).
|
|
3570
|
+
* @param T - Temporal namespace.
|
|
3571
|
+
* @param timeZone - IANA time zone for native `Date` conversion. Defaults to system time zone.
|
|
3572
|
+
*/
|
|
3573
|
+
function resolveFirstWeekSpec(spec, weekStartDay, T, timeZone) {
|
|
3574
|
+
if ("calendar" in spec) return snapToWeekStart(spec, weekStartDay);
|
|
3575
|
+
if (spec instanceof Date) {
|
|
3576
|
+
const tz = timeZone ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
3577
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
3578
|
+
timeZone: tz,
|
|
3579
|
+
year: "numeric",
|
|
3580
|
+
month: "numeric",
|
|
3581
|
+
day: "numeric"
|
|
3582
|
+
}).formatToParts(spec);
|
|
3583
|
+
const get = (type) => Number(parts.find((p) => p.type === type)?.value ?? 0);
|
|
3584
|
+
return snapToWeekStart(T.PlainDate.from({
|
|
3585
|
+
year: get("year"),
|
|
3586
|
+
month: get("month"),
|
|
3587
|
+
day: get("day")
|
|
3588
|
+
}), weekStartDay);
|
|
3589
|
+
}
|
|
3590
|
+
if ("isoWeek" in spec) {
|
|
3591
|
+
const { isoWeek, isoYear } = spec;
|
|
3592
|
+
const jan4 = T.PlainDate.from({
|
|
3593
|
+
year: isoYear,
|
|
3594
|
+
month: 1,
|
|
3595
|
+
day: 4
|
|
3596
|
+
});
|
|
3597
|
+
const jan4dow = jan4.dayOfWeek;
|
|
3598
|
+
return snapToWeekStart(jan4.subtract({ days: jan4dow - 1 }).add({ weeks: isoWeek - 1 }), weekStartDay);
|
|
3599
|
+
}
|
|
3600
|
+
if ("week" in spec && !("month" in spec)) {
|
|
3601
|
+
const { week, year } = spec;
|
|
3602
|
+
const jan1 = T.PlainDate.from({
|
|
3603
|
+
year,
|
|
3604
|
+
month: 1,
|
|
3605
|
+
day: 1
|
|
3606
|
+
});
|
|
3607
|
+
const daysUntilStart = (weekStartDay - jan1.dayOfWeek % 7 + 7) % 7;
|
|
3608
|
+
return jan1.add({ days: daysUntilStart }).add({ weeks: week - 1 });
|
|
3609
|
+
}
|
|
3610
|
+
const { month, year, day } = spec;
|
|
3611
|
+
const targetDay = day ?? 1;
|
|
3612
|
+
return snapToWeekStart(T.PlainDate.from({
|
|
3613
|
+
year,
|
|
3614
|
+
month,
|
|
3615
|
+
day: targetDay
|
|
3616
|
+
}), weekStartDay);
|
|
3617
|
+
}
|
|
3618
|
+
/**
|
|
3619
|
+
* Computes a new `firstWeek` date given a scroll target and snap mode.
|
|
3620
|
+
*
|
|
3621
|
+
* The `weekStartDay` is inferred from `currentFirstWeek` (its day of week).
|
|
3622
|
+
*
|
|
3623
|
+
* @param currentFirstWeek - The current first visible week (a week-start date).
|
|
3624
|
+
* @param weekCount - Number of visible week rows.
|
|
3625
|
+
* @param target - The week-start date to scroll to.
|
|
3626
|
+
* @param options - Optional snap mode (default `"start"`).
|
|
3627
|
+
*/
|
|
3628
|
+
function resolveFirstWeek(currentFirstWeek, weekCount, target, options) {
|
|
3629
|
+
switch (options?.snap ?? "start") {
|
|
3630
|
+
case "start": return target;
|
|
3631
|
+
case "center": return target.subtract({ weeks: Math.floor(weekCount / 2) });
|
|
3632
|
+
case "end": return target.subtract({ weeks: weekCount - 1 });
|
|
3633
|
+
case "nearest": {
|
|
3634
|
+
const lastWeek = currentFirstWeek.add({ weeks: weekCount - 1 });
|
|
3635
|
+
const cmp = (a, b) => {
|
|
3636
|
+
if (a.year !== b.year) return a.year - b.year;
|
|
3637
|
+
if (a.month !== b.month) return a.month - b.month;
|
|
3638
|
+
return a.day - b.day;
|
|
3639
|
+
};
|
|
3640
|
+
if (cmp(target, currentFirstWeek) < 0) return target;
|
|
3641
|
+
if (cmp(target, lastWeek) > 0) return target.subtract({ weeks: weekCount - 1 });
|
|
3642
|
+
return currentFirstWeek;
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
}
|
|
3646
|
+
//#endregion
|
|
3647
|
+
//#region src/weeks-view-state.ts
|
|
3648
|
+
/**
|
|
3649
|
+
* Core state management hook for WeeksView.Root.
|
|
3650
|
+
* Reads calendar-level context and manages weeks-specific state.
|
|
3651
|
+
*/
|
|
3652
|
+
function useWeeksViewRootState(props) {
|
|
3653
|
+
const { weekCount, firstWeek: firstWeekProp, defaultFirstWeek: defaultFirstWeekProp, onFirstWeekChange, scrollBy: scrollByProp, overflowBehavior: overflowBehaviorProp, onWindowChange } = props;
|
|
3654
|
+
const scrollBy = scrollByProp ?? "row";
|
|
3655
|
+
const overflowBehavior = overflowBehaviorProp ?? "unbounded";
|
|
3656
|
+
const calStable = useCalendarStable();
|
|
3657
|
+
const calState = useCalendarState();
|
|
3658
|
+
const T = calStable.temporal;
|
|
3659
|
+
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone } = calStable;
|
|
3660
|
+
const isControlled = firstWeekProp !== void 0;
|
|
3661
|
+
const resolveSpec = useCallback((spec) => resolveFirstWeekSpec(spec, weekStartDay, T), [weekStartDay, T]);
|
|
3662
|
+
const [internalFirstWeek, setInternalFirstWeek] = useState(() => {
|
|
3663
|
+
if (firstWeekProp !== void 0) return resolveSpec(firstWeekProp);
|
|
3664
|
+
if (defaultFirstWeekProp !== void 0) return resolveSpec(defaultFirstWeekProp);
|
|
3665
|
+
if (calState.selected) return resolveSpec(toZonedDateTime(calState.selected, timeZone, T).toPlainDate());
|
|
3666
|
+
return resolveSpec(T.Now.plainDateISO());
|
|
3667
|
+
});
|
|
3668
|
+
const resolvedFirstWeek = isControlled ? resolveSpec(firstWeekProp) : internalFirstWeek;
|
|
3669
|
+
const rawWeeks = useMemo(() => computeWeeksInWindow(resolvedFirstWeek, weekCount, weekStartDay, T), [
|
|
3670
|
+
resolvedFirstWeek,
|
|
3671
|
+
weekCount,
|
|
3672
|
+
weekStartDay,
|
|
3673
|
+
T
|
|
3674
|
+
]);
|
|
3675
|
+
const overflowResult = useMemo(() => applyOverflow({
|
|
3676
|
+
targetFirstWeek: resolvedFirstWeek,
|
|
3677
|
+
weekCount,
|
|
3678
|
+
behavior: overflowBehavior,
|
|
3679
|
+
min: minValue,
|
|
3680
|
+
max: maxValue,
|
|
3681
|
+
weekStartDay,
|
|
3682
|
+
T
|
|
3683
|
+
}), [
|
|
3684
|
+
resolvedFirstWeek,
|
|
3685
|
+
weekCount,
|
|
3686
|
+
overflowBehavior,
|
|
3687
|
+
minValue,
|
|
3688
|
+
maxValue,
|
|
3689
|
+
weekStartDay,
|
|
3690
|
+
T
|
|
3691
|
+
]);
|
|
3692
|
+
const adjustedWeeks = useMemo(() => {
|
|
3693
|
+
const sameFirst = T.PlainDate.compare(overflowResult.firstWeek, resolvedFirstWeek) === 0;
|
|
3694
|
+
const sameCount = overflowResult.weekCount === weekCount;
|
|
3695
|
+
if (sameFirst && sameCount) return rawWeeks;
|
|
3696
|
+
return computeWeeksInWindow(overflowResult.firstWeek, overflowResult.weekCount, weekStartDay, T);
|
|
3697
|
+
}, [
|
|
3698
|
+
overflowResult,
|
|
3699
|
+
resolvedFirstWeek,
|
|
3700
|
+
weekCount,
|
|
3701
|
+
rawWeeks,
|
|
3702
|
+
weekStartDay,
|
|
3703
|
+
T
|
|
3704
|
+
]);
|
|
3705
|
+
const isDateEnabled = useCallback((date) => {
|
|
3706
|
+
if (minValue && T.PlainDate.compare(date, minValue) < 0) return false;
|
|
3707
|
+
if (maxValue && T.PlainDate.compare(date, maxValue) > 0) return false;
|
|
3708
|
+
if (isDateDisabled?.(date)) return false;
|
|
3709
|
+
return true;
|
|
3710
|
+
}, [
|
|
3711
|
+
minValue,
|
|
3712
|
+
maxValue,
|
|
3713
|
+
isDateDisabled,
|
|
3714
|
+
T
|
|
3715
|
+
]);
|
|
3716
|
+
const windowInfo = useMemo(() => {
|
|
3717
|
+
const weeks = adjustedWeeks;
|
|
3718
|
+
if (weeks.length === 0) {
|
|
3719
|
+
const today = T.Now.plainDateISO();
|
|
3720
|
+
return {
|
|
3721
|
+
windowStart: today,
|
|
3722
|
+
windowEnd: today,
|
|
3723
|
+
weekCount: 0,
|
|
3724
|
+
dayCount: 0,
|
|
3725
|
+
enabledWeekCount: 0,
|
|
3726
|
+
enabledDayCount: 0,
|
|
3727
|
+
visibleMonths: []
|
|
3728
|
+
};
|
|
3729
|
+
}
|
|
3730
|
+
const windowStart = weeks[0].startDate;
|
|
3731
|
+
const windowEnd = weeks[weeks.length - 1].endDate;
|
|
3732
|
+
const wc = weeks.length;
|
|
3733
|
+
const dayCount = wc * 7;
|
|
3734
|
+
let enabledDayCount = 0;
|
|
3735
|
+
let enabledWeekCount = 0;
|
|
3736
|
+
for (const week of weeks) {
|
|
3737
|
+
let weekHasEnabled = false;
|
|
3738
|
+
let day = week.startDate;
|
|
3739
|
+
for (let d = 0; d < 7; d++) {
|
|
3740
|
+
if (isDateEnabled(day)) {
|
|
3741
|
+
enabledDayCount++;
|
|
3742
|
+
weekHasEnabled = true;
|
|
3743
|
+
}
|
|
3744
|
+
if (d < 6) day = day.add({ days: 1 });
|
|
3745
|
+
}
|
|
3746
|
+
if (weekHasEnabled) enabledWeekCount++;
|
|
3747
|
+
}
|
|
3748
|
+
const visibleMonths = [];
|
|
3749
|
+
const seenMonths = /* @__PURE__ */ new Set();
|
|
3750
|
+
for (const week of weeks) {
|
|
3751
|
+
let day = week.startDate;
|
|
3752
|
+
for (let d = 0; d < 7; d++) {
|
|
3753
|
+
const key = `${day.year}-${day.month}`;
|
|
3754
|
+
if (!seenMonths.has(key)) {
|
|
3755
|
+
seenMonths.add(key);
|
|
3756
|
+
visibleMonths.push({
|
|
3757
|
+
month: day.month,
|
|
3758
|
+
year: day.year
|
|
3759
|
+
});
|
|
3760
|
+
}
|
|
3761
|
+
if (d < 6) day = day.add({ days: 1 });
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3764
|
+
return {
|
|
3765
|
+
windowStart,
|
|
3766
|
+
windowEnd,
|
|
3767
|
+
weekCount: wc,
|
|
3768
|
+
dayCount,
|
|
3769
|
+
enabledWeekCount,
|
|
3770
|
+
enabledDayCount,
|
|
3771
|
+
visibleMonths
|
|
3772
|
+
};
|
|
3773
|
+
}, [
|
|
3774
|
+
adjustedWeeks,
|
|
3775
|
+
isDateEnabled,
|
|
3776
|
+
T
|
|
3777
|
+
]);
|
|
3778
|
+
const selectedPlain = useMemo(() => {
|
|
3779
|
+
if (!calState.selected) return void 0;
|
|
3780
|
+
return selectedToZdt(calState.selected, timeZone, T)?.toPlainDate();
|
|
3781
|
+
}, [
|
|
3782
|
+
calState.selected,
|
|
3783
|
+
timeZone,
|
|
3784
|
+
T
|
|
3785
|
+
]);
|
|
3786
|
+
const isInWindow = useCallback((date) => {
|
|
3787
|
+
return T.PlainDate.compare(date, windowInfo.windowStart) >= 0 && T.PlainDate.compare(date, windowInfo.windowEnd) <= 0;
|
|
3788
|
+
}, [
|
|
3789
|
+
windowInfo.windowStart,
|
|
3790
|
+
windowInfo.windowEnd,
|
|
3791
|
+
T
|
|
3792
|
+
]);
|
|
3793
|
+
const [focusedDate, setFocusedDate] = useState(() => {
|
|
3794
|
+
if (selectedPlain && isInWindow(selectedPlain)) return selectedPlain;
|
|
3795
|
+
return windowInfo.windowStart;
|
|
3796
|
+
});
|
|
3797
|
+
useEffect(() => {
|
|
3798
|
+
setFocusedDate((prev) => {
|
|
3799
|
+
if (isInWindow(prev)) return prev;
|
|
3800
|
+
return windowInfo.windowStart;
|
|
3801
|
+
});
|
|
3802
|
+
}, [windowInfo.windowStart, isInWindow]);
|
|
3803
|
+
const [gridHasFocus, setGridHasFocus] = useState(false);
|
|
3804
|
+
const gridFocusedRef = useRef(false);
|
|
3805
|
+
const [gridLabelIds, setGridLabelIds] = useState({});
|
|
3806
|
+
const setGridLabelId = useCallback((id) => {
|
|
3807
|
+
setGridLabelIds((prev) => {
|
|
3808
|
+
const key = 0;
|
|
3809
|
+
if (id === void 0) {
|
|
3810
|
+
if (!(key in prev)) return prev;
|
|
3811
|
+
const next = { ...prev };
|
|
3812
|
+
delete next[key];
|
|
3813
|
+
return next;
|
|
3814
|
+
}
|
|
3815
|
+
if (prev[key] === id) return prev;
|
|
3816
|
+
return {
|
|
3817
|
+
...prev,
|
|
3818
|
+
[key]: id
|
|
3819
|
+
};
|
|
3820
|
+
});
|
|
3821
|
+
}, []);
|
|
3822
|
+
const tabTargetDate = useMemo(() => {
|
|
3823
|
+
const allDays = [];
|
|
3824
|
+
for (const week of adjustedWeeks) {
|
|
3825
|
+
let day = week.startDate;
|
|
3826
|
+
for (let d = 0; d < 7; d++) {
|
|
3827
|
+
allDays.push(day);
|
|
3828
|
+
if (d < 6) day = day.add({ days: 1 });
|
|
3829
|
+
}
|
|
3830
|
+
}
|
|
3831
|
+
const inGrid = (d) => allDays.some((g) => T.PlainDate.compare(g, d) === 0);
|
|
3832
|
+
if (gridHasFocus && inGrid(focusedDate)) return focusedDate;
|
|
3833
|
+
if (selectedPlain && inGrid(selectedPlain)) return selectedPlain;
|
|
3834
|
+
if (!gridHasFocus && inGrid(focusedDate)) return focusedDate;
|
|
3835
|
+
return allDays.find((d) => isDateEnabled(d)) ?? allDays[0] ?? windowInfo.windowStart;
|
|
3836
|
+
}, [
|
|
3837
|
+
adjustedWeeks,
|
|
3838
|
+
focusedDate,
|
|
3839
|
+
selectedPlain,
|
|
3840
|
+
gridHasFocus,
|
|
3841
|
+
isDateEnabled,
|
|
3842
|
+
T,
|
|
3843
|
+
windowInfo.windowStart
|
|
3844
|
+
]);
|
|
3845
|
+
const selectedZdt = useMemo(() => selectedToZdt(calState.selected, timeZone, T), [
|
|
3846
|
+
calState.selected,
|
|
3847
|
+
timeZone,
|
|
3848
|
+
T
|
|
3849
|
+
]);
|
|
3850
|
+
const currentDateTime = useMemo(() => T.PlainDateTime.from({
|
|
3851
|
+
year: windowInfo.windowStart.year,
|
|
3852
|
+
month: windowInfo.windowStart.month,
|
|
3853
|
+
day: focusedDate.day,
|
|
3854
|
+
hour: selectedZdt?.hour ?? 0,
|
|
3855
|
+
minute: selectedZdt?.minute ?? 0,
|
|
3856
|
+
second: selectedZdt?.second ?? 0
|
|
3857
|
+
}, { overflow: "constrain" }), [
|
|
3858
|
+
windowInfo.windowStart,
|
|
3859
|
+
focusedDate.day,
|
|
3860
|
+
selectedZdt,
|
|
3861
|
+
T
|
|
3862
|
+
]);
|
|
3863
|
+
const updateFirstWeek = useCallback((newFirstWeek) => {
|
|
3864
|
+
if (!isControlled) setInternalFirstWeek(newFirstWeek);
|
|
3865
|
+
onFirstWeekChange?.(newFirstWeek);
|
|
3866
|
+
}, [isControlled, onFirstWeekChange]);
|
|
3867
|
+
const goNext = useCallback((shiftBy) => {
|
|
3868
|
+
const shift = shiftBy ?? weekCount;
|
|
3869
|
+
updateFirstWeek(applyOverflow({
|
|
3870
|
+
targetFirstWeek: resolvedFirstWeek.add({ weeks: shift }),
|
|
3871
|
+
weekCount,
|
|
3872
|
+
behavior: overflowBehavior,
|
|
3873
|
+
min: minValue,
|
|
3874
|
+
max: maxValue,
|
|
3875
|
+
weekStartDay,
|
|
3876
|
+
T
|
|
3877
|
+
}).firstWeek);
|
|
3878
|
+
}, [
|
|
3879
|
+
resolvedFirstWeek,
|
|
3880
|
+
weekCount,
|
|
3881
|
+
overflowBehavior,
|
|
3882
|
+
minValue,
|
|
3883
|
+
maxValue,
|
|
3884
|
+
weekStartDay,
|
|
3885
|
+
T,
|
|
3886
|
+
updateFirstWeek
|
|
3887
|
+
]);
|
|
3888
|
+
const goPrev = useCallback((shiftBy) => {
|
|
3889
|
+
const shift = shiftBy ?? weekCount;
|
|
3890
|
+
updateFirstWeek(applyOverflow({
|
|
3891
|
+
targetFirstWeek: resolvedFirstWeek.subtract({ weeks: shift }),
|
|
3892
|
+
weekCount,
|
|
3893
|
+
behavior: overflowBehavior,
|
|
3894
|
+
min: minValue,
|
|
3895
|
+
max: maxValue,
|
|
3896
|
+
weekStartDay,
|
|
3897
|
+
T
|
|
3898
|
+
}).firstWeek);
|
|
3899
|
+
}, [
|
|
3900
|
+
resolvedFirstWeek,
|
|
3901
|
+
weekCount,
|
|
3902
|
+
overflowBehavior,
|
|
3903
|
+
minValue,
|
|
3904
|
+
maxValue,
|
|
3905
|
+
weekStartDay,
|
|
3906
|
+
T,
|
|
3907
|
+
updateFirstWeek
|
|
3908
|
+
]);
|
|
3909
|
+
const scrollToWeek = useCallback((target, options) => {
|
|
3910
|
+
updateFirstWeek(applyOverflow({
|
|
3911
|
+
targetFirstWeek: resolveFirstWeek(resolvedFirstWeek, weekCount, resolveSpec(target), options),
|
|
3912
|
+
weekCount,
|
|
3913
|
+
behavior: overflowBehavior,
|
|
3914
|
+
min: minValue,
|
|
3915
|
+
max: maxValue,
|
|
3916
|
+
weekStartDay,
|
|
3917
|
+
T
|
|
3918
|
+
}).firstWeek);
|
|
3919
|
+
}, [
|
|
3920
|
+
resolvedFirstWeek,
|
|
3921
|
+
weekCount,
|
|
3922
|
+
resolveSpec,
|
|
3923
|
+
overflowBehavior,
|
|
3924
|
+
minValue,
|
|
3925
|
+
maxValue,
|
|
3926
|
+
weekStartDay,
|
|
3927
|
+
T,
|
|
3928
|
+
updateFirstWeek
|
|
3929
|
+
]);
|
|
3930
|
+
const onWindowChangeRef = useRef(onWindowChange);
|
|
3931
|
+
onWindowChangeRef.current = onWindowChange;
|
|
3932
|
+
const prevWindowInfoRef = useRef(null);
|
|
3933
|
+
useEffect(() => {
|
|
3934
|
+
const prev = prevWindowInfoRef.current;
|
|
3935
|
+
if (!prev || T.PlainDate.compare(prev.windowStart, windowInfo.windowStart) !== 0 || T.PlainDate.compare(prev.windowEnd, windowInfo.windowEnd) !== 0 || prev.weekCount !== windowInfo.weekCount) {
|
|
3936
|
+
prevWindowInfoRef.current = windowInfo;
|
|
3937
|
+
onWindowChangeRef.current?.(windowInfo);
|
|
3938
|
+
}
|
|
3939
|
+
}, [windowInfo, T]);
|
|
3940
|
+
return {
|
|
3941
|
+
stableCtx: useMemo(() => ({
|
|
3942
|
+
weekCount,
|
|
3943
|
+
scrollBy,
|
|
3944
|
+
overflowBehavior,
|
|
3945
|
+
goNext,
|
|
3946
|
+
goPrev,
|
|
3947
|
+
scrollToWeek,
|
|
3948
|
+
gridFocusedRef,
|
|
3949
|
+
setGridLabelId
|
|
3950
|
+
}), [
|
|
3951
|
+
weekCount,
|
|
3952
|
+
scrollBy,
|
|
3953
|
+
overflowBehavior,
|
|
3954
|
+
goNext,
|
|
3955
|
+
goPrev,
|
|
3956
|
+
scrollToWeek,
|
|
3957
|
+
setGridLabelId
|
|
3958
|
+
]),
|
|
3959
|
+
stateCtx: useMemo(() => ({
|
|
3960
|
+
focusedDate,
|
|
3961
|
+
windowInfo,
|
|
3962
|
+
gridLabelIds,
|
|
3963
|
+
weeks: adjustedWeeks,
|
|
3964
|
+
currentDateTime
|
|
3965
|
+
}), [
|
|
3966
|
+
focusedDate,
|
|
3967
|
+
windowInfo,
|
|
3968
|
+
gridLabelIds,
|
|
3969
|
+
adjustedWeeks,
|
|
3970
|
+
currentDateTime
|
|
3971
|
+
]),
|
|
3972
|
+
viewCtx: useMemo(() => ({
|
|
3973
|
+
viewType: "weeks",
|
|
3974
|
+
focusedDate,
|
|
3975
|
+
setFocusedDate,
|
|
3976
|
+
tabTargetDate,
|
|
3977
|
+
gridHasFocus,
|
|
3978
|
+
setGridHasFocus
|
|
3979
|
+
}), [
|
|
3980
|
+
focusedDate,
|
|
3981
|
+
tabTargetDate,
|
|
3982
|
+
gridHasFocus
|
|
3983
|
+
]),
|
|
3984
|
+
scrollToWeek
|
|
3985
|
+
};
|
|
3986
|
+
}
|
|
3987
|
+
//#endregion
|
|
3988
|
+
//#region src/weeks-view.tsx
|
|
3989
|
+
function WeeksViewRootFn(props, ref) {
|
|
3990
|
+
const { children } = props;
|
|
3991
|
+
const { stableCtx, stateCtx, viewCtx, scrollToWeek } = useWeeksViewRootState(props);
|
|
3992
|
+
useImperativeHandle(ref, () => ({ scrollToWeek }), [scrollToWeek]);
|
|
3993
|
+
const gridFocusedRef = useRef(false);
|
|
3994
|
+
const monthViewStableShim = useMemo(() => ({
|
|
3995
|
+
numberOfMonths: 1,
|
|
3996
|
+
fixedWeeks: false,
|
|
3997
|
+
outsideDays: "enabled",
|
|
3998
|
+
overflowBehavior: "unbounded",
|
|
3999
|
+
goNextMonth: () => {},
|
|
4000
|
+
goPrevMonth: () => {},
|
|
4001
|
+
setGridLabelId: () => {},
|
|
4002
|
+
gridFocusedRef
|
|
4003
|
+
}), []);
|
|
4004
|
+
const monthViewStateShim = useMemo(() => ({
|
|
4005
|
+
currentMonth: {
|
|
4006
|
+
year: stateCtx.currentDateTime.year,
|
|
4007
|
+
month: stateCtx.currentDateTime.month
|
|
4008
|
+
},
|
|
4009
|
+
weeks: [],
|
|
4010
|
+
allMonths: [],
|
|
4011
|
+
currentDateTime: stateCtx.currentDateTime,
|
|
4012
|
+
gridLabelIds: stateCtx.gridLabelIds,
|
|
4013
|
+
rootState: {}
|
|
4014
|
+
}), [stateCtx.currentDateTime, stateCtx.gridLabelIds]);
|
|
4015
|
+
return /* @__PURE__ */ jsx(MonthViewStableContext.Provider, {
|
|
4016
|
+
value: monthViewStableShim,
|
|
4017
|
+
children: /* @__PURE__ */ jsx(MonthViewStateContext.Provider, {
|
|
4018
|
+
value: monthViewStateShim,
|
|
4019
|
+
children: /* @__PURE__ */ jsx(WeeksViewStableContext.Provider, {
|
|
4020
|
+
value: stableCtx,
|
|
4021
|
+
children: /* @__PURE__ */ jsx(WeeksViewStateContext.Provider, {
|
|
4022
|
+
value: stateCtx,
|
|
4023
|
+
children: /* @__PURE__ */ jsx(ViewContext.Provider, {
|
|
4024
|
+
value: viewCtx,
|
|
4025
|
+
children
|
|
4026
|
+
})
|
|
4027
|
+
})
|
|
4028
|
+
})
|
|
4029
|
+
})
|
|
4030
|
+
});
|
|
4031
|
+
}
|
|
4032
|
+
const WeeksViewRoot = forwardRef(WeeksViewRootFn);
|
|
4033
|
+
function WeeksViewFn(props, ref) {
|
|
4034
|
+
const { weekCount, firstWeek, defaultFirstWeek, onFirstWeekChange, scrollBy, overflowBehavior, onWindowChange, children, ...calendarProps } = props;
|
|
4035
|
+
return /* @__PURE__ */ jsx(CalendarProvider, {
|
|
4036
|
+
...calendarProps,
|
|
4037
|
+
children: /* @__PURE__ */ jsx(WeeksViewRoot, {
|
|
4038
|
+
ref,
|
|
4039
|
+
weekCount,
|
|
4040
|
+
firstWeek,
|
|
4041
|
+
defaultFirstWeek,
|
|
4042
|
+
onFirstWeekChange,
|
|
4043
|
+
scrollBy,
|
|
4044
|
+
overflowBehavior,
|
|
4045
|
+
onWindowChange,
|
|
4046
|
+
children
|
|
4047
|
+
})
|
|
4048
|
+
});
|
|
4049
|
+
}
|
|
4050
|
+
const WeeksView = forwardRef(WeeksViewFn);
|
|
4051
|
+
WeeksView.Root = WeeksViewRoot;
|
|
4052
|
+
//#endregion
|
|
4053
|
+
//#region src/weeks-navigation.tsx
|
|
4054
|
+
const weeksNavButtonStateAttributesMapping = {
|
|
4055
|
+
root: () => null,
|
|
4056
|
+
direction: (v) => ({ "data-direction": v }),
|
|
4057
|
+
disabled: () => null
|
|
4058
|
+
};
|
|
4059
|
+
const weekCountStateAttributesMapping = {
|
|
4060
|
+
root: () => null,
|
|
4061
|
+
weekCount: () => null
|
|
4062
|
+
};
|
|
4063
|
+
function useWeeksNavButton(direction, shiftByProp) {
|
|
4064
|
+
const { disabled: globalDisabled, minValue, maxValue, temporal: T, weekStartDay } = useCalendarStable();
|
|
4065
|
+
const { weekCount, overflowBehavior, goNext, goPrev } = useWeeksViewStable();
|
|
4066
|
+
const { windowInfo } = useWeeksViewState();
|
|
4067
|
+
const { rootState } = useMonthViewState();
|
|
4068
|
+
const currentFirstWeek = windowInfo.windowStart;
|
|
4069
|
+
const isDisabled = useMemo(() => {
|
|
4070
|
+
if (globalDisabled) return true;
|
|
4071
|
+
return !canShift({
|
|
4072
|
+
currentFirstWeek,
|
|
4073
|
+
weekCount,
|
|
4074
|
+
direction: direction === "next" ? 1 : -1,
|
|
4075
|
+
shiftBy: shiftByProp,
|
|
4076
|
+
behavior: overflowBehavior,
|
|
4077
|
+
min: minValue,
|
|
4078
|
+
max: maxValue,
|
|
4079
|
+
weekStartDay,
|
|
4080
|
+
T
|
|
4081
|
+
});
|
|
4082
|
+
}, [
|
|
4083
|
+
globalDisabled,
|
|
4084
|
+
currentFirstWeek,
|
|
4085
|
+
weekCount,
|
|
4086
|
+
direction,
|
|
4087
|
+
shiftByProp,
|
|
4088
|
+
overflowBehavior,
|
|
4089
|
+
minValue,
|
|
4090
|
+
maxValue,
|
|
4091
|
+
weekStartDay,
|
|
4092
|
+
T
|
|
4093
|
+
]);
|
|
4094
|
+
const state = useMemo(() => ({
|
|
4095
|
+
root: rootState,
|
|
4096
|
+
direction,
|
|
4097
|
+
disabled: isDisabled
|
|
4098
|
+
}), [
|
|
4099
|
+
rootState,
|
|
4100
|
+
direction,
|
|
4101
|
+
isDisabled
|
|
4102
|
+
]);
|
|
4103
|
+
const goFn = direction === "prev" ? goPrev : goNext;
|
|
4104
|
+
return {
|
|
4105
|
+
state,
|
|
4106
|
+
defaultProps: {
|
|
4107
|
+
type: "button",
|
|
4108
|
+
"aria-label": `Go to ${direction === "prev" ? "previous" : "next"} weeks`,
|
|
4109
|
+
disabled: isDisabled,
|
|
4110
|
+
onClick: isDisabled ? void 0 : () => goFn(shiftByProp)
|
|
4111
|
+
}
|
|
4112
|
+
};
|
|
4113
|
+
}
|
|
4114
|
+
function PrevWeeksButtonFn(props, ref) {
|
|
4115
|
+
const { render, shiftBy, ...otherProps } = props;
|
|
4116
|
+
const { state, defaultProps } = useWeeksNavButton("prev", shiftBy);
|
|
4117
|
+
return useRender({
|
|
4118
|
+
defaultTagName: "button",
|
|
4119
|
+
render,
|
|
4120
|
+
ref: ref ? [ref] : [],
|
|
4121
|
+
state,
|
|
4122
|
+
stateAttributesMapping: weeksNavButtonStateAttributesMapping,
|
|
4123
|
+
props: mergeProps(defaultProps, otherProps)
|
|
4124
|
+
});
|
|
4125
|
+
}
|
|
4126
|
+
/**
|
|
4127
|
+
* Button that navigates to the previous week(s). Automatically disabled when
|
|
4128
|
+
* no more weeks are available based on `overflowBehavior` and `min`.
|
|
4129
|
+
* Exposes `data-direction="prev"`.
|
|
4130
|
+
*/
|
|
4131
|
+
const PrevWeeksButton = forwardRef(PrevWeeksButtonFn);
|
|
4132
|
+
function NextWeeksButtonFn(props, ref) {
|
|
4133
|
+
const { render, shiftBy, ...otherProps } = props;
|
|
4134
|
+
const { state, defaultProps } = useWeeksNavButton("next", shiftBy);
|
|
4135
|
+
return useRender({
|
|
4136
|
+
defaultTagName: "button",
|
|
4137
|
+
render,
|
|
4138
|
+
ref: ref ? [ref] : [],
|
|
4139
|
+
state,
|
|
4140
|
+
stateAttributesMapping: weeksNavButtonStateAttributesMapping,
|
|
4141
|
+
props: mergeProps(defaultProps, otherProps)
|
|
4142
|
+
});
|
|
4143
|
+
}
|
|
4144
|
+
/**
|
|
4145
|
+
* Button that navigates to the next week(s). Automatically disabled when
|
|
4146
|
+
* no more weeks are available based on `overflowBehavior` and `max`.
|
|
4147
|
+
* Exposes `data-direction="next"`.
|
|
4148
|
+
*/
|
|
4149
|
+
const NextWeeksButton = forwardRef(NextWeeksButtonFn);
|
|
4150
|
+
function WeekCountFn(props, ref) {
|
|
4151
|
+
const { render, ...otherProps } = props;
|
|
4152
|
+
const { windowInfo } = useWeeksViewState();
|
|
4153
|
+
const { rootState } = useMonthViewState();
|
|
4154
|
+
const count = windowInfo.weekCount;
|
|
4155
|
+
const state = useMemo(() => ({
|
|
4156
|
+
root: rootState,
|
|
4157
|
+
weekCount: count
|
|
4158
|
+
}), [rootState, count]);
|
|
4159
|
+
return useRender({
|
|
4160
|
+
defaultTagName: "span",
|
|
4161
|
+
render,
|
|
4162
|
+
ref: ref ? [ref] : [],
|
|
4163
|
+
state,
|
|
4164
|
+
stateAttributesMapping: weekCountStateAttributesMapping,
|
|
4165
|
+
props: mergeProps({ children: count }, otherProps)
|
|
4166
|
+
});
|
|
4167
|
+
}
|
|
4168
|
+
/**
|
|
4169
|
+
* Renders the current visible week count as a number. Useful with shrink
|
|
4170
|
+
* overflow modes where the actual count may be less than the `weekCount` prop.
|
|
4171
|
+
*/
|
|
4172
|
+
const WeekCount = forwardRef(WeekCountFn);
|
|
4173
|
+
//#endregion
|
|
4174
|
+
//#region src/temporal-polyfill.ts
|
|
4175
|
+
function daysInMonth(year, month) {
|
|
4176
|
+
return new Date(year, month, 0).getDate();
|
|
4177
|
+
}
|
|
4178
|
+
function toUTC(y, m, d) {
|
|
4179
|
+
return Date.UTC(y, m - 1, d);
|
|
4180
|
+
}
|
|
4181
|
+
function isoWeekday(utcDate) {
|
|
4182
|
+
const dow = utcDate.getUTCDay();
|
|
4183
|
+
return dow === 0 ? 7 : dow;
|
|
4184
|
+
}
|
|
4185
|
+
function pad2(n) {
|
|
4186
|
+
return n < 10 ? `0${n}` : `${n}`;
|
|
4187
|
+
}
|
|
4188
|
+
function clamp(val, min, max) {
|
|
4189
|
+
return val < min ? min : val > max ? max : val;
|
|
4190
|
+
}
|
|
4191
|
+
function parseDateISO(str) {
|
|
4192
|
+
const [datePart] = str.split("T");
|
|
4193
|
+
const [y, m, d] = datePart.split("-").map(Number);
|
|
4194
|
+
return {
|
|
4195
|
+
year: y,
|
|
4196
|
+
month: m,
|
|
4197
|
+
day: d
|
|
4198
|
+
};
|
|
4199
|
+
}
|
|
4200
|
+
function parseDateTimeISO(str) {
|
|
4201
|
+
const [datePart, timePart] = str.split("T");
|
|
4202
|
+
const [y, m, d] = datePart.split("-").map(Number);
|
|
4203
|
+
if (!timePart) return {
|
|
4204
|
+
year: y,
|
|
4205
|
+
month: m,
|
|
4206
|
+
day: d,
|
|
4207
|
+
hour: 0,
|
|
4208
|
+
minute: 0,
|
|
4209
|
+
second: 0
|
|
4210
|
+
};
|
|
4211
|
+
const [h, min, s] = timePart.split(":").map(Number);
|
|
4212
|
+
return {
|
|
4213
|
+
year: y,
|
|
4214
|
+
month: m,
|
|
4215
|
+
day: d,
|
|
4216
|
+
hour: h || 0,
|
|
4217
|
+
minute: min || 0,
|
|
4218
|
+
second: s || 0
|
|
4219
|
+
};
|
|
4220
|
+
}
|
|
4221
|
+
const dtfCache = /* @__PURE__ */ new Map();
|
|
4222
|
+
function getDTF(tz) {
|
|
4223
|
+
let dtf = dtfCache.get(tz);
|
|
4224
|
+
if (!dtf) {
|
|
4225
|
+
dtf = new Intl.DateTimeFormat("en-US", {
|
|
4226
|
+
timeZone: tz,
|
|
4227
|
+
year: "numeric",
|
|
4228
|
+
month: "numeric",
|
|
4229
|
+
day: "numeric",
|
|
4230
|
+
hour: "numeric",
|
|
4231
|
+
minute: "numeric",
|
|
4232
|
+
second: "numeric",
|
|
4233
|
+
hour12: false
|
|
4234
|
+
});
|
|
4235
|
+
dtfCache.set(tz, dtf);
|
|
4236
|
+
}
|
|
4237
|
+
return dtf;
|
|
4238
|
+
}
|
|
4239
|
+
function extractTzParts(epochMs, tz) {
|
|
4240
|
+
const parts = getDTF(tz).formatToParts(new Date(epochMs));
|
|
4241
|
+
const vals = {};
|
|
4242
|
+
for (const p of parts) if (p.type !== "literal") vals[p.type] = Number(p.value);
|
|
4243
|
+
return {
|
|
4244
|
+
year: vals.year,
|
|
4245
|
+
month: vals.month,
|
|
4246
|
+
day: vals.day,
|
|
4247
|
+
hour: vals.hour === 24 ? 0 : vals.hour,
|
|
4248
|
+
minute: vals.minute,
|
|
4249
|
+
second: vals.second
|
|
4250
|
+
};
|
|
4251
|
+
}
|
|
4252
|
+
function wallToEpoch(y, m, d, h, min, s, tz) {
|
|
4253
|
+
const utcGuess = Date.UTC(y, m - 1, d, h, min, s);
|
|
4254
|
+
const inTz = extractTzParts(utcGuess, tz);
|
|
4255
|
+
return utcGuess - (Date.UTC(inTz.year, inTz.month - 1, inTz.day, inTz.hour, inTz.minute, inTz.second) - utcGuess);
|
|
4256
|
+
}
|
|
4257
|
+
var MiniPlainDate = class MiniPlainDate {
|
|
4258
|
+
year;
|
|
4259
|
+
month;
|
|
4260
|
+
day;
|
|
4261
|
+
constructor(year, month, day) {
|
|
4262
|
+
this.year = year;
|
|
4263
|
+
this.month = month;
|
|
4264
|
+
this.day = day;
|
|
4265
|
+
}
|
|
4266
|
+
get dayOfWeek() {
|
|
4267
|
+
return isoWeekday(new Date(toUTC(this.year, this.month, this.day)));
|
|
4268
|
+
}
|
|
4269
|
+
get daysInWeek() {
|
|
4270
|
+
return 7;
|
|
4271
|
+
}
|
|
4272
|
+
get daysInMonth() {
|
|
4273
|
+
return daysInMonth(this.year, this.month);
|
|
4274
|
+
}
|
|
4275
|
+
get monthCode() {
|
|
4276
|
+
return `M${pad2(this.month)}`;
|
|
4277
|
+
}
|
|
4278
|
+
add(dur) {
|
|
4279
|
+
let y = this.year + (dur.years || 0);
|
|
4280
|
+
let m = this.month + (dur.months || 0);
|
|
4281
|
+
while (m > 12) {
|
|
4282
|
+
m -= 12;
|
|
4283
|
+
y += 1;
|
|
4284
|
+
}
|
|
4285
|
+
while (m < 1) {
|
|
4286
|
+
m += 12;
|
|
4287
|
+
y -= 1;
|
|
4288
|
+
}
|
|
4289
|
+
const d = Math.min(this.day, daysInMonth(y, m));
|
|
4290
|
+
const totalDays = (dur.days || 0) + (dur.weeks || 0) * 7;
|
|
4291
|
+
if (totalDays === 0) return new MiniPlainDate(y, m, d);
|
|
4292
|
+
const epoch = toUTC(y, m, d) + totalDays * 864e5;
|
|
4293
|
+
const ud = new Date(epoch);
|
|
4294
|
+
return new MiniPlainDate(ud.getUTCFullYear(), ud.getUTCMonth() + 1, ud.getUTCDate());
|
|
4295
|
+
}
|
|
4296
|
+
subtract(dur) {
|
|
4297
|
+
return this.add({
|
|
4298
|
+
days: dur.days ? -dur.days : void 0,
|
|
4299
|
+
weeks: dur.weeks ? -dur.weeks : void 0,
|
|
4300
|
+
months: dur.months ? -dur.months : void 0,
|
|
4301
|
+
years: dur.years ? -dur.years : void 0
|
|
4302
|
+
});
|
|
4303
|
+
}
|
|
4304
|
+
since(other, _opts) {
|
|
4305
|
+
const thisUTC = toUTC(this.year, this.month, this.day);
|
|
4306
|
+
const otherUTC = toUTC(other.year, other.month, other.day);
|
|
4307
|
+
return { days: Math.round((thisUTC - otherUTC) / 864e5) };
|
|
4308
|
+
}
|
|
4309
|
+
until(other, _opts) {
|
|
4310
|
+
const thisUTC = toUTC(this.year, this.month, this.day);
|
|
4311
|
+
const otherUTC = toUTC(other.year, other.month, other.day);
|
|
4312
|
+
return { days: Math.round((otherUTC - thisUTC) / 864e5) };
|
|
4313
|
+
}
|
|
4314
|
+
equals(other) {
|
|
4315
|
+
return this.year === other.year && this.month === other.month && this.day === other.day;
|
|
4316
|
+
}
|
|
4317
|
+
toString() {
|
|
4318
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}-${pad2(this.day)}`;
|
|
4319
|
+
}
|
|
4320
|
+
toZonedDateTime(tz) {
|
|
4321
|
+
const tzId = typeof tz === "string" ? tz : tz.timeZone;
|
|
4322
|
+
return this.toPlainDateTime().toZonedDateTime(tzId);
|
|
4323
|
+
}
|
|
4324
|
+
toPlainDateTime(timeLike) {
|
|
4325
|
+
return new MiniPlainDateTime(this.year, this.month, this.day, timeLike?.hour ?? 0, timeLike?.minute ?? 0, timeLike?.second ?? 0);
|
|
4326
|
+
}
|
|
4327
|
+
toPlainMonthDay() {
|
|
4328
|
+
return new MiniPlainMonthDay(this.month, this.day);
|
|
4329
|
+
}
|
|
4330
|
+
toPlainYearMonth() {
|
|
4331
|
+
return new MiniPlainYearMonth(this.year, this.month);
|
|
4332
|
+
}
|
|
4333
|
+
toLocaleString(locale, options) {
|
|
4334
|
+
return new Date(toUTC(this.year, this.month, this.day)).toLocaleDateString(locale, {
|
|
4335
|
+
timeZone: "UTC",
|
|
4336
|
+
...options
|
|
4337
|
+
});
|
|
4338
|
+
}
|
|
4339
|
+
};
|
|
4340
|
+
var MiniPlainDateTime = class {
|
|
4341
|
+
year;
|
|
4342
|
+
month;
|
|
4343
|
+
day;
|
|
4344
|
+
hour;
|
|
4345
|
+
minute;
|
|
4346
|
+
second;
|
|
4347
|
+
constructor(year, month, day, hour = 0, minute = 0, second = 0) {
|
|
4348
|
+
this.year = year;
|
|
4349
|
+
this.month = month;
|
|
4350
|
+
this.day = day;
|
|
4351
|
+
this.hour = hour;
|
|
4352
|
+
this.minute = minute;
|
|
4353
|
+
this.second = second;
|
|
4354
|
+
}
|
|
4355
|
+
get dayOfWeek() {
|
|
4356
|
+
return isoWeekday(new Date(toUTC(this.year, this.month, this.day)));
|
|
4357
|
+
}
|
|
4358
|
+
get daysInMonth() {
|
|
4359
|
+
return daysInMonth(this.year, this.month);
|
|
4360
|
+
}
|
|
4361
|
+
toZonedDateTime(tz) {
|
|
4362
|
+
const tzId = typeof tz === "string" ? tz : tz.timeZone;
|
|
4363
|
+
return new MiniZonedDateTime(wallToEpoch(this.year, this.month, this.day, this.hour, this.minute, this.second, tzId), tzId);
|
|
4364
|
+
}
|
|
4365
|
+
toPlainDate() {
|
|
4366
|
+
return new MiniPlainDate(this.year, this.month, this.day);
|
|
4367
|
+
}
|
|
4368
|
+
toString() {
|
|
4369
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}-${pad2(this.day)}T${pad2(this.hour)}:${pad2(this.minute)}:${pad2(this.second)}`;
|
|
4370
|
+
}
|
|
4371
|
+
};
|
|
4372
|
+
var MiniPlainMonthDay = class {
|
|
4373
|
+
monthCode;
|
|
4374
|
+
month;
|
|
4375
|
+
day;
|
|
4376
|
+
constructor(month, day) {
|
|
4377
|
+
this.month = month;
|
|
4378
|
+
this.day = day;
|
|
4379
|
+
this.monthCode = `M${pad2(month)}`;
|
|
4380
|
+
}
|
|
4381
|
+
toPlainDate(fields) {
|
|
4382
|
+
return new MiniPlainDate(fields.year, this.month, this.day);
|
|
4383
|
+
}
|
|
4384
|
+
equals(other) {
|
|
4385
|
+
return this.month === other.month && this.day === other.day;
|
|
4386
|
+
}
|
|
4387
|
+
toString() {
|
|
4388
|
+
return `${pad2(this.month)}-${pad2(this.day)}`;
|
|
4389
|
+
}
|
|
4390
|
+
};
|
|
4391
|
+
var MiniPlainYearMonth = class MiniPlainYearMonth {
|
|
4392
|
+
year;
|
|
4393
|
+
month;
|
|
4394
|
+
constructor(year, month) {
|
|
4395
|
+
this.year = year;
|
|
4396
|
+
this.month = month;
|
|
4397
|
+
}
|
|
4398
|
+
get monthCode() {
|
|
4399
|
+
return `M${pad2(this.month)}`;
|
|
4400
|
+
}
|
|
4401
|
+
get daysInMonth() {
|
|
4402
|
+
return daysInMonth(this.year, this.month);
|
|
4403
|
+
}
|
|
4404
|
+
toPlainDate(fields) {
|
|
4405
|
+
return new MiniPlainDate(this.year, this.month, fields.day);
|
|
4406
|
+
}
|
|
4407
|
+
add(dur) {
|
|
4408
|
+
let y = this.year + (dur.years || 0);
|
|
4409
|
+
let m = this.month + (dur.months || 0);
|
|
4410
|
+
while (m > 12) {
|
|
4411
|
+
m -= 12;
|
|
4412
|
+
y += 1;
|
|
4413
|
+
}
|
|
4414
|
+
while (m < 1) {
|
|
4415
|
+
m += 12;
|
|
4416
|
+
y -= 1;
|
|
4417
|
+
}
|
|
4418
|
+
return new MiniPlainYearMonth(y, m);
|
|
4419
|
+
}
|
|
4420
|
+
subtract(dur) {
|
|
4421
|
+
return this.add({
|
|
4422
|
+
months: dur.months ? -dur.months : void 0,
|
|
4423
|
+
years: dur.years ? -dur.years : void 0
|
|
4424
|
+
});
|
|
4425
|
+
}
|
|
4426
|
+
equals(other) {
|
|
4427
|
+
return this.year === other.year && this.month === other.month;
|
|
4428
|
+
}
|
|
4429
|
+
toString() {
|
|
4430
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}`;
|
|
4431
|
+
}
|
|
4432
|
+
};
|
|
4433
|
+
var MiniZonedDateTime = class {
|
|
4434
|
+
epochMilliseconds;
|
|
4435
|
+
timeZoneId;
|
|
4436
|
+
_parts;
|
|
4437
|
+
constructor(epochMs, timeZoneId) {
|
|
4438
|
+
this.epochMilliseconds = epochMs;
|
|
4439
|
+
this.timeZoneId = timeZoneId;
|
|
4440
|
+
}
|
|
4441
|
+
get parts() {
|
|
4442
|
+
if (!this._parts) this._parts = extractTzParts(this.epochMilliseconds, this.timeZoneId);
|
|
4443
|
+
return this._parts;
|
|
4444
|
+
}
|
|
4445
|
+
get year() {
|
|
4446
|
+
return this.parts.year;
|
|
4447
|
+
}
|
|
4448
|
+
get month() {
|
|
4449
|
+
return this.parts.month;
|
|
4450
|
+
}
|
|
4451
|
+
get day() {
|
|
4452
|
+
return this.parts.day;
|
|
4453
|
+
}
|
|
4454
|
+
get hour() {
|
|
4455
|
+
return this.parts.hour;
|
|
4456
|
+
}
|
|
4457
|
+
get minute() {
|
|
4458
|
+
return this.parts.minute;
|
|
4459
|
+
}
|
|
4460
|
+
get second() {
|
|
4461
|
+
return this.parts.second;
|
|
4462
|
+
}
|
|
4463
|
+
get dayOfWeek() {
|
|
4464
|
+
return isoWeekday(new Date(toUTC(this.year, this.month, this.day)));
|
|
4465
|
+
}
|
|
4466
|
+
toPlainDate() {
|
|
4467
|
+
return new MiniPlainDate(this.year, this.month, this.day);
|
|
4468
|
+
}
|
|
4469
|
+
toPlainDateTime() {
|
|
4470
|
+
return new MiniPlainDateTime(this.year, this.month, this.day, this.hour, this.minute, this.second);
|
|
4471
|
+
}
|
|
4472
|
+
toPlainTime() {
|
|
4473
|
+
return {
|
|
4474
|
+
hour: this.hour,
|
|
4475
|
+
minute: this.minute,
|
|
4476
|
+
second: this.second
|
|
4477
|
+
};
|
|
4478
|
+
}
|
|
4479
|
+
toPlainYearMonth() {
|
|
4480
|
+
return new MiniPlainYearMonth(this.year, this.month);
|
|
4481
|
+
}
|
|
4482
|
+
toString() {
|
|
4483
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}-${pad2(this.day)}T${pad2(this.hour)}:${pad2(this.minute)}:${pad2(this.second)}[${this.timeZoneId}]`;
|
|
4484
|
+
}
|
|
4485
|
+
};
|
|
4486
|
+
const PlainDate = {
|
|
4487
|
+
from(item, options) {
|
|
4488
|
+
if (typeof item === "string") {
|
|
4489
|
+
const { year, month, day } = parseDateISO(item);
|
|
4490
|
+
if (options?.overflow === "constrain") return new MiniPlainDate(year, month, clamp(day, 1, daysInMonth(year, month)));
|
|
4491
|
+
return new MiniPlainDate(year, month, day);
|
|
4492
|
+
}
|
|
4493
|
+
const year = item.year;
|
|
4494
|
+
const month = item.month;
|
|
4495
|
+
let day = item.day;
|
|
4496
|
+
if (options?.overflow === "constrain") day = clamp(day, 1, daysInMonth(year, month));
|
|
4497
|
+
return new MiniPlainDate(year, month, day);
|
|
4498
|
+
},
|
|
4499
|
+
compare(a, b) {
|
|
4500
|
+
if (a.year !== b.year) return Math.sign(a.year - b.year);
|
|
4501
|
+
if (a.month !== b.month) return Math.sign(a.month - b.month);
|
|
4502
|
+
if (a.day !== b.day) return Math.sign(a.day - b.day);
|
|
4503
|
+
return 0;
|
|
4504
|
+
}
|
|
4505
|
+
};
|
|
4506
|
+
const PlainDateTime = { from(item, _options) {
|
|
4507
|
+
if (typeof item === "string") {
|
|
4508
|
+
const { year, month, day, hour, minute, second } = parseDateTimeISO(item);
|
|
4509
|
+
return new MiniPlainDateTime(year, month, day, hour, minute, second);
|
|
4510
|
+
}
|
|
4511
|
+
return new MiniPlainDateTime(item.year, item.month, item.day, item.hour ?? 0, item.minute ?? 0, item.second ?? 0);
|
|
4512
|
+
} };
|
|
4513
|
+
const PlainMonthDay = { from(item) {
|
|
4514
|
+
if (typeof item === "string") {
|
|
4515
|
+
const parts = item.split("-").map(Number);
|
|
4516
|
+
if (parts.length >= 3) return new MiniPlainMonthDay(parts[1], parts[2]);
|
|
4517
|
+
return new MiniPlainMonthDay(parts[0], parts[1]);
|
|
4518
|
+
}
|
|
4519
|
+
return new MiniPlainMonthDay(item.month ?? (item.monthCode ? Number.parseInt(item.monthCode.slice(1), 10) : 1), item.day);
|
|
4520
|
+
} };
|
|
4521
|
+
const PlainYearMonth = {
|
|
4522
|
+
from(item) {
|
|
4523
|
+
if (typeof item === "string") {
|
|
4524
|
+
const [y, m] = item.split("-").map(Number);
|
|
4525
|
+
return new MiniPlainYearMonth(y, m);
|
|
4526
|
+
}
|
|
4527
|
+
return new MiniPlainYearMonth(item.year, item.month);
|
|
4528
|
+
},
|
|
4529
|
+
compare(a, b) {
|
|
4530
|
+
if (a.year !== b.year) return Math.sign(a.year - b.year);
|
|
4531
|
+
if (a.month !== b.month) return Math.sign(a.month - b.month);
|
|
4532
|
+
return 0;
|
|
4533
|
+
}
|
|
4534
|
+
};
|
|
4535
|
+
const Now = {
|
|
4536
|
+
timeZoneId() {
|
|
4537
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
4538
|
+
},
|
|
4539
|
+
zonedDateTimeISO(tz) {
|
|
4540
|
+
return new MiniZonedDateTime(Date.now(), tz ?? Now.timeZoneId());
|
|
4541
|
+
},
|
|
4542
|
+
plainDateISO() {
|
|
4543
|
+
const tz = Now.timeZoneId();
|
|
4544
|
+
const parts = extractTzParts(Date.now(), tz);
|
|
4545
|
+
return new MiniPlainDate(parts.year, parts.month, parts.day);
|
|
4546
|
+
}
|
|
4547
|
+
};
|
|
4548
|
+
const Temporal = {
|
|
4549
|
+
Now,
|
|
4550
|
+
PlainDate,
|
|
4551
|
+
PlainDateTime,
|
|
4552
|
+
PlainMonthDay,
|
|
4553
|
+
PlainYearMonth
|
|
4554
|
+
};
|
|
4555
|
+
//#endregion
|
|
4556
|
+
export { MonthSeparatorMonth as A, MonthViewRoot as B, WeekTemplate as C, useWeeksViewState as D, useWeeksViewStable as E, GridHeaderCell as F, computePreviewRange as G, useMonthViewStable as H, DayCellDataContext as I, useCalendarStable as K, GridContext as L, MonthSeparatorWeekCount as M, MonthSeparatorYear as N, MonthSeparator as O, GridHeader as P, WeekDataContext as R, GridBody as S, DayCellTemplate as T, useMonthViewState as U, useViewContext as V, CalendarProvider as W, MonthYearString as _, WeeksView as a, TimeString as b, resolveFirstWeekSpec as c, WeekNumberHeader as d, RangeEndDragHandle as f, DateString as g, RangeSelected as h, WeekCount as i, MonthSeparatorRow as j, MonthSeparatorCell as k, computeWeeksInWindow as l, RangePreview as m, NextWeeksButton as n, WeeksViewRoot as o, RangeStartDragHandle as p, useCalendarState as q, PrevWeeksButton as r, resolveFirstWeek as s, Temporal as t, WeekNumberCell as u, NextMonthButton as v, DayButton as w, Grid as x, PrevMonthButton as y, MonthView as z };
|