@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
package/src/grid.tsx
ADDED
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
4
|
+
import { useContext, useMemo, useCallback, useEffect, useRef, forwardRef } from "react";
|
|
5
|
+
import type { KeyboardEvent } from "react";
|
|
6
|
+
import { useCalendarStable } from "./calendar-context";
|
|
7
|
+
import { WeekDataContext, GridContext, GridMonthContext } from "./context";
|
|
8
|
+
import { GridHeader, GridHeaderCell } from "./grid-header";
|
|
9
|
+
import { computeNextFocusDate } from "./keyboard";
|
|
10
|
+
import {
|
|
11
|
+
MonthViewStableContext,
|
|
12
|
+
MonthViewStateContext,
|
|
13
|
+
useMonthViewStable,
|
|
14
|
+
useMonthViewState,
|
|
15
|
+
} from "./month-view-context";
|
|
16
|
+
import type { StateAttributesMapping } from "./types";
|
|
17
|
+
import type {
|
|
18
|
+
ValueFormat,
|
|
19
|
+
RootState,
|
|
20
|
+
GridState,
|
|
21
|
+
GridProps,
|
|
22
|
+
GridBodyState,
|
|
23
|
+
GridBodyProps,
|
|
24
|
+
WeekTemplateState,
|
|
25
|
+
WeekTemplateProps,
|
|
26
|
+
} from "./types";
|
|
27
|
+
import { useViewContext } from "./view-context";
|
|
28
|
+
import { WeeksGrid } from "./weeks-grid";
|
|
29
|
+
import { computeWeeksKeyNav } from "./weeks-keyboard";
|
|
30
|
+
import { WeeksViewStateContext, useWeeksViewState, useWeeksViewStable } from "./weeks-view-context";
|
|
31
|
+
export { GridHeader, GridHeaderCell } from "./grid-header";
|
|
32
|
+
export { DayCellTemplate, DayButton } from "./day-cell";
|
|
33
|
+
|
|
34
|
+
import { DayCellTemplate, DayButton } from "./day-cell";
|
|
35
|
+
|
|
36
|
+
function useGridKeyboard() {
|
|
37
|
+
const {
|
|
38
|
+
onSelect,
|
|
39
|
+
disabled,
|
|
40
|
+
readOnly,
|
|
41
|
+
isDateDisabled,
|
|
42
|
+
minValue,
|
|
43
|
+
maxValue,
|
|
44
|
+
temporal: T,
|
|
45
|
+
weekStartDay,
|
|
46
|
+
} = useCalendarStable();
|
|
47
|
+
const { focusedDate, setFocusedDate } = useViewContext();
|
|
48
|
+
|
|
49
|
+
return useCallback(
|
|
50
|
+
(e: KeyboardEvent<HTMLDivElement>) => {
|
|
51
|
+
const result = computeNextFocusDate({
|
|
52
|
+
key: e.key,
|
|
53
|
+
shiftKey: e.shiftKey,
|
|
54
|
+
focusedDate,
|
|
55
|
+
minValue,
|
|
56
|
+
maxValue,
|
|
57
|
+
disabled,
|
|
58
|
+
readOnly,
|
|
59
|
+
isDateDisabled,
|
|
60
|
+
T,
|
|
61
|
+
weekStartDay,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (result.action === "move") {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
setFocusedDate(result.date);
|
|
67
|
+
} else if (result.action === "select") {
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
onSelect(focusedDate);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
[
|
|
73
|
+
focusedDate,
|
|
74
|
+
setFocusedDate,
|
|
75
|
+
onSelect,
|
|
76
|
+
disabled,
|
|
77
|
+
readOnly,
|
|
78
|
+
isDateDisabled,
|
|
79
|
+
minValue,
|
|
80
|
+
maxValue,
|
|
81
|
+
T,
|
|
82
|
+
weekStartDay,
|
|
83
|
+
],
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function useWeeksGridKeyboard() {
|
|
88
|
+
const {
|
|
89
|
+
onSelect,
|
|
90
|
+
disabled,
|
|
91
|
+
readOnly,
|
|
92
|
+
isDateDisabled,
|
|
93
|
+
minValue,
|
|
94
|
+
maxValue,
|
|
95
|
+
temporal: T,
|
|
96
|
+
weekStartDay,
|
|
97
|
+
} = useCalendarStable();
|
|
98
|
+
const { focusedDate, setFocusedDate } = useViewContext();
|
|
99
|
+
const weeksStable = useWeeksViewStable();
|
|
100
|
+
const weeksState = useWeeksViewState();
|
|
101
|
+
|
|
102
|
+
return useCallback(
|
|
103
|
+
(e: KeyboardEvent<HTMLDivElement>) => {
|
|
104
|
+
const result = computeWeeksKeyNav({
|
|
105
|
+
key: e.key,
|
|
106
|
+
shiftKey: e.shiftKey,
|
|
107
|
+
focusedDate,
|
|
108
|
+
windowStart: weeksState.windowInfo.windowStart,
|
|
109
|
+
weekCount: weeksStable.weekCount,
|
|
110
|
+
minValue,
|
|
111
|
+
maxValue,
|
|
112
|
+
disabled,
|
|
113
|
+
readOnly,
|
|
114
|
+
isDateDisabled,
|
|
115
|
+
scrollBy: weeksStable.scrollBy,
|
|
116
|
+
T,
|
|
117
|
+
weekStartDay,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (result.action === "move") {
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
setFocusedDate(result.date);
|
|
123
|
+
if (result.followFocus) {
|
|
124
|
+
// Large jump (e.g. Shift+PageUp/Down for ±1 year) — reposition window around new focus
|
|
125
|
+
weeksStable.scrollToWeek(result.date, { snap: "start" });
|
|
126
|
+
} else if (result.windowShift !== 0) {
|
|
127
|
+
// Focus moved just outside the visible window — scroll minimally to keep it visible
|
|
128
|
+
weeksStable.scrollToWeek(result.date, { snap: "nearest" });
|
|
129
|
+
}
|
|
130
|
+
} else if (result.action === "select") {
|
|
131
|
+
e.preventDefault();
|
|
132
|
+
onSelect(focusedDate);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
[
|
|
136
|
+
focusedDate,
|
|
137
|
+
setFocusedDate,
|
|
138
|
+
onSelect,
|
|
139
|
+
disabled,
|
|
140
|
+
readOnly,
|
|
141
|
+
isDateDisabled,
|
|
142
|
+
minValue,
|
|
143
|
+
maxValue,
|
|
144
|
+
T,
|
|
145
|
+
weekStartDay,
|
|
146
|
+
weeksStable,
|
|
147
|
+
weeksState.windowInfo.windowStart,
|
|
148
|
+
],
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const gridStateAttributesMapping = {
|
|
153
|
+
root: () => null,
|
|
154
|
+
month: () => null,
|
|
155
|
+
year: () => null,
|
|
156
|
+
orientation: (v) => ({ "data-orientation": v }),
|
|
157
|
+
} as const satisfies StateAttributesMapping<GridState>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Calendar grid container. Renders a `<table>` with `role="grid"` by default.
|
|
161
|
+
*
|
|
162
|
+
* Sets CSS custom properties `--calendar-days-per-week` and
|
|
163
|
+
* `--calendar-weeks-in-month`, and matching `data-*` attributes.
|
|
164
|
+
* Manages keyboard navigation, grid focus tracking, and
|
|
165
|
+
* `aria-labelledby` linkage to {@link MonthYearString}.
|
|
166
|
+
*
|
|
167
|
+
* Automatically detects the active view type (MonthView or WeeksView) and
|
|
168
|
+
* delegates to the appropriate internal renderer.
|
|
169
|
+
*/
|
|
170
|
+
function GridFn(props: GridProps, ref: React.ForwardedRef<HTMLTableElement>) {
|
|
171
|
+
const monthState = useContext(MonthViewStableContext);
|
|
172
|
+
const weeksState = useContext(WeeksViewStateContext);
|
|
173
|
+
|
|
174
|
+
if (monthState) {
|
|
175
|
+
return <MonthGrid ref={ref} {...props} />;
|
|
176
|
+
}
|
|
177
|
+
if (weeksState) {
|
|
178
|
+
return <WeeksViewGrid ref={ref} {...props} />;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
throw new Error("Grid must be used inside MonthView.Root or WeeksView.Root.");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const Grid = forwardRef(GridFn) as <F extends ValueFormat = ValueFormat>(
|
|
185
|
+
props: GridProps<F> & React.RefAttributes<HTMLTableElement>,
|
|
186
|
+
) => React.ReactElement | null;
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// MonthGrid — existing month view rendering (unchanged logic)
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
function MonthGridFn(props: GridProps, ref: React.ForwardedRef<HTMLTableElement>) {
|
|
193
|
+
const {
|
|
194
|
+
render,
|
|
195
|
+
mode: _mode,
|
|
196
|
+
orientation,
|
|
197
|
+
autoFocus,
|
|
198
|
+
monthIndex: monthIndexProp,
|
|
199
|
+
children,
|
|
200
|
+
...otherProps
|
|
201
|
+
} = props;
|
|
202
|
+
const monthIndex = monthIndexProp ?? 0;
|
|
203
|
+
const { setHoveredDate } = useCalendarStable();
|
|
204
|
+
const { setGridHasFocus } = useViewContext();
|
|
205
|
+
const monthViewStable = useMonthViewStable();
|
|
206
|
+
const monthViewState = useMonthViewState();
|
|
207
|
+
const { gridFocusedRef } = monthViewStable;
|
|
208
|
+
const {
|
|
209
|
+
currentMonth: currentDateTime,
|
|
210
|
+
gridLabelIds,
|
|
211
|
+
rootState,
|
|
212
|
+
weeks: defaultWeeks,
|
|
213
|
+
allMonths,
|
|
214
|
+
} = monthViewState;
|
|
215
|
+
|
|
216
|
+
if (monthIndex >= allMonths.length) {
|
|
217
|
+
console.warn(
|
|
218
|
+
`[DatePicker] Grid monthIndex={${monthIndex}} is out of bounds (numberOfMonths=${allMonths.length}). Falling back to the first month.`,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
const monthData = allMonths[monthIndex];
|
|
222
|
+
const gridWeeks = monthData?.weeks ?? defaultWeeks;
|
|
223
|
+
|
|
224
|
+
const handleKeyDown = useGridKeyboard();
|
|
225
|
+
const gridRef = useRef<HTMLTableElement>(null);
|
|
226
|
+
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
if (autoFocus && gridRef.current) {
|
|
229
|
+
const target = gridRef.current.querySelector<HTMLElement>('[tabindex="0"]');
|
|
230
|
+
if (target) {
|
|
231
|
+
target.focus();
|
|
232
|
+
gridFocusedRef.current = true;
|
|
233
|
+
setGridHasFocus(true);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}, [autoFocus, gridFocusedRef, setGridHasFocus]);
|
|
237
|
+
|
|
238
|
+
const resolvedOrientation = orientation ?? "horizontal";
|
|
239
|
+
const daysPerWeek = gridWeeks[0]?.length ?? 7;
|
|
240
|
+
const weeksInMonth = gridWeeks.length;
|
|
241
|
+
|
|
242
|
+
const gridMonth = monthData ?? {
|
|
243
|
+
year: currentDateTime.year,
|
|
244
|
+
month: currentDateTime.month,
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const state = useMemo<GridState>(
|
|
248
|
+
() => ({
|
|
249
|
+
root: rootState as unknown as GridState["root"],
|
|
250
|
+
month: gridMonth.month,
|
|
251
|
+
year: gridMonth.year,
|
|
252
|
+
orientation: resolvedOrientation,
|
|
253
|
+
}),
|
|
254
|
+
[rootState, gridMonth.month, gridMonth.year, resolvedOrientation],
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const defaultProps: Record<string, unknown> = {
|
|
258
|
+
role: "grid",
|
|
259
|
+
"aria-labelledby": gridLabelIds[monthIndex] || undefined,
|
|
260
|
+
"aria-label": gridLabelIds[monthIndex] ? undefined : "Calendar",
|
|
261
|
+
"data-days-per-week": daysPerWeek,
|
|
262
|
+
"data-weeks-in-month": weeksInMonth,
|
|
263
|
+
style: {
|
|
264
|
+
"--calendar-days-per-week": daysPerWeek,
|
|
265
|
+
"--calendar-weeks-in-month": weeksInMonth,
|
|
266
|
+
} as React.CSSProperties,
|
|
267
|
+
onKeyDown: handleKeyDown,
|
|
268
|
+
onFocus: () => {
|
|
269
|
+
gridFocusedRef.current = true;
|
|
270
|
+
setGridHasFocus(true);
|
|
271
|
+
},
|
|
272
|
+
onBlur: () => {
|
|
273
|
+
gridFocusedRef.current = false;
|
|
274
|
+
setGridHasFocus(false);
|
|
275
|
+
},
|
|
276
|
+
onPointerLeave: () => {
|
|
277
|
+
setHoveredDate(undefined);
|
|
278
|
+
},
|
|
279
|
+
children: children ?? (
|
|
280
|
+
<>
|
|
281
|
+
<GridHeader>
|
|
282
|
+
<GridHeaderCell />
|
|
283
|
+
</GridHeader>
|
|
284
|
+
<GridBody>
|
|
285
|
+
<WeekTemplate>
|
|
286
|
+
<DayCellTemplate>
|
|
287
|
+
<DayButton />
|
|
288
|
+
</DayCellTemplate>
|
|
289
|
+
</WeekTemplate>
|
|
290
|
+
</GridBody>
|
|
291
|
+
</>
|
|
292
|
+
),
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const el = useRender({
|
|
296
|
+
defaultTagName: "table",
|
|
297
|
+
render,
|
|
298
|
+
ref: ref ? [ref, gridRef] : [gridRef],
|
|
299
|
+
state,
|
|
300
|
+
stateAttributesMapping: gridStateAttributesMapping,
|
|
301
|
+
props: mergeProps<"table">(defaultProps, otherProps),
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
const orientationCtx = useMemo(
|
|
305
|
+
() => ({ orientation: resolvedOrientation }),
|
|
306
|
+
[resolvedOrientation],
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const monthCtx = useMemo(
|
|
310
|
+
() => ({
|
|
311
|
+
weeks: gridWeeks,
|
|
312
|
+
year: gridMonth.year,
|
|
313
|
+
month: gridMonth.month,
|
|
314
|
+
}),
|
|
315
|
+
[gridWeeks, gridMonth.year, gridMonth.month],
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
return (
|
|
319
|
+
<GridMonthContext.Provider value={monthCtx}>
|
|
320
|
+
<GridContext.Provider value={orientationCtx}>{el}</GridContext.Provider>
|
|
321
|
+
</GridMonthContext.Provider>
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const MonthGrid = forwardRef(MonthGridFn) as <F extends ValueFormat = ValueFormat>(
|
|
326
|
+
props: GridProps<F> & React.RefAttributes<HTMLTableElement>,
|
|
327
|
+
) => React.ReactElement | null;
|
|
328
|
+
|
|
329
|
+
// ---------------------------------------------------------------------------
|
|
330
|
+
// WeeksViewGrid — continuous week rows for WeeksView
|
|
331
|
+
// ---------------------------------------------------------------------------
|
|
332
|
+
|
|
333
|
+
function WeeksViewGridFn(props: GridProps, ref: React.ForwardedRef<HTMLTableElement>) {
|
|
334
|
+
const {
|
|
335
|
+
render,
|
|
336
|
+
mode: _mode,
|
|
337
|
+
orientation,
|
|
338
|
+
autoFocus,
|
|
339
|
+
monthIndex: _monthIndex,
|
|
340
|
+
children,
|
|
341
|
+
...otherProps
|
|
342
|
+
} = props;
|
|
343
|
+
const { setHoveredDate } = useCalendarStable();
|
|
344
|
+
const { setGridHasFocus } = useViewContext();
|
|
345
|
+
const weeksStable = useWeeksViewStable();
|
|
346
|
+
const weeksState = useWeeksViewState();
|
|
347
|
+
const { gridFocusedRef } = weeksStable;
|
|
348
|
+
const { gridLabelIds, weeks, currentDateTime } = weeksState;
|
|
349
|
+
|
|
350
|
+
const handleKeyDown = useWeeksGridKeyboard();
|
|
351
|
+
const gridRef = useRef<HTMLTableElement>(null);
|
|
352
|
+
|
|
353
|
+
useEffect(() => {
|
|
354
|
+
if (autoFocus && gridRef.current) {
|
|
355
|
+
const target = gridRef.current.querySelector<HTMLElement>('[tabindex="0"]');
|
|
356
|
+
if (target) {
|
|
357
|
+
target.focus();
|
|
358
|
+
gridFocusedRef.current = true;
|
|
359
|
+
setGridHasFocus(true);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}, [autoFocus, gridFocusedRef, setGridHasFocus]);
|
|
363
|
+
|
|
364
|
+
const resolvedOrientation = orientation ?? "horizontal";
|
|
365
|
+
const weekCount = weeks.length;
|
|
366
|
+
|
|
367
|
+
// Use the first week's month/year as a reasonable default for state.
|
|
368
|
+
const firstWeek = weeks[0];
|
|
369
|
+
const gridMonth = firstWeek
|
|
370
|
+
? { month: firstWeek.month, year: firstWeek.year }
|
|
371
|
+
: { month: 1, year: 1970 };
|
|
372
|
+
|
|
373
|
+
const state = useMemo<GridState>(
|
|
374
|
+
() => ({
|
|
375
|
+
root: {} as GridState["root"],
|
|
376
|
+
month: gridMonth.month,
|
|
377
|
+
year: gridMonth.year,
|
|
378
|
+
orientation: resolvedOrientation,
|
|
379
|
+
}),
|
|
380
|
+
[gridMonth.month, gridMonth.year, resolvedOrientation],
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
// Default children: header + WeeksGrid tbody with a <tr> per week.
|
|
384
|
+
// WeeksGrid provides WeekDataContext per row so DayCellTemplate works.
|
|
385
|
+
const weekRowChildren = children ?? (
|
|
386
|
+
<>
|
|
387
|
+
<GridHeader>
|
|
388
|
+
<GridHeaderCell />
|
|
389
|
+
</GridHeader>
|
|
390
|
+
<WeeksGrid>
|
|
391
|
+
<tr>
|
|
392
|
+
<DayCellTemplate>
|
|
393
|
+
<DayButton />
|
|
394
|
+
</DayCellTemplate>
|
|
395
|
+
</tr>
|
|
396
|
+
</WeeksGrid>
|
|
397
|
+
</>
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
const defaultProps: Record<string, unknown> = {
|
|
401
|
+
role: "grid",
|
|
402
|
+
"aria-labelledby": gridLabelIds[0] || undefined,
|
|
403
|
+
"aria-label": gridLabelIds[0] ? undefined : "Calendar",
|
|
404
|
+
"data-days-per-week": 7,
|
|
405
|
+
"data-weeks-in-month": weekCount,
|
|
406
|
+
style: {
|
|
407
|
+
"--calendar-days-per-week": 7,
|
|
408
|
+
"--calendar-weeks-in-month": weekCount,
|
|
409
|
+
} as React.CSSProperties,
|
|
410
|
+
onKeyDown: handleKeyDown,
|
|
411
|
+
onFocus: () => {
|
|
412
|
+
gridFocusedRef.current = true;
|
|
413
|
+
setGridHasFocus(true);
|
|
414
|
+
},
|
|
415
|
+
onBlur: () => {
|
|
416
|
+
gridFocusedRef.current = false;
|
|
417
|
+
setGridHasFocus(false);
|
|
418
|
+
},
|
|
419
|
+
onPointerLeave: () => {
|
|
420
|
+
setHoveredDate(undefined);
|
|
421
|
+
},
|
|
422
|
+
children: weekRowChildren,
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const el = useRender({
|
|
426
|
+
defaultTagName: "table",
|
|
427
|
+
render,
|
|
428
|
+
ref: ref ? [ref, gridRef] : [gridRef],
|
|
429
|
+
state,
|
|
430
|
+
stateAttributesMapping: gridStateAttributesMapping,
|
|
431
|
+
props: mergeProps<"table">(defaultProps, otherProps),
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
const orientationCtx = useMemo(
|
|
435
|
+
() => ({ orientation: resolvedOrientation }),
|
|
436
|
+
[resolvedOrientation],
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
// Provide a MonthViewStableContext shim so shared components
|
|
440
|
+
// (DayCellTemplate, DayButton, etc.) that read it can function in WeeksView.
|
|
441
|
+
const monthViewStableShim = useMemo(
|
|
442
|
+
() => ({
|
|
443
|
+
numberOfMonths: 1,
|
|
444
|
+
fixedWeeks: false,
|
|
445
|
+
outsideDays: "enabled" as const,
|
|
446
|
+
overflowBehavior: "unbounded" as const,
|
|
447
|
+
goNextMonth: () => {},
|
|
448
|
+
goPrevMonth: () => {},
|
|
449
|
+
setGridLabelId: () => {},
|
|
450
|
+
gridFocusedRef,
|
|
451
|
+
}),
|
|
452
|
+
[gridFocusedRef],
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
// Provide a MonthViewStateContext shim with reasonable defaults derived
|
|
456
|
+
// from the WeeksView state so WeekTemplate/DayCellTemplate/DayButton work.
|
|
457
|
+
const monthViewStateShim = useMemo(
|
|
458
|
+
() => ({
|
|
459
|
+
currentMonth: {
|
|
460
|
+
year: currentDateTime.year,
|
|
461
|
+
month: currentDateTime.month,
|
|
462
|
+
},
|
|
463
|
+
weeks: [] as Temporal.PlainDate[][],
|
|
464
|
+
allMonths: [],
|
|
465
|
+
currentDateTime,
|
|
466
|
+
gridLabelIds,
|
|
467
|
+
rootState: {} as RootState,
|
|
468
|
+
}),
|
|
469
|
+
[currentDateTime, gridLabelIds],
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
return (
|
|
473
|
+
<MonthViewStableContext.Provider value={monthViewStableShim}>
|
|
474
|
+
<MonthViewStateContext.Provider value={monthViewStateShim}>
|
|
475
|
+
<GridContext.Provider value={orientationCtx}>{el}</GridContext.Provider>
|
|
476
|
+
</MonthViewStateContext.Provider>
|
|
477
|
+
</MonthViewStableContext.Provider>
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const WeeksViewGrid = forwardRef(WeeksViewGridFn) as <F extends ValueFormat = ValueFormat>(
|
|
482
|
+
props: GridProps<F> & React.RefAttributes<HTMLTableElement>,
|
|
483
|
+
) => React.ReactElement | null;
|
|
484
|
+
|
|
485
|
+
const gridBodyStateAttributesMapping = {
|
|
486
|
+
root: () => null,
|
|
487
|
+
} as const satisfies StateAttributesMapping<GridBodyState>;
|
|
488
|
+
|
|
489
|
+
/** Table body wrapping the week rows. Renders a `<tbody>` by default. */
|
|
490
|
+
function GridBodyFn(props: GridBodyProps, ref: React.ForwardedRef<HTMLTableSectionElement>) {
|
|
491
|
+
const weeksState = useContext(WeeksViewStateContext);
|
|
492
|
+
|
|
493
|
+
// In WeeksView, delegate to WeeksGrid which handles week iteration
|
|
494
|
+
// and MonthSeparator context provision.
|
|
495
|
+
if (weeksState) {
|
|
496
|
+
const { children, className, style } = props as any;
|
|
497
|
+
return (
|
|
498
|
+
<WeeksGrid className={className} style={style}>
|
|
499
|
+
{children}
|
|
500
|
+
</WeeksGrid>
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return <MonthGridBody ref={ref} {...props} />;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export const GridBody = forwardRef(GridBodyFn) as <F extends ValueFormat = ValueFormat>(
|
|
508
|
+
props: GridBodyProps<F> & React.RefAttributes<HTMLTableSectionElement>,
|
|
509
|
+
) => React.ReactElement | null;
|
|
510
|
+
|
|
511
|
+
function MonthGridBodyFn(props: GridBodyProps, ref: React.ForwardedRef<HTMLTableSectionElement>) {
|
|
512
|
+
const { render, ...otherProps } = props;
|
|
513
|
+
const { rootState } = useMonthViewState();
|
|
514
|
+
|
|
515
|
+
const state = useMemo<GridBodyState>(
|
|
516
|
+
() => ({ root: rootState as unknown as GridBodyState["root"] }),
|
|
517
|
+
[rootState],
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
const defaultProps: Record<string, unknown> = {
|
|
521
|
+
children: props.children ?? (
|
|
522
|
+
<WeekTemplate>
|
|
523
|
+
<DayCellTemplate>
|
|
524
|
+
<DayButton />
|
|
525
|
+
</DayCellTemplate>
|
|
526
|
+
</WeekTemplate>
|
|
527
|
+
),
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const { children: _children, ...restOtherProps } = otherProps;
|
|
531
|
+
|
|
532
|
+
return useRender({
|
|
533
|
+
defaultTagName: "tbody",
|
|
534
|
+
render,
|
|
535
|
+
ref: ref ? [ref] : [],
|
|
536
|
+
state,
|
|
537
|
+
stateAttributesMapping: gridBodyStateAttributesMapping,
|
|
538
|
+
props: mergeProps<"tbody">(defaultProps, restOtherProps),
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const MonthGridBody = forwardRef(MonthGridBodyFn) as <F extends ValueFormat = ValueFormat>(
|
|
543
|
+
props: GridBodyProps<F> & React.RefAttributes<HTMLTableSectionElement>,
|
|
544
|
+
) => React.ReactElement | null;
|
|
545
|
+
|
|
546
|
+
const weekInstanceStateAttributesMapping = {
|
|
547
|
+
root: () => null,
|
|
548
|
+
weekIndex: () => null,
|
|
549
|
+
gridRowIndex: () => null,
|
|
550
|
+
} as const satisfies StateAttributesMapping<WeekTemplateState>;
|
|
551
|
+
|
|
552
|
+
function WeekInstance<F extends ValueFormat = ValueFormat>(props: WeekTemplateProps<F>) {
|
|
553
|
+
const { render, ...otherProps } = props;
|
|
554
|
+
const weekData = useContext(WeekDataContext)!;
|
|
555
|
+
const { rootState } = useMonthViewState();
|
|
556
|
+
|
|
557
|
+
const state = useMemo<WeekTemplateState<F>>(
|
|
558
|
+
() => ({
|
|
559
|
+
root: rootState as unknown as WeekTemplateState<F>["root"],
|
|
560
|
+
weekIndex: weekData.weekIndex,
|
|
561
|
+
gridRowIndex: weekData.gridRowIndex,
|
|
562
|
+
}),
|
|
563
|
+
[rootState, weekData.weekIndex, weekData.gridRowIndex],
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
return useRender({
|
|
567
|
+
defaultTagName: "tr",
|
|
568
|
+
render,
|
|
569
|
+
ref: [],
|
|
570
|
+
state,
|
|
571
|
+
stateAttributesMapping: weekInstanceStateAttributesMapping,
|
|
572
|
+
props: mergeProps<"tr">({}, otherProps),
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Iterates over the weeks in the current month and renders one `<tr>` per week.
|
|
578
|
+
* Each instance receives its week's days and index via {@link WeekDataContext}.
|
|
579
|
+
*/
|
|
580
|
+
export function WeekTemplate<F extends ValueFormat = ValueFormat>(props: WeekTemplateProps<F>) {
|
|
581
|
+
const outerWeekData = useContext(WeekDataContext);
|
|
582
|
+
const gridMonthCtx = useContext(GridMonthContext);
|
|
583
|
+
const { weeks: defaultWeeks } = useMonthViewState();
|
|
584
|
+
const weeks = gridMonthCtx?.weeks ?? defaultWeeks;
|
|
585
|
+
const gridMonth = useMemo(
|
|
586
|
+
() => (gridMonthCtx ? { year: gridMonthCtx.year, month: gridMonthCtx.month } : undefined),
|
|
587
|
+
[gridMonthCtx],
|
|
588
|
+
);
|
|
589
|
+
const Instance = WeekInstance<F>;
|
|
590
|
+
|
|
591
|
+
return (
|
|
592
|
+
<>
|
|
593
|
+
{weeks.map((weekDays, i) => (
|
|
594
|
+
<WeekDataContext.Provider
|
|
595
|
+
key={weekDays[0].toString()}
|
|
596
|
+
value={{
|
|
597
|
+
days: weekDays,
|
|
598
|
+
weekIndex: i,
|
|
599
|
+
gridMonth,
|
|
600
|
+
gridRowIndex: outerWeekData?.gridRowIndex,
|
|
601
|
+
}}
|
|
602
|
+
>
|
|
603
|
+
<Instance {...props} />
|
|
604
|
+
</WeekDataContext.Provider>
|
|
605
|
+
))}
|
|
606
|
+
</>
|
|
607
|
+
);
|
|
608
|
+
}
|