@klinking/colander 3.1.0-alpha.5 → 3.1.0-alpha.7

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.
Files changed (52) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.d.cts +1 -1
  3. package/dist/formats/date.d.mts +1 -1
  4. package/dist/formats/date.mjs +1 -1
  5. package/dist/formats/object.cjs +1 -1
  6. package/dist/formats/object.d.cts +1 -1
  7. package/dist/formats/object.d.mts +1 -1
  8. package/dist/formats/object.mjs +1 -1
  9. package/dist/formats/plain-date-time.cjs +1 -1
  10. package/dist/formats/plain-date-time.d.cts +1 -1
  11. package/dist/formats/plain-date-time.d.mts +1 -1
  12. package/dist/formats/plain-date-time.mjs +1 -1
  13. package/dist/formats/plain-date.cjs +1 -1
  14. package/dist/formats/plain-date.d.cts +1 -1
  15. package/dist/formats/plain-date.d.mts +1 -1
  16. package/dist/formats/plain-date.mjs +1 -1
  17. package/dist/formats/plain-month-day.cjs +1 -1
  18. package/dist/formats/plain-month-day.d.cts +1 -1
  19. package/dist/formats/plain-month-day.d.mts +1 -1
  20. package/dist/formats/plain-month-day.mjs +1 -1
  21. package/dist/formats/plain-year-month.cjs +1 -1
  22. package/dist/formats/plain-year-month.d.cts +1 -1
  23. package/dist/formats/plain-year-month.d.mts +1 -1
  24. package/dist/formats/plain-year-month.mjs +1 -1
  25. package/dist/formats/zoned-date-time.cjs +1 -1
  26. package/dist/formats/zoned-date-time.d.cts +1 -1
  27. package/dist/formats/zoned-date-time.d.mts +1 -1
  28. package/dist/formats/zoned-date-time.mjs +1 -1
  29. package/dist/index.cjs +1 -1
  30. package/dist/index.d.cts +1 -1
  31. package/dist/index.d.mts +1 -1
  32. package/dist/index.mjs +1 -1
  33. package/dist/{weeks-navigation-CEV8w_69.mjs → weeks-navigation-BSKwFvdP.mjs} +367 -413
  34. package/dist/{weeks-navigation-q3beZn2n.cjs → weeks-navigation-CPevz3Xg.cjs} +367 -413
  35. package/dist/{weeks-view-context-BLZZm1fq.d.mts → weeks-view-context-BTojXkys.d.cts} +51 -10
  36. package/dist/{weeks-view-context-CkTcdZpj.d.cts → weeks-view-context-CFXV8orh.d.mts} +51 -10
  37. package/package.json +1 -1
  38. package/src/calendar-provider.tsx +10 -55
  39. package/src/day-cell-state.test.ts +214 -0
  40. package/src/day-cell-state.ts +314 -0
  41. package/src/day-cell.tsx +29 -383
  42. package/src/grid.tsx +1 -1
  43. package/src/out-of-range.test.ts +33 -53
  44. package/src/out-of-range.ts +47 -123
  45. package/src/range-preview.test.ts +120 -0
  46. package/src/root-selection.ts +99 -97
  47. package/src/weeks-navigation.tsx +12 -28
  48. package/src/weeks-view-state.ts +36 -84
  49. package/src/weeks-view-types.ts +4 -1
  50. package/src/weeks-view.bounds.test.tsx +246 -0
  51. package/src/weeks-window.test.ts +131 -0
  52. package/src/weeks-window.ts +102 -0
@@ -2,7 +2,7 @@ import type { Temporal } from "@js-temporal/polyfill";
2
2
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { useCalendarStable, useCalendarState } from "./calendar-context";
4
4
  import { computeWeeksInWindow } from "./compute-weeks-in-window";
5
- import { applyOutOfRange, type OutOfRangeBehavior } from "./out-of-range";
5
+ import type { OutOfRangeBehavior } from "./out-of-range";
6
6
  import {
7
7
  resolveFirstWeekSpec,
8
8
  resolveFirstWeek,
@@ -21,6 +21,7 @@ import type {
21
21
  WeeksViewStateContextValue,
22
22
  WindowInfo,
23
23
  } from "./weeks-view-types";
24
+ import { createWeekWindowPolicy } from "./weeks-window";
24
25
 
25
26
  /**
26
27
  * Core state management hook for WeeksView.Root.
@@ -79,26 +80,22 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
79
80
  );
80
81
 
81
82
  // --- Apply out-of-range behavior ---
82
- const outOfRangeResult = useMemo(
83
+ const windowPolicy = useMemo(
83
84
  () =>
84
- applyOutOfRange({
85
- targetFirstWeek: resolvedFirstWeek,
85
+ createWeekWindowPolicy({
86
86
  weekCount,
87
87
  behavior: outOfRangeBehavior,
88
- ...(minValue !== undefined && { min: minValue }),
89
- ...(maxValue !== undefined && { max: maxValue }),
88
+ min: minValue,
89
+ max: maxValue,
90
90
  weekStartDay,
91
91
  T,
92
92
  }),
93
- [
94
- resolvedFirstWeek,
95
- weekCount,
96
- outOfRangeBehavior,
97
- minValue,
98
- maxValue,
99
- weekStartDay,
100
- T,
101
- ],
93
+ [weekCount, outOfRangeBehavior, minValue, maxValue, weekStartDay, T],
94
+ );
95
+
96
+ const outOfRangeResult = useMemo(
97
+ () => windowPolicy.resolve(resolvedFirstWeek),
98
+ [windowPolicy, resolvedFirstWeek],
102
99
  );
103
100
 
104
101
  // Recompute weeks if the out-of-range behavior adjusted the window
@@ -310,58 +307,30 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
310
307
  [isControlled, onFirstWeekChange],
311
308
  );
312
309
 
310
+ // Moves start from the window as shown (post snap/shrink), not the requested
311
+ // first week, so they agree with what the Prev/Next buttons report.
312
+ const shownWindow = useMemo(
313
+ () => ({
314
+ firstWeek: windowInfo.windowStart,
315
+ weekCount: windowInfo.weekCount,
316
+ }),
317
+ [windowInfo.windowStart, windowInfo.weekCount],
318
+ );
319
+
313
320
  const goNext = useCallback(
314
321
  (shiftBy?: number) => {
315
- const shift = shiftBy ?? weekCount;
316
- const target = resolvedFirstWeek.add({ weeks: shift });
317
- const adjusted = applyOutOfRange({
318
- targetFirstWeek: target,
319
- weekCount,
320
- behavior: outOfRangeBehavior,
321
- ...(minValue !== undefined && { min: minValue }),
322
- ...(maxValue !== undefined && { max: maxValue }),
323
- weekStartDay,
324
- T,
325
- });
326
- updateFirstWeek(adjusted.firstWeek);
322
+ const next = windowPolicy.shift(shownWindow, 1, shiftBy);
323
+ if (next) updateFirstWeek(next.firstWeek);
327
324
  },
328
- [
329
- resolvedFirstWeek,
330
- weekCount,
331
- outOfRangeBehavior,
332
- minValue,
333
- maxValue,
334
- weekStartDay,
335
- T,
336
- updateFirstWeek,
337
- ],
325
+ [shownWindow, windowPolicy, updateFirstWeek],
338
326
  );
339
327
 
340
328
  const goPrev = useCallback(
341
329
  (shiftBy?: number) => {
342
- const shift = shiftBy ?? weekCount;
343
- const target = resolvedFirstWeek.subtract({ weeks: shift });
344
- const adjusted = applyOutOfRange({
345
- targetFirstWeek: target,
346
- weekCount,
347
- behavior: outOfRangeBehavior,
348
- ...(minValue !== undefined && { min: minValue }),
349
- ...(maxValue !== undefined && { max: maxValue }),
350
- weekStartDay,
351
- T,
352
- });
353
- updateFirstWeek(adjusted.firstWeek);
330
+ const next = windowPolicy.shift(shownWindow, -1, shiftBy);
331
+ if (next) updateFirstWeek(next.firstWeek);
354
332
  },
355
- [
356
- resolvedFirstWeek,
357
- weekCount,
358
- outOfRangeBehavior,
359
- minValue,
360
- maxValue,
361
- weekStartDay,
362
- T,
363
- updateFirstWeek,
364
- ],
333
+ [shownWindow, windowPolicy, updateFirstWeek],
365
334
  );
366
335
 
367
336
  // --- scrollToWeek ---
@@ -370,35 +339,16 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
370
339
  target: FirstWeekSpec,
371
340
  options?: { snap?: "start" | "center" | "end" | "nearest" },
372
341
  ) => {
373
- const resolvedTarget = resolveSpec(target);
374
- const newFirstWeek = resolveFirstWeek(
375
- resolvedFirstWeek,
342
+ const requested = resolveFirstWeek(
343
+ shownWindow.firstWeek,
376
344
  weekCount,
377
- resolvedTarget,
345
+ resolveSpec(target),
378
346
  options,
379
347
  );
380
- const adjusted = applyOutOfRange({
381
- targetFirstWeek: newFirstWeek,
382
- weekCount,
383
- behavior: outOfRangeBehavior,
384
- ...(minValue !== undefined && { min: minValue }),
385
- ...(maxValue !== undefined && { max: maxValue }),
386
- weekStartDay,
387
- T,
388
- });
389
- updateFirstWeek(adjusted.firstWeek);
348
+ const next = windowPolicy.moveTo(requested);
349
+ if (next) updateFirstWeek(next.firstWeek);
390
350
  },
391
- [
392
- resolvedFirstWeek,
393
- weekCount,
394
- resolveSpec,
395
- outOfRangeBehavior,
396
- minValue,
397
- maxValue,
398
- weekStartDay,
399
- T,
400
- updateFirstWeek,
401
- ],
351
+ [shownWindow, weekCount, resolveSpec, windowPolicy, updateFirstWeek],
402
352
  );
403
353
 
404
354
  // --- Fire onWindowChange ---
@@ -425,6 +375,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
425
375
  weekCount,
426
376
  scrollBy,
427
377
  outOfRangeBehavior,
378
+ windowPolicy,
428
379
  goNext,
429
380
  goPrev,
430
381
  scrollToWeek,
@@ -435,6 +386,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
435
386
  weekCount,
436
387
  scrollBy,
437
388
  outOfRangeBehavior,
389
+ windowPolicy,
438
390
  goNext,
439
391
  goPrev,
440
392
  scrollToWeek,
@@ -3,6 +3,7 @@ import type { WeekDescriptor } from "./compute-weeks-in-window";
3
3
  import type { OutOfRangeBehavior } from "./out-of-range";
4
4
  import type { FirstWeekSpec, ScrollToWeekSnap } from "./resolve-first-week";
5
5
  import type { RootState } from "./types";
6
+ import type { WeekWindowPolicy } from "./weeks-window";
6
7
 
7
8
  // ---------------------------------------------------------------------------
8
9
  // WeeksView.Root props
@@ -63,7 +64,7 @@ export interface WindowInfo {
63
64
  windowStart: Temporal.PlainDate;
64
65
  /** The last date of the visible window. */
65
66
  windowEnd: Temporal.PlainDate;
66
- /** Number of week rows in the window (the prop value, not actual when shrunk). */
67
+ /** Number of week rows actually shown (fewer than the `weekCount` prop when shrunk). */
67
68
  weekCount: number;
68
69
  /** Total number of calendar days in the window. */
69
70
  dayCount: number;
@@ -87,6 +88,8 @@ export interface WeeksViewStableContextValue {
87
88
  scrollBy: "row" | "page";
88
89
  /** How navigation behaves at bounds. */
89
90
  outOfRangeBehavior: OutOfRangeBehavior;
91
+ /** Where the window may sit given `min`/`max` and `outOfRangeBehavior`. */
92
+ windowPolicy: WeekWindowPolicy;
90
93
  /** Navigate to the next week(s). Optionally override the shift amount (in weeks). */
91
94
  goNext: (shiftBy?: number) => void;
92
95
  /** Navigate to the previous week(s). Optionally override the shift amount (in weeks). */
@@ -0,0 +1,246 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import {
3
+ render,
4
+ screen,
5
+ act,
6
+ cleanup,
7
+ fireEvent,
8
+ } from "@testing-library/react";
9
+ import { createRef } from "react";
10
+ import { describe, it, expect, afterEach } from "vitest";
11
+ import {
12
+ Grid,
13
+ GridBody,
14
+ WeekTemplate,
15
+ DayCellTemplate,
16
+ DayButton,
17
+ } from "./grid";
18
+ import type { OutOfRangeBehavior } from "./out-of-range";
19
+ import { useViewContext } from "./view-context";
20
+ import { NextWeeksButton, PrevWeeksButton } from "./weeks-navigation";
21
+ import { WeeksView, type WeeksViewRootHandle } from "./weeks-view";
22
+ import { useWeeksViewState } from "./weeks-view-context";
23
+
24
+ const T = Temporal;
25
+ const pd = (s: string) => T.PlainDate.from(s);
26
+
27
+ function WindowDisplay() {
28
+ const { windowInfo } = useWeeksViewState();
29
+ return (
30
+ <div>
31
+ <span data-testid="start">{windowInfo.windowStart.toString()}</span>
32
+ <span data-testid="count">{windowInfo.weekCount}</span>
33
+ </div>
34
+ );
35
+ }
36
+
37
+ function FocusDisplay() {
38
+ return (
39
+ <span data-testid="focus">{useViewContext().focusedDate.toString()}</span>
40
+ );
41
+ }
42
+
43
+ interface Setup {
44
+ behavior: OutOfRangeBehavior;
45
+ weekCount: number;
46
+ firstWeek: string;
47
+ min?: string;
48
+ max?: string;
49
+ value?: string;
50
+ handle?: React.RefObject<WeeksViewRootHandle | null>;
51
+ }
52
+
53
+ function renderWindow({
54
+ behavior,
55
+ weekCount,
56
+ firstWeek,
57
+ min,
58
+ max,
59
+ value,
60
+ handle,
61
+ }: Setup) {
62
+ return render(
63
+ <WeeksView
64
+ ref={handle}
65
+ temporal={T}
66
+ weekCount={weekCount}
67
+ defaultFirstWeek={pd(firstWeek)}
68
+ outOfRangeBehavior={behavior}
69
+ {...(min && { min: pd(min) })}
70
+ {...(max && { max: pd(max) })}
71
+ {...(value && { defaultValue: pd(value) })}
72
+ >
73
+ <FocusDisplay />
74
+ <WindowDisplay />
75
+ <PrevWeeksButton data-testid="prev" />
76
+ <NextWeeksButton data-testid="next" />
77
+ <Grid>
78
+ <GridBody>
79
+ <WeekTemplate>
80
+ <DayCellTemplate>
81
+ <DayButton />
82
+ </DayCellTemplate>
83
+ </WeekTemplate>
84
+ </GridBody>
85
+ </Grid>
86
+ </WeeksView>,
87
+ );
88
+ }
89
+
90
+ const start = () => screen.getByTestId("start").textContent;
91
+ const click = (id: string) =>
92
+ act(() => {
93
+ screen.getByTestId(id).click();
94
+ });
95
+
96
+ afterEach(cleanup);
97
+
98
+ describe("WeeksView window bounds: stop holds for every way of moving", () => {
99
+ it.each([
100
+ {
101
+ description: "scrollToWeek refuses to leave the bound under stop",
102
+ behavior: "stop" as const,
103
+ expected: { start: "2026-03-01" },
104
+ },
105
+ {
106
+ description: "scrollToWeek refuses to leave the bound under stop-shrink",
107
+ behavior: "stop-shrink" as const,
108
+ expected: { start: "2026-03-01" },
109
+ },
110
+ ])("$description", ({ behavior, expected }) => {
111
+ const handle = createRef<WeeksViewRootHandle>();
112
+ renderWindow({
113
+ behavior,
114
+ weekCount: 2,
115
+ firstWeek: "2026-03-01",
116
+ min: "2026-03-01",
117
+ max: "2026-03-14",
118
+ handle,
119
+ });
120
+
121
+ act(() => handle.current?.scrollToWeek(pd("2026-06-07")));
122
+
123
+ expect(start()).toBe(expected.start);
124
+ });
125
+
126
+ it("scrollToWeek still moves when the target window keeps an in-range day", () => {
127
+ const handle = createRef<WeeksViewRootHandle>();
128
+ renderWindow({
129
+ behavior: "stop",
130
+ weekCount: 2,
131
+ firstWeek: "2026-03-01",
132
+ min: "2026-03-01",
133
+ max: "2026-03-21",
134
+ handle,
135
+ });
136
+
137
+ act(() => handle.current?.scrollToWeek(pd("2026-03-08")));
138
+
139
+ expect(start()).toBe("2026-03-08");
140
+ });
141
+ });
142
+
143
+ describe("WeeksView window bounds: a single bound is honored", () => {
144
+ it.each([
145
+ {
146
+ description: "min only, stop: Prev cannot page before min",
147
+ behavior: "stop" as const,
148
+ bounds: { min: "2026-03-01" },
149
+ button: "prev",
150
+ expected: { start: "2026-03-01", disabled: true },
151
+ },
152
+ {
153
+ description: "max only, stop: Next cannot page past max",
154
+ behavior: "stop" as const,
155
+ bounds: { max: "2026-03-14" },
156
+ button: "next",
157
+ expected: { start: "2026-03-01", disabled: true },
158
+ },
159
+ {
160
+ description: "max only, snap: Next snaps back instead of overshooting",
161
+ behavior: "snap" as const,
162
+ bounds: { max: "2026-03-14" },
163
+ button: "next",
164
+ expected: { start: "2026-03-01", disabled: true },
165
+ },
166
+ ])("$description", ({ behavior, bounds, button, expected }) => {
167
+ renderWindow({
168
+ behavior,
169
+ weekCount: 2,
170
+ firstWeek: "2026-03-01",
171
+ ...bounds,
172
+ });
173
+
174
+ expect((screen.getByTestId(button) as HTMLButtonElement).disabled).toBe(
175
+ expected.disabled,
176
+ );
177
+ click(button);
178
+ expect(start()).toBe(expected.start);
179
+ });
180
+ });
181
+
182
+ describe("WeeksView window bounds: a shrunk window pages by the rows it shows", () => {
183
+ // 4-row prop, but max 03-14 trims the window to the 2 rows 03-01..03-14.
184
+ const shrunk = {
185
+ behavior: "stop-shrink" as const,
186
+ weekCount: 4,
187
+ firstWeek: "2026-03-01",
188
+ min: "2026-01-01",
189
+ max: "2026-03-14",
190
+ };
191
+
192
+ it("Prev shifts by the shown row count", () => {
193
+ renderWindow(shrunk);
194
+ expect(screen.getByTestId("count").textContent).toBe("2");
195
+
196
+ click("prev");
197
+
198
+ expect(start()).toBe("2026-02-15");
199
+ });
200
+
201
+ it("PageUp moves focus by the shown row count", () => {
202
+ renderWindow({ ...shrunk, value: "2026-03-08" });
203
+
204
+ fireEvent.keyDown(screen.getByRole("grid"), { key: "PageUp" });
205
+
206
+ expect(screen.getByTestId("focus").textContent).toBe("2026-02-22");
207
+ });
208
+ });
209
+
210
+ describe("WeeksView window bounds: End stays inside a trimmed window", () => {
211
+ it("End focuses the last shown day after leading rows are trimmed", () => {
212
+ // min trims 02-15 and 02-22, leaving the 2 rows 03-01..03-14.
213
+ renderWindow({
214
+ behavior: "stop-shrink",
215
+ weekCount: 4,
216
+ firstWeek: "2026-02-15",
217
+ min: "2026-03-01",
218
+ value: "2026-03-01",
219
+ });
220
+ expect(screen.getByTestId("count").textContent).toBe("2");
221
+
222
+ fireEvent.keyDown(screen.getByRole("grid"), { key: "End" });
223
+
224
+ expect(screen.getByTestId("focus").textContent).toBe("2026-03-14");
225
+ });
226
+ });
227
+
228
+ describe("WeeksView window bounds: buttons act on the window that is shown", () => {
229
+ it("Prev from an over-shoot initial window moves from the snapped window", () => {
230
+ // 03-22 overshoots max, so the shown window is the snapped 03-01 one.
231
+ renderWindow({
232
+ behavior: "snap",
233
+ weekCount: 2,
234
+ firstWeek: "2026-03-22",
235
+ max: "2026-03-14",
236
+ });
237
+ expect(start()).toBe("2026-03-01");
238
+ expect((screen.getByTestId("prev") as HTMLButtonElement).disabled).toBe(
239
+ false,
240
+ );
241
+
242
+ click("prev");
243
+
244
+ expect(start()).toBe("2026-02-15");
245
+ });
246
+ });
@@ -0,0 +1,131 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, it, expect } from "vitest";
3
+ import type { OutOfRangeBehavior } from "./out-of-range";
4
+ import { createWeekWindowPolicy } from "./weeks-window";
5
+
6
+ const T = Temporal;
7
+ const pd = (s: string) => T.PlainDate.from(s);
8
+
9
+ function policyFor(
10
+ behavior: OutOfRangeBehavior,
11
+ bounds: { min?: string; max?: string } = {
12
+ min: "2025-12-01",
13
+ max: "2026-01-10",
14
+ },
15
+ ) {
16
+ return createWeekWindowPolicy({
17
+ weekCount: 8,
18
+ behavior,
19
+ min: bounds.min ? pd(bounds.min) : undefined,
20
+ max: bounds.max ? pd(bounds.max) : undefined,
21
+ weekStartDay: 0,
22
+ T,
23
+ });
24
+ }
25
+
26
+ const shown = (firstWeek: string, weekCount = 8) => ({
27
+ firstWeek: pd(firstWeek),
28
+ weekCount,
29
+ });
30
+
31
+ describe("WeekWindowPolicy.shift", () => {
32
+ it.each([
33
+ {
34
+ description: "unbounded — always allowed",
35
+ behavior: "unbounded" as const,
36
+ from: shown("2026-06-01"),
37
+ direction: 1 as const,
38
+ expected: { allowed: true },
39
+ },
40
+ {
41
+ description: "stop — refused when the target window has no in-range week",
42
+ behavior: "stop" as const,
43
+ from: shown("2025-12-28"),
44
+ direction: 1 as const,
45
+ expected: { allowed: false },
46
+ },
47
+ {
48
+ description:
49
+ "stop-shrink — refused when the target window has no in-range week",
50
+ behavior: "stop-shrink" as const,
51
+ from: shown("2025-12-28"),
52
+ direction: 1 as const,
53
+ expected: { allowed: false },
54
+ },
55
+ {
56
+ description: "snap — refused when already at the clamp position",
57
+ behavior: "snap" as const,
58
+ from: shown("2025-11-16"),
59
+ direction: 1 as const,
60
+ expected: { allowed: false },
61
+ },
62
+ {
63
+ description: "snap — allowed away from the clamp position",
64
+ behavior: "snap" as const,
65
+ from: shown("2025-11-16"),
66
+ direction: -1 as const,
67
+ expected: { allowed: true },
68
+ },
69
+ ])("$description", ({ behavior, from, direction, expected }) => {
70
+ expect(policyFor(behavior).shift(from, direction) !== null).toBe(
71
+ expected.allowed,
72
+ );
73
+ });
74
+
75
+ it("shifts by the rows currently shown, not the weekCount prop", () => {
76
+ const next = policyFor("stop-shrink", {
77
+ min: "2025-01-01",
78
+ max: "2026-03-14",
79
+ }).shift(shown("2026-03-01", 2), -1);
80
+ expect(next?.firstWeek.toString()).toBe("2026-02-15");
81
+ });
82
+
83
+ it.each([
84
+ {
85
+ description: "min only, stop — Prev refused below the bound",
86
+ behavior: "stop" as const,
87
+ bounds: { min: "2026-03-01" },
88
+ direction: -1 as const,
89
+ expected: { allowed: false },
90
+ },
91
+ {
92
+ description: "max only, stop — Next refused past the bound",
93
+ behavior: "stop" as const,
94
+ bounds: { max: "2026-03-14" },
95
+ direction: 1 as const,
96
+ expected: { allowed: false },
97
+ },
98
+ ])("$description", ({ behavior, bounds, direction, expected }) => {
99
+ expect(
100
+ policyFor(behavior, bounds).shift(shown("2026-03-01"), direction) !==
101
+ null,
102
+ ).toBe(expected.allowed);
103
+ });
104
+ });
105
+
106
+ describe("WeekWindowPolicy.moveTo", () => {
107
+ it.each([
108
+ {
109
+ description: "stop — refused when no row is in range",
110
+ behavior: "stop" as const,
111
+ target: "2026-06-07",
112
+ expected: null,
113
+ },
114
+ {
115
+ description: "stop — allowed when part of the window is in range",
116
+ behavior: "stop" as const,
117
+ target: "2026-01-04",
118
+ expected: "2026-01-04",
119
+ },
120
+ {
121
+ description: "snap — pulls back instead of refusing",
122
+ behavior: "snap" as const,
123
+ target: "2026-06-07",
124
+ expected: "2025-11-16",
125
+ },
126
+ ])("$description", ({ behavior, target, expected }) => {
127
+ expect(
128
+ policyFor(behavior).moveTo(pd(target))?.firstWeek.toString() ?? null,
129
+ ).toBe(expected);
130
+ });
131
+ });
@@ -0,0 +1,102 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import {
3
+ applyOutOfRange,
4
+ windowHasValidWeek,
5
+ type OutOfRangeBehavior,
6
+ } from "./out-of-range";
7
+ import type { TemporalNamespace, WeekStartDay } from "./types";
8
+
9
+ /** Inputs that determine how a weeks window may move relative to `min`/`max`. */
10
+ export interface WeekWindowPolicyInput {
11
+ /** The `weekCount` prop: the most rows a window will ever show. */
12
+ weekCount: number;
13
+ behavior: OutOfRangeBehavior;
14
+ min: Temporal.PlainDate | undefined;
15
+ max: Temporal.PlainDate | undefined;
16
+ weekStartDay: WeekStartDay;
17
+ T: TemporalNamespace;
18
+ }
19
+
20
+ /** A window as shown: its first week and how many rows it has (≤ the prop). */
21
+ export interface EffectiveWindow {
22
+ firstWeek: Temporal.PlainDate;
23
+ weekCount: number;
24
+ }
25
+
26
+ /**
27
+ * The rules for where a WeeksView window may sit. Built once from the bounds and
28
+ * shared by every way of moving the window (buttons, keyboard, `scrollToWeek`).
29
+ */
30
+ export interface WeekWindowPolicy {
31
+ /** The window that is shown for a requested first week. Never refuses. */
32
+ resolve(requestedFirstWeek: Temporal.PlainDate): EffectiveWindow;
33
+ /**
34
+ * The window to move to for a requested first week, or `null` when the
35
+ * behavior forbids the move (`stop`/`stop-shrink` with no in-range day in it).
36
+ */
37
+ moveTo(requestedFirstWeek: Temporal.PlainDate): EffectiveWindow | null;
38
+ /**
39
+ * Moves by `shiftBy` weeks (default: the rows currently shown) from the
40
+ * window as shown. `null` when the move is forbidden or would not change
41
+ * the window (`snap` already at the edge).
42
+ */
43
+ shift(
44
+ current: EffectiveWindow,
45
+ direction: 1 | -1,
46
+ shiftBy?: number,
47
+ ): EffectiveWindow | null;
48
+ }
49
+
50
+ export function createWeekWindowPolicy({
51
+ weekCount,
52
+ behavior,
53
+ min,
54
+ max,
55
+ weekStartDay,
56
+ T,
57
+ }: WeekWindowPolicyInput): WeekWindowPolicy {
58
+ const bounds = {
59
+ weekCount,
60
+ behavior,
61
+ ...(min !== undefined && { min }),
62
+ ...(max !== undefined && { max }),
63
+ weekStartDay,
64
+ T,
65
+ };
66
+ const isStop = behavior === "stop" || behavior === "stop-shrink";
67
+ const isBounded = min !== undefined || max !== undefined;
68
+
69
+ const resolve: WeekWindowPolicy["resolve"] = (requestedFirstWeek) =>
70
+ applyOutOfRange({ ...bounds, targetFirstWeek: requestedFirstWeek });
71
+
72
+ const moveTo: WeekWindowPolicy["moveTo"] = (requestedFirstWeek) => {
73
+ if (
74
+ isStop &&
75
+ isBounded &&
76
+ !windowHasValidWeek(requestedFirstWeek, weekCount, min, max, T)
77
+ ) {
78
+ return null;
79
+ }
80
+ return resolve(requestedFirstWeek);
81
+ };
82
+
83
+ return {
84
+ resolve,
85
+ moveTo,
86
+ shift: (current, direction, shiftBy) => {
87
+ const by = shiftBy ?? current.weekCount;
88
+ const target = current.firstWeek.add({ weeks: direction * by });
89
+ const next = moveTo(target);
90
+ if (next === null) return null;
91
+ const isSnap = behavior === "snap" || behavior === "snap-shrink";
92
+ if (
93
+ isSnap &&
94
+ isBounded &&
95
+ T.PlainDate.compare(next.firstWeek, current.firstWeek) === 0
96
+ ) {
97
+ return null;
98
+ }
99
+ return next;
100
+ },
101
+ };
102
+ }