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

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 (51) 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-DEaASUAh.mjs → weeks-navigation-CEV8w_69.mjs} +239 -221
  34. package/dist/{weeks-navigation-DvryOSWF.cjs → weeks-navigation-q3beZn2n.cjs} +239 -221
  35. package/dist/{weeks-view-context-CNAq2Hiu.d.cts → weeks-view-context-BLZZm1fq.d.mts} +33 -2
  36. package/dist/{weeks-view-context-CsP3PBiu.d.mts → weeks-view-context-CkTcdZpj.d.cts} +33 -2
  37. package/package.json +1 -1
  38. package/src/calendar-provider.tsx +6 -0
  39. package/src/calendar-types.ts +2 -0
  40. package/src/day-cell.tsx +8 -1
  41. package/src/month-view-navigation.ts +180 -0
  42. package/src/month-view-types.ts +3 -0
  43. package/src/month-view.bounds.test.tsx +136 -0
  44. package/src/month-view.tsx +41 -217
  45. package/src/month-window.test.ts +207 -0
  46. package/src/month-window.ts +93 -0
  47. package/src/navigation.tsx +15 -74
  48. package/src/today.test.tsx +98 -0
  49. package/src/utils.test.ts +25 -0
  50. package/src/utils.ts +17 -0
  51. package/src/weeks-view-state.ts +8 -15
@@ -238,84 +238,25 @@ export const MonthYearString = forwardRef(MonthYearStringFn) as (
238
238
  ) => React.ReactElement | null;
239
239
 
240
240
  function useNavButton(direction: "prev" | "next") {
241
- const {
242
- disabled: globalDisabled,
243
- minValue,
244
- maxValue,
245
- temporal: T,
246
- } = useCalendarStable();
247
- const monthViewStable = useMonthViewStable();
248
- const monthViewState = useMonthViewState();
241
+ const { disabled: globalDisabled, temporal: T } = useCalendarStable();
249
242
  const {
250
243
  goNextMonth: goToNextMonth,
251
244
  goPrevMonth: goToPrevMonth,
252
- numberOfMonths,
253
- outOfRangeBehavior,
254
- } = monthViewStable;
255
- const {
256
- currentMonth: currentDateTime,
257
- allMonths,
258
- rootState,
259
- } = monthViewState;
260
-
261
- // For "next", compute destination from the last visible month
262
- // For "prev", compute destination from the first visible month
263
- const refMonth =
264
- direction === "next"
265
- ? (allMonths[numberOfMonths - 1] ?? currentDateTime)
266
- : currentDateTime;
267
-
268
- const destMonth =
269
- direction === "prev"
270
- ? refMonth.month === 1
271
- ? 12
272
- : refMonth.month - 1
273
- : refMonth.month === 12
274
- ? 1
275
- : refMonth.month + 1;
276
-
277
- const destYear =
278
- direction === "prev"
279
- ? refMonth.month === 1
280
- ? refMonth.year - 1
281
- : refMonth.year
282
- : refMonth.month === 12
283
- ? refMonth.year + 1
284
- : refMonth.year;
285
-
286
- const boundValue = direction === "prev" ? minValue : maxValue;
287
-
288
- const isDisabled = useMemo(() => {
289
- if (globalDisabled) return true;
290
- // "unbounded" never disables on min/max — the bounds only gate which days
291
- // are selectable, not which months can be viewed. "stop" halts navigation
292
- // once the destination month crosses the boundary.
293
- if (outOfRangeBehavior === "unbounded") return false;
294
- if (!boundValue) return false;
295
- if (direction === "prev") {
296
- return (
297
- destYear < boundValue.year ||
298
- (destYear === boundValue.year && destMonth < boundValue.month)
299
- );
300
- }
301
- return (
302
- destYear > boundValue.year ||
303
- (destYear === boundValue.year && destMonth > boundValue.month)
304
- );
305
- }, [
306
- globalDisabled,
307
- outOfRangeBehavior,
308
- destYear,
309
- destMonth,
310
- boundValue,
311
- direction,
312
- ]);
313
-
314
- // ISO — `destYear`/`destMonth` are ISO numbers; the locale calendar only
315
- // affects display, so it must not be injected into this value.
245
+ windowPolicy,
246
+ } = useMonthViewStable();
247
+ const { currentMonth, rootState } = useMonthViewState();
248
+
249
+ const step = useMemo(
250
+ () => windowPolicy.step(currentMonth, direction),
251
+ [windowPolicy, currentMonth, direction],
252
+ );
253
+ const isDisabled = globalDisabled || step.blocked;
254
+
255
+ // ISO — the locale calendar only affects display, so it must not be
256
+ // injected into this value.
316
257
  const target = useMemo(
317
- () => T.PlainYearMonth.from({ year: destYear, month: destMonth }),
318
- [destYear, destMonth, T],
258
+ () => T.PlainYearMonth.from(step.entering),
259
+ [step.entering, T],
319
260
  );
320
261
 
321
262
  const state = useMemo<NavButtonState>(
@@ -0,0 +1,98 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { render, screen, cleanup } from "@testing-library/react";
3
+ import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
4
+ import {
5
+ Grid,
6
+ GridBody,
7
+ WeekTemplate,
8
+ DayCellTemplate,
9
+ DayButton,
10
+ } from "./grid";
11
+ import { MonthView } from "./month-view";
12
+ import { useViewContext } from "./view-context";
13
+ import { WeeksView } from "./weeks-view";
14
+
15
+ const T = Temporal;
16
+
17
+ function FocusDisplay() {
18
+ const view = useViewContext();
19
+ return <span data-testid="focus">{view.focusedDate.toString()}</span>;
20
+ }
21
+
22
+ const days = (
23
+ <Grid>
24
+ <GridBody>
25
+ <WeekTemplate>
26
+ <DayCellTemplate>
27
+ <DayButton />
28
+ </DayCellTemplate>
29
+ </WeekTemplate>
30
+ </GridBody>
31
+ </Grid>
32
+ );
33
+
34
+ // 2026-03-31T20:00Z is 2026-04-01 in Auckland (NZDT, +13) and 2026-03-31 in Pago Pago (-11).
35
+ // 2026-04-04T20:00Z is Sunday 2026-04-05 in Auckland (NZST, +12) and Saturday 2026-04-04 in Pago Pago.
36
+ describe("today follows the provider time zone", () => {
37
+ beforeEach(() => {
38
+ vi.useFakeTimers({ toFake: ["Date"] });
39
+ });
40
+ afterEach(() => {
41
+ cleanup();
42
+ vi.useRealTimers();
43
+ });
44
+
45
+ it.each([
46
+ {
47
+ description: "ahead of the clock's UTC date",
48
+ timeZone: "Pacific/Auckland",
49
+ expected: { today: "2026-04-01", todayCell: "1" },
50
+ },
51
+ {
52
+ description: "behind the clock's UTC date",
53
+ timeZone: "Pacific/Pago_Pago",
54
+ expected: { today: "2026-03-31", todayCell: "31" },
55
+ },
56
+ ])("MonthView: $description", ({ timeZone, expected }) => {
57
+ vi.setSystemTime(new Date("2026-03-31T20:00:00Z"));
58
+ const { container } = render(
59
+ <MonthView temporal={T} timeZone={timeZone}>
60
+ <FocusDisplay />
61
+ {days}
62
+ </MonthView>,
63
+ );
64
+
65
+ expect(screen.getByTestId("focus").textContent).toBe(expected.today);
66
+ const marked = container.querySelectorAll("td[data-today]");
67
+ expect(marked.length).toBe(1);
68
+ expect(marked[0]?.textContent).toBe(expected.todayCell);
69
+ expect(marked[0]?.hasAttribute("data-outside-month")).toBe(false);
70
+ });
71
+
72
+ it.each([
73
+ {
74
+ description: "ahead of the clock's UTC date",
75
+ timeZone: "Pacific/Auckland",
76
+ expected: { firstCell: "5", todayCell: "5" },
77
+ },
78
+ {
79
+ description: "behind the clock's UTC date",
80
+ timeZone: "Pacific/Pago_Pago",
81
+ expected: { firstCell: "29", todayCell: "4" },
82
+ },
83
+ ])("WeeksView: $description", ({ timeZone, expected }) => {
84
+ vi.setSystemTime(new Date("2026-04-04T20:00:00Z"));
85
+ const { container } = render(
86
+ <WeeksView temporal={T} timeZone={timeZone} weekCount={1}>
87
+ {days}
88
+ </WeeksView>,
89
+ );
90
+
91
+ expect(container.querySelector("td[role='gridcell']")?.textContent).toBe(
92
+ expected.firstCell,
93
+ );
94
+ const marked = container.querySelectorAll("td[data-today]");
95
+ expect(marked.length).toBe(1);
96
+ expect(marked[0]?.textContent).toBe(expected.todayCell);
97
+ });
98
+ });
package/src/utils.test.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  toZonedDateTime,
9
9
  fromZonedDateTime,
10
10
  selectedToZdt,
11
+ resolveInitialAnchor,
11
12
  getMonthWeeks,
12
13
  zdtToNativeDate,
13
14
  sameCalendarDay,
@@ -249,6 +250,30 @@ describe("selectedToZdt", () => {
249
250
  });
250
251
  });
251
252
 
253
+ describe("resolveInitialAnchor", () => {
254
+ const today = () => Temporal.PlainDate.from("2026-04-01");
255
+
256
+ it.each([
257
+ {
258
+ description: "falls back to today when nothing is selected",
259
+ selected: undefined,
260
+ expected: "2026-04-01",
261
+ },
262
+ {
263
+ description: "uses the selected date over today",
264
+ selected: {
265
+ format: "PlainDate",
266
+ value: Temporal.PlainDate.from("2026-03-15"),
267
+ } as const,
268
+ expected: "2026-03-15",
269
+ },
270
+ ])("$description", ({ selected, expected }) => {
271
+ expect(resolveInitialAnchor(selected, today, "UTC", T).toString()).toBe(
272
+ expected,
273
+ );
274
+ });
275
+ });
276
+
252
277
  describe("zdtToNativeDate", () => {
253
278
  it("converts a ZonedDateTime to a native Date with the correct epoch", () => {
254
279
  const zdt = Temporal.PlainDateTime.from(
package/src/utils.ts CHANGED
@@ -39,6 +39,23 @@ export function getSystemTimeZone(T: TemporalNamespace): string {
39
39
  return T.Now.timeZoneId();
40
40
  }
41
41
 
42
+ /**
43
+ * The date a view opens on: the selected date, else today in the provider's time zone.
44
+ *
45
+ * @param selected - The currently selected value, or `undefined`.
46
+ * @param today - Reads today's date in the provider's time zone.
47
+ * @param timeZone - IANA time zone.
48
+ * @param T - Temporal namespace.
49
+ */
50
+ export function resolveInitialAnchor(
51
+ selected: DateValueObject | undefined,
52
+ today: () => Temporal.PlainDate,
53
+ timeZone: string,
54
+ T: TemporalNamespace,
55
+ ): Temporal.PlainDate {
56
+ return selectedToZdt(selected, timeZone, T)?.toPlainDate() ?? today();
57
+ }
58
+
42
59
  /**
43
60
  * Converts any {@link DateValueObject} into a `Temporal.ZonedDateTime`.
44
61
  *
@@ -9,7 +9,7 @@ import {
9
9
  type FirstWeekSpec,
10
10
  } from "./resolve-first-week";
11
11
  import type { RootState } from "./types";
12
- import { selectedToZdt, toZonedDateTime } from "./utils";
12
+ import { resolveInitialAnchor, selectedToZdt } from "./utils";
13
13
  import type {
14
14
  ViewContextValue,
15
15
  ViewStableContextValue,
@@ -46,7 +46,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
46
46
  const calState = useCalendarState();
47
47
 
48
48
  const T = calStable.temporal;
49
- const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone } =
49
+ const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone, today } =
50
50
  calStable;
51
51
 
52
52
  // --- Controlled/uncontrolled firstWeek ---
@@ -63,16 +63,9 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
63
63
  if (firstWeekProp !== undefined) return resolveSpec(firstWeekProp);
64
64
  if (defaultFirstWeekProp !== undefined)
65
65
  return resolveSpec(defaultFirstWeekProp);
66
- // Derive from selection or today
67
- if (calState.selected) {
68
- const plain = toZonedDateTime(
69
- calState.selected,
70
- timeZone,
71
- T,
72
- ).toPlainDate();
73
- return resolveSpec(plain);
74
- }
75
- return resolveSpec(T.Now.plainDateISO());
66
+ return resolveSpec(
67
+ resolveInitialAnchor(calState.selected, today, timeZone, T),
68
+ );
76
69
  });
77
70
 
78
71
  const resolvedFirstWeek = isControlled
@@ -145,10 +138,10 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
145
138
  const firstWeek = weeks[0];
146
139
  const lastWeek = weeks[weeks.length - 1];
147
140
  if (firstWeek === undefined || lastWeek === undefined) {
148
- const today = T.Now.plainDateISO();
141
+ const now = today();
149
142
  return {
150
- windowStart: today,
151
- windowEnd: today,
143
+ windowStart: now,
144
+ windowEnd: now,
152
145
  weekCount: 0,
153
146
  dayCount: 0,
154
147
  enabledWeekCount: 0,