@klinking/colander 3.1.0-alpha.2 → 3.1.0-alpha.4
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 +11 -2
- package/dist/index.d.mts +11 -2
- package/dist/index.mjs +1 -1
- package/dist/{weeks-navigation-BRxzsy0z.cjs → weeks-navigation-DV538__x.cjs} +115 -135
- package/dist/{weeks-navigation-CXRaVP02.mjs → weeks-navigation-elCpdfQ3.mjs} +115 -135
- package/dist/{index-CJgr0MEp.d.cts → weeks-view-context-CUCy8njs.d.cts} +3 -8
- package/dist/{index-DB6V_Y6x.d.mts → weeks-view-context-E1NwyNgQ.d.mts} +3 -8
- package/package.json +1 -1
- package/src/calendar-provider.tsx +6 -0
- package/src/calendar-types.ts +2 -0
- package/src/day-cell.tsx +17 -16
- package/src/drag-handle.tsx +2 -2
- package/src/grid-header.tsx +3 -3
- package/src/grid.tsx +20 -61
- package/src/month-view.tsx +32 -17
- package/src/navigation.tsx +3 -2
- package/src/range-preview.tsx +3 -5
- package/src/selected-range.tsx +3 -5
- package/src/today.test.tsx +98 -0
- package/src/utils.test.ts +25 -0
- package/src/utils.ts +17 -0
- package/src/view-context.test.tsx +126 -0
- package/src/view-context.ts +43 -0
- package/src/week-number.tsx +3 -3
- package/src/weeks-navigation.tsx +3 -3
- package/src/weeks-view-state.ts +31 -17
- package/src/weeks-view.test.tsx +115 -13
- package/src/weeks-view.tsx +22 -51
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
+
import { act, fireEvent, render, screen } from "@testing-library/react";
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
import { describe, expect, it } from "vitest";
|
|
6
|
+
import {
|
|
7
|
+
DayButton,
|
|
8
|
+
DayCellTemplate,
|
|
9
|
+
Grid,
|
|
10
|
+
GridBody,
|
|
11
|
+
WeekTemplate,
|
|
12
|
+
} from "./grid";
|
|
13
|
+
import { MonthView } from "./month-view";
|
|
14
|
+
import { useViewStable, useViewState } from "./view-context";
|
|
15
|
+
import { WeeksView } from "./weeks-view";
|
|
16
|
+
|
|
17
|
+
const T = Temporal;
|
|
18
|
+
|
|
19
|
+
const counts = { stable: 0, state: 0 };
|
|
20
|
+
|
|
21
|
+
function StableProbe() {
|
|
22
|
+
useViewStable();
|
|
23
|
+
counts.stable++;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function StateProbe() {
|
|
28
|
+
useViewState();
|
|
29
|
+
counts.state++;
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const grid = (
|
|
34
|
+
<Grid>
|
|
35
|
+
<GridBody>
|
|
36
|
+
<WeekTemplate>
|
|
37
|
+
<DayCellTemplate>
|
|
38
|
+
<DayButton />
|
|
39
|
+
</DayCellTemplate>
|
|
40
|
+
</WeekTemplate>
|
|
41
|
+
</GridBody>
|
|
42
|
+
</Grid>
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const views: {
|
|
46
|
+
description: string;
|
|
47
|
+
view: (children: ReactNode) => ReactNode;
|
|
48
|
+
}[] = [
|
|
49
|
+
{
|
|
50
|
+
description: "MonthView",
|
|
51
|
+
view: (children) => (
|
|
52
|
+
<MonthView
|
|
53
|
+
temporal={T}
|
|
54
|
+
defaultValue={T.PlainDate.from("2026-03-15")}
|
|
55
|
+
defaultMonth={T.PlainYearMonth.from("2026-03")}
|
|
56
|
+
>
|
|
57
|
+
{children}
|
|
58
|
+
</MonthView>
|
|
59
|
+
),
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
description: "WeeksView",
|
|
63
|
+
view: (children) => (
|
|
64
|
+
<WeeksView
|
|
65
|
+
temporal={T}
|
|
66
|
+
weekCount={4}
|
|
67
|
+
defaultFirstWeek={T.PlainDate.from("2026-03-01")}
|
|
68
|
+
defaultValue={T.PlainDate.from("2026-03-15")}
|
|
69
|
+
>
|
|
70
|
+
{children}
|
|
71
|
+
</WeeksView>
|
|
72
|
+
),
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
describe.each(views)("$description stable/state view contexts", ({ view }) => {
|
|
77
|
+
function setup() {
|
|
78
|
+
render(
|
|
79
|
+
view(
|
|
80
|
+
<>
|
|
81
|
+
<StableProbe />
|
|
82
|
+
<StateProbe />
|
|
83
|
+
{grid}
|
|
84
|
+
</>,
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
const day = (n: number) =>
|
|
88
|
+
screen.getAllByRole("button").find((b) => b.textContent === String(n))!;
|
|
89
|
+
const reset = () => {
|
|
90
|
+
counts.stable = 0;
|
|
91
|
+
counts.state = 0;
|
|
92
|
+
};
|
|
93
|
+
return { day, reset };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
it.each([
|
|
97
|
+
{
|
|
98
|
+
description: "grid focus does not re-render either context's consumers",
|
|
99
|
+
act: (day: (n: number) => HTMLElement) => day(15).focus(),
|
|
100
|
+
expected: { stable: 0, state: 0 },
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
description: "hovering a day does not re-render either",
|
|
104
|
+
act: (day: (n: number) => HTMLElement) => {
|
|
105
|
+
fireEvent.pointerEnter(day(20));
|
|
106
|
+
fireEvent.mouseOver(day(20));
|
|
107
|
+
},
|
|
108
|
+
expected: { stable: 0, state: 0 },
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
description: "moving focus re-renders state consumers only",
|
|
112
|
+
act: (day: (n: number) => HTMLElement) => {
|
|
113
|
+
fireEvent.keyDown(day(15), { key: "ArrowRight" });
|
|
114
|
+
},
|
|
115
|
+
expected: { stable: 0, state: 1 },
|
|
116
|
+
},
|
|
117
|
+
])("$description", ({ act: action, expected }) => {
|
|
118
|
+
const { day, reset } = setup();
|
|
119
|
+
act(() => day(15).focus());
|
|
120
|
+
reset();
|
|
121
|
+
|
|
122
|
+
act(() => action(day));
|
|
123
|
+
|
|
124
|
+
expect(counts).toEqual(expected);
|
|
125
|
+
});
|
|
126
|
+
});
|
package/src/view-context.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { createContext, useContext } from "react";
|
|
3
|
+
import type { OutsideDays, RootState } from "./types";
|
|
3
4
|
|
|
4
5
|
/** Shared context provided by both MonthView.Root and WeeksView.Root. */
|
|
5
6
|
export interface ViewContextValue {
|
|
@@ -17,6 +18,22 @@ export interface ViewContextValue {
|
|
|
17
18
|
setGridHasFocus: (v: boolean) => void;
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
/** @internal Rarely-changing values shared by both views' parts. */
|
|
22
|
+
export interface ViewStableContextValue {
|
|
23
|
+
/** Ref mirroring whether the grid holds DOM focus, without triggering re-renders. */
|
|
24
|
+
gridFocusedRef: React.RefObject<boolean>;
|
|
25
|
+
/** How days outside the viewed month are displayed. Always `"enabled"` in WeeksView. */
|
|
26
|
+
outsideDays: OutsideDays;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @internal Volatile values shared by both views' parts. */
|
|
30
|
+
export interface ViewStateContextValue {
|
|
31
|
+
/** The viewed month with the selection's time, used for "today" highlighting and labels. */
|
|
32
|
+
currentDateTime: Temporal.PlainDateTime;
|
|
33
|
+
/** The root component's state object for render functions. */
|
|
34
|
+
rootState: RootState;
|
|
35
|
+
}
|
|
36
|
+
|
|
20
37
|
/** @internal React context for the active view (MonthView or WeeksView). */
|
|
21
38
|
export const ViewContext = createContext<ViewContextValue | null>(null);
|
|
22
39
|
|
|
@@ -29,3 +46,29 @@ export function useViewContext(): ViewContextValue {
|
|
|
29
46
|
);
|
|
30
47
|
return ctx;
|
|
31
48
|
}
|
|
49
|
+
|
|
50
|
+
/** @internal Context for {@link ViewStableContextValue}. */
|
|
51
|
+
export const ViewStableContext = createContext<ViewStableContextValue | null>(
|
|
52
|
+
null,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
/** @internal Context for {@link ViewStateContextValue}. */
|
|
56
|
+
export const ViewStateContext = createContext<ViewStateContextValue | null>(
|
|
57
|
+
null,
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
/** Returns the nearest view's stable values. */
|
|
61
|
+
export function useViewStable(): ViewStableContextValue {
|
|
62
|
+
const ctx = useContext(ViewStableContext);
|
|
63
|
+
if (!ctx)
|
|
64
|
+
throw new Error("useViewStable must be used within MonthView or WeeksView");
|
|
65
|
+
return ctx;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Returns the nearest view's volatile state. */
|
|
69
|
+
export function useViewState(): ViewStateContextValue {
|
|
70
|
+
const ctx = useContext(ViewStateContext);
|
|
71
|
+
if (!ctx)
|
|
72
|
+
throw new Error("useViewState must be used within MonthView or WeeksView");
|
|
73
|
+
return ctx;
|
|
74
|
+
}
|
package/src/week-number.tsx
CHANGED
|
@@ -3,7 +3,6 @@ import { useRender } from "@base-ui/react/use-render";
|
|
|
3
3
|
import { forwardRef, useContext, useMemo } from "react";
|
|
4
4
|
import { useCalendarStable } from "./calendar-context";
|
|
5
5
|
import { WeekDataContext } from "./context";
|
|
6
|
-
import { useMonthViewState } from "./month-view-context";
|
|
7
6
|
import type { StateAttributesMapping } from "./types";
|
|
8
7
|
import type {
|
|
9
8
|
WeekNumberCellState,
|
|
@@ -12,6 +11,7 @@ import type {
|
|
|
12
11
|
WeekNumberHeaderProps,
|
|
13
12
|
} from "./types";
|
|
14
13
|
import { getISOWeekNumber } from "./utils";
|
|
14
|
+
import { useViewState } from "./view-context";
|
|
15
15
|
|
|
16
16
|
const weekNumberCellStateAttributesMapping = {
|
|
17
17
|
root: () => null,
|
|
@@ -24,7 +24,7 @@ function WeekNumberCellFn(
|
|
|
24
24
|
) {
|
|
25
25
|
const { render, ...otherProps } = props;
|
|
26
26
|
const weekData = useContext(WeekDataContext);
|
|
27
|
-
const { rootState } =
|
|
27
|
+
const { rootState } = useViewState();
|
|
28
28
|
const { temporal: T } = useCalendarStable();
|
|
29
29
|
|
|
30
30
|
const days = weekData?.days ?? [];
|
|
@@ -84,7 +84,7 @@ function WeekNumberHeaderFn(
|
|
|
84
84
|
ref: React.ForwardedRef<HTMLTableCellElement>,
|
|
85
85
|
) {
|
|
86
86
|
const { render, ...otherProps } = props;
|
|
87
|
-
const { rootState } =
|
|
87
|
+
const { rootState } = useViewState();
|
|
88
88
|
|
|
89
89
|
const state = useMemo<WeekNumberHeaderState>(
|
|
90
90
|
() => ({ root: rootState as unknown as WeekNumberHeaderState["root"] }),
|
package/src/weeks-navigation.tsx
CHANGED
|
@@ -2,9 +2,9 @@ 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 { useMonthViewState } from "./month-view-context";
|
|
6
5
|
import { canShift } from "./out-of-range";
|
|
7
6
|
import type { RootState, StateAttributesMapping } from "./types";
|
|
7
|
+
import { useViewState } from "./view-context";
|
|
8
8
|
import { useWeeksViewStable, useWeeksViewState } from "./weeks-view-context";
|
|
9
9
|
|
|
10
10
|
// ---------------------------------------------------------------------------
|
|
@@ -80,7 +80,7 @@ function useWeeksNavButton(
|
|
|
80
80
|
const { weekCount, outOfRangeBehavior, goNext, goPrev } =
|
|
81
81
|
useWeeksViewStable();
|
|
82
82
|
const { windowInfo } = useWeeksViewState();
|
|
83
|
-
const { rootState } =
|
|
83
|
+
const { rootState } = useViewState();
|
|
84
84
|
|
|
85
85
|
const currentFirstWeek = windowInfo.windowStart;
|
|
86
86
|
|
|
@@ -193,7 +193,7 @@ function WeekCountFn(
|
|
|
193
193
|
) {
|
|
194
194
|
const { render, ...otherProps } = props;
|
|
195
195
|
const { windowInfo } = useWeeksViewState();
|
|
196
|
-
const { rootState } =
|
|
196
|
+
const { rootState } = useViewState();
|
|
197
197
|
const count = windowInfo.weekCount;
|
|
198
198
|
|
|
199
199
|
const state = useMemo<WeekCountState>(
|
package/src/weeks-view-state.ts
CHANGED
|
@@ -9,8 +9,12 @@ import {
|
|
|
9
9
|
type FirstWeekSpec,
|
|
10
10
|
} from "./resolve-first-week";
|
|
11
11
|
import type { RootState } from "./types";
|
|
12
|
-
import {
|
|
13
|
-
import type {
|
|
12
|
+
import { resolveInitialAnchor, selectedToZdt } from "./utils";
|
|
13
|
+
import type {
|
|
14
|
+
ViewContextValue,
|
|
15
|
+
ViewStableContextValue,
|
|
16
|
+
ViewStateContextValue,
|
|
17
|
+
} from "./view-context";
|
|
14
18
|
import type {
|
|
15
19
|
WeeksViewRootProps,
|
|
16
20
|
WeeksViewStableContextValue,
|
|
@@ -42,7 +46,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
|
|
|
42
46
|
const calState = useCalendarState();
|
|
43
47
|
|
|
44
48
|
const T = calStable.temporal;
|
|
45
|
-
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone } =
|
|
49
|
+
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone, today } =
|
|
46
50
|
calStable;
|
|
47
51
|
|
|
48
52
|
// --- Controlled/uncontrolled firstWeek ---
|
|
@@ -59,16 +63,9 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
|
|
|
59
63
|
if (firstWeekProp !== undefined) return resolveSpec(firstWeekProp);
|
|
60
64
|
if (defaultFirstWeekProp !== undefined)
|
|
61
65
|
return resolveSpec(defaultFirstWeekProp);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
calState.selected,
|
|
66
|
-
timeZone,
|
|
67
|
-
T,
|
|
68
|
-
).toPlainDate();
|
|
69
|
-
return resolveSpec(plain);
|
|
70
|
-
}
|
|
71
|
-
return resolveSpec(T.Now.plainDateISO());
|
|
66
|
+
return resolveSpec(
|
|
67
|
+
resolveInitialAnchor(calState.selected, today, timeZone, T),
|
|
68
|
+
);
|
|
72
69
|
});
|
|
73
70
|
|
|
74
71
|
const resolvedFirstWeek = isControlled
|
|
@@ -141,10 +138,10 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
|
|
|
141
138
|
const firstWeek = weeks[0];
|
|
142
139
|
const lastWeek = weeks[weeks.length - 1];
|
|
143
140
|
if (firstWeek === undefined || lastWeek === undefined) {
|
|
144
|
-
const
|
|
141
|
+
const now = today();
|
|
145
142
|
return {
|
|
146
|
-
windowStart:
|
|
147
|
-
windowEnd:
|
|
143
|
+
windowStart: now,
|
|
144
|
+
windowEnd: now,
|
|
148
145
|
weekCount: 0,
|
|
149
146
|
dayCount: 0,
|
|
150
147
|
enabledWeekCount: 0,
|
|
@@ -491,5 +488,22 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
|
|
|
491
488
|
[focusedDate, tabTargetDate, gridHasFocus],
|
|
492
489
|
);
|
|
493
490
|
|
|
494
|
-
|
|
491
|
+
const viewStableCtx = useMemo<ViewStableContextValue>(
|
|
492
|
+
() => ({ gridFocusedRef, outsideDays: "enabled" }),
|
|
493
|
+
[],
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
const viewStateCtx = useMemo<ViewStateContextValue>(
|
|
497
|
+
() => ({ currentDateTime, rootState }),
|
|
498
|
+
[currentDateTime, rootState],
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
return {
|
|
502
|
+
stableCtx,
|
|
503
|
+
stateCtx,
|
|
504
|
+
viewCtx,
|
|
505
|
+
viewStableCtx,
|
|
506
|
+
viewStateCtx,
|
|
507
|
+
scrollToWeek,
|
|
508
|
+
};
|
|
495
509
|
}
|
package/src/weeks-view.test.tsx
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
render,
|
|
4
|
+
screen,
|
|
5
|
+
act,
|
|
6
|
+
cleanup,
|
|
7
|
+
fireEvent,
|
|
8
|
+
within,
|
|
9
|
+
} from "@testing-library/react";
|
|
3
10
|
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
4
11
|
import {
|
|
5
12
|
Grid,
|
|
@@ -16,7 +23,7 @@ import {
|
|
|
16
23
|
MonthSeparatorWeekCount,
|
|
17
24
|
} from "./month-separator";
|
|
18
25
|
import { useMonthViewState } from "./month-view-context";
|
|
19
|
-
import { useViewContext } from "./view-context";
|
|
26
|
+
import { useViewContext, useViewState } from "./view-context";
|
|
20
27
|
import { NextWeeksButton, PrevWeeksButton } from "./weeks-navigation";
|
|
21
28
|
import { WeeksView } from "./weeks-view";
|
|
22
29
|
import { useWeeksViewState, useWeeksViewStable } from "./weeks-view-context";
|
|
@@ -538,21 +545,19 @@ describe("WeeksView regression: goNext/goPrev shift by weekCount (Bug #3)", () =
|
|
|
538
545
|
});
|
|
539
546
|
});
|
|
540
547
|
|
|
541
|
-
describe("WeeksView regression: rootState
|
|
542
|
-
function
|
|
548
|
+
describe("WeeksView regression: rootState is populated", () => {
|
|
549
|
+
function RootStateCapture({
|
|
543
550
|
onCapture,
|
|
544
551
|
}: {
|
|
545
|
-
onCapture: (
|
|
546
|
-
root: ReturnType<typeof useMonthViewState>["rootState"],
|
|
547
|
-
) => void;
|
|
552
|
+
onCapture: (root: ReturnType<typeof useViewState>["rootState"]) => void;
|
|
548
553
|
}) {
|
|
549
|
-
const { rootState } =
|
|
554
|
+
const { rootState } = useViewState();
|
|
550
555
|
onCapture(rootState);
|
|
551
556
|
return null;
|
|
552
557
|
}
|
|
553
558
|
|
|
554
|
-
it("exposes a real rootState to components reading
|
|
555
|
-
let root: ReturnType<typeof
|
|
559
|
+
it("exposes a real rootState to components reading useViewState()", () => {
|
|
560
|
+
let root: ReturnType<typeof useViewState>["rootState"] | undefined;
|
|
556
561
|
render(
|
|
557
562
|
<WeeksView
|
|
558
563
|
temporal={T}
|
|
@@ -560,7 +565,7 @@ describe("WeeksView regression: rootState shim is populated", () => {
|
|
|
560
565
|
defaultFirstWeek={T.PlainDate.from("2026-03-01")}
|
|
561
566
|
defaultValue={T.PlainDate.from("2026-03-15")}
|
|
562
567
|
>
|
|
563
|
-
<
|
|
568
|
+
<RootStateCapture
|
|
564
569
|
onCapture={(r) => {
|
|
565
570
|
root = r;
|
|
566
571
|
}}
|
|
@@ -568,7 +573,6 @@ describe("WeeksView regression: rootState shim is populated", () => {
|
|
|
568
573
|
</WeeksView>,
|
|
569
574
|
);
|
|
570
575
|
|
|
571
|
-
// Pre-fix this shim was `{} as RootState` — every field undefined at runtime.
|
|
572
576
|
expect(root).toBeDefined();
|
|
573
577
|
expect(root!.viewing).toBeInstanceOf(Temporal.PlainYearMonth);
|
|
574
578
|
expect(root!.focused).toBeInstanceOf(Temporal.PlainDate);
|
|
@@ -576,7 +580,7 @@ describe("WeeksView regression: rootState shim is populated", () => {
|
|
|
576
580
|
expect(root!.timeZone.length).toBeGreaterThan(0);
|
|
577
581
|
expect(typeof root!.locale).toBe("string");
|
|
578
582
|
expect(typeof root!.readOnly).toBe("boolean");
|
|
579
|
-
// Selection flows through the shared baseRootState
|
|
583
|
+
// Selection flows through the shared baseRootState.
|
|
580
584
|
expect(root!.hasSelection).toBe(true);
|
|
581
585
|
expect(root!.selected).toBeInstanceOf(Temporal.PlainDate);
|
|
582
586
|
expect(root!.selected!.toString()).toBe("2026-03-15");
|
|
@@ -585,3 +589,101 @@ describe("WeeksView regression: rootState shim is populated", () => {
|
|
|
585
589
|
).toBe(true);
|
|
586
590
|
});
|
|
587
591
|
});
|
|
592
|
+
|
|
593
|
+
describe("WeeksView does not provide MonthView contexts", () => {
|
|
594
|
+
function MonthStateProbe() {
|
|
595
|
+
useMonthViewState();
|
|
596
|
+
return null;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
it("useMonthViewState throws instead of returning placeholder values", () => {
|
|
600
|
+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
601
|
+
expect(() =>
|
|
602
|
+
render(
|
|
603
|
+
<WeeksView temporal={T} weekCount={4}>
|
|
604
|
+
<MonthStateProbe />
|
|
605
|
+
</WeeksView>,
|
|
606
|
+
),
|
|
607
|
+
).toThrow();
|
|
608
|
+
error.mockRestore();
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
describe("WeeksView: Grid renders the weeks grid shell", () => {
|
|
613
|
+
function renderWeeksGrid() {
|
|
614
|
+
return render(
|
|
615
|
+
<WeeksView
|
|
616
|
+
temporal={T}
|
|
617
|
+
weekCount={4}
|
|
618
|
+
defaultFirstWeek={T.PlainDate.from("2026-03-01")}
|
|
619
|
+
defaultValue={T.PlainDate.from("2026-03-15")}
|
|
620
|
+
>
|
|
621
|
+
<FocusDisplay />
|
|
622
|
+
<WindowDisplay />
|
|
623
|
+
<Grid>
|
|
624
|
+
<GridBody>
|
|
625
|
+
<WeekTemplate>
|
|
626
|
+
<DayCellTemplate>
|
|
627
|
+
<DayButton />
|
|
628
|
+
</DayCellTemplate>
|
|
629
|
+
</WeekTemplate>
|
|
630
|
+
</GridBody>
|
|
631
|
+
</Grid>
|
|
632
|
+
</WeeksView>,
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
it("reports the window's week count on the grid", () => {
|
|
637
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
638
|
+
renderWeeksGrid();
|
|
639
|
+
|
|
640
|
+
const grid = screen.getByRole("grid");
|
|
641
|
+
expect(grid.getAttribute("data-weeks-in-month")).toBe("4");
|
|
642
|
+
expect(grid.style.getPropertyValue("--calendar-weeks-in-month")).toBe("4");
|
|
643
|
+
expect(warn).not.toHaveBeenCalled();
|
|
644
|
+
warn.mockRestore();
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it.each([
|
|
648
|
+
{
|
|
649
|
+
description: "PageDown moves focus by weekCount weeks",
|
|
650
|
+
key: "PageDown",
|
|
651
|
+
shiftKey: false,
|
|
652
|
+
expected: { focus: "2026-04-12" },
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
description: "Shift+PageDown moves focus by a year",
|
|
656
|
+
key: "PageDown",
|
|
657
|
+
shiftKey: true,
|
|
658
|
+
expected: { focus: "2027-03-15" },
|
|
659
|
+
},
|
|
660
|
+
])("$description", ({ key, shiftKey, expected }) => {
|
|
661
|
+
renderWeeksGrid();
|
|
662
|
+
|
|
663
|
+
fireEvent.keyDown(screen.getByRole("grid"), { key, shiftKey });
|
|
664
|
+
|
|
665
|
+
expect(screen.getByTestId("focus").textContent).toBe(expected.focus);
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
it("moves DOM focus to the newly focused day after an arrow key", () => {
|
|
669
|
+
renderWeeksGrid();
|
|
670
|
+
const start = screen.getByRole("button", { name: /15/ });
|
|
671
|
+
act(() => start.focus());
|
|
672
|
+
|
|
673
|
+
fireEvent.keyDown(start, { key: "ArrowRight" });
|
|
674
|
+
|
|
675
|
+
expect(screen.getByTestId("focus").textContent).toBe("2026-03-16");
|
|
676
|
+
expect(document.activeElement).toBe(
|
|
677
|
+
screen.getByRole("button", { name: /16/ }),
|
|
678
|
+
);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
it("End moves focus to the last day of the window and keeps the window", () => {
|
|
682
|
+
renderWeeksGrid();
|
|
683
|
+
|
|
684
|
+
fireEvent.keyDown(screen.getByRole("grid"), { key: "End" });
|
|
685
|
+
|
|
686
|
+
expect(screen.getByTestId("focus").textContent).toBe("2026-03-28");
|
|
687
|
+
expect(screen.getByTestId("start").textContent).toBe("2026-03-01");
|
|
688
|
+
});
|
|
689
|
+
});
|
package/src/weeks-view.tsx
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import React, { forwardRef, useImperativeHandle, useMemo, useRef } from "react";
|
|
1
|
+
import React, { forwardRef, useImperativeHandle } from "react";
|
|
3
2
|
import { CalendarProvider } from "./calendar-provider";
|
|
4
3
|
import type { CalendarProviderProps } from "./calendar-types";
|
|
5
|
-
import {
|
|
6
|
-
MonthViewStableContext,
|
|
7
|
-
MonthViewStateContext,
|
|
8
|
-
} from "./month-view-context";
|
|
9
4
|
import type { FirstWeekSpec, ScrollToWeekSnap } from "./resolve-first-week";
|
|
10
5
|
import type { ValueFormat } from "./types";
|
|
11
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
ViewContext,
|
|
8
|
+
ViewStableContext,
|
|
9
|
+
ViewStateContext,
|
|
10
|
+
} from "./view-context";
|
|
12
11
|
import {
|
|
13
12
|
WeeksViewStableContext,
|
|
14
13
|
WeeksViewStateContext,
|
|
@@ -33,57 +32,29 @@ function WeeksViewRootFn(
|
|
|
33
32
|
ref: React.ForwardedRef<WeeksViewRootHandle>,
|
|
34
33
|
) {
|
|
35
34
|
const { children } = props;
|
|
36
|
-
const {
|
|
37
|
-
|
|
35
|
+
const {
|
|
36
|
+
stableCtx,
|
|
37
|
+
stateCtx,
|
|
38
|
+
viewCtx,
|
|
39
|
+
viewStableCtx,
|
|
40
|
+
viewStateCtx,
|
|
41
|
+
scrollToWeek,
|
|
42
|
+
} = useWeeksViewRootState(props);
|
|
38
43
|
|
|
39
44
|
useImperativeHandle(ref, () => ({ scrollToWeek }), [scrollToWeek]);
|
|
40
45
|
|
|
41
|
-
const gridFocusedRef = useRef(false);
|
|
42
|
-
|
|
43
|
-
// Provide MonthViewStableContext/MonthViewStateContext shims so shared
|
|
44
|
-
// components (WeeksNavButton, WeekCount, etc.) that read rootState via
|
|
45
|
-
// useMonthViewState() can function outside the Grid in WeeksView.
|
|
46
|
-
const monthViewStableShim = useMemo(
|
|
47
|
-
() => ({
|
|
48
|
-
numberOfMonths: 1,
|
|
49
|
-
fixedWeeks: false,
|
|
50
|
-
outsideDays: "enabled" as const,
|
|
51
|
-
outOfRangeBehavior: "unbounded" as const,
|
|
52
|
-
goNextMonth: () => {},
|
|
53
|
-
goPrevMonth: () => {},
|
|
54
|
-
setGridLabelId: () => {},
|
|
55
|
-
gridFocusedRef,
|
|
56
|
-
}),
|
|
57
|
-
[],
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
const monthViewStateShim = useMemo(
|
|
61
|
-
() => ({
|
|
62
|
-
currentMonth: {
|
|
63
|
-
year: stateCtx.currentDateTime.year,
|
|
64
|
-
month: stateCtx.currentDateTime.month,
|
|
65
|
-
},
|
|
66
|
-
weeks: [] as Temporal.PlainDate[][],
|
|
67
|
-
allMonths: [],
|
|
68
|
-
currentDateTime: stateCtx.currentDateTime,
|
|
69
|
-
gridLabelIds: stateCtx.gridLabelIds,
|
|
70
|
-
rootState: stateCtx.rootState,
|
|
71
|
-
}),
|
|
72
|
-
[stateCtx.currentDateTime, stateCtx.gridLabelIds, stateCtx.rootState],
|
|
73
|
-
);
|
|
74
|
-
|
|
75
46
|
return (
|
|
76
|
-
<
|
|
77
|
-
<
|
|
78
|
-
<
|
|
79
|
-
<
|
|
47
|
+
<WeeksViewStableContext.Provider value={stableCtx}>
|
|
48
|
+
<WeeksViewStateContext.Provider value={stateCtx}>
|
|
49
|
+
<ViewStableContext.Provider value={viewStableCtx}>
|
|
50
|
+
<ViewStateContext.Provider value={viewStateCtx}>
|
|
80
51
|
<ViewContext.Provider value={viewCtx}>
|
|
81
52
|
{children}
|
|
82
53
|
</ViewContext.Provider>
|
|
83
|
-
</
|
|
84
|
-
</
|
|
85
|
-
</
|
|
86
|
-
</
|
|
54
|
+
</ViewStateContext.Provider>
|
|
55
|
+
</ViewStableContext.Provider>
|
|
56
|
+
</WeeksViewStateContext.Provider>
|
|
57
|
+
</WeeksViewStableContext.Provider>
|
|
87
58
|
);
|
|
88
59
|
}
|
|
89
60
|
|