@klinking/colander 3.1.0-alpha.5 → 3.1.0-alpha.7
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-CEV8w_69.mjs → weeks-navigation-BSKwFvdP.mjs} +367 -413
- package/dist/{weeks-navigation-q3beZn2n.cjs → weeks-navigation-CPevz3Xg.cjs} +367 -413
- package/dist/{weeks-view-context-BLZZm1fq.d.mts → weeks-view-context-BTojXkys.d.cts} +51 -10
- package/dist/{weeks-view-context-CkTcdZpj.d.cts → weeks-view-context-CFXV8orh.d.mts} +51 -10
- package/package.json +1 -1
- package/src/calendar-provider.tsx +10 -55
- package/src/day-cell-state.test.ts +214 -0
- package/src/day-cell-state.ts +314 -0
- package/src/day-cell.tsx +29 -383
- package/src/grid.tsx +1 -1
- package/src/out-of-range.test.ts +33 -53
- package/src/out-of-range.ts +47 -123
- package/src/range-preview.test.ts +120 -0
- package/src/root-selection.ts +99 -97
- package/src/weeks-navigation.tsx +12 -28
- package/src/weeks-view-state.ts +36 -84
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-view.bounds.test.tsx +246 -0
- package/src/weeks-window.test.ts +131 -0
- package/src/weeks-window.ts +102 -0
package/src/out-of-range.ts
CHANGED
|
@@ -12,7 +12,8 @@ import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
|
12
12
|
* - `"unbounded"` — Navigation is never restricted by `min`/`max`; any range of
|
|
13
13
|
* weeks can be viewed. Out-of-range days simply render disabled. (Default.)
|
|
14
14
|
* - `"stop"` — Navigation halts at the boundary: the Prev/Next button is
|
|
15
|
-
* disabled
|
|
15
|
+
* disabled, and `goNext`/`goPrev`/`scrollToWeek` and keyboard paging do
|
|
16
|
+
* nothing, once the next step would show a window containing no in-range day.
|
|
16
17
|
* - `"stop-shrink"` — Like `"stop"`, but near the edge the window **shrinks** to
|
|
17
18
|
* fewer week rows instead of showing rows that are entirely out of range.
|
|
18
19
|
* - `"snap"` — A jump that would overshoot **snaps** the window so its edge
|
|
@@ -20,6 +21,8 @@ import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
|
20
21
|
* - `"snap-shrink"` — Snap to the boundary, then **shrink** the window to just
|
|
21
22
|
* the in-range weeks.
|
|
22
23
|
*
|
|
24
|
+
* A single bound (`min` only or `max` only) is honored; the missing side is unlimited.
|
|
25
|
+
*
|
|
23
26
|
* `"snap"` and `"snap-shrink"` are identical **unless the selectable range spans
|
|
24
27
|
* fewer weeks than `weekCount`** — i.e. the window is taller than the range.
|
|
25
28
|
* Snapping can only pin *one* edge to a bound; if the range is narrower than the
|
|
@@ -63,19 +66,33 @@ export type MonthOutOfRangeBehavior = "unbounded" | "stop";
|
|
|
63
66
|
/** Returns true if any day in the 7-day span starting at weekStart falls within [min, max]. */
|
|
64
67
|
function weekHasValidDay(
|
|
65
68
|
weekStart: Temporal.PlainDate,
|
|
66
|
-
min: Temporal.PlainDate,
|
|
67
|
-
max: Temporal.PlainDate,
|
|
69
|
+
min: Temporal.PlainDate | undefined,
|
|
70
|
+
max: Temporal.PlainDate | undefined,
|
|
68
71
|
T: TemporalNamespace,
|
|
69
72
|
): boolean {
|
|
70
73
|
// Week spans [weekStart, weekStart+6]. Overlaps with [min, max] iff
|
|
71
|
-
// weekStart <= max AND weekStart+6 >= min.
|
|
74
|
+
// weekStart <= max AND weekStart+6 >= min. A missing bound is unlimited.
|
|
72
75
|
const weekEnd = weekStart.add({ days: 6 });
|
|
73
76
|
return (
|
|
74
|
-
T.PlainDate.compare(weekStart, max) <= 0 &&
|
|
75
|
-
T.PlainDate.compare(weekEnd, min) >= 0
|
|
77
|
+
(!max || T.PlainDate.compare(weekStart, max) <= 0) &&
|
|
78
|
+
(!min || T.PlainDate.compare(weekEnd, min) >= 0)
|
|
76
79
|
);
|
|
77
80
|
}
|
|
78
81
|
|
|
82
|
+
/** Returns true if any of the `weekCount` weeks starting at firstWeek has a day in [min, max]. */
|
|
83
|
+
export function windowHasValidWeek(
|
|
84
|
+
firstWeek: Temporal.PlainDate,
|
|
85
|
+
weekCount: number,
|
|
86
|
+
min: Temporal.PlainDate | undefined,
|
|
87
|
+
max: Temporal.PlainDate | undefined,
|
|
88
|
+
T: TemporalNamespace,
|
|
89
|
+
): boolean {
|
|
90
|
+
for (let i = 0; i < weekCount; i++) {
|
|
91
|
+
if (weekHasValidDay(firstWeek.add({ weeks: i }), min, max, T)) return true;
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
79
96
|
/**
|
|
80
97
|
* Returns the start of the week (per weekStartDay) that contains the given date.
|
|
81
98
|
* Uses dayOfWeek % 7 → Sun=0…Sat=6 convention.
|
|
@@ -83,31 +100,12 @@ function weekHasValidDay(
|
|
|
83
100
|
function weekStartContaining(
|
|
84
101
|
date: Temporal.PlainDate,
|
|
85
102
|
weekStartDay: WeekStartDay,
|
|
86
|
-
_T: TemporalNamespace,
|
|
87
103
|
): Temporal.PlainDate {
|
|
88
104
|
const dow = date.dayOfWeek % 7; // Sun=0…Sat=6
|
|
89
105
|
const daysFromStart = (dow - weekStartDay + 7) % 7;
|
|
90
106
|
return date.subtract({ days: daysFromStart });
|
|
91
107
|
}
|
|
92
108
|
|
|
93
|
-
/** Find the week start (per weekStartDay) whose 7-day span contains min. */
|
|
94
|
-
function findFirstValidWeekStart(
|
|
95
|
-
min: Temporal.PlainDate,
|
|
96
|
-
weekStartDay: WeekStartDay,
|
|
97
|
-
T: TemporalNamespace,
|
|
98
|
-
): Temporal.PlainDate {
|
|
99
|
-
return weekStartContaining(min, weekStartDay, T);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Find the week start (per weekStartDay) whose 7-day span contains max. */
|
|
103
|
-
function findLastValidWeekStart(
|
|
104
|
-
max: Temporal.PlainDate,
|
|
105
|
-
weekStartDay: WeekStartDay,
|
|
106
|
-
T: TemporalNamespace,
|
|
107
|
-
): Temporal.PlainDate {
|
|
108
|
-
return weekStartContaining(max, weekStartDay, T);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
109
|
/**
|
|
112
110
|
* Trim leading and trailing fully-disabled weeks from a window.
|
|
113
111
|
* Returns new { firstWeek, weekCount }.
|
|
@@ -115,8 +113,8 @@ function findLastValidWeekStart(
|
|
|
115
113
|
function shrinkWindow(
|
|
116
114
|
firstWeek: Temporal.PlainDate,
|
|
117
115
|
weekCount: number,
|
|
118
|
-
min: Temporal.PlainDate,
|
|
119
|
-
max: Temporal.PlainDate,
|
|
116
|
+
min: Temporal.PlainDate | undefined,
|
|
117
|
+
max: Temporal.PlainDate | undefined,
|
|
120
118
|
T: TemporalNamespace,
|
|
121
119
|
): { firstWeek: Temporal.PlainDate; weekCount: number } {
|
|
122
120
|
let start = 0;
|
|
@@ -174,13 +172,14 @@ export function applyOutOfRange({
|
|
|
174
172
|
weekStartDay,
|
|
175
173
|
T,
|
|
176
174
|
}: ApplyOutOfRangeInput): { firstWeek: Temporal.PlainDate; weekCount: number } {
|
|
177
|
-
// Without bounds, all modes behave as unbounded
|
|
178
|
-
|
|
175
|
+
// Without bounds, all modes behave as unbounded. A single bound is honored;
|
|
176
|
+
// the missing side is unlimited.
|
|
177
|
+
if ((!min && !max) || behavior === "unbounded") {
|
|
179
178
|
return { firstWeek: targetFirstWeek, weekCount };
|
|
180
179
|
}
|
|
181
180
|
|
|
182
181
|
if (behavior === "stop") {
|
|
183
|
-
//
|
|
182
|
+
// Whether the move is allowed is decided by the caller (see WeekWindowPolicy)
|
|
184
183
|
return { firstWeek: targetFirstWeek, weekCount };
|
|
185
184
|
}
|
|
186
185
|
|
|
@@ -188,100 +187,25 @@ export function applyOutOfRange({
|
|
|
188
187
|
return shrinkWindow(targetFirstWeek, weekCount, min, max, T);
|
|
189
188
|
}
|
|
190
189
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (T.PlainDate.compare(targetLastWeek, lastValid) <= 0) {
|
|
207
|
-
// Target window fits within max — also check if it goes before min
|
|
208
|
-
const firstValid = findFirstValidWeekStart(min, weekStartDay, T);
|
|
209
|
-
if (T.PlainDate.compare(targetFirstWeek, firstValid) >= 0) {
|
|
210
|
-
resolvedFirst = targetFirstWeek;
|
|
211
|
-
} else {
|
|
212
|
-
// Overshoots min: snap forward so window starts at firstValid
|
|
213
|
-
resolvedFirst = firstValid;
|
|
214
|
-
}
|
|
215
|
-
} else {
|
|
216
|
-
// Overshoots max: snap back
|
|
217
|
-
resolvedFirst = snappedFirst;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (behavior === "snap-shrink") {
|
|
221
|
-
return shrinkWindow(resolvedFirst, weekCount, min, max, T);
|
|
222
|
-
}
|
|
223
|
-
return { firstWeek: resolvedFirst, weekCount };
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return { firstWeek: targetFirstWeek, weekCount };
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export interface CanShiftInput {
|
|
230
|
-
currentFirstWeek: Temporal.PlainDate;
|
|
231
|
-
weekCount: number;
|
|
232
|
-
direction: 1 | -1;
|
|
233
|
-
shiftBy?: number;
|
|
234
|
-
behavior: OutOfRangeBehavior;
|
|
235
|
-
min?: Temporal.PlainDate;
|
|
236
|
-
max?: Temporal.PlainDate;
|
|
237
|
-
weekStartDay: WeekStartDay;
|
|
238
|
-
T: TemporalNamespace;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Returns whether the navigation button for the given direction should be enabled.
|
|
243
|
-
*/
|
|
244
|
-
export function canShift({
|
|
245
|
-
currentFirstWeek,
|
|
246
|
-
weekCount,
|
|
247
|
-
direction,
|
|
248
|
-
shiftBy,
|
|
249
|
-
behavior,
|
|
250
|
-
min,
|
|
251
|
-
max,
|
|
252
|
-
weekStartDay,
|
|
253
|
-
T,
|
|
254
|
-
}: CanShiftInput): boolean {
|
|
255
|
-
if (behavior === "unbounded" || !min || !max) {
|
|
256
|
-
return true;
|
|
190
|
+
// snap | snap-shrink
|
|
191
|
+
const targetLastWeek = targetFirstWeek.add({ weeks: weekCount - 1 });
|
|
192
|
+
const lastValid = max && weekStartContaining(max, weekStartDay);
|
|
193
|
+
const firstValid = min && weekStartContaining(min, weekStartDay);
|
|
194
|
+
|
|
195
|
+
let resolvedFirst = targetFirstWeek;
|
|
196
|
+
if (lastValid && T.PlainDate.compare(targetLastWeek, lastValid) > 0) {
|
|
197
|
+
// Overshoots max: snap back so the window ends at the last in-range week
|
|
198
|
+
resolvedFirst = lastValid.subtract({ weeks: weekCount - 1 });
|
|
199
|
+
} else if (
|
|
200
|
+
firstValid &&
|
|
201
|
+
T.PlainDate.compare(targetFirstWeek, firstValid) < 0
|
|
202
|
+
) {
|
|
203
|
+
// Overshoots min: snap forward so the window starts at the first in-range week
|
|
204
|
+
resolvedFirst = firstValid;
|
|
257
205
|
}
|
|
258
206
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
if (behavior === "stop" || behavior === "stop-shrink") {
|
|
263
|
-
// Check if any week in the target window has valid days
|
|
264
|
-
for (let i = 0; i < weekCount; i++) {
|
|
265
|
-
const ws = targetFirstWeek.add({ days: i * 7 });
|
|
266
|
-
if (weekHasValidDay(ws, min, max, T)) return true;
|
|
267
|
-
}
|
|
268
|
-
return false;
|
|
207
|
+
if (behavior === "snap-shrink") {
|
|
208
|
+
return shrinkWindow(resolvedFirst, weekCount, min, max, T);
|
|
269
209
|
}
|
|
270
|
-
|
|
271
|
-
if (behavior === "snap" || behavior === "snap-shrink") {
|
|
272
|
-
// Disabled when the current position is already the snapped position
|
|
273
|
-
// (i.e. applying the out-of-range behavior to the target produces the same firstWeek)
|
|
274
|
-
const applied = applyOutOfRange({
|
|
275
|
-
targetFirstWeek,
|
|
276
|
-
weekCount,
|
|
277
|
-
behavior,
|
|
278
|
-
min,
|
|
279
|
-
max,
|
|
280
|
-
weekStartDay,
|
|
281
|
-
T,
|
|
282
|
-
});
|
|
283
|
-
return T.PlainDate.compare(applied.firstWeek, currentFirstWeek) !== 0;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return true;
|
|
210
|
+
return { firstWeek: resolvedFirst, weekCount };
|
|
287
211
|
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
computePreviewRange,
|
|
5
|
+
computeRangePreview,
|
|
6
|
+
decodeRange,
|
|
7
|
+
} from "./root-selection";
|
|
8
|
+
|
|
9
|
+
const T = Temporal;
|
|
10
|
+
const d = (iso: string) => T.PlainDate.from(iso);
|
|
11
|
+
const iso = (r: ReturnType<typeof computeRangePreview>) =>
|
|
12
|
+
r && [r[0].toString(), r[1].toString()];
|
|
13
|
+
|
|
14
|
+
describe("decodeRange", () => {
|
|
15
|
+
const toPlain = (raw: string) => d(raw);
|
|
16
|
+
|
|
17
|
+
it.each([
|
|
18
|
+
{
|
|
19
|
+
description: "both ends",
|
|
20
|
+
range: { start: "2026-03-10", end: "2026-03-12" },
|
|
21
|
+
expected: ["2026-03-10", "2026-03-12"],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
description: "start only keeps a null end",
|
|
25
|
+
range: { start: "2026-03-10", end: null },
|
|
26
|
+
expected: ["2026-03-10", null],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
description: "end only becomes [end, null] (pending start)",
|
|
30
|
+
range: { start: null, end: "2026-03-12" },
|
|
31
|
+
expected: ["2026-03-12", null],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
description: "neither end is empty",
|
|
35
|
+
range: { start: null, end: null },
|
|
36
|
+
expected: [],
|
|
37
|
+
},
|
|
38
|
+
])("$description", ({ range, expected }) => {
|
|
39
|
+
const decoded = decodeRange(range as never, toPlain as never);
|
|
40
|
+
expect(decoded.map((x) => x?.toString() ?? null)).toEqual(expected);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("computeRangePreview", () => {
|
|
45
|
+
const base = {
|
|
46
|
+
hoveredDate: d("2026-03-15"),
|
|
47
|
+
committedDates: [d("2026-03-10"), null],
|
|
48
|
+
rangeMode: "start-end" as const,
|
|
49
|
+
allowRangeReversal: true,
|
|
50
|
+
readOnly: false,
|
|
51
|
+
isDateDisabled: () => false,
|
|
52
|
+
T,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
it.each([
|
|
56
|
+
{
|
|
57
|
+
description: "pending end previews start..hovered",
|
|
58
|
+
input: {},
|
|
59
|
+
expected: ["2026-03-10", "2026-03-15"],
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
description: "hovering before the start reverses when allowed",
|
|
63
|
+
input: { hoveredDate: d("2026-03-05") },
|
|
64
|
+
expected: ["2026-03-05", "2026-03-10"],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
description: "read-only has no preview",
|
|
68
|
+
input: { readOnly: true },
|
|
69
|
+
expected: null,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
description: "a disabled hovered date has no preview",
|
|
73
|
+
input: { isDateDisabled: (x: Temporal.PlainDate) => x.day === 15 },
|
|
74
|
+
expected: null,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
description:
|
|
78
|
+
"no committed dates: the first click only anchors, so no preview",
|
|
79
|
+
input: { committedDates: [] },
|
|
80
|
+
expected: null,
|
|
81
|
+
},
|
|
82
|
+
])("$description", ({ input, expected }) => {
|
|
83
|
+
expect(iso(computeRangePreview({ ...base, ...input }))).toEqual(expected);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("computePreviewRange", () => {
|
|
88
|
+
const range = { start: d("2026-03-10"), end: null } as never;
|
|
89
|
+
const run = (options?: Parameters<typeof computePreviewRange>[5]) => {
|
|
90
|
+
const r = computePreviewRange(
|
|
91
|
+
d("2026-03-15"),
|
|
92
|
+
range,
|
|
93
|
+
"start-end",
|
|
94
|
+
false,
|
|
95
|
+
T,
|
|
96
|
+
options,
|
|
97
|
+
);
|
|
98
|
+
return r && [String(r.start), String(r.end)];
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
it.each([
|
|
102
|
+
{
|
|
103
|
+
description: "defaults to every date selectable",
|
|
104
|
+
options: undefined,
|
|
105
|
+
expected: ["2026-03-10", "2026-03-15"],
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
description: "honors isDateDisabled",
|
|
109
|
+
options: { isDateDisabled: (x: Temporal.PlainDate) => x.day === 15 },
|
|
110
|
+
expected: null,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
description: "honors readOnly",
|
|
114
|
+
options: { readOnly: true },
|
|
115
|
+
expected: null,
|
|
116
|
+
},
|
|
117
|
+
])("$description", ({ options, expected }) => {
|
|
118
|
+
expect(run(options)).toEqual(expected);
|
|
119
|
+
});
|
|
120
|
+
});
|
package/src/root-selection.ts
CHANGED
|
@@ -7,64 +7,9 @@ import type {
|
|
|
7
7
|
TemporalNamespace,
|
|
8
8
|
ValueChangeMeta,
|
|
9
9
|
ValueFormat,
|
|
10
|
-
WeekStartDay,
|
|
11
|
-
OutsideDays,
|
|
12
10
|
} from "./types";
|
|
13
11
|
import { fromZonedDateTime } from "./utils";
|
|
14
12
|
|
|
15
|
-
export interface UseRootStateParamsBase<F extends ValueFormat> {
|
|
16
|
-
format: F;
|
|
17
|
-
min?: RawValueForFormat<F>;
|
|
18
|
-
max?: RawValueForFormat<F>;
|
|
19
|
-
disabled: boolean;
|
|
20
|
-
readOnly: boolean;
|
|
21
|
-
isDateDisabled?: (date: Temporal.PlainDate) => boolean;
|
|
22
|
-
timeZone: string;
|
|
23
|
-
locale: string;
|
|
24
|
-
temporal: TemporalNamespace;
|
|
25
|
-
weekStartDay: WeekStartDay;
|
|
26
|
-
fixedWeeks: boolean;
|
|
27
|
-
numberOfMonths: number;
|
|
28
|
-
outsideDays: OutsideDays;
|
|
29
|
-
onMonthChange?: (month: Temporal.PlainYearMonth) => void;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type UseRootStateParams<F extends ValueFormat> =
|
|
33
|
-
UseRootStateParamsBase<F> &
|
|
34
|
-
(
|
|
35
|
-
| {
|
|
36
|
-
selectionMode: "single";
|
|
37
|
-
value?: RawValueForFormat<F> | null;
|
|
38
|
-
defaultValue?: RawValueForFormat<F>;
|
|
39
|
-
onValueChange?: (
|
|
40
|
-
value: RawValueForFormat<F> | null,
|
|
41
|
-
meta: ValueChangeMeta<RawValueForFormat<F> | null>,
|
|
42
|
-
) => void;
|
|
43
|
-
}
|
|
44
|
-
| {
|
|
45
|
-
selectionMode: "range";
|
|
46
|
-
value?: DateRange<F> | null;
|
|
47
|
-
defaultValue?: DateRange<F>;
|
|
48
|
-
onValueChange?: (
|
|
49
|
-
value: DateRange<F> | null,
|
|
50
|
-
meta: ValueChangeMeta<DateRange<F> | null>,
|
|
51
|
-
) => void;
|
|
52
|
-
rangeMode?: RangeMode;
|
|
53
|
-
preventRangeReversal?: boolean;
|
|
54
|
-
previewRange?: DateRange<F> | null;
|
|
55
|
-
onHoveredDateChange?: (date: Temporal.PlainDate | undefined) => void;
|
|
56
|
-
}
|
|
57
|
-
| {
|
|
58
|
-
selectionMode: "multiple";
|
|
59
|
-
value?: RawValueForFormat<F>[] | null;
|
|
60
|
-
defaultValue?: RawValueForFormat<F>[];
|
|
61
|
-
onValueChange?: (
|
|
62
|
-
value: RawValueForFormat<F>[],
|
|
63
|
-
meta: ValueChangeMeta<RawValueForFormat<F>[]>,
|
|
64
|
-
) => void;
|
|
65
|
-
}
|
|
66
|
-
);
|
|
67
|
-
|
|
68
13
|
/** Construct a DateRange (workaround for TS generic conditional type widening). */
|
|
69
14
|
function mkRange<F extends ValueFormat>(
|
|
70
15
|
start: unknown,
|
|
@@ -87,6 +32,22 @@ export function tagRaw<F extends ValueFormat>(
|
|
|
87
32
|
return { format, value: raw } as DateValueObject;
|
|
88
33
|
}
|
|
89
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Decodes a range value into committed dates. An empty range is `[]`; a range
|
|
37
|
+
* with only an end is `[end, null]` (pending start, so the end is still the
|
|
38
|
+
* anchor of the next click).
|
|
39
|
+
*/
|
|
40
|
+
export function decodeRange<F extends ValueFormat>(
|
|
41
|
+
range: DateRange<F>,
|
|
42
|
+
toPlain: (raw: RawValueForFormat<F>) => Temporal.PlainDate,
|
|
43
|
+
): (Temporal.PlainDate | null)[] {
|
|
44
|
+
const start = range.start != null ? toPlain(range.start) : null;
|
|
45
|
+
const end = range.end != null ? toPlain(range.end) : null;
|
|
46
|
+
if (start === null && end === null) return [];
|
|
47
|
+
if (start === null && end !== null) return [end, null];
|
|
48
|
+
return [start, end];
|
|
49
|
+
}
|
|
50
|
+
|
|
90
51
|
/** Resolve a `RangeMode` to a concrete "start" | "end" | "reset" action for inside-range clicks. */
|
|
91
52
|
export function resolveInsideAction(
|
|
92
53
|
mode: RangeMode,
|
|
@@ -639,47 +600,48 @@ export function truncateDatesForMode<F extends ValueFormat>(opts: {
|
|
|
639
600
|
return clamped;
|
|
640
601
|
}
|
|
641
602
|
|
|
603
|
+
/** Inputs shared by the provider's hover preview and the public {@link computePreviewRange}. */
|
|
604
|
+
export interface RangePreviewInput {
|
|
605
|
+
hoveredDate: Temporal.PlainDate;
|
|
606
|
+
committedDates: readonly (Temporal.PlainDate | null)[];
|
|
607
|
+
rangeMode: RangeMode;
|
|
608
|
+
allowRangeReversal: boolean;
|
|
609
|
+
readOnly: boolean;
|
|
610
|
+
isDateDisabled: (date: Temporal.PlainDate) => boolean;
|
|
611
|
+
T: TemporalNamespace;
|
|
612
|
+
}
|
|
613
|
+
|
|
642
614
|
/**
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
615
|
+
* The range a click on `hoveredDate` would produce, or `null` when the click
|
|
616
|
+
* would do nothing (read-only, disabled date). One path for both the
|
|
617
|
+
* provider's own preview and the public {@link computePreviewRange}.
|
|
646
618
|
*/
|
|
647
|
-
export function
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
}
|
|
619
|
+
export function computeRangePreview(
|
|
620
|
+
input: RangePreviewInput,
|
|
621
|
+
): [Temporal.PlainDate, Temporal.PlainDate] | null {
|
|
622
|
+
const {
|
|
623
|
+
hoveredDate,
|
|
624
|
+
committedDates,
|
|
625
|
+
rangeMode,
|
|
626
|
+
allowRangeReversal,
|
|
627
|
+
readOnly,
|
|
628
|
+
isDateDisabled,
|
|
629
|
+
T,
|
|
630
|
+
} = input;
|
|
660
631
|
|
|
661
|
-
const rawStart =
|
|
662
|
-
currentRange?.start != null
|
|
663
|
-
? Temporal.PlainDate.from(currentRange.start as any)
|
|
664
|
-
: null;
|
|
665
|
-
const rawEnd =
|
|
666
|
-
currentRange?.end != null
|
|
667
|
-
? Temporal.PlainDate.from(currentRange.end as any)
|
|
668
|
-
: null;
|
|
669
|
-
// Normalize {start: null, end} → [end, null] (pending start)
|
|
670
|
-
const committedDates = currentRange
|
|
671
|
-
? rawStart === null && rawEnd !== null
|
|
672
|
-
? ([rawEnd, null] as const)
|
|
673
|
-
: ([rawStart, rawEnd] as const)
|
|
674
|
-
: ([] as const);
|
|
675
632
|
const committedStart = committedDates[0] ?? undefined;
|
|
676
|
-
const committedEnd =
|
|
677
|
-
|
|
678
|
-
|
|
633
|
+
const committedEnd =
|
|
634
|
+
committedDates.length > 1
|
|
635
|
+
? (committedDates[committedDates.length - 1] ?? undefined)
|
|
636
|
+
: committedStart;
|
|
637
|
+
|
|
638
|
+
// The preview never emits a value, so the formatting and time-zone inputs
|
|
639
|
+
// of a real selection are unused.
|
|
640
|
+
const unused = () => [] as never;
|
|
679
641
|
const result = computeSelectionUpdate<"PlainDate">({
|
|
680
642
|
date: hoveredDate,
|
|
681
|
-
readOnly
|
|
682
|
-
isDateDisabled
|
|
643
|
+
readOnly,
|
|
644
|
+
isDateDisabled,
|
|
683
645
|
isMultiple: false,
|
|
684
646
|
isRange: true,
|
|
685
647
|
committedDates,
|
|
@@ -687,19 +649,59 @@ export function computePreviewRange(
|
|
|
687
649
|
committedEnd,
|
|
688
650
|
selectedZdt: undefined,
|
|
689
651
|
rangeMode,
|
|
690
|
-
allowRangeReversal
|
|
691
|
-
sortDates: (d) => [...d].sort((a, b) =>
|
|
692
|
-
currentSingleFormatted:
|
|
693
|
-
currentRangeFormatted:
|
|
694
|
-
currentMultipleFormatted:
|
|
652
|
+
allowRangeReversal,
|
|
653
|
+
sortDates: (d) => [...d].sort((a, b) => T.PlainDate.compare(a, b)),
|
|
654
|
+
currentSingleFormatted: unused,
|
|
655
|
+
currentRangeFormatted: unused,
|
|
656
|
+
currentMultipleFormatted: unused,
|
|
695
657
|
resolvedFormat: "PlainDate",
|
|
696
658
|
timeZone: "UTC",
|
|
697
|
-
T
|
|
659
|
+
T,
|
|
698
660
|
});
|
|
699
661
|
|
|
700
662
|
if (result.skip || result.newDates.length === 0) return null;
|
|
701
663
|
const start = result.newDates[0];
|
|
702
664
|
const end = result.newDates.length > 1 ? result.newDates[1] : start;
|
|
703
665
|
if (start == null || end == null) return null;
|
|
704
|
-
return
|
|
666
|
+
return [start, end];
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Pure utility that computes the preview range for a hovered date, using the
|
|
671
|
+
* same logic as the built-in hover preview. Useful for deriving the preview in
|
|
672
|
+
* an `onHoveredDateChange` handler without needing a controlled `previewRange`.
|
|
673
|
+
*
|
|
674
|
+
* Pass `options.isDateDisabled` and `options.readOnly` to match a calendar that
|
|
675
|
+
* disables dates or is read-only; by default every date is selectable.
|
|
676
|
+
*/
|
|
677
|
+
export function computePreviewRange(
|
|
678
|
+
hoveredDate: Temporal.PlainDate,
|
|
679
|
+
currentRange: DateRange | null,
|
|
680
|
+
rangeMode: RangeMode,
|
|
681
|
+
preventRangeReversal = false,
|
|
682
|
+
T?: TemporalNamespace,
|
|
683
|
+
options: {
|
|
684
|
+
isDateDisabled?: (date: Temporal.PlainDate) => boolean;
|
|
685
|
+
readOnly?: boolean;
|
|
686
|
+
} = {},
|
|
687
|
+
): DateRange<"PlainDate"> | null {
|
|
688
|
+
const Temporal: TemporalNamespace = T ?? (globalThis as any).Temporal;
|
|
689
|
+
if (!Temporal) {
|
|
690
|
+
throw new Error(
|
|
691
|
+
"computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.",
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
const preview = computeRangePreview({
|
|
696
|
+
hoveredDate,
|
|
697
|
+
committedDates: currentRange
|
|
698
|
+
? decodeRange(currentRange, (raw) => Temporal.PlainDate.from(raw as any))
|
|
699
|
+
: [],
|
|
700
|
+
rangeMode,
|
|
701
|
+
allowRangeReversal: !preventRangeReversal,
|
|
702
|
+
readOnly: options.readOnly ?? false,
|
|
703
|
+
isDateDisabled: options.isDateDisabled ?? (() => false),
|
|
704
|
+
T: Temporal,
|
|
705
|
+
});
|
|
706
|
+
return preview ? mkRange<"PlainDate">(preview[0], preview[1]) : null;
|
|
705
707
|
}
|
package/src/weeks-navigation.tsx
CHANGED
|
@@ -2,7 +2,6 @@ import { mergeProps } from "@base-ui/react/merge-props";
|
|
|
2
2
|
import { useRender } from "@base-ui/react/use-render";
|
|
3
3
|
import React, { forwardRef, useMemo } from "react";
|
|
4
4
|
import { useCalendarStable } from "./calendar-context";
|
|
5
|
-
import { canShift } from "./out-of-range";
|
|
6
5
|
import type { RootState, StateAttributesMapping } from "./types";
|
|
7
6
|
import { useViewState } from "./view-context";
|
|
8
7
|
import { useWeeksViewStable, useWeeksViewState } from "./weeks-view-context";
|
|
@@ -70,44 +69,29 @@ function useWeeksNavButton(
|
|
|
70
69
|
direction: "prev" | "next",
|
|
71
70
|
shiftByProp: number | undefined,
|
|
72
71
|
) {
|
|
73
|
-
const {
|
|
74
|
-
|
|
75
|
-
minValue,
|
|
76
|
-
maxValue,
|
|
77
|
-
temporal: T,
|
|
78
|
-
weekStartDay,
|
|
79
|
-
} = useCalendarStable();
|
|
80
|
-
const { weekCount, outOfRangeBehavior, goNext, goPrev } =
|
|
81
|
-
useWeeksViewStable();
|
|
72
|
+
const { disabled: globalDisabled } = useCalendarStable();
|
|
73
|
+
const { windowPolicy, goNext, goPrev } = useWeeksViewStable();
|
|
82
74
|
const { windowInfo } = useWeeksViewState();
|
|
83
75
|
const { rootState } = useViewState();
|
|
84
76
|
|
|
85
|
-
const
|
|
77
|
+
const { windowStart, weekCount } = windowInfo;
|
|
86
78
|
|
|
87
79
|
const isDisabled = useMemo(() => {
|
|
88
80
|
if (globalDisabled) return true;
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
...(maxValue !== undefined && { max: maxValue }),
|
|
97
|
-
weekStartDay,
|
|
98
|
-
T,
|
|
99
|
-
});
|
|
81
|
+
return (
|
|
82
|
+
windowPolicy.shift(
|
|
83
|
+
{ firstWeek: windowStart, weekCount },
|
|
84
|
+
direction === "next" ? 1 : -1,
|
|
85
|
+
shiftByProp,
|
|
86
|
+
) === null
|
|
87
|
+
);
|
|
100
88
|
}, [
|
|
101
89
|
globalDisabled,
|
|
102
|
-
|
|
90
|
+
windowPolicy,
|
|
91
|
+
windowStart,
|
|
103
92
|
weekCount,
|
|
104
93
|
direction,
|
|
105
94
|
shiftByProp,
|
|
106
|
-
outOfRangeBehavior,
|
|
107
|
-
minValue,
|
|
108
|
-
maxValue,
|
|
109
|
-
weekStartDay,
|
|
110
|
-
T,
|
|
111
95
|
]);
|
|
112
96
|
|
|
113
97
|
const state = useMemo<WeeksNavButtonState>(
|