@klinking/colander 3.1.0-alpha.4 → 3.1.0-alpha.6
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/{weeks-view-context-CUCy8njs.d.cts → index-DnFCKyyw.d.cts} +71 -8
- package/dist/{weeks-view-context-E1NwyNgQ.d.mts → index-q3cQKJdA.d.mts} +71 -8
- 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 -1
- package/dist/{weeks-navigation-elCpdfQ3.mjs → weeks-navigation-Dn3UtG76.mjs} +578 -589
- package/dist/{weeks-navigation-DV538__x.cjs → weeks-navigation-SuKJZJCM.cjs} +578 -589
- 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/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
- package/src/range-preview.test.ts +120 -0
- package/src/root-selection.ts +99 -97
- package/src/weeks-navigation.tsx +7 -32
- package/src/weeks-view-state.ts +24 -81
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-window.test.ts +43 -0
- package/src/weeks-window.ts +64 -0
|
@@ -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
|
@@ -238,84 +238,25 @@ export const MonthYearString = forwardRef(MonthYearStringFn) as (
|
|
|
238
238
|
) => React.ReactElement | null;
|
|
239
239
|
|
|
240
240
|
function useNavButton(direction: "prev" | "next") {
|
|
241
|
-
const {
|
|
242
|
-
disabled: globalDisabled,
|
|
243
|
-
minValue,
|
|
244
|
-
maxValue,
|
|
245
|
-
temporal: T,
|
|
246
|
-
} = useCalendarStable();
|
|
247
|
-
const monthViewStable = useMonthViewStable();
|
|
248
|
-
const monthViewState = useMonthViewState();
|
|
241
|
+
const { disabled: globalDisabled, temporal: T } = useCalendarStable();
|
|
249
242
|
const {
|
|
250
243
|
goNextMonth: goToNextMonth,
|
|
251
244
|
goPrevMonth: goToPrevMonth,
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
} =
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
//
|
|
263
|
-
|
|
264
|
-
direction === "next"
|
|
265
|
-
? (allMonths[numberOfMonths - 1] ?? currentDateTime)
|
|
266
|
-
: currentDateTime;
|
|
267
|
-
|
|
268
|
-
const destMonth =
|
|
269
|
-
direction === "prev"
|
|
270
|
-
? refMonth.month === 1
|
|
271
|
-
? 12
|
|
272
|
-
: refMonth.month - 1
|
|
273
|
-
: refMonth.month === 12
|
|
274
|
-
? 1
|
|
275
|
-
: refMonth.month + 1;
|
|
276
|
-
|
|
277
|
-
const destYear =
|
|
278
|
-
direction === "prev"
|
|
279
|
-
? refMonth.month === 1
|
|
280
|
-
? refMonth.year - 1
|
|
281
|
-
: refMonth.year
|
|
282
|
-
: refMonth.month === 12
|
|
283
|
-
? refMonth.year + 1
|
|
284
|
-
: refMonth.year;
|
|
285
|
-
|
|
286
|
-
const boundValue = direction === "prev" ? minValue : maxValue;
|
|
287
|
-
|
|
288
|
-
const isDisabled = useMemo(() => {
|
|
289
|
-
if (globalDisabled) return true;
|
|
290
|
-
// "unbounded" never disables on min/max — the bounds only gate which days
|
|
291
|
-
// are selectable, not which months can be viewed. "stop" halts navigation
|
|
292
|
-
// once the destination month crosses the boundary.
|
|
293
|
-
if (outOfRangeBehavior === "unbounded") return false;
|
|
294
|
-
if (!boundValue) return false;
|
|
295
|
-
if (direction === "prev") {
|
|
296
|
-
return (
|
|
297
|
-
destYear < boundValue.year ||
|
|
298
|
-
(destYear === boundValue.year && destMonth < boundValue.month)
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
return (
|
|
302
|
-
destYear > boundValue.year ||
|
|
303
|
-
(destYear === boundValue.year && destMonth > boundValue.month)
|
|
304
|
-
);
|
|
305
|
-
}, [
|
|
306
|
-
globalDisabled,
|
|
307
|
-
outOfRangeBehavior,
|
|
308
|
-
destYear,
|
|
309
|
-
destMonth,
|
|
310
|
-
boundValue,
|
|
311
|
-
direction,
|
|
312
|
-
]);
|
|
313
|
-
|
|
314
|
-
// ISO — `destYear`/`destMonth` are ISO numbers; the locale calendar only
|
|
315
|
-
// affects display, so it must not be injected into this value.
|
|
245
|
+
windowPolicy,
|
|
246
|
+
} = useMonthViewStable();
|
|
247
|
+
const { currentMonth, rootState } = useMonthViewState();
|
|
248
|
+
|
|
249
|
+
const step = useMemo(
|
|
250
|
+
() => windowPolicy.step(currentMonth, direction),
|
|
251
|
+
[windowPolicy, currentMonth, direction],
|
|
252
|
+
);
|
|
253
|
+
const isDisabled = globalDisabled || step.blocked;
|
|
254
|
+
|
|
255
|
+
// ISO — the locale calendar only affects display, so it must not be
|
|
256
|
+
// injected into this value.
|
|
316
257
|
const target = useMemo(
|
|
317
|
-
() => T.PlainYearMonth.from(
|
|
318
|
-
[
|
|
258
|
+
() => T.PlainYearMonth.from(step.entering),
|
|
259
|
+
[step.entering, T],
|
|
319
260
|
);
|
|
320
261
|
|
|
321
262
|
const state = useMemo<NavButtonState>(
|
|
@@ -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,15 +69,8 @@ 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
|
|
|
@@ -86,29 +78,12 @@ function useWeeksNavButton(
|
|
|
86
78
|
|
|
87
79
|
const isDisabled = useMemo(() => {
|
|
88
80
|
if (globalDisabled) return true;
|
|
89
|
-
return !canShift(
|
|
81
|
+
return !windowPolicy.canShift(
|
|
90
82
|
currentFirstWeek,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
...(minValue !== undefined && { min: minValue }),
|
|
96
|
-
...(maxValue !== undefined && { max: maxValue }),
|
|
97
|
-
weekStartDay,
|
|
98
|
-
T,
|
|
99
|
-
});
|
|
100
|
-
}, [
|
|
101
|
-
globalDisabled,
|
|
102
|
-
currentFirstWeek,
|
|
103
|
-
weekCount,
|
|
104
|
-
direction,
|
|
105
|
-
shiftByProp,
|
|
106
|
-
outOfRangeBehavior,
|
|
107
|
-
minValue,
|
|
108
|
-
maxValue,
|
|
109
|
-
weekStartDay,
|
|
110
|
-
T,
|
|
111
|
-
]);
|
|
83
|
+
direction === "next" ? 1 : -1,
|
|
84
|
+
shiftByProp,
|
|
85
|
+
);
|
|
86
|
+
}, [globalDisabled, windowPolicy, currentFirstWeek, direction, shiftByProp]);
|
|
112
87
|
|
|
113
88
|
const state = useMemo<WeeksNavButtonState>(
|
|
114
89
|
() => ({ root: rootState, direction, disabled: isDisabled }),
|