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

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 (48) 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/{weeks-view-context-BLZZm1fq.d.mts → index-DnFCKyyw.d.cts} +44 -10
  30. package/dist/{weeks-view-context-CkTcdZpj.d.cts → index-q3cQKJdA.d.mts} +44 -10
  31. package/dist/index.cjs +1 -1
  32. package/dist/index.d.cts +2 -11
  33. package/dist/index.d.mts +2 -11
  34. package/dist/index.mjs +1 -1
  35. package/dist/{weeks-navigation-CEV8w_69.mjs → weeks-navigation-Dn3UtG76.mjs} +364 -389
  36. package/dist/{weeks-navigation-q3beZn2n.cjs → weeks-navigation-SuKJZJCM.cjs} +364 -389
  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/range-preview.test.ts +120 -0
  43. package/src/root-selection.ts +99 -97
  44. package/src/weeks-navigation.tsx +7 -32
  45. package/src/weeks-view-state.ts +24 -81
  46. package/src/weeks-view-types.ts +4 -1
  47. package/src/weeks-window.test.ts +43 -0
  48. package/src/weeks-window.ts +64 -0
@@ -7,64 +7,9 @@ import type {
7
7
  TemporalNamespace,
8
8
  ValueChangeMeta,
9
9
  ValueFormat,
10
- WeekStartDay,
11
- OutsideDays,
12
10
  } from "./types";
13
11
  import { fromZonedDateTime } from "./utils";
14
12
 
15
- export interface UseRootStateParamsBase<F extends ValueFormat> {
16
- format: F;
17
- min?: RawValueForFormat<F>;
18
- max?: RawValueForFormat<F>;
19
- disabled: boolean;
20
- readOnly: boolean;
21
- isDateDisabled?: (date: Temporal.PlainDate) => boolean;
22
- timeZone: string;
23
- locale: string;
24
- temporal: TemporalNamespace;
25
- weekStartDay: WeekStartDay;
26
- fixedWeeks: boolean;
27
- numberOfMonths: number;
28
- outsideDays: OutsideDays;
29
- onMonthChange?: (month: Temporal.PlainYearMonth) => void;
30
- }
31
-
32
- export type UseRootStateParams<F extends ValueFormat> =
33
- UseRootStateParamsBase<F> &
34
- (
35
- | {
36
- selectionMode: "single";
37
- value?: RawValueForFormat<F> | null;
38
- defaultValue?: RawValueForFormat<F>;
39
- onValueChange?: (
40
- value: RawValueForFormat<F> | null,
41
- meta: ValueChangeMeta<RawValueForFormat<F> | null>,
42
- ) => void;
43
- }
44
- | {
45
- selectionMode: "range";
46
- value?: DateRange<F> | null;
47
- defaultValue?: DateRange<F>;
48
- onValueChange?: (
49
- value: DateRange<F> | null,
50
- meta: ValueChangeMeta<DateRange<F> | null>,
51
- ) => void;
52
- rangeMode?: RangeMode;
53
- preventRangeReversal?: boolean;
54
- previewRange?: DateRange<F> | null;
55
- onHoveredDateChange?: (date: Temporal.PlainDate | undefined) => void;
56
- }
57
- | {
58
- selectionMode: "multiple";
59
- value?: RawValueForFormat<F>[] | null;
60
- defaultValue?: RawValueForFormat<F>[];
61
- onValueChange?: (
62
- value: RawValueForFormat<F>[],
63
- meta: ValueChangeMeta<RawValueForFormat<F>[]>,
64
- ) => void;
65
- }
66
- );
67
-
68
13
  /** Construct a DateRange (workaround for TS generic conditional type widening). */
69
14
  function mkRange<F extends ValueFormat>(
70
15
  start: unknown,
@@ -87,6 +32,22 @@ export function tagRaw<F extends ValueFormat>(
87
32
  return { format, value: raw } as DateValueObject;
88
33
  }
89
34
 
35
+ /**
36
+ * Decodes a range value into committed dates. An empty range is `[]`; a range
37
+ * with only an end is `[end, null]` (pending start, so the end is still the
38
+ * anchor of the next click).
39
+ */
40
+ export function decodeRange<F extends ValueFormat>(
41
+ range: DateRange<F>,
42
+ toPlain: (raw: RawValueForFormat<F>) => Temporal.PlainDate,
43
+ ): (Temporal.PlainDate | null)[] {
44
+ const start = range.start != null ? toPlain(range.start) : null;
45
+ const end = range.end != null ? toPlain(range.end) : null;
46
+ if (start === null && end === null) return [];
47
+ if (start === null && end !== null) return [end, null];
48
+ return [start, end];
49
+ }
50
+
90
51
  /** Resolve a `RangeMode` to a concrete "start" | "end" | "reset" action for inside-range clicks. */
91
52
  export function resolveInsideAction(
92
53
  mode: RangeMode,
@@ -639,47 +600,48 @@ export function truncateDatesForMode<F extends ValueFormat>(opts: {
639
600
  return clamped;
640
601
  }
641
602
 
603
+ /** Inputs shared by the provider's hover preview and the public {@link computePreviewRange}. */
604
+ export interface RangePreviewInput {
605
+ hoveredDate: Temporal.PlainDate;
606
+ committedDates: readonly (Temporal.PlainDate | null)[];
607
+ rangeMode: RangeMode;
608
+ allowRangeReversal: boolean;
609
+ readOnly: boolean;
610
+ isDateDisabled: (date: Temporal.PlainDate) => boolean;
611
+ T: TemporalNamespace;
612
+ }
613
+
642
614
  /**
643
- * Pure utility that computes the preview range for a hovered date, using the
644
- * same logic as the internal hook. Useful for deriving the preview in an
645
- * `onHoveredDateChange` handler without needing a controlled `previewRange`.
615
+ * The range a click on `hoveredDate` would produce, or `null` when the click
616
+ * would do nothing (read-only, disabled date). One path for both the
617
+ * provider's own preview and the public {@link computePreviewRange}.
646
618
  */
647
- export function computePreviewRange(
648
- hoveredDate: Temporal.PlainDate,
649
- currentRange: DateRange | null,
650
- rangeMode: RangeMode,
651
- preventRangeReversal = false,
652
- T?: TemporalNamespace,
653
- ): DateRange<"PlainDate"> | null {
654
- const Temporal: TemporalNamespace = T ?? (globalThis as any).Temporal;
655
- if (!Temporal) {
656
- throw new Error(
657
- "computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.",
658
- );
659
- }
619
+ export function computeRangePreview(
620
+ input: RangePreviewInput,
621
+ ): [Temporal.PlainDate, Temporal.PlainDate] | null {
622
+ const {
623
+ hoveredDate,
624
+ committedDates,
625
+ rangeMode,
626
+ allowRangeReversal,
627
+ readOnly,
628
+ isDateDisabled,
629
+ T,
630
+ } = input;
660
631
 
661
- const rawStart =
662
- currentRange?.start != null
663
- ? Temporal.PlainDate.from(currentRange.start as any)
664
- : null;
665
- const rawEnd =
666
- currentRange?.end != null
667
- ? Temporal.PlainDate.from(currentRange.end as any)
668
- : null;
669
- // Normalize {start: null, end} → [end, null] (pending start)
670
- const committedDates = currentRange
671
- ? rawStart === null && rawEnd !== null
672
- ? ([rawEnd, null] as const)
673
- : ([rawStart, rawEnd] as const)
674
- : ([] as const);
675
632
  const committedStart = committedDates[0] ?? undefined;
676
- const committedEnd = committedDates[1] ?? undefined;
677
-
678
- const noop = () => [] as any;
633
+ const committedEnd =
634
+ committedDates.length > 1
635
+ ? (committedDates[committedDates.length - 1] ?? undefined)
636
+ : committedStart;
637
+
638
+ // The preview never emits a value, so the formatting and time-zone inputs
639
+ // of a real selection are unused.
640
+ const unused = () => [] as never;
679
641
  const result = computeSelectionUpdate<"PlainDate">({
680
642
  date: hoveredDate,
681
- readOnly: false,
682
- isDateDisabled: () => false,
643
+ readOnly,
644
+ isDateDisabled,
683
645
  isMultiple: false,
684
646
  isRange: true,
685
647
  committedDates,
@@ -687,19 +649,59 @@ export function computePreviewRange(
687
649
  committedEnd,
688
650
  selectedZdt: undefined,
689
651
  rangeMode,
690
- allowRangeReversal: !preventRangeReversal,
691
- sortDates: (d) => [...d].sort((a, b) => Temporal.PlainDate.compare(a, b)),
692
- currentSingleFormatted: noop,
693
- currentRangeFormatted: () => currentRange as DateRange<"PlainDate"> | null,
694
- currentMultipleFormatted: noop,
652
+ allowRangeReversal,
653
+ sortDates: (d) => [...d].sort((a, b) => T.PlainDate.compare(a, b)),
654
+ currentSingleFormatted: unused,
655
+ currentRangeFormatted: unused,
656
+ currentMultipleFormatted: unused,
695
657
  resolvedFormat: "PlainDate",
696
658
  timeZone: "UTC",
697
- T: Temporal,
659
+ T,
698
660
  });
699
661
 
700
662
  if (result.skip || result.newDates.length === 0) return null;
701
663
  const start = result.newDates[0];
702
664
  const end = result.newDates.length > 1 ? result.newDates[1] : start;
703
665
  if (start == null || end == null) return null;
704
- return mkRange<"PlainDate">(start, end);
666
+ return [start, end];
667
+ }
668
+
669
+ /**
670
+ * Pure utility that computes the preview range for a hovered date, using the
671
+ * same logic as the built-in hover preview. Useful for deriving the preview in
672
+ * an `onHoveredDateChange` handler without needing a controlled `previewRange`.
673
+ *
674
+ * Pass `options.isDateDisabled` and `options.readOnly` to match a calendar that
675
+ * disables dates or is read-only; by default every date is selectable.
676
+ */
677
+ export function computePreviewRange(
678
+ hoveredDate: Temporal.PlainDate,
679
+ currentRange: DateRange | null,
680
+ rangeMode: RangeMode,
681
+ preventRangeReversal = false,
682
+ T?: TemporalNamespace,
683
+ options: {
684
+ isDateDisabled?: (date: Temporal.PlainDate) => boolean;
685
+ readOnly?: boolean;
686
+ } = {},
687
+ ): DateRange<"PlainDate"> | null {
688
+ const Temporal: TemporalNamespace = T ?? (globalThis as any).Temporal;
689
+ if (!Temporal) {
690
+ throw new Error(
691
+ "computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.",
692
+ );
693
+ }
694
+
695
+ const preview = computeRangePreview({
696
+ hoveredDate,
697
+ committedDates: currentRange
698
+ ? decodeRange(currentRange, (raw) => Temporal.PlainDate.from(raw as any))
699
+ : [],
700
+ rangeMode,
701
+ allowRangeReversal: !preventRangeReversal,
702
+ readOnly: options.readOnly ?? false,
703
+ isDateDisabled: options.isDateDisabled ?? (() => false),
704
+ T: Temporal,
705
+ });
706
+ return preview ? mkRange<"PlainDate">(preview[0], preview[1]) : null;
705
707
  }
@@ -2,7 +2,6 @@ import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
3
  import React, { forwardRef, useMemo } from "react";
4
4
  import { useCalendarStable } from "./calendar-context";
5
- import { canShift } from "./out-of-range";
6
5
  import type { RootState, StateAttributesMapping } from "./types";
7
6
  import { useViewState } from "./view-context";
8
7
  import { useWeeksViewStable, useWeeksViewState } from "./weeks-view-context";
@@ -70,15 +69,8 @@ function useWeeksNavButton(
70
69
  direction: "prev" | "next",
71
70
  shiftByProp: number | undefined,
72
71
  ) {
73
- const {
74
- disabled: globalDisabled,
75
- minValue,
76
- maxValue,
77
- temporal: T,
78
- weekStartDay,
79
- } = useCalendarStable();
80
- const { weekCount, outOfRangeBehavior, goNext, goPrev } =
81
- useWeeksViewStable();
72
+ const { disabled: globalDisabled } = useCalendarStable();
73
+ const { windowPolicy, goNext, goPrev } = useWeeksViewStable();
82
74
  const { windowInfo } = useWeeksViewState();
83
75
  const { rootState } = useViewState();
84
76
 
@@ -86,29 +78,12 @@ function useWeeksNavButton(
86
78
 
87
79
  const isDisabled = useMemo(() => {
88
80
  if (globalDisabled) return true;
89
- return !canShift({
81
+ return !windowPolicy.canShift(
90
82
  currentFirstWeek,
91
- weekCount,
92
- direction: direction === "next" ? 1 : -1,
93
- ...(shiftByProp !== undefined && { shiftBy: shiftByProp }),
94
- behavior: outOfRangeBehavior,
95
- ...(minValue !== undefined && { min: minValue }),
96
- ...(maxValue !== undefined && { max: maxValue }),
97
- weekStartDay,
98
- T,
99
- });
100
- }, [
101
- globalDisabled,
102
- currentFirstWeek,
103
- weekCount,
104
- direction,
105
- shiftByProp,
106
- outOfRangeBehavior,
107
- minValue,
108
- maxValue,
109
- weekStartDay,
110
- T,
111
- ]);
83
+ direction === "next" ? 1 : -1,
84
+ shiftByProp,
85
+ );
86
+ }, [globalDisabled, windowPolicy, currentFirstWeek, direction, shiftByProp]);
112
87
 
113
88
  const state = useMemo<WeeksNavButtonState>(
114
89
  () => ({ root: rootState, direction, disabled: isDisabled }),
@@ -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
@@ -312,56 +309,20 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
312
309
 
313
310
  const goNext = useCallback(
314
311
  (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);
312
+ const target = resolvedFirstWeek.add({ weeks: shiftBy ?? weekCount });
313
+ updateFirstWeek(windowPolicy.resolve(target).firstWeek);
327
314
  },
328
- [
329
- resolvedFirstWeek,
330
- weekCount,
331
- outOfRangeBehavior,
332
- minValue,
333
- maxValue,
334
- weekStartDay,
335
- T,
336
- updateFirstWeek,
337
- ],
315
+ [resolvedFirstWeek, weekCount, windowPolicy, updateFirstWeek],
338
316
  );
339
317
 
340
318
  const goPrev = useCallback(
341
319
  (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,
320
+ const target = resolvedFirstWeek.subtract({
321
+ weeks: shiftBy ?? weekCount,
352
322
  });
353
- updateFirstWeek(adjusted.firstWeek);
323
+ updateFirstWeek(windowPolicy.resolve(target).firstWeek);
354
324
  },
355
- [
356
- resolvedFirstWeek,
357
- weekCount,
358
- outOfRangeBehavior,
359
- minValue,
360
- maxValue,
361
- weekStartDay,
362
- T,
363
- updateFirstWeek,
364
- ],
325
+ [resolvedFirstWeek, weekCount, windowPolicy, updateFirstWeek],
365
326
  );
366
327
 
367
328
  // --- scrollToWeek ---
@@ -370,35 +331,15 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
370
331
  target: FirstWeekSpec,
371
332
  options?: { snap?: "start" | "center" | "end" | "nearest" },
372
333
  ) => {
373
- const resolvedTarget = resolveSpec(target);
374
334
  const newFirstWeek = resolveFirstWeek(
375
335
  resolvedFirstWeek,
376
336
  weekCount,
377
- resolvedTarget,
337
+ resolveSpec(target),
378
338
  options,
379
339
  );
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);
340
+ updateFirstWeek(windowPolicy.resolve(newFirstWeek).firstWeek);
390
341
  },
391
- [
392
- resolvedFirstWeek,
393
- weekCount,
394
- resolveSpec,
395
- outOfRangeBehavior,
396
- minValue,
397
- maxValue,
398
- weekStartDay,
399
- T,
400
- updateFirstWeek,
401
- ],
342
+ [resolvedFirstWeek, weekCount, resolveSpec, windowPolicy, updateFirstWeek],
402
343
  );
403
344
 
404
345
  // --- Fire onWindowChange ---
@@ -425,6 +366,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
425
366
  weekCount,
426
367
  scrollBy,
427
368
  outOfRangeBehavior,
369
+ windowPolicy,
428
370
  goNext,
429
371
  goPrev,
430
372
  scrollToWeek,
@@ -435,6 +377,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
435
377
  weekCount,
436
378
  scrollBy,
437
379
  outOfRangeBehavior,
380
+ windowPolicy,
438
381
  goNext,
439
382
  goPrev,
440
383
  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,43 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, it, expect } from "vitest";
3
+ import { createWeekWindowPolicy } from "./weeks-window";
4
+
5
+ const T = Temporal;
6
+ const pd = (s: string) => T.PlainDate.from(s);
7
+
8
+ describe("createWeekWindowPolicy", () => {
9
+ const policy = createWeekWindowPolicy({
10
+ weekCount: 4,
11
+ behavior: "snap",
12
+ min: pd("2026-03-01"),
13
+ max: pd("2026-04-30"),
14
+ weekStartDay: 0,
15
+ T,
16
+ });
17
+
18
+ it("resolve snaps an overshooting first week back to the max edge", () => {
19
+ const { firstWeek, weekCount } = policy.resolve(pd("2026-05-10"));
20
+ expect({ firstWeek: firstWeek.toString(), weekCount }).toEqual({
21
+ firstWeek: "2026-04-05",
22
+ weekCount: 4,
23
+ });
24
+ });
25
+
26
+ it.each([
27
+ {
28
+ description:
29
+ "next is blocked once the window already sits at the max edge",
30
+ from: "2026-04-05",
31
+ direction: 1 as const,
32
+ expected: false,
33
+ },
34
+ {
35
+ description: "prev is allowed away from the max edge",
36
+ from: "2026-04-05",
37
+ direction: -1 as const,
38
+ expected: true,
39
+ },
40
+ ])("canShift: $description", ({ from, direction, expected }) => {
41
+ expect(policy.canShift(pd(from), direction)).toBe(expected);
42
+ });
43
+ });
@@ -0,0 +1,64 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import {
3
+ applyOutOfRange,
4
+ canShift,
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
+ weekCount: number;
12
+ behavior: OutOfRangeBehavior;
13
+ min: Temporal.PlainDate | undefined;
14
+ max: Temporal.PlainDate | undefined;
15
+ weekStartDay: WeekStartDay;
16
+ T: TemporalNamespace;
17
+ }
18
+
19
+ /**
20
+ * The rules for where a WeeksView window may sit. Built once from the bounds and
21
+ * shared by every way of moving the window (buttons, keyboard, `scrollToWeek`).
22
+ */
23
+ export interface WeekWindowPolicy {
24
+ /** Applies the out-of-range behavior to a requested first week. */
25
+ resolve(requestedFirstWeek: Temporal.PlainDate): {
26
+ firstWeek: Temporal.PlainDate;
27
+ weekCount: number;
28
+ };
29
+ /** Whether a Prev/Next step of `shiftBy` weeks (default `weekCount`) is allowed. */
30
+ canShift(
31
+ currentFirstWeek: Temporal.PlainDate,
32
+ direction: 1 | -1,
33
+ shiftBy?: number,
34
+ ): boolean;
35
+ }
36
+
37
+ export function createWeekWindowPolicy({
38
+ weekCount,
39
+ behavior,
40
+ min,
41
+ max,
42
+ weekStartDay,
43
+ T,
44
+ }: WeekWindowPolicyInput): WeekWindowPolicy {
45
+ const bounds = {
46
+ weekCount,
47
+ behavior,
48
+ ...(min !== undefined && { min }),
49
+ ...(max !== undefined && { max }),
50
+ weekStartDay,
51
+ T,
52
+ };
53
+ return {
54
+ resolve: (requestedFirstWeek) =>
55
+ applyOutOfRange({ ...bounds, targetFirstWeek: requestedFirstWeek }),
56
+ canShift: (currentFirstWeek, direction, shiftBy) =>
57
+ canShift({
58
+ ...bounds,
59
+ currentFirstWeek,
60
+ direction,
61
+ ...(shiftBy !== undefined && { shiftBy }),
62
+ }),
63
+ };
64
+ }