@klinking/colander 0.0.0 → 2.0.1
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 +51 -48
- package/dist/formats/date.d.cts +10 -37
- package/dist/formats/date.d.mts +10 -37
- package/dist/formats/date.mjs +6 -2
- package/dist/formats/object.cjs +51 -48
- package/dist/formats/object.d.cts +10 -37
- package/dist/formats/object.d.mts +10 -37
- package/dist/formats/object.mjs +6 -2
- package/dist/formats/plain-date-time.cjs +51 -48
- package/dist/formats/plain-date-time.d.cts +10 -37
- package/dist/formats/plain-date-time.d.mts +10 -37
- package/dist/formats/plain-date-time.mjs +6 -2
- package/dist/formats/plain-date.cjs +51 -48
- package/dist/formats/plain-date.d.cts +10 -37
- package/dist/formats/plain-date.d.mts +10 -37
- package/dist/formats/plain-date.mjs +6 -2
- package/dist/formats/plain-month-day.cjs +51 -48
- package/dist/formats/plain-month-day.d.cts +10 -37
- package/dist/formats/plain-month-day.d.mts +10 -37
- package/dist/formats/plain-month-day.mjs +6 -2
- package/dist/formats/plain-year-month.cjs +51 -48
- package/dist/formats/plain-year-month.d.cts +10 -37
- package/dist/formats/plain-year-month.d.mts +10 -37
- package/dist/formats/plain-year-month.mjs +6 -2
- package/dist/formats/zoned-date-time.cjs +51 -48
- package/dist/formats/zoned-date-time.d.cts +10 -37
- package/dist/formats/zoned-date-time.d.mts +10 -37
- package/dist/formats/zoned-date-time.mjs +6 -2
- package/dist/index.cjs +47 -48
- package/dist/index.d.cts +11 -2
- package/dist/index.d.mts +11 -2
- package/dist/index.mjs +2 -2
- package/dist/{temporal-polyfill-DTx0VIX5.mjs → weeks-navigation-B-Nch3LS.mjs} +357 -562
- package/dist/{temporal-polyfill-DU8VfleL.cjs → weeks-navigation-FxcECgCR.cjs} +357 -568
- package/dist/{weeks-view-context-BN6qR-83.d.mts → weeks-view-context-BZeWx9N8.d.cts} +275 -258
- package/dist/{weeks-view-context-aHGgYkpt.d.cts → weeks-view-context-MVe1b-BS.d.mts} +275 -258
- package/package.json +20 -20
- package/src/calendar-context.ts +12 -5
- package/src/calendar-provider.test.tsx +18 -4
- package/src/calendar-provider.tsx +130 -35
- package/src/calendar-types.ts +21 -4
- package/src/compute-weeks-in-window.test.ts +9 -7
- package/src/context.ts +1 -1
- package/src/day-cell.test.tsx +109 -0
- package/src/day-cell.tsx +132 -49
- package/src/drag-handle.tsx +23 -24
- package/src/formats/date.ts +120 -56
- package/src/formats/formats.test.ts +8 -4
- package/src/formats/object.ts +120 -56
- package/src/formats/plain-date-time.ts +120 -56
- package/src/formats/plain-date.ts +120 -56
- package/src/formats/plain-month-day.ts +120 -56
- package/src/formats/plain-year-month.ts +120 -56
- package/src/formats/zoned-date-time.ts +120 -56
- package/src/grid-header.tsx +24 -18
- package/src/grid.event-perf.test.tsx +30 -13
- package/src/grid.perf.test.tsx +97 -33
- package/src/grid.tsx +75 -43
- package/src/index.ts +16 -4
- package/src/keyboard.test.ts +110 -42
- package/src/keyboard.ts +18 -7
- package/src/month-separator.test.tsx +3 -1
- package/src/month-separator.tsx +30 -9
- package/src/month-view-context.ts +8 -3
- package/src/month-view-types.ts +14 -10
- package/src/month-view.test.tsx +687 -68
- package/src/month-view.tsx +176 -71
- package/src/navigation.tsx +77 -47
- package/src/overflow.test.ts +15 -13
- package/src/overflow.ts +10 -2
- package/src/range-preview.tsx +37 -12
- package/src/resolve-first-week.test.ts +14 -10
- package/src/resolve-first-week.ts +6 -2
- package/src/root-selection.ts +185 -101
- package/src/selected-range.tsx +47 -22
- package/src/test-temporal.ts +0 -2
- package/src/types.ts +126 -103
- package/src/utils.bench.ts +30 -6
- package/src/utils.test.ts +203 -55
- package/src/utils.ts +156 -54
- package/src/view-context.ts +4 -1
- package/src/week-number.tsx +5 -8
- package/src/weeks-grid.tsx +45 -19
- package/src/weeks-keyboard.test.ts +6 -7
- package/src/weeks-keyboard.ts +8 -3
- package/src/weeks-navigation.tsx +21 -9
- package/src/weeks-view-context.ts +8 -3
- package/src/weeks-view-state.ts +100 -33
- package/src/weeks-view-types.ts +14 -8
- package/src/weeks-view.test.tsx +127 -19
- package/src/weeks-view.tsx +30 -12
- package/src/temporal-polyfill.test.ts +0 -317
- package/src/temporal-polyfill.ts +0 -566
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
|
|
2
1
|
{
|
|
3
2
|
"name": "@klinking/colander",
|
|
4
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"homepage": "https://github.com/klink-ing/colander#readme",
|
|
5
|
+
"bugs": "https://github.com/klink-ing/colander/issues",
|
|
5
6
|
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/klink-ing/colander.git",
|
|
10
|
+
"directory": "package"
|
|
11
|
+
},
|
|
6
12
|
"files": [
|
|
7
13
|
"dist",
|
|
8
14
|
"src"
|
|
@@ -54,36 +60,30 @@
|
|
|
54
60
|
"require": "./dist/formats/date.cjs"
|
|
55
61
|
}
|
|
56
62
|
},
|
|
57
|
-
"scripts": {
|
|
58
|
-
"prepublishOnly": "vp run build"
|
|
59
|
-
},
|
|
60
63
|
"devDependencies": {
|
|
61
|
-
"@base-ui/react": "^1.
|
|
64
|
+
"@base-ui/react": "^1.3.0",
|
|
62
65
|
"@js-temporal/polyfill": "^0.5.1",
|
|
63
66
|
"@playwright/test": "^1.58.2",
|
|
64
|
-
"@size-limit/esbuild": "^12.0.1",
|
|
65
|
-
"@size-limit/esbuild-why": "^12.0.1",
|
|
66
|
-
"@size-limit/file": "^12.0.1",
|
|
67
67
|
"@testing-library/dom": "^10.4.1",
|
|
68
68
|
"@testing-library/react": "^16.3.2",
|
|
69
|
-
"@types/react": "^19.
|
|
70
|
-
"@types/react-dom": "^19.
|
|
69
|
+
"@types/react": "^19.2.0",
|
|
70
|
+
"@types/react-dom": "^19.2.0",
|
|
71
71
|
"@vitejs/plugin-react": "^4.7.0",
|
|
72
72
|
"jsdom": "^28.1.0",
|
|
73
73
|
"oxlint": "^1.58.0",
|
|
74
|
-
"react": "^19.
|
|
75
|
-
"react-dom": "^19.
|
|
74
|
+
"react": "^19.2.0",
|
|
75
|
+
"react-dom": "^19.2.0",
|
|
76
76
|
"rollup-plugin-visualizer": "^7.0.1",
|
|
77
|
-
"size-limit": "^12.0.1",
|
|
78
77
|
"tsx": "^4.21.0",
|
|
79
|
-
"typescript": "
|
|
80
|
-
"vite": "
|
|
81
|
-
"vite-plus": "
|
|
82
|
-
"vitest": "
|
|
78
|
+
"typescript": "^5",
|
|
79
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@^0.1.16",
|
|
80
|
+
"vite-plus": "latest",
|
|
81
|
+
"vitest": "npm:@voidzero-dev/vite-plus-test@latest"
|
|
83
82
|
},
|
|
84
83
|
"peerDependencies": {
|
|
85
84
|
"@base-ui/react": ">=1.0.0",
|
|
86
85
|
"react": ">=18",
|
|
87
86
|
"react-dom": ">=18"
|
|
88
|
-
}
|
|
89
|
-
}
|
|
87
|
+
},
|
|
88
|
+
"scripts": {}
|
|
89
|
+
}
|
package/src/calendar-context.ts
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
import { createContext, useContext } from "react";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
CalendarStableContextValue,
|
|
4
|
+
CalendarStateContextValue,
|
|
5
|
+
} from "./calendar-types";
|
|
3
6
|
|
|
4
7
|
/** @internal React context carrying stable CalendarProvider values (callbacks, config). */
|
|
5
|
-
export const CalendarStableContext =
|
|
8
|
+
export const CalendarStableContext =
|
|
9
|
+
createContext<CalendarStableContextValue | null>(null);
|
|
6
10
|
|
|
7
11
|
/** @internal React context carrying volatile CalendarProvider state. */
|
|
8
|
-
export const CalendarStateContext =
|
|
12
|
+
export const CalendarStateContext =
|
|
13
|
+
createContext<CalendarStateContextValue | null>(null);
|
|
9
14
|
|
|
10
15
|
/** Returns the stable (callbacks/config) part of CalendarProvider context. */
|
|
11
16
|
export function useCalendarStable(): CalendarStableContextValue {
|
|
12
17
|
const ctx = useContext(CalendarStableContext);
|
|
13
|
-
if (!ctx)
|
|
18
|
+
if (!ctx)
|
|
19
|
+
throw new Error("useCalendarStable must be used within CalendarProvider");
|
|
14
20
|
return ctx;
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
/** Returns the volatile state part of CalendarProvider context. */
|
|
18
24
|
export function useCalendarState(): CalendarStateContextValue {
|
|
19
25
|
const ctx = useContext(CalendarStateContext);
|
|
20
|
-
if (!ctx)
|
|
26
|
+
if (!ctx)
|
|
27
|
+
throw new Error("useCalendarState must be used within CalendarProvider");
|
|
21
28
|
return ctx;
|
|
22
29
|
}
|
|
@@ -24,7 +24,9 @@ function StateConsumer() {
|
|
|
24
24
|
return (
|
|
25
25
|
<div>
|
|
26
26
|
<span data-testid="has-selection">{state.selected ? "yes" : "no"}</span>
|
|
27
|
-
<span data-testid="range-start">
|
|
27
|
+
<span data-testid="range-start">
|
|
28
|
+
{state.rangeStart?.toString() ?? "none"}
|
|
29
|
+
</span>
|
|
28
30
|
</div>
|
|
29
31
|
);
|
|
30
32
|
}
|
|
@@ -32,7 +34,10 @@ function StateConsumer() {
|
|
|
32
34
|
function SelectButton() {
|
|
33
35
|
const stable = useCalendarStable();
|
|
34
36
|
return (
|
|
35
|
-
<button
|
|
37
|
+
<button
|
|
38
|
+
type="button"
|
|
39
|
+
onClick={() => stable.onSelect(T.PlainDate.from("2026-03-15"))}
|
|
40
|
+
>
|
|
36
41
|
Select
|
|
37
42
|
</button>
|
|
38
43
|
);
|
|
@@ -76,7 +81,12 @@ describe("CalendarProvider", () => {
|
|
|
76
81
|
it("calls onValueChange for controlled single selection", async () => {
|
|
77
82
|
const onChange = vi.fn();
|
|
78
83
|
const { getByText } = render(
|
|
79
|
-
<CalendarProvider
|
|
84
|
+
<CalendarProvider
|
|
85
|
+
temporal={T}
|
|
86
|
+
selectionMode="single"
|
|
87
|
+
value={null}
|
|
88
|
+
onValueChange={onChange}
|
|
89
|
+
>
|
|
80
90
|
<SelectButton />
|
|
81
91
|
</CalendarProvider>,
|
|
82
92
|
);
|
|
@@ -89,7 +99,11 @@ describe("CalendarProvider", () => {
|
|
|
89
99
|
it("deselects when clicking an already-selected date in single mode", async () => {
|
|
90
100
|
const onChange = vi.fn();
|
|
91
101
|
const { getByTestId, getByText } = render(
|
|
92
|
-
<CalendarProvider
|
|
102
|
+
<CalendarProvider
|
|
103
|
+
temporal={T}
|
|
104
|
+
selectionMode="single"
|
|
105
|
+
onValueChange={onChange}
|
|
106
|
+
>
|
|
93
107
|
<StateConsumer />
|
|
94
108
|
<SelectButton />
|
|
95
109
|
</CalendarProvider>,
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
CalendarStableContext,
|
|
5
|
+
CalendarStateContext,
|
|
6
|
+
} from "./calendar-context";
|
|
4
7
|
import type {
|
|
5
8
|
CalendarProviderProps,
|
|
6
9
|
CalendarStableContextValue,
|
|
@@ -15,11 +18,11 @@ import {
|
|
|
15
18
|
type SelectionResult,
|
|
16
19
|
} from "./root-selection";
|
|
17
20
|
import type {
|
|
21
|
+
BaseRootState,
|
|
18
22
|
DateRange,
|
|
19
23
|
DateValueObject,
|
|
20
24
|
RangeMode,
|
|
21
25
|
RawValueForFormat,
|
|
22
|
-
ValueChangeMeta,
|
|
23
26
|
ValueFormat,
|
|
24
27
|
} from "./types";
|
|
25
28
|
import { fromZonedDateTime, toZonedDateTime, selectedToZdt } from "./utils";
|
|
@@ -29,7 +32,9 @@ import { getSystemTimeZone, resolveTemporal } from "./utils";
|
|
|
29
32
|
* Provides shared calendar state (selection, config, range preview) to all
|
|
30
33
|
* descendant view components. Does NOT manage focus, navigation, or grid data.
|
|
31
34
|
*/
|
|
32
|
-
function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
35
|
+
function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
36
|
+
props: CalendarProviderProps<F>,
|
|
37
|
+
) {
|
|
33
38
|
const {
|
|
34
39
|
format: formatProp,
|
|
35
40
|
selectionMode: selectionModeProp,
|
|
@@ -75,9 +80,13 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
75
80
|
const onValueChange = (props as any).onValueChange;
|
|
76
81
|
|
|
77
82
|
const singleValue: RawValueForFormat<F> | null | undefined =
|
|
78
|
-
!isRange && !isMultiple
|
|
83
|
+
!isRange && !isMultiple
|
|
84
|
+
? (valueProp as RawValueForFormat<F> | null | undefined)
|
|
85
|
+
: undefined;
|
|
79
86
|
const singleDefault: RawValueForFormat<F> | undefined =
|
|
80
|
-
!isRange && !isMultiple
|
|
87
|
+
!isRange && !isMultiple
|
|
88
|
+
? (defaultValueProp as RawValueForFormat<F> | undefined)
|
|
89
|
+
: undefined;
|
|
81
90
|
|
|
82
91
|
const rangeValue: DateRange<F> | null | undefined = isRange
|
|
83
92
|
? valueProp === null
|
|
@@ -87,7 +96,9 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
87
96
|
: undefined
|
|
88
97
|
: undefined;
|
|
89
98
|
const rangeDefault: DateRange<F> | undefined =
|
|
90
|
-
isRange &&
|
|
99
|
+
isRange &&
|
|
100
|
+
defaultValueProp != null &&
|
|
101
|
+
isDateRange<F>(defaultValueProp as any)
|
|
91
102
|
? (defaultValueProp as DateRange<F>)
|
|
92
103
|
: undefined;
|
|
93
104
|
|
|
@@ -143,14 +154,21 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
143
154
|
);
|
|
144
155
|
|
|
145
156
|
const sortDates = useCallback(
|
|
146
|
-
(dates: Temporal.PlainDate[]) =>
|
|
157
|
+
(dates: Temporal.PlainDate[]) =>
|
|
158
|
+
[...dates].sort((a, b) => T.PlainDate.compare(a, b)),
|
|
147
159
|
[T],
|
|
148
160
|
);
|
|
149
161
|
|
|
150
|
-
const maxDatesForMode = isMultiple
|
|
162
|
+
const maxDatesForMode = isMultiple
|
|
163
|
+
? Number.POSITIVE_INFINITY
|
|
164
|
+
: isRange
|
|
165
|
+
? 2
|
|
166
|
+
: 1;
|
|
151
167
|
|
|
152
168
|
// --- Controlled/Uncontrolled dates ---
|
|
153
|
-
const controlledDates = useMemo<
|
|
169
|
+
const controlledDates = useMemo<
|
|
170
|
+
(Temporal.PlainDate | null)[] | undefined
|
|
171
|
+
>(() => {
|
|
154
172
|
if (isMultiple) {
|
|
155
173
|
if (multipleValue === null) return [];
|
|
156
174
|
if (multipleValue) return sortDates(multipleValue.map(rawToPlain));
|
|
@@ -159,7 +177,8 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
159
177
|
if (isRange) {
|
|
160
178
|
if (rangeValue === null) return [];
|
|
161
179
|
if (rangeValue) {
|
|
162
|
-
const start =
|
|
180
|
+
const start =
|
|
181
|
+
rangeValue.start != null ? rawToPlain(rangeValue.start) : null;
|
|
163
182
|
const end = rangeValue.end != null ? rawToPlain(rangeValue.end) : null;
|
|
164
183
|
if (start === null && end === null) return [];
|
|
165
184
|
if (start === null && end !== null) return [end, null];
|
|
@@ -170,7 +189,15 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
170
189
|
if (singleValue === null) return [];
|
|
171
190
|
if (singleValue !== undefined) return [rawToPlain(singleValue)];
|
|
172
191
|
return undefined;
|
|
173
|
-
}, [
|
|
192
|
+
}, [
|
|
193
|
+
isMultiple,
|
|
194
|
+
isRange,
|
|
195
|
+
multipleValue,
|
|
196
|
+
rangeValue,
|
|
197
|
+
singleValue,
|
|
198
|
+
rawToPlain,
|
|
199
|
+
sortDates,
|
|
200
|
+
]);
|
|
174
201
|
|
|
175
202
|
const defaultDates = useMemo<(Temporal.PlainDate | null)[]>(() => {
|
|
176
203
|
if (isMultiple) {
|
|
@@ -178,33 +205,53 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
178
205
|
return [];
|
|
179
206
|
}
|
|
180
207
|
if (rangeDefault) {
|
|
181
|
-
const start =
|
|
182
|
-
|
|
208
|
+
const start =
|
|
209
|
+
rangeDefault.start != null ? rawToPlain(rangeDefault.start) : null;
|
|
210
|
+
const end =
|
|
211
|
+
rangeDefault.end != null ? rawToPlain(rangeDefault.end) : null;
|
|
183
212
|
if (start === null && end === null) return [];
|
|
184
213
|
if (start === null && end !== null) return [end, null];
|
|
185
214
|
return [start, end];
|
|
186
215
|
}
|
|
187
216
|
if (singleDefault != null) return [rawToPlain(singleDefault)];
|
|
188
217
|
return [];
|
|
189
|
-
}, [
|
|
218
|
+
}, [
|
|
219
|
+
isMultiple,
|
|
220
|
+
multipleDefault,
|
|
221
|
+
rangeDefault,
|
|
222
|
+
singleDefault,
|
|
223
|
+
rawToPlain,
|
|
224
|
+
sortDates,
|
|
225
|
+
]);
|
|
190
226
|
|
|
191
|
-
const [internalDates, setInternalDates] =
|
|
227
|
+
const [internalDates, setInternalDates] =
|
|
228
|
+
useState<(Temporal.PlainDate | null)[]>(defaultDates);
|
|
192
229
|
|
|
193
230
|
const isControlled = controlledDates !== undefined;
|
|
194
231
|
const committedDates = isControlled ? controlledDates : internalDates;
|
|
195
232
|
|
|
196
233
|
const rangeStart =
|
|
197
|
-
!isMultiple && committedDates.length > 0
|
|
234
|
+
!isMultiple && committedDates.length > 0
|
|
235
|
+
? (committedDates[0] ?? undefined)
|
|
236
|
+
: undefined;
|
|
198
237
|
const rangeEnd =
|
|
199
|
-
!isMultiple && committedDates.length >= 2
|
|
200
|
-
|
|
238
|
+
!isMultiple && committedDates.length >= 2
|
|
239
|
+
? (committedDates[1] ?? undefined)
|
|
240
|
+
: undefined;
|
|
241
|
+
const committedStart = (committedDates[0] ?? undefined) as
|
|
242
|
+
| Temporal.PlainDate
|
|
243
|
+
| undefined;
|
|
201
244
|
const committedEnd =
|
|
202
245
|
committedDates.length > 1
|
|
203
|
-
? ((committedDates[committedDates.length - 1] ?? undefined) as
|
|
246
|
+
? ((committedDates[committedDates.length - 1] ?? undefined) as
|
|
247
|
+
| Temporal.PlainDate
|
|
248
|
+
| undefined)
|
|
204
249
|
: committedStart;
|
|
205
250
|
|
|
206
251
|
// --- Hover state for range preview ---
|
|
207
|
-
const [hoveredDate, setHoveredDateRaw] = useState<
|
|
252
|
+
const [hoveredDate, setHoveredDateRaw] = useState<
|
|
253
|
+
Temporal.PlainDate | undefined
|
|
254
|
+
>(undefined);
|
|
208
255
|
|
|
209
256
|
const setHoveredDate = useCallback(
|
|
210
257
|
(date: Temporal.PlainDate | undefined) => {
|
|
@@ -219,7 +266,8 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
219
266
|
const zdt = plain
|
|
220
267
|
.toPlainDateTime({ hour: 0, minute: 0, second: 0 })
|
|
221
268
|
.toZonedDateTime(timeZone);
|
|
222
|
-
return fromZonedDateTime(zdt, resolvedFormat, T)
|
|
269
|
+
return fromZonedDateTime(zdt, resolvedFormat, T)
|
|
270
|
+
.value as RawValueForFormat<F>;
|
|
223
271
|
},
|
|
224
272
|
[resolvedFormat, timeZone, T],
|
|
225
273
|
);
|
|
@@ -256,7 +304,9 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
256
304
|
|
|
257
305
|
const currentMultipleFormatted = useCallback(
|
|
258
306
|
(): RawValueForFormat<F>[] =>
|
|
259
|
-
committedDates
|
|
307
|
+
committedDates
|
|
308
|
+
.filter((d): d is Temporal.PlainDate => d != null)
|
|
309
|
+
.map(plainToFormatValue),
|
|
260
310
|
[committedDates, plainToFormatValue],
|
|
261
311
|
);
|
|
262
312
|
|
|
@@ -266,11 +316,16 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
266
316
|
if (committedDates.length === 0) return undefined;
|
|
267
317
|
const first = committedDates[0];
|
|
268
318
|
if (!first) return undefined;
|
|
269
|
-
const zdt = first
|
|
319
|
+
const zdt = first
|
|
320
|
+
.toPlainDateTime({ hour: 0, minute: 0, second: 0 })
|
|
321
|
+
.toZonedDateTime(timeZone);
|
|
270
322
|
return fromZonedDateTime(zdt, resolvedFormat, T);
|
|
271
323
|
}, [taggedValue, committedDates, timeZone, resolvedFormat, T]);
|
|
272
324
|
|
|
273
|
-
const selectedZdt = useMemo(
|
|
325
|
+
const selectedZdt = useMemo(
|
|
326
|
+
() => selectedToZdt(selected, timeZone, T),
|
|
327
|
+
[selected, timeZone, T],
|
|
328
|
+
);
|
|
274
329
|
|
|
275
330
|
// --- Compute preview range ---
|
|
276
331
|
const [previewStart, previewEnd] = useMemo<
|
|
@@ -278,7 +333,9 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
278
333
|
>(() => {
|
|
279
334
|
if (previewRangeProp !== undefined) {
|
|
280
335
|
if (!previewRangeProp) return [undefined, undefined];
|
|
281
|
-
const rawToPlainLocal = (
|
|
336
|
+
const rawToPlainLocal = (
|
|
337
|
+
raw: RawValueForFormat<F>,
|
|
338
|
+
): Temporal.PlainDate => {
|
|
282
339
|
const tagged = {
|
|
283
340
|
format: resolvedFormat,
|
|
284
341
|
value: raw,
|
|
@@ -286,8 +343,13 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
286
343
|
return toZonedDateTime(tagged, timeZone, T).toPlainDate();
|
|
287
344
|
};
|
|
288
345
|
const s =
|
|
289
|
-
previewRangeProp.start != null
|
|
290
|
-
|
|
346
|
+
previewRangeProp.start != null
|
|
347
|
+
? rawToPlainLocal(previewRangeProp.start)
|
|
348
|
+
: undefined;
|
|
349
|
+
const e =
|
|
350
|
+
previewRangeProp.end != null
|
|
351
|
+
? rawToPlainLocal(previewRangeProp.end)
|
|
352
|
+
: undefined;
|
|
291
353
|
return [s, e];
|
|
292
354
|
}
|
|
293
355
|
if (!hoveredDate || !isRange) return [undefined, undefined];
|
|
@@ -311,7 +373,8 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
311
373
|
timeZone,
|
|
312
374
|
T,
|
|
313
375
|
});
|
|
314
|
-
if (result.skip || result.newDates.length === 0)
|
|
376
|
+
if (result.skip || result.newDates.length === 0)
|
|
377
|
+
return [undefined, undefined];
|
|
315
378
|
const ps = result.newDates[0];
|
|
316
379
|
const pe = result.newDates.length > 1 ? result.newDates[1] : ps;
|
|
317
380
|
if (ps == null || pe == null) return [undefined, undefined];
|
|
@@ -351,7 +414,13 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
351
414
|
onValueChange,
|
|
352
415
|
}),
|
|
353
416
|
);
|
|
354
|
-
}, [
|
|
417
|
+
}, [
|
|
418
|
+
selectionMode,
|
|
419
|
+
maxDatesForMode,
|
|
420
|
+
onValueChange,
|
|
421
|
+
plainToFormatValue,
|
|
422
|
+
isControlled,
|
|
423
|
+
]);
|
|
355
424
|
|
|
356
425
|
// --- Out-of-bounds cleanup for single mode ---
|
|
357
426
|
useEffect(() => {
|
|
@@ -370,7 +439,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
370
439
|
onValueChange as
|
|
371
440
|
| ((
|
|
372
441
|
v: RawValueForFormat<F> | null,
|
|
373
|
-
m:
|
|
442
|
+
m: { date: undefined; previous: RawValueForFormat<F> | null },
|
|
374
443
|
) => void)
|
|
375
444
|
| undefined
|
|
376
445
|
)?.(null, { date: undefined, previous: prev });
|
|
@@ -482,9 +551,9 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
482
551
|
selectionMode,
|
|
483
552
|
disabled,
|
|
484
553
|
readOnly,
|
|
485
|
-
isDateDisabled,
|
|
486
|
-
minValue,
|
|
487
|
-
maxValue,
|
|
554
|
+
...(isDateDisabled !== undefined && { isDateDisabled }),
|
|
555
|
+
...(minValue !== undefined && { minValue }),
|
|
556
|
+
...(maxValue !== undefined && { maxValue }),
|
|
488
557
|
timeZone,
|
|
489
558
|
locale,
|
|
490
559
|
temporal: T,
|
|
@@ -514,6 +583,20 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
514
583
|
],
|
|
515
584
|
);
|
|
516
585
|
|
|
586
|
+
const baseRootState = useMemo<BaseRootState>(
|
|
587
|
+
() => ({
|
|
588
|
+
hasSelection: nonNullDates.length > 0,
|
|
589
|
+
selected: nonNullDates[0],
|
|
590
|
+
selectedDates: nonNullDates,
|
|
591
|
+
rangeStart,
|
|
592
|
+
rangeEnd,
|
|
593
|
+
timeZone,
|
|
594
|
+
locale,
|
|
595
|
+
readOnly,
|
|
596
|
+
}),
|
|
597
|
+
[nonNullDates, rangeStart, rangeEnd, timeZone, locale, readOnly],
|
|
598
|
+
);
|
|
599
|
+
|
|
517
600
|
const stateCtx = useMemo<CalendarStateContextValue>(
|
|
518
601
|
() => ({
|
|
519
602
|
selected,
|
|
@@ -523,13 +606,25 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarPr
|
|
|
523
606
|
hoveredDate,
|
|
524
607
|
previewStart,
|
|
525
608
|
previewEnd,
|
|
609
|
+
baseRootState,
|
|
526
610
|
}),
|
|
527
|
-
[
|
|
611
|
+
[
|
|
612
|
+
selected,
|
|
613
|
+
nonNullDates,
|
|
614
|
+
rangeStart,
|
|
615
|
+
rangeEnd,
|
|
616
|
+
hoveredDate,
|
|
617
|
+
previewStart,
|
|
618
|
+
previewEnd,
|
|
619
|
+
baseRootState,
|
|
620
|
+
],
|
|
528
621
|
);
|
|
529
622
|
|
|
530
623
|
return (
|
|
531
624
|
<CalendarStableContext.Provider value={stableCtx}>
|
|
532
|
-
<CalendarStateContext.Provider value={stateCtx}>
|
|
625
|
+
<CalendarStateContext.Provider value={stateCtx}>
|
|
626
|
+
{children}
|
|
627
|
+
</CalendarStateContext.Provider>
|
|
533
628
|
</CalendarStableContext.Provider>
|
|
534
629
|
);
|
|
535
630
|
}
|
package/src/calendar-types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import type {
|
|
3
|
+
BaseRootState,
|
|
3
4
|
DateRange,
|
|
4
5
|
DateValueObject,
|
|
5
6
|
RangeMode,
|
|
@@ -53,7 +54,9 @@ interface CalendarProviderPropsBase<F extends ValueFormat = ValueFormat> {
|
|
|
53
54
|
*/
|
|
54
55
|
locale?: string;
|
|
55
56
|
/**
|
|
56
|
-
*
|
|
57
|
+
* The `Temporal` implementation to use. Not bundled — provide a `Temporal`
|
|
58
|
+
* namespace (e.g. from `temporal-polyfill` or `@js-temporal/polyfill`) unless
|
|
59
|
+
* the host exposes native `Temporal`. See `RootOwnProps.temporal` for links.
|
|
57
60
|
*/
|
|
58
61
|
temporal?: TemporalNamespace;
|
|
59
62
|
/**
|
|
@@ -97,7 +100,10 @@ interface CalendarRangeControlledProps<F extends ValueFormat = ValueFormat> {
|
|
|
97
100
|
selectionMode: "range";
|
|
98
101
|
value: DateRange<F> | null;
|
|
99
102
|
defaultValue?: never;
|
|
100
|
-
onValueChange?: (
|
|
103
|
+
onValueChange?: (
|
|
104
|
+
value: DateRange<F> | null,
|
|
105
|
+
meta: ValueChangeMeta<DateRange<F> | null>,
|
|
106
|
+
) => void;
|
|
101
107
|
/** @default "nearest-end" */
|
|
102
108
|
rangeMode?: RangeMode;
|
|
103
109
|
/** @default false */
|
|
@@ -110,7 +116,10 @@ interface CalendarRangeUncontrolledProps<F extends ValueFormat = ValueFormat> {
|
|
|
110
116
|
selectionMode: "range";
|
|
111
117
|
value?: never;
|
|
112
118
|
defaultValue?: DateRange<F>;
|
|
113
|
-
onValueChange?: (
|
|
119
|
+
onValueChange?: (
|
|
120
|
+
value: DateRange<F> | null,
|
|
121
|
+
meta: ValueChangeMeta<DateRange<F> | null>,
|
|
122
|
+
) => void;
|
|
114
123
|
/** @default "nearest-end" */
|
|
115
124
|
rangeMode?: RangeMode;
|
|
116
125
|
/** @default false */
|
|
@@ -131,7 +140,9 @@ interface CalendarMultipleControlledProps<F extends ValueFormat = ValueFormat> {
|
|
|
131
140
|
) => void;
|
|
132
141
|
}
|
|
133
142
|
|
|
134
|
-
interface CalendarMultipleUncontrolledProps<
|
|
143
|
+
interface CalendarMultipleUncontrolledProps<
|
|
144
|
+
F extends ValueFormat = ValueFormat,
|
|
145
|
+
> {
|
|
135
146
|
selectionMode: "multiple";
|
|
136
147
|
value?: never;
|
|
137
148
|
defaultValue?: RawValueForFormat<F>[];
|
|
@@ -209,4 +220,10 @@ export interface CalendarStateContextValue {
|
|
|
209
220
|
previewStart: Temporal.PlainDate | undefined;
|
|
210
221
|
/** End of the computed preview range. */
|
|
211
222
|
previewEnd: Temporal.PlainDate | undefined;
|
|
223
|
+
/**
|
|
224
|
+
* View-independent part of the render-prop `RootState` (selection + resolved
|
|
225
|
+
* config), built once here so MonthView and WeeksView expose identical state.
|
|
226
|
+
* Each view adds its own `focused`/`viewing`.
|
|
227
|
+
*/
|
|
228
|
+
baseRootState: BaseRootState;
|
|
212
229
|
}
|
|
@@ -61,17 +61,19 @@ describe("computeWeeksInWindow", () => {
|
|
|
61
61
|
])("$description", ({ firstWeek, weekCount, weekStartDay, expected }) => {
|
|
62
62
|
const weeks = computeWeeksInWindow(firstWeek, weekCount, weekStartDay, T);
|
|
63
63
|
expect(weeks).toHaveLength(expected.length);
|
|
64
|
-
expect(weeks[0]
|
|
65
|
-
expect(weeks[0]
|
|
66
|
-
expect(weeks[weeks.length - 1]
|
|
67
|
-
|
|
64
|
+
expect(weeks[0]!.startDate.toString()).toBe(expected.firstStart);
|
|
65
|
+
expect(weeks[0]!.endDate.toString()).toBe(expected.firstEnd);
|
|
66
|
+
expect(weeks[weeks.length - 1]!.startDate.toString()).toBe(
|
|
67
|
+
expected.lastStart,
|
|
68
|
+
);
|
|
69
|
+
expect(weeks[weeks.length - 1]!.endDate.toString()).toBe(expected.lastEnd);
|
|
68
70
|
});
|
|
69
71
|
|
|
70
72
|
it("assigns correct month/year to each week based on start date", () => {
|
|
71
73
|
const weeks = computeWeeksInWindow(T.PlainDate.from("2026-03-29"), 1, 0, T);
|
|
72
|
-
expect(weeks[0]
|
|
73
|
-
expect(weeks[0]
|
|
74
|
-
expect(weeks[0]
|
|
74
|
+
expect(weeks[0]!.startDate.toString()).toBe("2026-03-29");
|
|
75
|
+
expect(weeks[0]!.month).toBe(3);
|
|
76
|
+
expect(weeks[0]!.year).toBe(2026);
|
|
75
77
|
});
|
|
76
78
|
|
|
77
79
|
it("populates weekIndex sequentially from 0", () => {
|
package/src/context.ts
CHANGED
|
@@ -22,7 +22,7 @@ export const GridContext = createContext<{ orientation: GridOrientation }>({
|
|
|
22
22
|
/** @internal Provides the date and column index from a `DayCellTemplate` to its child `DayButton`. */
|
|
23
23
|
export const DayCellDataContext = createContext<{
|
|
24
24
|
date: Temporal.PlainDate;
|
|
25
|
-
columnIndex?: number;
|
|
25
|
+
columnIndex?: number | undefined;
|
|
26
26
|
outsideDisabled?: boolean;
|
|
27
27
|
} | null>(null);
|
|
28
28
|
|