@klinking/colander 3.0.1-alpha.1 → 3.1.0-alpha.10
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 +2 -3
- package/dist/formats/date.d.cts +10 -14
- package/dist/formats/date.d.mts +10 -14
- package/dist/formats/date.mjs +2 -3
- package/dist/formats/object.cjs +2 -3
- package/dist/formats/object.d.cts +10 -14
- package/dist/formats/object.d.mts +10 -14
- package/dist/formats/object.mjs +2 -3
- package/dist/formats/plain-date-time.cjs +2 -3
- package/dist/formats/plain-date-time.d.cts +10 -14
- package/dist/formats/plain-date-time.d.mts +10 -14
- package/dist/formats/plain-date-time.mjs +2 -3
- package/dist/formats/plain-date.cjs +2 -3
- package/dist/formats/plain-date.d.cts +10 -14
- package/dist/formats/plain-date.d.mts +10 -14
- package/dist/formats/plain-date.mjs +2 -3
- package/dist/formats/plain-month-day.cjs +2 -3
- package/dist/formats/plain-month-day.d.cts +10 -14
- package/dist/formats/plain-month-day.d.mts +10 -14
- package/dist/formats/plain-month-day.mjs +2 -3
- package/dist/formats/plain-year-month.cjs +2 -3
- package/dist/formats/plain-year-month.d.cts +10 -14
- package/dist/formats/plain-year-month.d.mts +10 -14
- package/dist/formats/plain-year-month.mjs +2 -3
- package/dist/formats/zoned-date-time.cjs +2 -3
- package/dist/formats/zoned-date-time.d.cts +10 -14
- package/dist/formats/zoned-date-time.d.mts +10 -14
- package/dist/formats/zoned-date-time.mjs +2 -3
- package/dist/{weeks-view-context-CNAq2Hiu.d.cts → index-K1_zOIpa.d.mts} +198 -114
- package/dist/{weeks-view-context-CsP3PBiu.d.mts → index-bRoYX03d.d.cts} +198 -114
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -11
- package/dist/index.d.mts +2 -11
- package/dist/index.mjs +1 -2
- package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-3UTRiTUP.cjs} +1102 -952
- package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-BoHqK5fq.mjs} +1102 -951
- package/package.json +21 -18
- package/src/calendar-provider.test.tsx +88 -1
- package/src/calendar-provider.tsx +89 -73
- package/src/calendar-types.ts +2 -0
- package/src/compute-weeks-in-window.test.ts +1 -1
- package/src/day-cell-state.test.ts +214 -0
- package/src/day-cell-state.ts +244 -0
- package/src/day-cell-store.test.tsx +273 -0
- package/src/day-cell-store.tsx +204 -0
- package/src/day-cell.test.tsx +73 -90
- package/src/day-cell.tsx +106 -444
- package/src/drag-handle.tsx +2 -2
- package/src/formats/date.ts +2 -0
- package/src/formats/formats.test.ts +1 -1
- package/src/formats/object.ts +2 -0
- package/src/formats/plain-date-time.ts +2 -0
- package/src/formats/plain-date.ts +2 -0
- package/src/formats/plain-month-day.ts +2 -0
- package/src/formats/plain-year-month.ts +2 -0
- package/src/formats/zoned-date-time.ts +2 -0
- package/src/grid-header.tsx +3 -3
- package/src/grid.event-perf.test.tsx +7 -1
- package/src/grid.perf.test.tsx +7 -1
- package/src/grid.tsx +38 -77
- package/src/index.ts +2 -0
- package/src/keyboard.test.ts +1 -1
- package/src/month-separator.test.tsx +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.test.tsx +89 -1
- package/src/month-view.tsx +74 -217
- package/src/month-window.test.ts +207 -0
- package/src/month-window.ts +93 -0
- package/src/navigation.tsx +18 -76
- package/src/out-of-range.test.ts +34 -54
- package/src/out-of-range.ts +47 -123
- package/src/range-preview.test.ts +120 -0
- package/src/range-preview.tsx +3 -5
- package/src/resolve-first-week.test.ts +1 -1
- package/src/root-selection.ts +99 -97
- package/src/selected-range.tsx +3 -5
- package/src/store.ts +72 -0
- package/src/today.test.tsx +105 -0
- package/src/types.ts +24 -1
- package/src/utils.bench.ts +158 -106
- package/src/utils.test.ts +26 -1
- package/src/utils.ts +40 -9
- package/src/view-context.test.tsx +126 -0
- package/src/view-context.ts +45 -0
- package/src/week-number.test.tsx +76 -0
- package/src/week-number.tsx +48 -19
- package/src/weeks-grid.tsx +20 -10
- package/src/weeks-keyboard.test.ts +1 -1
- package/src/weeks-navigation.tsx +15 -31
- package/src/weeks-view-state.ts +67 -101
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-view.bounds.test.tsx +246 -0
- package/src/weeks-view.test.tsx +116 -14
- package/src/weeks-view.tsx +24 -52
- package/src/weeks-window.test.ts +131 -0
- package/src/weeks-window.ts +102 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { describe, expect, it } from "vite-plus/test";
|
|
3
|
+
import { createMonthWindowPolicy } from "./month-window";
|
|
4
|
+
import type { MonthWindowPolicyInput } from "./month-window";
|
|
5
|
+
|
|
6
|
+
const T = Temporal;
|
|
7
|
+
|
|
8
|
+
const policy = (overrides: Partial<MonthWindowPolicyInput> = {}) =>
|
|
9
|
+
createMonthWindowPolicy({
|
|
10
|
+
numberOfMonths: 1,
|
|
11
|
+
behavior: "stop",
|
|
12
|
+
min: undefined,
|
|
13
|
+
max: undefined,
|
|
14
|
+
T,
|
|
15
|
+
...overrides,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const date = (iso: string) => T.PlainDate.from(iso);
|
|
19
|
+
|
|
20
|
+
describe("MonthWindowPolicy.step", () => {
|
|
21
|
+
it.each([
|
|
22
|
+
{
|
|
23
|
+
description: "next across a year boundary",
|
|
24
|
+
start: { year: 2026, month: 12 },
|
|
25
|
+
direction: "next" as const,
|
|
26
|
+
expected: {
|
|
27
|
+
start: { year: 2027, month: 1 },
|
|
28
|
+
entering: { year: 2027, month: 1 },
|
|
29
|
+
firstDay: "2027-01-01",
|
|
30
|
+
blocked: false,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
description: "prev across a year boundary",
|
|
35
|
+
start: { year: 2026, month: 1 },
|
|
36
|
+
direction: "prev" as const,
|
|
37
|
+
expected: {
|
|
38
|
+
start: { year: 2025, month: 12 },
|
|
39
|
+
entering: { year: 2025, month: 12 },
|
|
40
|
+
firstDay: "2025-12-01",
|
|
41
|
+
blocked: false,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
description:
|
|
46
|
+
"next with two months visible enters the month after the window",
|
|
47
|
+
start: { year: 2026, month: 3 },
|
|
48
|
+
direction: "next" as const,
|
|
49
|
+
numberOfMonths: 2,
|
|
50
|
+
expected: {
|
|
51
|
+
start: { year: 2026, month: 4 },
|
|
52
|
+
entering: { year: 2026, month: 5 },
|
|
53
|
+
firstDay: "2026-04-01",
|
|
54
|
+
blocked: false,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
])("$description", ({ start, direction, numberOfMonths, expected }) => {
|
|
58
|
+
const step = policy({ numberOfMonths: numberOfMonths ?? 1 }).step(
|
|
59
|
+
start,
|
|
60
|
+
direction,
|
|
61
|
+
);
|
|
62
|
+
expect({ ...step, firstDay: step.firstDay.toString() }).toEqual(expected);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it.each([
|
|
66
|
+
{
|
|
67
|
+
description: "stop: prev into the min month is allowed (min mid-month)",
|
|
68
|
+
direction: "prev" as const,
|
|
69
|
+
start: { year: 2026, month: 4 },
|
|
70
|
+
min: "2026-03-20",
|
|
71
|
+
expected: false,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
description: "stop: prev before the min month is blocked",
|
|
75
|
+
direction: "prev" as const,
|
|
76
|
+
start: { year: 2026, month: 3 },
|
|
77
|
+
min: "2026-03-20",
|
|
78
|
+
expected: true,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
description: "stop: next into the max month is allowed",
|
|
82
|
+
direction: "next" as const,
|
|
83
|
+
start: { year: 2026, month: 2 },
|
|
84
|
+
max: "2026-03-05",
|
|
85
|
+
expected: false,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
description: "stop: next past the max month is blocked",
|
|
89
|
+
direction: "next" as const,
|
|
90
|
+
start: { year: 2026, month: 3 },
|
|
91
|
+
max: "2026-03-05",
|
|
92
|
+
expected: true,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
description: "stop: the window's last month is what must stay within max",
|
|
96
|
+
direction: "next" as const,
|
|
97
|
+
start: { year: 2026, month: 2 },
|
|
98
|
+
numberOfMonths: 2,
|
|
99
|
+
max: "2026-03-31",
|
|
100
|
+
expected: true,
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
description: "stop: min only never blocks next",
|
|
104
|
+
direction: "next" as const,
|
|
105
|
+
start: { year: 2030, month: 1 },
|
|
106
|
+
min: "2026-03-20",
|
|
107
|
+
expected: false,
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
description: "stop: max only never blocks prev",
|
|
111
|
+
direction: "prev" as const,
|
|
112
|
+
start: { year: 2020, month: 1 },
|
|
113
|
+
max: "2026-03-20",
|
|
114
|
+
expected: false,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
description: "unbounded: never blocks past min",
|
|
118
|
+
direction: "prev" as const,
|
|
119
|
+
behavior: "unbounded" as const,
|
|
120
|
+
start: { year: 2026, month: 3 },
|
|
121
|
+
min: "2026-03-20",
|
|
122
|
+
expected: false,
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
description: "unbounded: never blocks past max",
|
|
126
|
+
direction: "next" as const,
|
|
127
|
+
behavior: "unbounded" as const,
|
|
128
|
+
start: { year: 2026, month: 3 },
|
|
129
|
+
max: "2026-03-05",
|
|
130
|
+
expected: false,
|
|
131
|
+
},
|
|
132
|
+
])(
|
|
133
|
+
"$description",
|
|
134
|
+
({ direction, start, min, max, numberOfMonths, behavior, expected }) => {
|
|
135
|
+
const blocked = policy({
|
|
136
|
+
numberOfMonths: numberOfMonths ?? 1,
|
|
137
|
+
behavior: behavior ?? "stop",
|
|
138
|
+
min: min ? date(min) : undefined,
|
|
139
|
+
max: max ? date(max) : undefined,
|
|
140
|
+
}).step(start, direction).blocked;
|
|
141
|
+
expect(blocked).toBe(expected);
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("MonthWindowPolicy.isVisible", () => {
|
|
147
|
+
it.each([
|
|
148
|
+
{
|
|
149
|
+
description: "the first month",
|
|
150
|
+
target: { year: 2026, month: 3 },
|
|
151
|
+
expected: true,
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
description: "the last month",
|
|
155
|
+
target: { year: 2026, month: 5 },
|
|
156
|
+
expected: true,
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
description: "the month before the window",
|
|
160
|
+
target: { year: 2026, month: 2 },
|
|
161
|
+
expected: false,
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
description: "the month after the window",
|
|
165
|
+
target: { year: 2026, month: 6 },
|
|
166
|
+
expected: false,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
description: "the same month number in another year",
|
|
170
|
+
target: { year: 2027, month: 3 },
|
|
171
|
+
expected: false,
|
|
172
|
+
},
|
|
173
|
+
])("$description", ({ target, expected }) => {
|
|
174
|
+
expect(
|
|
175
|
+
policy({ numberOfMonths: 3 }).isVisible({ year: 2026, month: 3 }, target),
|
|
176
|
+
).toBe(expected);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe("MonthWindowPolicy.reveal", () => {
|
|
181
|
+
it.each([
|
|
182
|
+
{
|
|
183
|
+
description: "keeps the window when the target is visible",
|
|
184
|
+
target: { year: 2026, month: 4 },
|
|
185
|
+
expected: { year: 2026, month: 3 },
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
description: "target before the window becomes the first month",
|
|
189
|
+
target: { year: 2026, month: 1 },
|
|
190
|
+
expected: { year: 2026, month: 1 },
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
description: "target after the window becomes the last month",
|
|
194
|
+
target: { year: 2026, month: 8 },
|
|
195
|
+
expected: { year: 2026, month: 7 },
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
description: "target after the window across a year boundary",
|
|
199
|
+
target: { year: 2027, month: 2 },
|
|
200
|
+
expected: { year: 2027, month: 1 },
|
|
201
|
+
},
|
|
202
|
+
])("$description", ({ target, expected }) => {
|
|
203
|
+
expect(
|
|
204
|
+
policy({ numberOfMonths: 2 }).reveal({ year: 2026, month: 3 }, target),
|
|
205
|
+
).toEqual(expected);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { MonthOutOfRangeBehavior } from "./out-of-range";
|
|
3
|
+
import type { TemporalNamespace } from "./types";
|
|
4
|
+
import { computeAdjacentMonth, shiftWindowToMonth } from "./utils";
|
|
5
|
+
|
|
6
|
+
/** An ISO year + month. */
|
|
7
|
+
export interface MonthRef {
|
|
8
|
+
year: number;
|
|
9
|
+
month: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** One step of the visible month window. */
|
|
13
|
+
export interface MonthStep {
|
|
14
|
+
/** The window's first month after the step. */
|
|
15
|
+
start: MonthRef;
|
|
16
|
+
/** The month that enters view (before the window for `prev`, after it for `next`). */
|
|
17
|
+
entering: MonthRef;
|
|
18
|
+
/** First day of `start`, the focus target when focus isn't already in `start`. */
|
|
19
|
+
firstDay: Temporal.PlainDate;
|
|
20
|
+
/** Whether `stop` forbids the step because `entering` lies beyond `min`/`max`. */
|
|
21
|
+
blocked: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The one seam for moving the MonthView window. */
|
|
25
|
+
export interface MonthWindowPolicy {
|
|
26
|
+
/** Steps the window one month towards `direction`. */
|
|
27
|
+
step(start: MonthRef, direction: "prev" | "next"): MonthStep;
|
|
28
|
+
/** Whether `target` is inside the window starting at `start`. */
|
|
29
|
+
isVisible(start: MonthRef, target: MonthRef): boolean;
|
|
30
|
+
/** The window start that reveals `target` with the smallest shift. */
|
|
31
|
+
reveal(start: MonthRef, target: MonthRef): MonthRef;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MonthWindowPolicyInput {
|
|
35
|
+
numberOfMonths: number;
|
|
36
|
+
behavior: MonthOutOfRangeBehavior;
|
|
37
|
+
min: Temporal.PlainDate | undefined;
|
|
38
|
+
max: Temporal.PlainDate | undefined;
|
|
39
|
+
T: TemporalNamespace;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const monthIndex = (m: MonthRef): number => m.year * 12 + (m.month - 1);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Builds the policy for a MonthView window of `numberOfMonths` months.
|
|
46
|
+
*
|
|
47
|
+
* `unbounded` never blocks a step: `min`/`max` only gate which days are
|
|
48
|
+
* selectable, not which months can be viewed. Any other behavior is `stop`,
|
|
49
|
+
* which blocks a step whose entering month lies beyond the bound's month.
|
|
50
|
+
*/
|
|
51
|
+
export function createMonthWindowPolicy(
|
|
52
|
+
input: MonthWindowPolicyInput,
|
|
53
|
+
): MonthWindowPolicy {
|
|
54
|
+
const { numberOfMonths, behavior, min, max, T } = input;
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
step(start, direction) {
|
|
58
|
+
const adjacent = computeAdjacentMonth(start, direction, T);
|
|
59
|
+
const newStart = { year: adjacent.year, month: adjacent.month };
|
|
60
|
+
const entering =
|
|
61
|
+
direction === "prev" ? newStart : monthAt(newStart, numberOfMonths - 1);
|
|
62
|
+
|
|
63
|
+
let blocked = false;
|
|
64
|
+
if (behavior !== "unbounded") {
|
|
65
|
+
const bound = direction === "prev" ? min : max;
|
|
66
|
+
if (bound) {
|
|
67
|
+
const diff = monthIndex(entering) - monthIndex(bound);
|
|
68
|
+
blocked = direction === "prev" ? diff < 0 : diff > 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
start: newStart,
|
|
73
|
+
entering,
|
|
74
|
+
firstDay: adjacent.firstDay,
|
|
75
|
+
blocked,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
isVisible(start, target) {
|
|
80
|
+
const offset = monthIndex(target) - monthIndex(start);
|
|
81
|
+
return offset >= 0 && offset < numberOfMonths;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
reveal(start, target) {
|
|
85
|
+
return shiftWindowToMonth(start, target, numberOfMonths);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function monthAt(start: MonthRef, offset: number): MonthRef {
|
|
91
|
+
const index = monthIndex(start) + offset;
|
|
92
|
+
return { year: Math.floor(index / 12), month: (index % 12) + 1 };
|
|
93
|
+
}
|
package/src/navigation.tsx
CHANGED
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
NavButtonState,
|
|
17
17
|
} from "./types";
|
|
18
18
|
import { selectedToZdt, zdtToNativeDate } from "./utils";
|
|
19
|
+
import { useViewState } from "./view-context";
|
|
19
20
|
|
|
20
21
|
const dateStringStateAttributesMapping = {
|
|
21
22
|
root: () => null,
|
|
@@ -44,7 +45,7 @@ function DateStringFn(
|
|
|
44
45
|
const { render, locales, options, ...otherProps } = props;
|
|
45
46
|
const { locale, temporal: T, timeZone } = useCalendarStable();
|
|
46
47
|
const { selected } = useCalendarState();
|
|
47
|
-
const { currentDateTime, rootState } =
|
|
48
|
+
const { currentDateTime, rootState } = useViewState();
|
|
48
49
|
|
|
49
50
|
const selectedZdt = selectedToZdt(selected, timeZone, T);
|
|
50
51
|
const displayDate = selectedZdt
|
|
@@ -97,7 +98,7 @@ function TimeStringFn(
|
|
|
97
98
|
const { render, locales, options, ...otherProps } = props;
|
|
98
99
|
const { locale, temporal: T, timeZone } = useCalendarStable();
|
|
99
100
|
const { selected } = useCalendarState();
|
|
100
|
-
const { rootState } =
|
|
101
|
+
const { rootState } = useViewState();
|
|
101
102
|
|
|
102
103
|
const selZdt = selectedToZdt(selected, timeZone, T);
|
|
103
104
|
const displayDate = selZdt
|
|
@@ -237,84 +238,25 @@ export const MonthYearString = forwardRef(MonthYearStringFn) as (
|
|
|
237
238
|
) => React.ReactElement | null;
|
|
238
239
|
|
|
239
240
|
function useNavButton(direction: "prev" | "next") {
|
|
240
|
-
const {
|
|
241
|
-
disabled: globalDisabled,
|
|
242
|
-
minValue,
|
|
243
|
-
maxValue,
|
|
244
|
-
temporal: T,
|
|
245
|
-
} = useCalendarStable();
|
|
246
|
-
const monthViewStable = useMonthViewStable();
|
|
247
|
-
const monthViewState = useMonthViewState();
|
|
241
|
+
const { disabled: globalDisabled, temporal: T } = useCalendarStable();
|
|
248
242
|
const {
|
|
249
243
|
goNextMonth: goToNextMonth,
|
|
250
244
|
goPrevMonth: goToPrevMonth,
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
} =
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
direction === "next"
|
|
264
|
-
? (allMonths[numberOfMonths - 1] ?? currentDateTime)
|
|
265
|
-
: currentDateTime;
|
|
266
|
-
|
|
267
|
-
const destMonth =
|
|
268
|
-
direction === "prev"
|
|
269
|
-
? refMonth.month === 1
|
|
270
|
-
? 12
|
|
271
|
-
: refMonth.month - 1
|
|
272
|
-
: refMonth.month === 12
|
|
273
|
-
? 1
|
|
274
|
-
: refMonth.month + 1;
|
|
275
|
-
|
|
276
|
-
const destYear =
|
|
277
|
-
direction === "prev"
|
|
278
|
-
? refMonth.month === 1
|
|
279
|
-
? refMonth.year - 1
|
|
280
|
-
: refMonth.year
|
|
281
|
-
: refMonth.month === 12
|
|
282
|
-
? refMonth.year + 1
|
|
283
|
-
: refMonth.year;
|
|
284
|
-
|
|
285
|
-
const boundValue = direction === "prev" ? minValue : maxValue;
|
|
286
|
-
|
|
287
|
-
const isDisabled = useMemo(() => {
|
|
288
|
-
if (globalDisabled) return true;
|
|
289
|
-
// "unbounded" never disables on min/max — the bounds only gate which days
|
|
290
|
-
// are selectable, not which months can be viewed. "stop" halts navigation
|
|
291
|
-
// once the destination month crosses the boundary.
|
|
292
|
-
if (outOfRangeBehavior === "unbounded") return false;
|
|
293
|
-
if (!boundValue) return false;
|
|
294
|
-
if (direction === "prev") {
|
|
295
|
-
return (
|
|
296
|
-
destYear < boundValue.year ||
|
|
297
|
-
(destYear === boundValue.year && destMonth < boundValue.month)
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
return (
|
|
301
|
-
destYear > boundValue.year ||
|
|
302
|
-
(destYear === boundValue.year && destMonth > boundValue.month)
|
|
303
|
-
);
|
|
304
|
-
}, [
|
|
305
|
-
globalDisabled,
|
|
306
|
-
outOfRangeBehavior,
|
|
307
|
-
destYear,
|
|
308
|
-
destMonth,
|
|
309
|
-
boundValue,
|
|
310
|
-
direction,
|
|
311
|
-
]);
|
|
312
|
-
|
|
313
|
-
// ISO — `destYear`/`destMonth` are ISO numbers; the locale calendar only
|
|
314
|
-
// 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.
|
|
315
257
|
const target = useMemo(
|
|
316
|
-
() => T.PlainYearMonth.from(
|
|
317
|
-
[
|
|
258
|
+
() => T.PlainYearMonth.from(step.entering),
|
|
259
|
+
[step.entering, T],
|
|
318
260
|
);
|
|
319
261
|
|
|
320
262
|
const state = useMemo<NavButtonState>(
|
package/src/out-of-range.test.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
-
import { describe, it, expect } from "
|
|
3
|
-
import {
|
|
4
|
-
applyOutOfRange,
|
|
5
|
-
canShift,
|
|
6
|
-
type OutOfRangeBehavior,
|
|
7
|
-
} from "./out-of-range";
|
|
2
|
+
import { describe, it, expect } from "vite-plus/test";
|
|
3
|
+
import { applyOutOfRange, type OutOfRangeBehavior } from "./out-of-range";
|
|
8
4
|
|
|
9
5
|
const T = Temporal;
|
|
10
6
|
const pd = (s: string) => T.PlainDate.from(s);
|
|
@@ -19,7 +15,7 @@ describe("applyOutOfRange", () => {
|
|
|
19
15
|
targetFirstWeek: pd("2026-02-01"),
|
|
20
16
|
weekCount: 8,
|
|
21
17
|
behavior: "unbounded" as OutOfRangeBehavior,
|
|
22
|
-
useMinMax:
|
|
18
|
+
useMinMax: "both" as const,
|
|
23
19
|
expected: { firstWeek: "2026-02-01", weekCount: 8 },
|
|
24
20
|
},
|
|
25
21
|
{
|
|
@@ -27,7 +23,7 @@ describe("applyOutOfRange", () => {
|
|
|
27
23
|
targetFirstWeek: pd("2025-12-07"),
|
|
28
24
|
weekCount: 8,
|
|
29
25
|
behavior: "stop" as OutOfRangeBehavior,
|
|
30
|
-
useMinMax:
|
|
26
|
+
useMinMax: "both" as const,
|
|
31
27
|
expected: { firstWeek: "2025-12-07", weekCount: 8 },
|
|
32
28
|
},
|
|
33
29
|
{
|
|
@@ -35,7 +31,7 @@ describe("applyOutOfRange", () => {
|
|
|
35
31
|
targetFirstWeek: pd("2026-01-11"),
|
|
36
32
|
weekCount: 8,
|
|
37
33
|
behavior: "snap" as OutOfRangeBehavior,
|
|
38
|
-
useMinMax:
|
|
34
|
+
useMinMax: "both" as const,
|
|
39
35
|
expected: {
|
|
40
36
|
firstWeek: "2025-11-16",
|
|
41
37
|
weekCount: 8,
|
|
@@ -46,7 +42,7 @@ describe("applyOutOfRange", () => {
|
|
|
46
42
|
targetFirstWeek: pd("2026-01-11"),
|
|
47
43
|
weekCount: 8,
|
|
48
44
|
behavior: "snap-shrink" as OutOfRangeBehavior,
|
|
49
|
-
useMinMax:
|
|
45
|
+
useMinMax: "both" as const,
|
|
50
46
|
expected: {
|
|
51
47
|
firstWeek: "2025-11-30",
|
|
52
48
|
weekCount: 6,
|
|
@@ -57,18 +53,42 @@ describe("applyOutOfRange", () => {
|
|
|
57
53
|
targetFirstWeek: pd("2025-12-07"),
|
|
58
54
|
weekCount: 8,
|
|
59
55
|
behavior: "stop-shrink" as OutOfRangeBehavior,
|
|
60
|
-
useMinMax:
|
|
56
|
+
useMinMax: "both" as const,
|
|
61
57
|
expected: {
|
|
62
58
|
firstWeek: "2025-12-07",
|
|
63
59
|
weekCount: 5,
|
|
64
60
|
},
|
|
65
61
|
},
|
|
62
|
+
{
|
|
63
|
+
description: "max only, snap — pulls back when target overshoots max",
|
|
64
|
+
targetFirstWeek: pd("2026-01-11"),
|
|
65
|
+
weekCount: 8,
|
|
66
|
+
behavior: "snap" as OutOfRangeBehavior,
|
|
67
|
+
useMinMax: "max" as const,
|
|
68
|
+
expected: { firstWeek: "2025-11-16", weekCount: 8 },
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
description: "min only, snap — pulls forward when target undershoots min",
|
|
72
|
+
targetFirstWeek: pd("2025-10-01"),
|
|
73
|
+
weekCount: 8,
|
|
74
|
+
behavior: "snap" as OutOfRangeBehavior,
|
|
75
|
+
useMinMax: "min" as const,
|
|
76
|
+
expected: { firstWeek: "2025-11-30", weekCount: 8 },
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
description: "min only, stop-shrink — trims leading disabled rows",
|
|
80
|
+
targetFirstWeek: pd("2025-11-16"),
|
|
81
|
+
weekCount: 8,
|
|
82
|
+
behavior: "stop-shrink" as OutOfRangeBehavior,
|
|
83
|
+
useMinMax: "min" as const,
|
|
84
|
+
expected: { firstWeek: "2025-11-30", weekCount: 6 },
|
|
85
|
+
},
|
|
66
86
|
{
|
|
67
87
|
description: "no min/max — all modes behave as unbounded",
|
|
68
88
|
targetFirstWeek: pd("2026-06-01"),
|
|
69
89
|
weekCount: 8,
|
|
70
90
|
behavior: "snap" as OutOfRangeBehavior,
|
|
71
|
-
useMinMax:
|
|
91
|
+
useMinMax: "none" as const,
|
|
72
92
|
expected: { firstWeek: "2026-06-01", weekCount: 8 },
|
|
73
93
|
},
|
|
74
94
|
])(
|
|
@@ -78,7 +98,8 @@ describe("applyOutOfRange", () => {
|
|
|
78
98
|
targetFirstWeek,
|
|
79
99
|
weekCount,
|
|
80
100
|
behavior,
|
|
81
|
-
...(useMinMax && { min
|
|
101
|
+
...((useMinMax === "both" || useMinMax === "min") && { min }),
|
|
102
|
+
...((useMinMax === "both" || useMinMax === "max") && { max }),
|
|
82
103
|
weekStartDay: 0,
|
|
83
104
|
T,
|
|
84
105
|
});
|
|
@@ -87,44 +108,3 @@ describe("applyOutOfRange", () => {
|
|
|
87
108
|
},
|
|
88
109
|
);
|
|
89
110
|
});
|
|
90
|
-
|
|
91
|
-
describe("canShift", () => {
|
|
92
|
-
const min = pd("2025-12-01");
|
|
93
|
-
const max = pd("2026-01-10");
|
|
94
|
-
|
|
95
|
-
it.each([
|
|
96
|
-
{
|
|
97
|
-
description: "unbounded — always true",
|
|
98
|
-
behavior: "unbounded" as OutOfRangeBehavior,
|
|
99
|
-
currentFirstWeek: pd("2026-06-01"),
|
|
100
|
-
direction: 1 as const,
|
|
101
|
-
expected: true,
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
description: "stop — false when target has no valid weeks",
|
|
105
|
-
behavior: "stop" as OutOfRangeBehavior,
|
|
106
|
-
currentFirstWeek: pd("2025-12-28"),
|
|
107
|
-
direction: 1 as const,
|
|
108
|
-
expected: false,
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
description: "snap — false when already at clamp position",
|
|
112
|
-
behavior: "snap" as OutOfRangeBehavior,
|
|
113
|
-
currentFirstWeek: pd("2025-11-16"),
|
|
114
|
-
direction: 1 as const,
|
|
115
|
-
expected: false,
|
|
116
|
-
},
|
|
117
|
-
])("$description", ({ behavior, currentFirstWeek, direction, expected }) => {
|
|
118
|
-
const result = canShift({
|
|
119
|
-
currentFirstWeek,
|
|
120
|
-
weekCount: 8,
|
|
121
|
-
direction,
|
|
122
|
-
behavior,
|
|
123
|
-
min,
|
|
124
|
-
max,
|
|
125
|
-
weekStartDay: 0,
|
|
126
|
-
T,
|
|
127
|
-
});
|
|
128
|
-
expect(result).toBe(expected);
|
|
129
|
-
});
|
|
130
|
-
});
|