@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,265 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
/**
|
|
3
|
+
* @internal WeeksGrid — renders continuous week rows for WeeksView.
|
|
4
|
+
* Not exported from the public API.
|
|
5
|
+
*/
|
|
6
|
+
import React, { useMemo, Children, isValidElement } from "react";
|
|
7
|
+
import { useCalendarStable } from "./calendar-context";
|
|
8
|
+
import type { WeekDescriptor } from "./compute-weeks-in-window";
|
|
9
|
+
import { WeekDataContext, GridMonthContext } from "./context";
|
|
10
|
+
import { MonthSeparatorDataContext, MonthSeparator, MonthSeparatorRow } from "./month-separator";
|
|
11
|
+
import type { MonthSeparatorState } from "./month-separator";
|
|
12
|
+
import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
13
|
+
import { useWeeksViewState } from "./weeks-view-context";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Expand a WeekDescriptor into the 7 PlainDate values for that week.
|
|
17
|
+
*/
|
|
18
|
+
function weekDays(week: WeekDescriptor, _T: TemporalNamespace): Temporal.PlainDate[] {
|
|
19
|
+
const days: Temporal.PlainDate[] = [];
|
|
20
|
+
let d = week.startDate;
|
|
21
|
+
for (let i = 0; i < 7; i++) {
|
|
22
|
+
days.push(d);
|
|
23
|
+
if (i < 6) d = d.add({ days: 1 });
|
|
24
|
+
}
|
|
25
|
+
return days;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Check if a React element is a MonthSeparator (or wraps one).
|
|
30
|
+
*/
|
|
31
|
+
function isSeparatorElement(child: React.ReactNode): boolean {
|
|
32
|
+
if (!isValidElement(child)) return false;
|
|
33
|
+
return (
|
|
34
|
+
child.type === MonthSeparator ||
|
|
35
|
+
child.type === MonthSeparatorRow ||
|
|
36
|
+
child.type === (MonthSeparator as any)
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Renders the `<tbody>` contents for a WeeksView grid.
|
|
42
|
+
* Iterates over the weeks array and provides WeekDataContext per row.
|
|
43
|
+
* Renders MonthSeparator children at month boundaries with proper context.
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* Compute the 0-based column index of a date within a week row.
|
|
47
|
+
* If hasWeekNumbers, column 0 is the week number column, and day columns are 1-7.
|
|
48
|
+
* Otherwise day columns are 0-6.
|
|
49
|
+
*/
|
|
50
|
+
function dayColumnIndex(
|
|
51
|
+
date: Temporal.PlainDate,
|
|
52
|
+
weekStartDay: WeekStartDay,
|
|
53
|
+
hasWeekNumbers: boolean,
|
|
54
|
+
): number {
|
|
55
|
+
const dow = date.dayOfWeek % 7; // 0=Sun
|
|
56
|
+
const col = (dow - weekStartDay + 7) % 7;
|
|
57
|
+
return hasWeekNumbers ? col + 1 : col;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function WeeksGrid(props: {
|
|
61
|
+
children: React.ReactNode;
|
|
62
|
+
className?: string;
|
|
63
|
+
style?: React.CSSProperties;
|
|
64
|
+
hasWeekNumbers?: boolean;
|
|
65
|
+
}) {
|
|
66
|
+
const { children, className, style, hasWeekNumbers = false } = props;
|
|
67
|
+
const { temporal: T, weekStartDay } = useCalendarStable();
|
|
68
|
+
const { weeks } = useWeeksViewState();
|
|
69
|
+
|
|
70
|
+
// Separate children into separator template and week row templates.
|
|
71
|
+
// Also detect if week numbers are present by checking for WeekNumberCell-like children.
|
|
72
|
+
const separatorTemplate: React.ReactNode[] = [];
|
|
73
|
+
const weekRowChildren: React.ReactNode[] = [];
|
|
74
|
+
Children.forEach(children, (child) => {
|
|
75
|
+
if (isSeparatorElement(child)) {
|
|
76
|
+
separatorTemplate.push(child);
|
|
77
|
+
} else {
|
|
78
|
+
weekRowChildren.push(child);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Pre-compute days arrays for all weeks (memoised on weeks identity).
|
|
83
|
+
const weekDaysArrays = useMemo(() => weeks.map((w) => weekDays(w, T)), [weeks, T]);
|
|
84
|
+
|
|
85
|
+
const totalColumns = hasWeekNumbers ? 8 : 7;
|
|
86
|
+
|
|
87
|
+
// Pre-compute month boundary data for separators.
|
|
88
|
+
// A separator appears before the first week where ANY day is in a new month.
|
|
89
|
+
const monthBoundaries = useMemo(() => {
|
|
90
|
+
const boundaries: Array<{ index: number; data: MonthSeparatorState }> = [];
|
|
91
|
+
// Track which months we've already created separators for
|
|
92
|
+
const seenMonths = new Set<string>();
|
|
93
|
+
|
|
94
|
+
for (let i = 0; i < weeks.length; i++) {
|
|
95
|
+
const days = weekDaysArrays[i];
|
|
96
|
+
const isFirstWeek = i === 0;
|
|
97
|
+
|
|
98
|
+
// Find all distinct months in this week's days
|
|
99
|
+
const monthsInWeek: Array<{ month: number; year: number; key: string }> = [];
|
|
100
|
+
for (const day of days) {
|
|
101
|
+
const monthKey = `${day.year}-${day.month}`;
|
|
102
|
+
if (!seenMonths.has(monthKey) && !monthsInWeek.some((m) => m.key === monthKey)) {
|
|
103
|
+
monthsInWeek.push({
|
|
104
|
+
month: day.month,
|
|
105
|
+
year: day.year,
|
|
106
|
+
key: monthKey,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// If the first week spans two months, only show the second (newer) month
|
|
112
|
+
const monthsToShow =
|
|
113
|
+
isFirstWeek && monthsInWeek.length > 1 ? monthsInWeek.slice(1) : monthsInWeek;
|
|
114
|
+
|
|
115
|
+
// Mark all months in this week as seen (even skipped ones)
|
|
116
|
+
for (const m of monthsInWeek) {
|
|
117
|
+
seenMonths.add(m.key);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (const { month: newMonth, year: newYear } of monthsToShow) {
|
|
121
|
+
// Compute the column of the 1st of this month in the week row
|
|
122
|
+
const firstOfMonth = T.PlainDate.from({
|
|
123
|
+
year: newYear,
|
|
124
|
+
month: newMonth,
|
|
125
|
+
day: 1,
|
|
126
|
+
});
|
|
127
|
+
const firstDayCol = dayColumnIndex(firstOfMonth, weekStartDay, hasWeekNumbers);
|
|
128
|
+
|
|
129
|
+
// Check if the 1st day of this month is actually visible in the window
|
|
130
|
+
const windowStart = weekDaysArrays[0]?.[0];
|
|
131
|
+
const windowEnd = weekDaysArrays[weekDaysArrays.length - 1]?.[6];
|
|
132
|
+
const firstDayVisible =
|
|
133
|
+
windowStart && windowEnd
|
|
134
|
+
? T.PlainDate.compare(firstOfMonth, windowStart) >= 0 &&
|
|
135
|
+
T.PlainDate.compare(firstOfMonth, windowEnd) <= 0
|
|
136
|
+
: false;
|
|
137
|
+
|
|
138
|
+
// Count weeks visible before (weeks above that belong to previous month)
|
|
139
|
+
let weeksVisibleBefore = 0;
|
|
140
|
+
for (let j = i - 1; j >= 0; j--) {
|
|
141
|
+
// Check if all days in that week are before this month
|
|
142
|
+
const prevDays = weekDaysArrays[j];
|
|
143
|
+
const hasThisMonth = prevDays.some((d) => d.month === newMonth && d.year === newYear);
|
|
144
|
+
if (!hasThisMonth) {
|
|
145
|
+
weeksVisibleBefore++;
|
|
146
|
+
} else {
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Count weeks visible after (weeks containing days of this month)
|
|
152
|
+
let weeksVisibleAfter = 0;
|
|
153
|
+
for (let j = i; j < weeks.length; j++) {
|
|
154
|
+
const wDays = weekDaysArrays[j];
|
|
155
|
+
const hasThisMonth = wDays.some((d) => d.month === newMonth && d.year === newYear);
|
|
156
|
+
if (hasThisMonth) {
|
|
157
|
+
weeksVisibleAfter++;
|
|
158
|
+
} else {
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Determine firstOfYear
|
|
164
|
+
const isNewYear =
|
|
165
|
+
boundaries.length > 0
|
|
166
|
+
? boundaries[boundaries.length - 1].data.year !== newYear
|
|
167
|
+
: newMonth === 1;
|
|
168
|
+
|
|
169
|
+
boundaries.push({
|
|
170
|
+
index: i,
|
|
171
|
+
data: {
|
|
172
|
+
month: newMonth,
|
|
173
|
+
year: newYear,
|
|
174
|
+
firstOfYear: isFirstWeek ? newMonth === 1 : isNewYear,
|
|
175
|
+
firstVisible: boundaries.length === 0,
|
|
176
|
+
weeksVisibleBefore,
|
|
177
|
+
weeksVisibleAfter,
|
|
178
|
+
firstDayColumn: firstDayCol,
|
|
179
|
+
totalColumns,
|
|
180
|
+
firstDayVisible,
|
|
181
|
+
fullWeeksVisibleAfter: 0, // computed in second pass
|
|
182
|
+
gridRowStart: 0, // computed in second pass
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Second pass: compute fullWeeksVisibleAfter and gridRowStart.
|
|
189
|
+
// fullWeeksVisibleAfter = week rows between this separator and the next
|
|
190
|
+
// (or end of window). gridRowStart = 1-based row in the grid body
|
|
191
|
+
// (accounting for header row offset +1).
|
|
192
|
+
for (let b = 0; b < boundaries.length; b++) {
|
|
193
|
+
const current = boundaries[b];
|
|
194
|
+
const nextIndex = b + 1 < boundaries.length ? boundaries[b + 1].index : weeks.length;
|
|
195
|
+
current.data.fullWeeksVisibleAfter = nextIndex - current.index;
|
|
196
|
+
// Grid row: header is row 1, week rows start at row 2.
|
|
197
|
+
// gridRowStart matches the week row it labels: index + 2 (1-based + header).
|
|
198
|
+
current.data.gridRowStart = current.index + 2;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return boundaries;
|
|
202
|
+
}, [weeks, weekDaysArrays, weekStartDay, hasWeekNumbers, totalColumns, T]);
|
|
203
|
+
|
|
204
|
+
// Build the boundary lookup — multiple boundaries can exist at the same week index
|
|
205
|
+
const boundariesAtIndex = useMemo(() => {
|
|
206
|
+
const map = new Map<number, MonthSeparatorState[]>();
|
|
207
|
+
for (const b of monthBoundaries) {
|
|
208
|
+
const existing = map.get(b.index);
|
|
209
|
+
if (existing) {
|
|
210
|
+
existing.push(b.data);
|
|
211
|
+
} else {
|
|
212
|
+
map.set(b.index, [b.data]);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return map;
|
|
216
|
+
}, [monthBoundaries]);
|
|
217
|
+
|
|
218
|
+
const rows: React.ReactNode[] = [];
|
|
219
|
+
|
|
220
|
+
for (let i = 0; i < weeks.length; i++) {
|
|
221
|
+
const week = weeks[i];
|
|
222
|
+
const days = weekDaysArrays[i];
|
|
223
|
+
const separators = boundariesAtIndex.get(i);
|
|
224
|
+
|
|
225
|
+
// Render separators at month boundaries
|
|
226
|
+
if (separators && separatorTemplate.length > 0) {
|
|
227
|
+
for (const separatorData of separators) {
|
|
228
|
+
rows.push(
|
|
229
|
+
<MonthSeparatorDataContext.Provider
|
|
230
|
+
key={`sep-${separatorData.year}-${separatorData.month}`}
|
|
231
|
+
value={separatorData}
|
|
232
|
+
>
|
|
233
|
+
{separatorTemplate}
|
|
234
|
+
</MonthSeparatorDataContext.Provider>,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Provide WeekDataContext so DayCellTemplate and children work.
|
|
240
|
+
// gridRowIndex: 1-based row index in the CSS grid (header is row 1, weeks start at row 2).
|
|
241
|
+
const weekGridRow = i + 2; // +1 for 1-based, +1 for header
|
|
242
|
+
rows.push(
|
|
243
|
+
<WeekDataContext.Provider
|
|
244
|
+
key={week.startDate.toString()}
|
|
245
|
+
value={{ days, weekIndex: week.weekIndex, gridRowIndex: weekGridRow }}
|
|
246
|
+
>
|
|
247
|
+
<GridMonthContext.Provider
|
|
248
|
+
value={{
|
|
249
|
+
weeks: [days],
|
|
250
|
+
year: week.year,
|
|
251
|
+
month: week.month,
|
|
252
|
+
}}
|
|
253
|
+
>
|
|
254
|
+
{weekRowChildren}
|
|
255
|
+
</GridMonthContext.Provider>
|
|
256
|
+
</WeekDataContext.Provider>,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return (
|
|
261
|
+
<tbody className={className} style={style}>
|
|
262
|
+
{rows}
|
|
263
|
+
</tbody>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import { computeWeeksKeyNav, type WeeksKeyNavInput } from "./weeks-keyboard";
|
|
4
|
+
|
|
5
|
+
const T = Temporal;
|
|
6
|
+
const pd = (s: string) => T.PlainDate.from(s);
|
|
7
|
+
|
|
8
|
+
const baseInput: WeeksKeyNavInput = {
|
|
9
|
+
key: "",
|
|
10
|
+
shiftKey: false,
|
|
11
|
+
focusedDate: pd("2026-03-15"),
|
|
12
|
+
windowStart: pd("2026-03-01"),
|
|
13
|
+
weekCount: 8,
|
|
14
|
+
minValue: undefined,
|
|
15
|
+
maxValue: undefined,
|
|
16
|
+
disabled: false,
|
|
17
|
+
readOnly: false,
|
|
18
|
+
isDateDisabled: undefined,
|
|
19
|
+
scrollBy: "row",
|
|
20
|
+
T,
|
|
21
|
+
weekStartDay: 0,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe("computeWeeksKeyNav", () => {
|
|
25
|
+
it("ArrowRight moves focus +1 day", () => {
|
|
26
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "ArrowRight" });
|
|
27
|
+
expect(result).toEqual({
|
|
28
|
+
action: "move",
|
|
29
|
+
date: pd("2026-03-16"),
|
|
30
|
+
windowShift: 0,
|
|
31
|
+
followFocus: false,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("ArrowLeft moves focus -1 day", () => {
|
|
36
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "ArrowLeft" });
|
|
37
|
+
expect(result).toEqual({
|
|
38
|
+
action: "move",
|
|
39
|
+
date: pd("2026-03-14"),
|
|
40
|
+
windowShift: 0,
|
|
41
|
+
followFocus: false,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("ArrowDown moves focus +1 week", () => {
|
|
46
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "ArrowDown" });
|
|
47
|
+
expect(result).toEqual({
|
|
48
|
+
action: "move",
|
|
49
|
+
date: pd("2026-03-22"),
|
|
50
|
+
windowShift: 0,
|
|
51
|
+
followFocus: false,
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("ArrowDown at bottom of window with scrollBy=row shifts window by 1", () => {
|
|
56
|
+
// Window: Mar 1 to Apr 25 (8 weeks). Focus on Apr 22 (Wed of last week).
|
|
57
|
+
// ArrowDown → Apr 29, outside window → shift by 1
|
|
58
|
+
const result = computeWeeksKeyNav({
|
|
59
|
+
...baseInput,
|
|
60
|
+
key: "ArrowDown",
|
|
61
|
+
focusedDate: pd("2026-04-22"),
|
|
62
|
+
});
|
|
63
|
+
expect(result.action).toBe("move");
|
|
64
|
+
expect(result.windowShift).toBe(1);
|
|
65
|
+
expect(result.followFocus).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("ArrowDown at bottom of window with scrollBy=page shifts window by weekCount", () => {
|
|
69
|
+
const result = computeWeeksKeyNav({
|
|
70
|
+
...baseInput,
|
|
71
|
+
key: "ArrowDown",
|
|
72
|
+
focusedDate: pd("2026-04-22"),
|
|
73
|
+
scrollBy: "page",
|
|
74
|
+
});
|
|
75
|
+
expect(result.action).toBe("move");
|
|
76
|
+
expect(result.windowShift).toBe(8);
|
|
77
|
+
expect(result.followFocus).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("PageDown shifts focus and window by weekCount", () => {
|
|
81
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "PageDown" });
|
|
82
|
+
expect(result).toEqual({
|
|
83
|
+
action: "move",
|
|
84
|
+
date: pd("2026-05-10"),
|
|
85
|
+
windowShift: 8,
|
|
86
|
+
followFocus: false,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("Shift+PageDown shifts focus +1 year with followFocus", () => {
|
|
91
|
+
const result = computeWeeksKeyNav({
|
|
92
|
+
...baseInput,
|
|
93
|
+
key: "PageDown",
|
|
94
|
+
shiftKey: true,
|
|
95
|
+
});
|
|
96
|
+
expect(result.action).toBe("move");
|
|
97
|
+
expect((result as Extract<typeof result, { action: "move" }>).date.toString()).toBe(
|
|
98
|
+
"2027-03-15",
|
|
99
|
+
);
|
|
100
|
+
expect(result.followFocus).toBe(true);
|
|
101
|
+
expect(result.windowShift).toBe(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("Home moves focus to first day of window", () => {
|
|
105
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "Home" });
|
|
106
|
+
expect(result).toEqual({
|
|
107
|
+
action: "move",
|
|
108
|
+
date: pd("2026-03-01"),
|
|
109
|
+
windowShift: 0,
|
|
110
|
+
followFocus: false,
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("End moves focus to last day of window", () => {
|
|
115
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "End" });
|
|
116
|
+
// 8 weeks from Mar 1: last day is Apr 25
|
|
117
|
+
expect(result).toEqual({
|
|
118
|
+
action: "move",
|
|
119
|
+
date: pd("2026-04-25"),
|
|
120
|
+
windowShift: 0,
|
|
121
|
+
followFocus: false,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("Enter selects", () => {
|
|
126
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "Enter" });
|
|
127
|
+
expect(result).toEqual({
|
|
128
|
+
action: "select",
|
|
129
|
+
windowShift: 0,
|
|
130
|
+
followFocus: false,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("Space selects", () => {
|
|
135
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: " " });
|
|
136
|
+
expect(result).toEqual({
|
|
137
|
+
action: "select",
|
|
138
|
+
windowShift: 0,
|
|
139
|
+
followFocus: false,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("disabled calendar returns none", () => {
|
|
144
|
+
const result = computeWeeksKeyNav({
|
|
145
|
+
...baseInput,
|
|
146
|
+
key: "ArrowRight",
|
|
147
|
+
disabled: true,
|
|
148
|
+
});
|
|
149
|
+
expect(result).toEqual({
|
|
150
|
+
action: "none",
|
|
151
|
+
windowShift: 0,
|
|
152
|
+
followFocus: false,
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("readOnly blocks selection but allows navigation", () => {
|
|
157
|
+
const selectResult = computeWeeksKeyNav({
|
|
158
|
+
...baseInput,
|
|
159
|
+
key: "Enter",
|
|
160
|
+
readOnly: true,
|
|
161
|
+
});
|
|
162
|
+
expect(selectResult.action).toBe("none");
|
|
163
|
+
|
|
164
|
+
const navResult = computeWeeksKeyNav({
|
|
165
|
+
...baseInput,
|
|
166
|
+
key: "ArrowRight",
|
|
167
|
+
readOnly: true,
|
|
168
|
+
});
|
|
169
|
+
expect(navResult.action).toBe("move");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("Enter on isDateDisabled date returns none", () => {
|
|
173
|
+
const result = computeWeeksKeyNav({
|
|
174
|
+
...baseInput,
|
|
175
|
+
key: "Enter",
|
|
176
|
+
isDateDisabled: () => true,
|
|
177
|
+
});
|
|
178
|
+
expect(result.action).toBe("none");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("arrow keys can move onto disabled dates (focus allowed, selection blocked)", () => {
|
|
182
|
+
const result = computeWeeksKeyNav({
|
|
183
|
+
...baseInput,
|
|
184
|
+
key: "ArrowRight",
|
|
185
|
+
isDateDisabled: () => true,
|
|
186
|
+
});
|
|
187
|
+
expect(result.action).toBe("move"); // focus is allowed
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("clamps to maxValue", () => {
|
|
191
|
+
const result = computeWeeksKeyNav({
|
|
192
|
+
...baseInput,
|
|
193
|
+
key: "ArrowRight",
|
|
194
|
+
focusedDate: pd("2026-03-15"),
|
|
195
|
+
maxValue: pd("2026-03-15"),
|
|
196
|
+
});
|
|
197
|
+
expect(result.action).toBe("none"); // already at max, can't move right
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("PageUp shifts focus and window backward by weekCount", () => {
|
|
201
|
+
const result = computeWeeksKeyNav({ ...baseInput, key: "PageUp" });
|
|
202
|
+
expect(result).toEqual({
|
|
203
|
+
action: "move",
|
|
204
|
+
date: pd("2026-01-18"),
|
|
205
|
+
windowShift: -8,
|
|
206
|
+
followFocus: false,
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("Shift+PageUp shifts focus -1 year with followFocus", () => {
|
|
211
|
+
const result = computeWeeksKeyNav({
|
|
212
|
+
...baseInput,
|
|
213
|
+
key: "PageUp",
|
|
214
|
+
shiftKey: true,
|
|
215
|
+
});
|
|
216
|
+
expect(result.action).toBe("move");
|
|
217
|
+
expect((result as Extract<typeof result, { action: "move" }>).date.toString()).toBe(
|
|
218
|
+
"2025-03-15",
|
|
219
|
+
);
|
|
220
|
+
expect(result.followFocus).toBe(true);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
3
|
+
|
|
4
|
+
/** Result of a weeks-view keyboard navigation action. */
|
|
5
|
+
export type WeeksKeyNavResult =
|
|
6
|
+
| {
|
|
7
|
+
action: "move";
|
|
8
|
+
date: Temporal.PlainDate;
|
|
9
|
+
windowShift: number;
|
|
10
|
+
followFocus: boolean;
|
|
11
|
+
}
|
|
12
|
+
| { action: "select"; windowShift: number; followFocus: boolean }
|
|
13
|
+
| { action: "none"; windowShift: number; followFocus: boolean };
|
|
14
|
+
|
|
15
|
+
/** Input parameters for weeks-view keyboard navigation computation. */
|
|
16
|
+
export interface WeeksKeyNavInput {
|
|
17
|
+
/** The `KeyboardEvent.key` value. */
|
|
18
|
+
key: string;
|
|
19
|
+
/** Whether the Shift modifier was held. */
|
|
20
|
+
shiftKey: boolean;
|
|
21
|
+
/** The currently focused date in the grid. */
|
|
22
|
+
focusedDate: Temporal.PlainDate;
|
|
23
|
+
/** The first day of the visible window. */
|
|
24
|
+
windowStart: Temporal.PlainDate;
|
|
25
|
+
/** Number of weeks visible in the window. */
|
|
26
|
+
weekCount: number;
|
|
27
|
+
/** Minimum selectable date bound. */
|
|
28
|
+
minValue: Temporal.PlainDate | undefined;
|
|
29
|
+
/** Maximum selectable date bound. */
|
|
30
|
+
maxValue: Temporal.PlainDate | undefined;
|
|
31
|
+
/** Whether the entire calendar is disabled. */
|
|
32
|
+
disabled: boolean;
|
|
33
|
+
/** Whether the calendar is read-only (navigation allowed, selection blocked). */
|
|
34
|
+
readOnly?: boolean;
|
|
35
|
+
/** Predicate for individually disabled dates. */
|
|
36
|
+
isDateDisabled?: (date: Temporal.PlainDate) => boolean;
|
|
37
|
+
/** Controls how the window scrolls when arrow keys push focus outside. */
|
|
38
|
+
scrollBy: "row" | "page";
|
|
39
|
+
/** Temporal namespace. */
|
|
40
|
+
T: TemporalNamespace;
|
|
41
|
+
/** Day the week starts on (`0` = Sunday). */
|
|
42
|
+
weekStartDay: WeekStartDay;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const NONE: WeeksKeyNavResult = {
|
|
46
|
+
action: "none",
|
|
47
|
+
windowShift: 0,
|
|
48
|
+
followFocus: false,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Pure function that maps a keyboard event to a weeks-view navigation result.
|
|
53
|
+
*
|
|
54
|
+
* Handles Arrow keys (±1 day / ±1 week), Home/End (first/last day of window),
|
|
55
|
+
* PageUp/PageDown (±weekCount weeks, ±1 year with Shift), and Enter/Space (select).
|
|
56
|
+
* Results are clamped to `minValue`/`maxValue` bounds.
|
|
57
|
+
*/
|
|
58
|
+
export function computeWeeksKeyNav(input: WeeksKeyNavInput): WeeksKeyNavResult {
|
|
59
|
+
const {
|
|
60
|
+
key,
|
|
61
|
+
shiftKey,
|
|
62
|
+
focusedDate,
|
|
63
|
+
windowStart,
|
|
64
|
+
weekCount,
|
|
65
|
+
minValue,
|
|
66
|
+
maxValue,
|
|
67
|
+
disabled,
|
|
68
|
+
readOnly,
|
|
69
|
+
isDateDisabled,
|
|
70
|
+
scrollBy,
|
|
71
|
+
T,
|
|
72
|
+
} = input;
|
|
73
|
+
|
|
74
|
+
if (disabled) return NONE;
|
|
75
|
+
|
|
76
|
+
// windowEnd is the last day of the visible window (inclusive)
|
|
77
|
+
const windowEnd = windowStart.add({ weeks: weekCount }).subtract({ days: 1 });
|
|
78
|
+
|
|
79
|
+
function isOutsideWindow(date: Temporal.PlainDate): boolean {
|
|
80
|
+
return T.PlainDate.compare(date, windowStart) < 0 || T.PlainDate.compare(date, windowEnd) > 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function windowShiftFor(target: Temporal.PlainDate): number {
|
|
84
|
+
if (T.PlainDate.compare(target, windowEnd) > 0) {
|
|
85
|
+
return scrollBy === "row" ? 1 : weekCount;
|
|
86
|
+
}
|
|
87
|
+
if (T.PlainDate.compare(target, windowStart) < 0) {
|
|
88
|
+
return scrollBy === "row" ? -1 : -weekCount;
|
|
89
|
+
}
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function clampToMove(
|
|
94
|
+
target: Temporal.PlainDate,
|
|
95
|
+
windowShift: number,
|
|
96
|
+
followFocus: boolean,
|
|
97
|
+
): WeeksKeyNavResult {
|
|
98
|
+
let clamped = target;
|
|
99
|
+
if (minValue && T.PlainDate.compare(clamped, minValue) < 0) clamped = minValue;
|
|
100
|
+
if (maxValue && T.PlainDate.compare(clamped, maxValue) > 0) clamped = maxValue;
|
|
101
|
+
if (T.PlainDate.compare(clamped, focusedDate) === 0) return NONE;
|
|
102
|
+
return { action: "move", date: clamped, windowShift, followFocus };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
switch (key) {
|
|
106
|
+
case "ArrowRight": {
|
|
107
|
+
const target = focusedDate.add({ days: 1 });
|
|
108
|
+
const shift = isOutsideWindow(target) ? windowShiftFor(target) : 0;
|
|
109
|
+
return clampToMove(target, shift, false);
|
|
110
|
+
}
|
|
111
|
+
case "ArrowLeft": {
|
|
112
|
+
const target = focusedDate.subtract({ days: 1 });
|
|
113
|
+
const shift = isOutsideWindow(target) ? windowShiftFor(target) : 0;
|
|
114
|
+
return clampToMove(target, shift, false);
|
|
115
|
+
}
|
|
116
|
+
case "ArrowDown": {
|
|
117
|
+
const target = focusedDate.add({ weeks: 1 });
|
|
118
|
+
const shift = isOutsideWindow(target) ? windowShiftFor(target) : 0;
|
|
119
|
+
return clampToMove(target, shift, false);
|
|
120
|
+
}
|
|
121
|
+
case "ArrowUp": {
|
|
122
|
+
const target = focusedDate.subtract({ weeks: 1 });
|
|
123
|
+
const shift = isOutsideWindow(target) ? windowShiftFor(target) : 0;
|
|
124
|
+
return clampToMove(target, shift, false);
|
|
125
|
+
}
|
|
126
|
+
case "Home":
|
|
127
|
+
return clampToMove(windowStart, 0, false);
|
|
128
|
+
case "End":
|
|
129
|
+
return clampToMove(windowEnd, 0, false);
|
|
130
|
+
case "PageDown": {
|
|
131
|
+
if (shiftKey) {
|
|
132
|
+
const target = focusedDate.add({ years: 1 });
|
|
133
|
+
return clampToMove(target, 0, true);
|
|
134
|
+
}
|
|
135
|
+
const target = focusedDate.add({ weeks: weekCount });
|
|
136
|
+
return clampToMove(target, weekCount, false);
|
|
137
|
+
}
|
|
138
|
+
case "PageUp": {
|
|
139
|
+
if (shiftKey) {
|
|
140
|
+
const target = focusedDate.subtract({ years: 1 });
|
|
141
|
+
return clampToMove(target, 0, true);
|
|
142
|
+
}
|
|
143
|
+
const target = focusedDate.subtract({ weeks: weekCount });
|
|
144
|
+
return clampToMove(target, -weekCount, false);
|
|
145
|
+
}
|
|
146
|
+
case "Enter":
|
|
147
|
+
case " ":
|
|
148
|
+
if (readOnly) return NONE;
|
|
149
|
+
if (!isDateDisabled?.(focusedDate)) {
|
|
150
|
+
return { action: "select", windowShift: 0, followFocus: false };
|
|
151
|
+
}
|
|
152
|
+
return NONE;
|
|
153
|
+
default:
|
|
154
|
+
return NONE;
|
|
155
|
+
}
|
|
156
|
+
}
|