@klinking/colander 3.1.0-alpha.4 → 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.
- package/dist/formats/date.cjs +1 -1
- package/dist/formats/date.d.cts +1 -1
- package/dist/formats/date.d.mts +1 -1
- package/dist/formats/date.mjs +1 -1
- package/dist/formats/object.cjs +1 -1
- package/dist/formats/object.d.cts +1 -1
- package/dist/formats/object.d.mts +1 -1
- package/dist/formats/object.mjs +1 -1
- package/dist/formats/plain-date-time.cjs +1 -1
- package/dist/formats/plain-date-time.d.cts +1 -1
- package/dist/formats/plain-date-time.d.mts +1 -1
- package/dist/formats/plain-date-time.mjs +1 -1
- package/dist/formats/plain-date.cjs +1 -1
- package/dist/formats/plain-date.d.cts +1 -1
- package/dist/formats/plain-date.d.mts +1 -1
- package/dist/formats/plain-date.mjs +1 -1
- package/dist/formats/plain-month-day.cjs +1 -1
- package/dist/formats/plain-month-day.d.cts +1 -1
- package/dist/formats/plain-month-day.d.mts +1 -1
- package/dist/formats/plain-month-day.mjs +1 -1
- package/dist/formats/plain-year-month.cjs +1 -1
- package/dist/formats/plain-year-month.d.cts +1 -1
- package/dist/formats/plain-year-month.d.mts +1 -1
- package/dist/formats/plain-year-month.mjs +1 -1
- package/dist/formats/zoned-date-time.cjs +1 -1
- package/dist/formats/zoned-date-time.d.cts +1 -1
- package/dist/formats/zoned-date-time.d.mts +1 -1
- package/dist/formats/zoned-date-time.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{weeks-navigation-elCpdfQ3.mjs → weeks-navigation-CEV8w_69.mjs} +210 -196
- package/dist/{weeks-navigation-DV538__x.cjs → weeks-navigation-q3beZn2n.cjs} +210 -196
- package/dist/{weeks-view-context-CUCy8njs.d.cts → weeks-view-context-BLZZm1fq.d.mts} +31 -2
- package/dist/{weeks-view-context-E1NwyNgQ.d.mts → weeks-view-context-CkTcdZpj.d.cts} +31 -2
- package/package.json +1 -1
- package/src/month-view-navigation.ts +180 -0
- package/src/month-view-types.ts +3 -0
- package/src/month-view.bounds.test.tsx +136 -0
- package/src/month-view.tsx +37 -205
- package/src/month-window.test.ts +207 -0
- package/src/month-window.ts +93 -0
- package/src/navigation.tsx +15 -74
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { Dispatch, SetStateAction } from "react";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import type { MonthRef, MonthWindowPolicy } from "./month-window";
|
|
5
|
+
import type { TemporalNamespace } from "./types";
|
|
6
|
+
import { focusedDateForMonth } from "./utils";
|
|
7
|
+
|
|
8
|
+
export interface UseMonthNavigationInput {
|
|
9
|
+
/** Controlled first visible month. */
|
|
10
|
+
month: MonthRef | undefined;
|
|
11
|
+
/** Initial first visible month when uncontrolled. */
|
|
12
|
+
defaultMonth: MonthRef | undefined;
|
|
13
|
+
/** Date the view opens on when neither `month` nor `defaultMonth` is given. */
|
|
14
|
+
initialAnchor: () => Temporal.PlainDate;
|
|
15
|
+
onMonthChange: ((month: Temporal.PlainYearMonth) => void) | undefined;
|
|
16
|
+
policy: MonthWindowPolicy;
|
|
17
|
+
focusedDate: Temporal.PlainDate;
|
|
18
|
+
setFocusedDate: Dispatch<SetStateAction<Temporal.PlainDate>>;
|
|
19
|
+
T: TemporalNamespace;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MonthNavigation {
|
|
23
|
+
/** The first visible month. */
|
|
24
|
+
currentMonth: MonthRef;
|
|
25
|
+
/** The first visible month as an ISO `PlainYearMonth`. */
|
|
26
|
+
viewingYearMonth: Temporal.PlainYearMonth;
|
|
27
|
+
goNextMonth: () => void;
|
|
28
|
+
goPrevMonth: () => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Owns which month the view shows, in both controlled and uncontrolled mode:
|
|
33
|
+
* the month state, the Prev/Next steps, and syncing the window with focus.
|
|
34
|
+
*
|
|
35
|
+
* `onMonthChange` fires exactly once per move: from the effect below when
|
|
36
|
+
* uncontrolled, and from the step or the focus-sync effect when controlled.
|
|
37
|
+
*/
|
|
38
|
+
export function useMonthNavigation(
|
|
39
|
+
input: UseMonthNavigationInput,
|
|
40
|
+
): MonthNavigation {
|
|
41
|
+
const {
|
|
42
|
+
month,
|
|
43
|
+
defaultMonth,
|
|
44
|
+
initialAnchor,
|
|
45
|
+
onMonthChange,
|
|
46
|
+
policy,
|
|
47
|
+
focusedDate,
|
|
48
|
+
setFocusedDate,
|
|
49
|
+
T,
|
|
50
|
+
} = input;
|
|
51
|
+
|
|
52
|
+
const isControlled = month !== undefined;
|
|
53
|
+
|
|
54
|
+
const [internalMonth, setInternalMonth] = useState<MonthRef>(() => {
|
|
55
|
+
if (month) return { year: month.year, month: month.month };
|
|
56
|
+
if (defaultMonth)
|
|
57
|
+
return { year: defaultMonth.year, month: defaultMonth.month };
|
|
58
|
+
const anchor = initialAnchor();
|
|
59
|
+
return { year: anchor.year, month: anchor.month };
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const year = isControlled ? month.year : internalMonth.year;
|
|
63
|
+
const monthNumber = isControlled ? month.month : internalMonth.month;
|
|
64
|
+
const currentMonth = useMemo<MonthRef>(
|
|
65
|
+
() => ({ year, month: monthNumber }),
|
|
66
|
+
[year, monthNumber],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// ISO — the locale calendar only affects display, not this value.
|
|
70
|
+
const viewingYearMonth = useMemo(
|
|
71
|
+
() => T.PlainYearMonth.from({ year, month: monthNumber }),
|
|
72
|
+
[year, monthNumber, T],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Always-current ref so callbacks/effects don't re-subscribe on every
|
|
76
|
+
// onMonthChange identity change.
|
|
77
|
+
const onMonthChangeRef = useRef(onMonthChange);
|
|
78
|
+
onMonthChangeRef.current = onMonthChange;
|
|
79
|
+
|
|
80
|
+
// Request the parent move the view to `{year, month}` (controlled mode).
|
|
81
|
+
// Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
|
|
82
|
+
// only affects display (see MonthYearString). Passing `calendar` here would
|
|
83
|
+
// reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
|
|
84
|
+
// shifting the value by centuries.
|
|
85
|
+
const notifyMonth = useCallback(
|
|
86
|
+
(target: MonthRef) => {
|
|
87
|
+
onMonthChangeRef.current?.(T.PlainYearMonth.from(target));
|
|
88
|
+
},
|
|
89
|
+
[T],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Button navigation in controlled mode notifies the parent directly, then
|
|
93
|
+
// moves focus into the target month. This ref holds that focus value so the
|
|
94
|
+
// focus-sync effect skips re-notifying for it. Keyed by the value (not a
|
|
95
|
+
// one-shot flag) so it can't strand and suppress a later keyboard crossing
|
|
96
|
+
// if the button's focus move is a no-op or the parent ignores the change.
|
|
97
|
+
const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
|
|
98
|
+
const prevFocusedRef = useRef(focusedDate);
|
|
99
|
+
|
|
100
|
+
// Clear a stale skip target whenever the parent actually moves the view.
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
skipFocusSyncForRef.current = null;
|
|
103
|
+
}, [month?.year, month?.month]);
|
|
104
|
+
|
|
105
|
+
// Sync month when the focused date crosses a month boundary. Uncontrolled:
|
|
106
|
+
// shift internal state. Controlled: ask the parent to move the view, but
|
|
107
|
+
// only when focus actually moved out of the visible window (keyboard nav) —
|
|
108
|
+
// never as an echo of an external `month` update or a button click.
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const prevFocused = prevFocusedRef.current;
|
|
111
|
+
prevFocusedRef.current = focusedDate;
|
|
112
|
+
const target = { year: focusedDate.year, month: focusedDate.month };
|
|
113
|
+
if (isControlled) {
|
|
114
|
+
if (skipFocusSyncForRef.current === focusedDate) {
|
|
115
|
+
skipFocusSyncForRef.current = null;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (prevFocused === focusedDate) return;
|
|
119
|
+
if (policy.isVisible(currentMonth, target)) return;
|
|
120
|
+
// Emit the minimally-shifted first-visible month (what the parent binds
|
|
121
|
+
// to `month`), not the focused month — so a controlled window scrolls by
|
|
122
|
+
// the minimum, matching uncontrolled mode and the nav buttons.
|
|
123
|
+
notifyMonth(policy.reveal(currentMonth, target));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
setInternalMonth((prev) => {
|
|
127
|
+
const next = policy.reveal(prev, target);
|
|
128
|
+
if (next.year === prev.year && next.month === prev.month) return prev;
|
|
129
|
+
return next;
|
|
130
|
+
});
|
|
131
|
+
}, [focusedDate, isControlled, policy, currentMonth, notifyMonth]);
|
|
132
|
+
|
|
133
|
+
// Fire onMonthChange when uncontrolled navigation moves the view (not on
|
|
134
|
+
// mount). In controlled mode the parent owns `month`, so notification flows
|
|
135
|
+
// from the steps and the focus-sync effect instead; firing here would just
|
|
136
|
+
// echo the parent's own update back to it.
|
|
137
|
+
const mountedRef = useRef(false);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!mountedRef.current) {
|
|
140
|
+
mountedRef.current = true;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (isControlled) return;
|
|
144
|
+
onMonthChangeRef.current?.(viewingYearMonth);
|
|
145
|
+
}, [viewingYearMonth, isControlled]);
|
|
146
|
+
|
|
147
|
+
const go = useCallback(
|
|
148
|
+
(direction: "prev" | "next") => {
|
|
149
|
+
if (isControlled) {
|
|
150
|
+
const step = policy.step(month, direction);
|
|
151
|
+
if (step.blocked) return;
|
|
152
|
+
// Notify the parent directly; the follow-on focus move is suppressed in
|
|
153
|
+
// the focus-sync effect (keyed on the value) so onMonthChange fires once.
|
|
154
|
+
const nextFocus = focusedDateForMonth(
|
|
155
|
+
focusedDate,
|
|
156
|
+
step.start,
|
|
157
|
+
step.firstDay,
|
|
158
|
+
);
|
|
159
|
+
skipFocusSyncForRef.current = nextFocus;
|
|
160
|
+
notifyMonth(step.start);
|
|
161
|
+
setFocusedDate(nextFocus);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
setInternalMonth((m) => {
|
|
165
|
+
const step = policy.step(m, direction);
|
|
166
|
+
if (step.blocked) return m;
|
|
167
|
+
setFocusedDate((prev) =>
|
|
168
|
+
focusedDateForMonth(prev, step.start, step.firstDay),
|
|
169
|
+
);
|
|
170
|
+
return step.start;
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
[isControlled, month, policy, notifyMonth, focusedDate, setFocusedDate],
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const goNextMonth = useCallback(() => go("next"), [go]);
|
|
177
|
+
const goPrevMonth = useCallback(() => go("prev"), [go]);
|
|
178
|
+
|
|
179
|
+
return { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth };
|
|
180
|
+
}
|
package/src/month-view-types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { MonthWindowPolicy } from "./month-window";
|
|
2
3
|
import type { MonthOutOfRangeBehavior } from "./out-of-range";
|
|
3
4
|
import type { MonthData, OutsideDays, RootState } from "./types";
|
|
4
5
|
|
|
@@ -71,6 +72,8 @@ export interface MonthViewStableContextValue {
|
|
|
71
72
|
outsideDays: OutsideDays;
|
|
72
73
|
/** How month navigation behaves at bounds. */
|
|
73
74
|
outOfRangeBehavior: MonthOutOfRangeBehavior;
|
|
75
|
+
/** Steps and reveals the visible month window. */
|
|
76
|
+
windowPolicy: MonthWindowPolicy;
|
|
74
77
|
/** Navigate to the next month(s). */
|
|
75
78
|
goNextMonth: () => void;
|
|
76
79
|
/** Navigate to the previous month(s). */
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { act, cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { MonthView } from "./month-view";
|
|
5
|
+
import type { MonthViewProps } from "./month-view";
|
|
6
|
+
import { useMonthViewStable, useMonthViewState } from "./month-view-context";
|
|
7
|
+
import { useViewContext } from "./view-context";
|
|
8
|
+
|
|
9
|
+
const T = Temporal;
|
|
10
|
+
|
|
11
|
+
let go: { next: () => void; prev: () => void };
|
|
12
|
+
|
|
13
|
+
function Probe() {
|
|
14
|
+
const { goNextMonth, goPrevMonth } = useMonthViewStable();
|
|
15
|
+
const { currentMonth } = useMonthViewState();
|
|
16
|
+
const { focusedDate } = useViewContext();
|
|
17
|
+
go = { next: goNextMonth, prev: goPrevMonth };
|
|
18
|
+
return (
|
|
19
|
+
<>
|
|
20
|
+
<span data-testid="month">
|
|
21
|
+
{currentMonth.year}-{String(currentMonth.month).padStart(2, "0")}
|
|
22
|
+
</span>
|
|
23
|
+
<span data-testid="focus">{focusedDate.toString()}</span>
|
|
24
|
+
</>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const base = {
|
|
29
|
+
temporal: T,
|
|
30
|
+
timeZone: "UTC",
|
|
31
|
+
defaultValue: T.PlainDate.from("2026-03-15"),
|
|
32
|
+
} as const;
|
|
33
|
+
|
|
34
|
+
const month = () => screen.getByTestId("month").textContent;
|
|
35
|
+
const focus = () => screen.getByTestId("focus").textContent;
|
|
36
|
+
|
|
37
|
+
afterEach(cleanup);
|
|
38
|
+
|
|
39
|
+
describe("MonthView goNextMonth/goPrevMonth honor outOfRangeBehavior", () => {
|
|
40
|
+
it.each([
|
|
41
|
+
{
|
|
42
|
+
description: "stop: goPrevMonth into the min month moves",
|
|
43
|
+
props: {
|
|
44
|
+
min: T.PlainDate.from("2026-02-10"),
|
|
45
|
+
outOfRangeBehavior: "stop",
|
|
46
|
+
},
|
|
47
|
+
call: "prev" as const,
|
|
48
|
+
expected: { month: "2026-02", focus: "2026-02-01" },
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
description: "stop: goPrevMonth before the min month does nothing",
|
|
52
|
+
props: {
|
|
53
|
+
min: T.PlainDate.from("2026-03-10"),
|
|
54
|
+
outOfRangeBehavior: "stop",
|
|
55
|
+
},
|
|
56
|
+
call: "prev" as const,
|
|
57
|
+
expected: { month: "2026-03", focus: "2026-03-15" },
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
description: "stop: goNextMonth past the max month does nothing",
|
|
61
|
+
props: {
|
|
62
|
+
max: T.PlainDate.from("2026-03-20"),
|
|
63
|
+
outOfRangeBehavior: "stop",
|
|
64
|
+
},
|
|
65
|
+
call: "next" as const,
|
|
66
|
+
expected: { month: "2026-03", focus: "2026-03-15" },
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
description: "stop: goNextMonth with only min set still moves",
|
|
70
|
+
props: {
|
|
71
|
+
min: T.PlainDate.from("2026-03-10"),
|
|
72
|
+
outOfRangeBehavior: "stop",
|
|
73
|
+
},
|
|
74
|
+
call: "next" as const,
|
|
75
|
+
expected: { month: "2026-04", focus: "2026-04-01" },
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
description: "unbounded: goNextMonth moves past max",
|
|
79
|
+
props: { max: T.PlainDate.from("2026-03-20") },
|
|
80
|
+
call: "next" as const,
|
|
81
|
+
expected: { month: "2026-04", focus: "2026-04-01" },
|
|
82
|
+
},
|
|
83
|
+
] satisfies {
|
|
84
|
+
description: string;
|
|
85
|
+
props: Partial<MonthViewProps>;
|
|
86
|
+
call: "next" | "prev";
|
|
87
|
+
expected: { month: string; focus: string };
|
|
88
|
+
}[])("uncontrolled, $description", ({ props, call, expected }) => {
|
|
89
|
+
render(
|
|
90
|
+
<MonthView {...base} {...props}>
|
|
91
|
+
<Probe />
|
|
92
|
+
</MonthView>,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
act(() => go[call]());
|
|
96
|
+
|
|
97
|
+
expect({ month: month(), focus: focus() }).toEqual(expected);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it.each([
|
|
101
|
+
{
|
|
102
|
+
description: "stop: blocked step does not notify the parent",
|
|
103
|
+
props: {
|
|
104
|
+
max: T.PlainDate.from("2026-03-20"),
|
|
105
|
+
outOfRangeBehavior: "stop",
|
|
106
|
+
},
|
|
107
|
+
expected: { calls: 0 },
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
description: "unbounded: step notifies the parent once",
|
|
111
|
+
props: { max: T.PlainDate.from("2026-03-20") },
|
|
112
|
+
expected: { calls: 1 },
|
|
113
|
+
},
|
|
114
|
+
] satisfies {
|
|
115
|
+
description: string;
|
|
116
|
+
props: Partial<MonthViewProps>;
|
|
117
|
+
expected: { calls: number };
|
|
118
|
+
}[])("controlled, $description", ({ props, expected }) => {
|
|
119
|
+
const onMonthChange = vi.fn();
|
|
120
|
+
render(
|
|
121
|
+
<MonthView
|
|
122
|
+
{...base}
|
|
123
|
+
{...props}
|
|
124
|
+
month={T.PlainYearMonth.from("2026-03")}
|
|
125
|
+
onMonthChange={onMonthChange}
|
|
126
|
+
>
|
|
127
|
+
<Probe />
|
|
128
|
+
</MonthView>,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
act(() => go.next());
|
|
132
|
+
|
|
133
|
+
expect({ calls: onMonthChange.mock.calls.length }).toEqual(expected);
|
|
134
|
+
expect(month()).toBe("2026-03");
|
|
135
|
+
});
|
|
136
|
+
});
|
package/src/month-view.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
-
import { useCallback,
|
|
2
|
+
import { useCallback, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
4
4
|
import { CalendarProvider } from "./calendar-provider";
|
|
5
5
|
import type { CalendarProviderProps } from "./calendar-types";
|
|
@@ -7,20 +7,19 @@ import {
|
|
|
7
7
|
MonthViewStableContext,
|
|
8
8
|
MonthViewStateContext,
|
|
9
9
|
} from "./month-view-context";
|
|
10
|
+
import { useMonthNavigation } from "./month-view-navigation";
|
|
10
11
|
import type {
|
|
11
12
|
MonthViewRootProps,
|
|
12
13
|
MonthViewStableContextValue,
|
|
13
14
|
MonthViewStateContextValue,
|
|
14
15
|
} from "./month-view-types";
|
|
16
|
+
import { createMonthWindowPolicy } from "./month-window";
|
|
15
17
|
import type { MonthData, RootState, ValueFormat } from "./types";
|
|
16
18
|
import {
|
|
17
|
-
computeAdjacentMonth,
|
|
18
|
-
focusedDateForMonth,
|
|
19
19
|
getMonthWeeks,
|
|
20
20
|
resolveFocusTarget,
|
|
21
|
-
selectedToZdt,
|
|
22
|
-
shiftWindowToMonth,
|
|
23
21
|
resolveInitialAnchor,
|
|
22
|
+
selectedToZdt,
|
|
24
23
|
} from "./utils";
|
|
25
24
|
import {
|
|
26
25
|
ViewContext,
|
|
@@ -59,24 +58,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
59
58
|
const T = calStable.temporal;
|
|
60
59
|
const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
|
|
61
60
|
|
|
62
|
-
// --- Month navigation state (controlled/uncontrolled) ---
|
|
63
|
-
const isMonthControlled = monthProp !== undefined;
|
|
64
|
-
|
|
65
|
-
const [internalMonth, setInternalMonth] = useState<{
|
|
66
|
-
year: number;
|
|
67
|
-
month: number;
|
|
68
|
-
}>(() => {
|
|
69
|
-
if (monthProp) return { year: monthProp.year, month: monthProp.month };
|
|
70
|
-
if (defaultMonthProp)
|
|
71
|
-
return { year: defaultMonthProp.year, month: defaultMonthProp.month };
|
|
72
|
-
const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
|
|
73
|
-
return { year: anchor.year, month: anchor.month };
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const currentMonth = isMonthControlled
|
|
77
|
-
? { year: monthProp.year, month: monthProp.month }
|
|
78
|
-
: internalMonth;
|
|
79
|
-
|
|
80
61
|
// --- Focus state ---
|
|
81
62
|
const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() =>
|
|
82
63
|
resolveInitialAnchor(calState.selected, today, timeZone, T),
|
|
@@ -85,6 +66,37 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
85
66
|
const [gridHasFocus, setGridHasFocus] = useState(false);
|
|
86
67
|
const gridFocusedRef = useRef(false);
|
|
87
68
|
|
|
69
|
+
// --- Month window and navigation ---
|
|
70
|
+
const { minValue, maxValue } = calStable;
|
|
71
|
+
const windowPolicy = useMemo(
|
|
72
|
+
() =>
|
|
73
|
+
createMonthWindowPolicy({
|
|
74
|
+
numberOfMonths,
|
|
75
|
+
behavior: outOfRangeBehavior,
|
|
76
|
+
min: minValue,
|
|
77
|
+
max: maxValue,
|
|
78
|
+
T,
|
|
79
|
+
}),
|
|
80
|
+
[numberOfMonths, outOfRangeBehavior, minValue, maxValue, T],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const initialAnchor = useCallback(
|
|
84
|
+
() => resolveInitialAnchor(calState.selected, today, timeZone, T),
|
|
85
|
+
[calState.selected, today, timeZone, T],
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth } =
|
|
89
|
+
useMonthNavigation({
|
|
90
|
+
month: monthProp,
|
|
91
|
+
defaultMonth: defaultMonthProp,
|
|
92
|
+
initialAnchor,
|
|
93
|
+
onMonthChange,
|
|
94
|
+
policy: windowPolicy,
|
|
95
|
+
focusedDate,
|
|
96
|
+
setFocusedDate,
|
|
97
|
+
T,
|
|
98
|
+
});
|
|
99
|
+
|
|
88
100
|
// --- Grid label IDs ---
|
|
89
101
|
const [gridLabelIds, setGridLabelIds] = useState<Record<number, string>>({});
|
|
90
102
|
const setGridLabelId = useCallback(
|
|
@@ -103,162 +115,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
103
115
|
[],
|
|
104
116
|
);
|
|
105
117
|
|
|
106
|
-
// Always-current ref so callbacks/effects don't re-subscribe on every
|
|
107
|
-
// onMonthChange identity change.
|
|
108
|
-
const onMonthChangeRef = useRef(onMonthChange);
|
|
109
|
-
onMonthChangeRef.current = onMonthChange;
|
|
110
|
-
|
|
111
|
-
// Request the parent move the view to `{year, month}` (controlled mode).
|
|
112
|
-
// Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
|
|
113
|
-
// only affects display (see MonthYearString). Passing `calendar` here would
|
|
114
|
-
// reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
|
|
115
|
-
// shifting the value by centuries.
|
|
116
|
-
const notifyMonth = useCallback(
|
|
117
|
-
(year: number, month: number) => {
|
|
118
|
-
onMonthChangeRef.current?.(T.PlainYearMonth.from({ year, month }));
|
|
119
|
-
},
|
|
120
|
-
[T],
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
// Whether `{year, month}` falls within the currently displayed window.
|
|
124
|
-
const isMonthVisible = useCallback(
|
|
125
|
-
(targetYear: number, targetMonth: number) => {
|
|
126
|
-
for (let i = 0; i < numberOfMonths; i++) {
|
|
127
|
-
const totalMonths =
|
|
128
|
-
currentMonth.year * 12 + (currentMonth.month - 1) + i;
|
|
129
|
-
const y = Math.floor(totalMonths / 12);
|
|
130
|
-
const m = (totalMonths % 12) + 1;
|
|
131
|
-
if (targetYear === y && targetMonth === m) return true;
|
|
132
|
-
}
|
|
133
|
-
return false;
|
|
134
|
-
},
|
|
135
|
-
[currentMonth.year, currentMonth.month, numberOfMonths],
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
// --- navigateToMonth: shift the window by the minimum needed to reveal the
|
|
139
|
-
// target month (keeps keyboard crossing symmetric with the Prev/Next
|
|
140
|
-
// buttons, which step one month at a time). ---
|
|
141
|
-
const navigateToMonth = useCallback(
|
|
142
|
-
(targetYear: number, targetMonth: number) => {
|
|
143
|
-
if (isMonthControlled) return; // controlled mode: parent manages
|
|
144
|
-
setInternalMonth((prev) => {
|
|
145
|
-
const next = shiftWindowToMonth(
|
|
146
|
-
prev,
|
|
147
|
-
{ year: targetYear, month: targetMonth },
|
|
148
|
-
numberOfMonths,
|
|
149
|
-
);
|
|
150
|
-
if (next.year === prev.year && next.month === prev.month) return prev;
|
|
151
|
-
return next;
|
|
152
|
-
});
|
|
153
|
-
},
|
|
154
|
-
[isMonthControlled, numberOfMonths],
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
// Button navigation in controlled mode notifies the parent directly, then
|
|
158
|
-
// moves focus into the target month. This ref holds that focus value so the
|
|
159
|
-
// focus-sync effect skips re-notifying for it. Keyed by the value (not a
|
|
160
|
-
// one-shot flag) so it can't strand and suppress a later keyboard crossing
|
|
161
|
-
// if the button's focus move is a no-op or the parent ignores the change.
|
|
162
|
-
const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
|
|
163
|
-
const prevFocusedRef = useRef(focusedDate);
|
|
164
|
-
|
|
165
|
-
// Clear a stale skip target whenever the parent actually moves the view.
|
|
166
|
-
useEffect(() => {
|
|
167
|
-
skipFocusSyncForRef.current = null;
|
|
168
|
-
}, [monthProp?.year, monthProp?.month]);
|
|
169
|
-
|
|
170
|
-
// Sync month when the focused date crosses a month boundary. Uncontrolled:
|
|
171
|
-
// shift internal state. Controlled: ask the parent to move the view, but
|
|
172
|
-
// only when focus actually moved out of the visible window (keyboard nav) —
|
|
173
|
-
// never as an echo of an external `month` update or a button click.
|
|
174
|
-
useEffect(() => {
|
|
175
|
-
const prevFocused = prevFocusedRef.current;
|
|
176
|
-
prevFocusedRef.current = focusedDate;
|
|
177
|
-
if (isMonthControlled) {
|
|
178
|
-
if (skipFocusSyncForRef.current === focusedDate) {
|
|
179
|
-
skipFocusSyncForRef.current = null;
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
if (prevFocused === focusedDate) return;
|
|
183
|
-
if (isMonthVisible(focusedDate.year, focusedDate.month)) return;
|
|
184
|
-
// Emit the minimally-shifted first-visible month (what the parent binds
|
|
185
|
-
// to `month`), not the focused month — so a controlled window scrolls by
|
|
186
|
-
// the minimum, matching uncontrolled mode and the nav buttons.
|
|
187
|
-
const next = shiftWindowToMonth(
|
|
188
|
-
{ year: currentMonth.year, month: currentMonth.month },
|
|
189
|
-
{ year: focusedDate.year, month: focusedDate.month },
|
|
190
|
-
numberOfMonths,
|
|
191
|
-
);
|
|
192
|
-
notifyMonth(next.year, next.month);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
navigateToMonth(focusedDate.year, focusedDate.month);
|
|
196
|
-
}, [
|
|
197
|
-
focusedDate,
|
|
198
|
-
navigateToMonth,
|
|
199
|
-
isMonthControlled,
|
|
200
|
-
isMonthVisible,
|
|
201
|
-
notifyMonth,
|
|
202
|
-
currentMonth.year,
|
|
203
|
-
currentMonth.month,
|
|
204
|
-
numberOfMonths,
|
|
205
|
-
]);
|
|
206
|
-
|
|
207
|
-
// --- Month navigation callbacks ---
|
|
208
|
-
const goNextMonth = useCallback(() => {
|
|
209
|
-
if (isMonthControlled) {
|
|
210
|
-
const { year, month, firstDay } = computeAdjacentMonth(
|
|
211
|
-
{ year: monthProp!.year, month: monthProp!.month },
|
|
212
|
-
"next",
|
|
213
|
-
T,
|
|
214
|
-
);
|
|
215
|
-
// Notify the parent directly; the follow-on focus move is suppressed in
|
|
216
|
-
// the focus-sync effect (keyed on the value) so onMonthChange fires once.
|
|
217
|
-
const nextFocus = focusedDateForMonth(
|
|
218
|
-
focusedDate,
|
|
219
|
-
{ year, month },
|
|
220
|
-
firstDay,
|
|
221
|
-
);
|
|
222
|
-
skipFocusSyncForRef.current = nextFocus;
|
|
223
|
-
notifyMonth(year, month);
|
|
224
|
-
setFocusedDate(nextFocus);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
setInternalMonth((m) => {
|
|
228
|
-
const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
|
|
229
|
-
setFocusedDate((prev) =>
|
|
230
|
-
focusedDateForMonth(prev, { year, month }, firstDay),
|
|
231
|
-
);
|
|
232
|
-
return { year, month };
|
|
233
|
-
});
|
|
234
|
-
}, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
|
|
235
|
-
|
|
236
|
-
const goPrevMonth = useCallback(() => {
|
|
237
|
-
if (isMonthControlled) {
|
|
238
|
-
const { year, month, firstDay } = computeAdjacentMonth(
|
|
239
|
-
{ year: monthProp!.year, month: monthProp!.month },
|
|
240
|
-
"prev",
|
|
241
|
-
T,
|
|
242
|
-
);
|
|
243
|
-
const nextFocus = focusedDateForMonth(
|
|
244
|
-
focusedDate,
|
|
245
|
-
{ year, month },
|
|
246
|
-
firstDay,
|
|
247
|
-
);
|
|
248
|
-
skipFocusSyncForRef.current = nextFocus;
|
|
249
|
-
notifyMonth(year, month);
|
|
250
|
-
setFocusedDate(nextFocus);
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
setInternalMonth((m) => {
|
|
254
|
-
const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
|
|
255
|
-
setFocusedDate((prev) =>
|
|
256
|
-
focusedDateForMonth(prev, { year, month }, firstDay),
|
|
257
|
-
);
|
|
258
|
-
return { year, month };
|
|
259
|
-
});
|
|
260
|
-
}, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
|
|
261
|
-
|
|
262
118
|
// --- Grid computation ---
|
|
263
119
|
const allMonths = useMemo<MonthData[]>(() => {
|
|
264
120
|
const opts = { weekStartDay, fixedWeeks };
|
|
@@ -340,32 +196,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
340
196
|
],
|
|
341
197
|
);
|
|
342
198
|
|
|
343
|
-
// --- viewingYearMonth (for the render-prop rootState) ---
|
|
344
|
-
// ISO — the locale calendar only affects display, not this value.
|
|
345
|
-
const viewingYearMonth = useMemo(
|
|
346
|
-
() =>
|
|
347
|
-
T.PlainYearMonth.from({
|
|
348
|
-
year: currentMonth.year,
|
|
349
|
-
month: currentMonth.month,
|
|
350
|
-
}),
|
|
351
|
-
[currentMonth, T],
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
// --- Fire onMonthChange when uncontrolled navigation moves the view (not on
|
|
355
|
-
// mount). In controlled mode the parent owns `month`, so notification flows
|
|
356
|
-
// from button clicks (goNext/goPrevMonth) and the focus-sync effect instead;
|
|
357
|
-
// firing here would just echo the parent's own update back to it. ---
|
|
358
|
-
const mountedRef = useRef(false);
|
|
359
|
-
|
|
360
|
-
useEffect(() => {
|
|
361
|
-
if (!mountedRef.current) {
|
|
362
|
-
mountedRef.current = true;
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
if (isMonthControlled) return;
|
|
366
|
-
onMonthChangeRef.current?.(viewingYearMonth);
|
|
367
|
-
}, [viewingYearMonth, isMonthControlled]);
|
|
368
|
-
|
|
369
199
|
// --- rootState (for render functions) ---
|
|
370
200
|
const rootState = useMemo<RootState>(
|
|
371
201
|
() => ({
|
|
@@ -383,6 +213,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
383
213
|
fixedWeeks,
|
|
384
214
|
outsideDays,
|
|
385
215
|
outOfRangeBehavior,
|
|
216
|
+
windowPolicy,
|
|
386
217
|
goNextMonth,
|
|
387
218
|
goPrevMonth,
|
|
388
219
|
setGridLabelId,
|
|
@@ -393,6 +224,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
393
224
|
fixedWeeks,
|
|
394
225
|
outsideDays,
|
|
395
226
|
outOfRangeBehavior,
|
|
227
|
+
windowPolicy,
|
|
396
228
|
goNextMonth,
|
|
397
229
|
goPrevMonth,
|
|
398
230
|
setGridLabelId,
|