@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klinking/colander",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0-alpha.10",
|
|
4
4
|
"homepage": "https://github.com/klink-ing/colander#readme",
|
|
5
5
|
"bugs": "https://github.com/klink-ing/colander/issues",
|
|
6
6
|
"license": "MIT",
|
|
@@ -126,25 +126,28 @@
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
},
|
|
129
|
+
"dependencies": {
|
|
130
|
+
"use-sync-external-store": "^1.7.0"
|
|
131
|
+
},
|
|
129
132
|
"devDependencies": {
|
|
130
|
-
"@base-ui/react": "^1.
|
|
133
|
+
"@base-ui/react": "^1.9.0",
|
|
131
134
|
"@js-temporal/polyfill": "^0.5.1",
|
|
132
|
-
"@playwright/test": "^1.
|
|
133
|
-
"@testing-library/dom": "^10.4.
|
|
134
|
-
"@testing-library/react": "^16.3.
|
|
135
|
-
"@types/react": "^19.
|
|
136
|
-
"@types/react-dom": "^19.
|
|
137
|
-
"@
|
|
138
|
-
"
|
|
139
|
-
"
|
|
140
|
-
"react": "^19.
|
|
141
|
-
"react-dom": "^19.
|
|
142
|
-
"rollup-plugin-visualizer": "^7.0
|
|
143
|
-
"
|
|
144
|
-
"
|
|
145
|
-
"
|
|
146
|
-
"vite
|
|
147
|
-
"
|
|
135
|
+
"@playwright/test": "^1.65.2",
|
|
136
|
+
"@testing-library/dom": "^10.4.2",
|
|
137
|
+
"@testing-library/react": "^16.3.3",
|
|
138
|
+
"@types/react": "^19.3.3",
|
|
139
|
+
"@types/react-dom": "^19.3.0",
|
|
140
|
+
"@types/use-sync-external-store": "^1.7.0",
|
|
141
|
+
"@vitejs/plugin-react": "^6.3.0",
|
|
142
|
+
"jsdom": "^31.0.1",
|
|
143
|
+
"react": "^19.3.1",
|
|
144
|
+
"react-dom": "^19.3.1",
|
|
145
|
+
"rollup-plugin-visualizer": "^7.3.0",
|
|
146
|
+
"ts-morph": "^28.0.0",
|
|
147
|
+
"tsx": "^4.23.15",
|
|
148
|
+
"typescript": "^7.0.2",
|
|
149
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8001",
|
|
150
|
+
"vite-plus": "1.8.0"
|
|
148
151
|
},
|
|
149
152
|
"peerDependencies": {
|
|
150
153
|
"@base-ui/react": ">=1.0.0",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { render, act, cleanup } from "@testing-library/react";
|
|
3
|
-
import {
|
|
3
|
+
import type { ComponentProps } from "react";
|
|
4
|
+
import { describe, it, expect, vi, afterEach } from "vite-plus/test";
|
|
4
5
|
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
5
6
|
import { CalendarProvider } from "./calendar-provider";
|
|
6
7
|
|
|
@@ -135,3 +136,89 @@ describe("CalendarProvider", () => {
|
|
|
135
136
|
spy.mockRestore();
|
|
136
137
|
});
|
|
137
138
|
});
|
|
139
|
+
|
|
140
|
+
describe("CalendarProvider — controlled-value integrity (M7)", () => {
|
|
141
|
+
it.each([
|
|
142
|
+
{
|
|
143
|
+
description: "range value that is not a { start, end } DateRange",
|
|
144
|
+
selectionMode: "range" as const,
|
|
145
|
+
value: "2026-03-15",
|
|
146
|
+
expected: { matches: /range/i },
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
description: "multiple value that is not an array",
|
|
150
|
+
selectionMode: "multiple" as const,
|
|
151
|
+
value: T.PlainDate.from("2026-03-15"),
|
|
152
|
+
expected: { matches: /multiple|array/i },
|
|
153
|
+
},
|
|
154
|
+
])(
|
|
155
|
+
"warns instead of silently going uncontrolled for a malformed $description",
|
|
156
|
+
({ selectionMode, value, expected }) => {
|
|
157
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
158
|
+
render(
|
|
159
|
+
// selectionMode is a union here (it.each), which can't narrow the
|
|
160
|
+
// discriminated props union — and value is intentionally wrong-shaped.
|
|
161
|
+
<CalendarProvider
|
|
162
|
+
{...({
|
|
163
|
+
temporal: T,
|
|
164
|
+
selectionMode,
|
|
165
|
+
value,
|
|
166
|
+
onValueChange: vi.fn(),
|
|
167
|
+
} as unknown as ComponentProps<typeof CalendarProvider>)}
|
|
168
|
+
>
|
|
169
|
+
<div />
|
|
170
|
+
</CalendarProvider>,
|
|
171
|
+
);
|
|
172
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
173
|
+
expect(warn).toHaveBeenCalledWith(
|
|
174
|
+
expect.stringContaining("[DatePicker]"),
|
|
175
|
+
);
|
|
176
|
+
expect(warn.mock.calls[0]?.[0]).toMatch(expected.matches);
|
|
177
|
+
warn.mockRestore();
|
|
178
|
+
},
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
it("warns once (not every render) for an out-of-bounds controlled value and never fires onValueChange", () => {
|
|
182
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
183
|
+
const onChange = vi.fn();
|
|
184
|
+
const make = (onValueChange: () => void) => (
|
|
185
|
+
<CalendarProvider
|
|
186
|
+
temporal={T}
|
|
187
|
+
format="PlainDate"
|
|
188
|
+
selectionMode="single"
|
|
189
|
+
value={T.PlainDate.from("2020-06-15")}
|
|
190
|
+
min={T.PlainDate.from("2026-01-01")}
|
|
191
|
+
max={T.PlainDate.from("2026-12-31")}
|
|
192
|
+
onValueChange={onValueChange}
|
|
193
|
+
>
|
|
194
|
+
<div />
|
|
195
|
+
</CalendarProvider>
|
|
196
|
+
);
|
|
197
|
+
const { rerender } = render(make(onChange));
|
|
198
|
+
// Re-render with a fresh inline onValueChange (the common case): the
|
|
199
|
+
// out-of-bounds effect re-runs, but the warning must not repeat.
|
|
200
|
+
rerender(make(vi.fn()));
|
|
201
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
202
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
203
|
+
warn.mockRestore();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("still clears and fires onValueChange for an out-of-bounds uncontrolled defaultValue", () => {
|
|
207
|
+
const onChange = vi.fn();
|
|
208
|
+
render(
|
|
209
|
+
<CalendarProvider
|
|
210
|
+
temporal={T}
|
|
211
|
+
format="PlainDate"
|
|
212
|
+
selectionMode="single"
|
|
213
|
+
defaultValue={T.PlainDate.from("2020-06-15")}
|
|
214
|
+
min={T.PlainDate.from("2026-01-01")}
|
|
215
|
+
max={T.PlainDate.from("2026-12-31")}
|
|
216
|
+
onValueChange={onChange}
|
|
217
|
+
>
|
|
218
|
+
<div />
|
|
219
|
+
</CalendarProvider>,
|
|
220
|
+
);
|
|
221
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
222
|
+
expect(onChange).toHaveBeenCalledWith(null, expect.anything());
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -12,11 +12,14 @@ import type {
|
|
|
12
12
|
import {
|
|
13
13
|
isDateRange,
|
|
14
14
|
tagRaw,
|
|
15
|
+
computeRangePreview,
|
|
15
16
|
computeSelectionUpdate,
|
|
16
17
|
computeSetRangeUpdate,
|
|
18
|
+
decodeRange,
|
|
17
19
|
truncateDatesForMode,
|
|
18
20
|
type SelectionResult,
|
|
19
21
|
} from "./root-selection";
|
|
22
|
+
import { useStableCallback } from "./store";
|
|
20
23
|
import type {
|
|
21
24
|
BaseRootState,
|
|
22
25
|
DateRange,
|
|
@@ -55,6 +58,10 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
55
58
|
const locale = localeProp ?? "en-US";
|
|
56
59
|
const timeZone = timeZoneProp ?? getSystemTimeZone(T);
|
|
57
60
|
const weekStartDay = weekStartDayProp ?? 0;
|
|
61
|
+
const today = useCallback(
|
|
62
|
+
() => T.Now.zonedDateTimeISO(timeZone).toPlainDate(),
|
|
63
|
+
[T, timeZone],
|
|
64
|
+
);
|
|
58
65
|
const resolvedFormat = (formatProp ?? "PlainDate") as F;
|
|
59
66
|
const selectionMode = selectionModeProp ?? "single";
|
|
60
67
|
const disabled = disabledProp ?? false;
|
|
@@ -176,14 +183,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
176
183
|
}
|
|
177
184
|
if (isRange) {
|
|
178
185
|
if (rangeValue === null) return [];
|
|
179
|
-
if (rangeValue)
|
|
180
|
-
const start =
|
|
181
|
-
rangeValue.start != null ? rawToPlain(rangeValue.start) : null;
|
|
182
|
-
const end = rangeValue.end != null ? rawToPlain(rangeValue.end) : null;
|
|
183
|
-
if (start === null && end === null) return [];
|
|
184
|
-
if (start === null && end !== null) return [end, null];
|
|
185
|
-
return [start, end];
|
|
186
|
-
}
|
|
186
|
+
if (rangeValue) return decodeRange(rangeValue, rawToPlain);
|
|
187
187
|
return undefined;
|
|
188
188
|
}
|
|
189
189
|
if (singleValue === null) return [];
|
|
@@ -199,20 +199,41 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
199
199
|
sortDates,
|
|
200
200
|
]);
|
|
201
201
|
|
|
202
|
+
// Surface (instead of silently self-managing) a controlled `value` whose
|
|
203
|
+
// shape doesn't match the selection mode — otherwise range/multiple values
|
|
204
|
+
// fall through to `undefined` above and the component quietly becomes
|
|
205
|
+
// uncontrolled, which looks like the prop being ignored. Warn once per
|
|
206
|
+
// malformed episode: this effect re-runs whenever the parent inlines a fresh
|
|
207
|
+
// `value` reference, but we only warn when entering the malformed state.
|
|
208
|
+
const warnedMalformedRef = useRef(false);
|
|
209
|
+
useEffect(() => {
|
|
210
|
+
const malformed =
|
|
211
|
+
valueProp != null &&
|
|
212
|
+
((isRange && !isDateRange<F>(valueProp)) ||
|
|
213
|
+
(isMultiple && !Array.isArray(valueProp)));
|
|
214
|
+
if (!malformed) {
|
|
215
|
+
warnedMalformedRef.current = false;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (warnedMalformedRef.current) return;
|
|
219
|
+
warnedMalformedRef.current = true;
|
|
220
|
+
if (isRange) {
|
|
221
|
+
console.warn(
|
|
222
|
+
`[DatePicker] selectionMode="range" expects \`value\` to be a { start, end } DateRange or null, but received ${Array.isArray(valueProp) ? "an array" : typeof valueProp}. Ignoring it and falling back to uncontrolled.`,
|
|
223
|
+
);
|
|
224
|
+
} else {
|
|
225
|
+
console.warn(
|
|
226
|
+
`[DatePicker] selectionMode="multiple" expects \`value\` to be an array or null, but received ${typeof valueProp}. Ignoring it and falling back to uncontrolled.`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
}, [valueProp, isRange, isMultiple]);
|
|
230
|
+
|
|
202
231
|
const defaultDates = useMemo<(Temporal.PlainDate | null)[]>(() => {
|
|
203
232
|
if (isMultiple) {
|
|
204
233
|
if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
|
|
205
234
|
return [];
|
|
206
235
|
}
|
|
207
|
-
if (rangeDefault)
|
|
208
|
-
const start =
|
|
209
|
-
rangeDefault.start != null ? rawToPlain(rangeDefault.start) : null;
|
|
210
|
-
const end =
|
|
211
|
-
rangeDefault.end != null ? rawToPlain(rangeDefault.end) : null;
|
|
212
|
-
if (start === null && end === null) return [];
|
|
213
|
-
if (start === null && end !== null) return [end, null];
|
|
214
|
-
return [start, end];
|
|
215
|
-
}
|
|
236
|
+
if (rangeDefault) return decodeRange(rangeDefault, rawToPlain);
|
|
216
237
|
if (singleDefault != null) return [rawToPlain(singleDefault)];
|
|
217
238
|
return [];
|
|
218
239
|
}, [
|
|
@@ -333,52 +354,27 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
333
354
|
>(() => {
|
|
334
355
|
if (previewRangeProp !== undefined) {
|
|
335
356
|
if (!previewRangeProp) return [undefined, undefined];
|
|
336
|
-
const rawToPlainLocal = (
|
|
337
|
-
raw: RawValueForFormat<F>,
|
|
338
|
-
): Temporal.PlainDate => {
|
|
339
|
-
const tagged = {
|
|
340
|
-
format: resolvedFormat,
|
|
341
|
-
value: raw,
|
|
342
|
-
} as DateValueObject;
|
|
343
|
-
return toZonedDateTime(tagged, timeZone, T).toPlainDate();
|
|
344
|
-
};
|
|
345
357
|
const s =
|
|
346
358
|
previewRangeProp.start != null
|
|
347
|
-
?
|
|
359
|
+
? rawToPlain(previewRangeProp.start)
|
|
348
360
|
: undefined;
|
|
349
361
|
const e =
|
|
350
362
|
previewRangeProp.end != null
|
|
351
|
-
?
|
|
363
|
+
? rawToPlain(previewRangeProp.end)
|
|
352
364
|
: undefined;
|
|
353
365
|
return [s, e];
|
|
354
366
|
}
|
|
355
367
|
if (!hoveredDate || !isRange) return [undefined, undefined];
|
|
356
|
-
const
|
|
357
|
-
|
|
368
|
+
const preview = computeRangePreview({
|
|
369
|
+
hoveredDate,
|
|
358
370
|
readOnly,
|
|
359
371
|
isDateDisabled,
|
|
360
|
-
isMultiple: false,
|
|
361
|
-
isRange: true,
|
|
362
372
|
committedDates,
|
|
363
|
-
committedStart,
|
|
364
|
-
committedEnd,
|
|
365
|
-
selectedZdt: undefined,
|
|
366
373
|
rangeMode,
|
|
367
374
|
allowRangeReversal,
|
|
368
|
-
sortDates,
|
|
369
|
-
currentSingleFormatted,
|
|
370
|
-
currentRangeFormatted,
|
|
371
|
-
currentMultipleFormatted,
|
|
372
|
-
resolvedFormat,
|
|
373
|
-
timeZone,
|
|
374
375
|
T,
|
|
375
376
|
});
|
|
376
|
-
|
|
377
|
-
return [undefined, undefined];
|
|
378
|
-
const ps = result.newDates[0];
|
|
379
|
-
const pe = result.newDates.length > 1 ? result.newDates[1] : ps;
|
|
380
|
-
if (ps == null || pe == null) return [undefined, undefined];
|
|
381
|
-
return [ps, pe];
|
|
377
|
+
return preview ?? [undefined, undefined];
|
|
382
378
|
}, [
|
|
383
379
|
previewRangeProp,
|
|
384
380
|
hoveredDate,
|
|
@@ -386,16 +382,9 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
386
382
|
readOnly,
|
|
387
383
|
isDateDisabled,
|
|
388
384
|
committedDates,
|
|
389
|
-
committedStart,
|
|
390
|
-
committedEnd,
|
|
391
385
|
rangeMode,
|
|
392
386
|
allowRangeReversal,
|
|
393
|
-
|
|
394
|
-
currentSingleFormatted,
|
|
395
|
-
currentRangeFormatted,
|
|
396
|
-
currentMultipleFormatted,
|
|
397
|
-
resolvedFormat,
|
|
398
|
-
timeZone,
|
|
387
|
+
rawToPlain,
|
|
399
388
|
T,
|
|
400
389
|
]);
|
|
401
390
|
|
|
@@ -423,29 +412,48 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
423
412
|
]);
|
|
424
413
|
|
|
425
414
|
// --- Out-of-bounds cleanup for single mode ---
|
|
415
|
+
const warnedOutOfBoundsRef = useRef(false);
|
|
426
416
|
useEffect(() => {
|
|
427
417
|
if (isRange || isMultiple) return;
|
|
428
418
|
const start = committedStart;
|
|
429
|
-
if (!start)
|
|
419
|
+
if (!start) {
|
|
420
|
+
warnedOutOfBoundsRef.current = false;
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
430
423
|
const outOfBounds =
|
|
431
424
|
(minValue && T.PlainDate.compare(start, minValue) < 0) ||
|
|
432
425
|
(maxValue && T.PlainDate.compare(start, maxValue) > 0);
|
|
433
|
-
if (outOfBounds) {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
426
|
+
if (!outOfBounds) {
|
|
427
|
+
warnedOutOfBoundsRef.current = false;
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (isControlled) {
|
|
431
|
+
// The parent owns a controlled `value`. Don't clear it or fire
|
|
432
|
+
// onValueChange on mount — that fights the parent and risks a render
|
|
433
|
+
// loop. Warn once (this effect re-runs whenever the parent inlines
|
|
434
|
+
// `onValueChange`) so they can reconcile `value` with min/max.
|
|
435
|
+
if (!warnedOutOfBoundsRef.current) {
|
|
436
|
+
warnedOutOfBoundsRef.current = true;
|
|
437
|
+
console.warn(
|
|
438
|
+
`[DatePicker] The controlled \`value\` is outside the min/max range. It stays selected (rendered disabled) until you update \`value\`; the component will not clear it for you.`,
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
return;
|
|
448
442
|
}
|
|
443
|
+
const prev = currentSingleFormatted();
|
|
444
|
+
commitSelection({
|
|
445
|
+
newDates: [],
|
|
446
|
+
fireCallback: (onValueChange) => {
|
|
447
|
+
(
|
|
448
|
+
onValueChange as
|
|
449
|
+
| ((
|
|
450
|
+
v: RawValueForFormat<F> | null,
|
|
451
|
+
m: { date: undefined; previous: RawValueForFormat<F> | null },
|
|
452
|
+
) => void)
|
|
453
|
+
| undefined
|
|
454
|
+
)?.(null, { date: undefined, previous: prev });
|
|
455
|
+
},
|
|
456
|
+
});
|
|
449
457
|
}, [
|
|
450
458
|
minValue,
|
|
451
459
|
maxValue,
|
|
@@ -455,10 +463,11 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
455
463
|
T,
|
|
456
464
|
isRange,
|
|
457
465
|
isMultiple,
|
|
466
|
+
isControlled,
|
|
458
467
|
]);
|
|
459
468
|
|
|
460
469
|
// --- onSelect ---
|
|
461
|
-
const
|
|
470
|
+
const onSelectLatest = useCallback(
|
|
462
471
|
(date: Temporal.PlainDate) => {
|
|
463
472
|
const result = computeSelectionUpdate<F>({
|
|
464
473
|
date,
|
|
@@ -507,7 +516,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
507
516
|
);
|
|
508
517
|
|
|
509
518
|
// --- setRange ---
|
|
510
|
-
const
|
|
519
|
+
const setRangeLatest = useCallback(
|
|
511
520
|
(start: Temporal.PlainDate, end: Temporal.PlainDate) => {
|
|
512
521
|
const result = computeSetRangeUpdate<F>({
|
|
513
522
|
start,
|
|
@@ -537,6 +546,11 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
537
546
|
],
|
|
538
547
|
);
|
|
539
548
|
|
|
549
|
+
// Stable identities: both close over the committed selection, and the stable
|
|
550
|
+
// context holding them would otherwise change on every selection.
|
|
551
|
+
const onSelect = useStableCallback(onSelectLatest);
|
|
552
|
+
const setRange = useStableCallback(setRangeLatest);
|
|
553
|
+
|
|
540
554
|
// --- Selected dates (non-null) ---
|
|
541
555
|
const nonNullDates = useMemo(
|
|
542
556
|
() => committedDates.filter((d): d is Temporal.PlainDate => d != null),
|
|
@@ -555,6 +569,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
555
569
|
...(minValue !== undefined && { minValue }),
|
|
556
570
|
...(maxValue !== undefined && { maxValue }),
|
|
557
571
|
timeZone,
|
|
572
|
+
today,
|
|
558
573
|
locale,
|
|
559
574
|
temporal: T,
|
|
560
575
|
weekStartDay,
|
|
@@ -573,6 +588,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
573
588
|
minValue,
|
|
574
589
|
maxValue,
|
|
575
590
|
timeZone,
|
|
591
|
+
today,
|
|
576
592
|
locale,
|
|
577
593
|
T,
|
|
578
594
|
weekStartDay,
|
package/src/calendar-types.ts
CHANGED
|
@@ -188,6 +188,8 @@ export interface CalendarStableContextValue {
|
|
|
188
188
|
maxValue?: Temporal.PlainDate;
|
|
189
189
|
/** Resolved IANA time zone. */
|
|
190
190
|
timeZone: string;
|
|
191
|
+
/** Today's date in the resolved {@link timeZone}. Reads the clock on every call. */
|
|
192
|
+
today: () => Temporal.PlainDate;
|
|
191
193
|
/** Resolved BCP 47 locale. */
|
|
192
194
|
locale: string;
|
|
193
195
|
/** Resolved Temporal namespace. */
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { describe, expect, it } from "vite-plus/test";
|
|
3
|
+
import { computeDayCellState, dayCellStateEqual } from "./day-cell-state";
|
|
4
|
+
import type { DayCellInputs } from "./day-cell-state";
|
|
5
|
+
import type { OutsideDays } from "./types";
|
|
6
|
+
|
|
7
|
+
const T = Temporal;
|
|
8
|
+
const d = (iso: string) => T.PlainDate.from(iso);
|
|
9
|
+
|
|
10
|
+
const inputs = (overrides: Partial<DayCellInputs> = {}): DayCellInputs => ({
|
|
11
|
+
T,
|
|
12
|
+
today: d("2026-03-10"),
|
|
13
|
+
orientation: "horizontal",
|
|
14
|
+
rootState: {} as DayCellInputs["rootState"],
|
|
15
|
+
selectionMode: "single",
|
|
16
|
+
selectedDates: [],
|
|
17
|
+
rangeStart: undefined,
|
|
18
|
+
rangeEnd: undefined,
|
|
19
|
+
previewStart: undefined,
|
|
20
|
+
previewEnd: undefined,
|
|
21
|
+
disabled: false,
|
|
22
|
+
isDateDisabled: undefined,
|
|
23
|
+
focusedDate: d("2026-03-15"),
|
|
24
|
+
tabTargetDate: d("2026-03-15"),
|
|
25
|
+
outsideDays: "enabled",
|
|
26
|
+
...overrides,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const march = { columnIndex: 0, month: { year: 2026, month: 3 } };
|
|
30
|
+
|
|
31
|
+
describe("computeDayCellState", () => {
|
|
32
|
+
it.each([
|
|
33
|
+
{
|
|
34
|
+
description:
|
|
35
|
+
"today, selected, focused and tab target in the viewed month",
|
|
36
|
+
date: "2026-03-10",
|
|
37
|
+
overrides: {
|
|
38
|
+
selectedDates: [d("2026-03-10")],
|
|
39
|
+
focusedDate: d("2026-03-10"),
|
|
40
|
+
tabTargetDate: d("2026-03-10"),
|
|
41
|
+
},
|
|
42
|
+
expected: {
|
|
43
|
+
today: true,
|
|
44
|
+
selected: true,
|
|
45
|
+
focused: true,
|
|
46
|
+
isTabTarget: true,
|
|
47
|
+
outsideMonth: false,
|
|
48
|
+
disabled: false,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
description: "isDateDisabled disables the cell",
|
|
53
|
+
date: "2026-03-12",
|
|
54
|
+
overrides: { isDateDisabled: (x: Temporal.PlainDate) => x.day === 12 },
|
|
55
|
+
expected: { disabled: true, selected: false },
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
description: "range mode never reports selected, only range state",
|
|
59
|
+
date: "2026-03-12",
|
|
60
|
+
overrides: {
|
|
61
|
+
selectionMode: "range" as const,
|
|
62
|
+
selectedDates: [d("2026-03-12")],
|
|
63
|
+
rangeStart: d("2026-03-11"),
|
|
64
|
+
rangeEnd: d("2026-03-13"),
|
|
65
|
+
},
|
|
66
|
+
expected: {
|
|
67
|
+
selected: false,
|
|
68
|
+
inRange: true,
|
|
69
|
+
rangeBoundary: false,
|
|
70
|
+
rangeIndex: 1,
|
|
71
|
+
rangeLength: 3,
|
|
72
|
+
rangeHasStart: true,
|
|
73
|
+
rangeHasEnd: true,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
description: "half-open range: only the start is set",
|
|
78
|
+
date: "2026-03-11",
|
|
79
|
+
overrides: {
|
|
80
|
+
selectionMode: "range" as const,
|
|
81
|
+
rangeStart: d("2026-03-11"),
|
|
82
|
+
},
|
|
83
|
+
expected: {
|
|
84
|
+
rangeStart: true,
|
|
85
|
+
rangeEnd: false,
|
|
86
|
+
inRange: true,
|
|
87
|
+
rangeIndex: 0,
|
|
88
|
+
rangeLength: 1,
|
|
89
|
+
rangeHasStart: true,
|
|
90
|
+
rangeHasEnd: false,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
description: "preview range marks its boundary",
|
|
95
|
+
date: "2026-03-14",
|
|
96
|
+
overrides: {
|
|
97
|
+
selectionMode: "range" as const,
|
|
98
|
+
previewStart: d("2026-03-12"),
|
|
99
|
+
previewEnd: d("2026-03-14"),
|
|
100
|
+
},
|
|
101
|
+
expected: {
|
|
102
|
+
rangePreviewEnd: true,
|
|
103
|
+
rangePreviewBoundary: true,
|
|
104
|
+
rangePreviewInRange: true,
|
|
105
|
+
rangePreviewIndex: 2,
|
|
106
|
+
rangePreviewLength: 3,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
])("$description", ({ date, overrides, expected }) => {
|
|
110
|
+
const state = computeDayCellState(d(date), march, inputs(overrides));
|
|
111
|
+
expect(state).toMatchObject(expected);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it.each([
|
|
115
|
+
{
|
|
116
|
+
description: "enabled: outside day is a normal cell",
|
|
117
|
+
outsideDays: "enabled" as OutsideDays,
|
|
118
|
+
expected: {
|
|
119
|
+
outsideMonth: true,
|
|
120
|
+
hidden: false,
|
|
121
|
+
disabled: false,
|
|
122
|
+
inRange: true,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
description: "readOnly: outside day is disabled but keeps range state",
|
|
127
|
+
outsideDays: "readOnly" as OutsideDays,
|
|
128
|
+
expected: {
|
|
129
|
+
outsideMonth: true,
|
|
130
|
+
hidden: false,
|
|
131
|
+
disabled: true,
|
|
132
|
+
inRange: true,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
description: "disabled: outside day is disabled and drops range state",
|
|
137
|
+
outsideDays: "disabled" as OutsideDays,
|
|
138
|
+
expected: {
|
|
139
|
+
outsideMonth: true,
|
|
140
|
+
hidden: false,
|
|
141
|
+
disabled: true,
|
|
142
|
+
inRange: false,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
description: "hidden: outside day is hidden, not today, no range state",
|
|
147
|
+
outsideDays: "hidden" as OutsideDays,
|
|
148
|
+
expected: {
|
|
149
|
+
outsideMonth: true,
|
|
150
|
+
hidden: true,
|
|
151
|
+
disabled: true,
|
|
152
|
+
today: false,
|
|
153
|
+
inRange: false,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
])("outside day, $description", ({ outsideDays, expected }) => {
|
|
157
|
+
const state = computeDayCellState(
|
|
158
|
+
d("2026-04-02"),
|
|
159
|
+
march,
|
|
160
|
+
inputs({
|
|
161
|
+
outsideDays,
|
|
162
|
+
selectionMode: "range",
|
|
163
|
+
rangeStart: d("2026-04-01"),
|
|
164
|
+
rangeEnd: d("2026-04-03"),
|
|
165
|
+
selectedDates: [d("2026-04-02")],
|
|
166
|
+
focusedDate: d("2026-04-02"),
|
|
167
|
+
tabTargetDate: d("2026-04-02"),
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
expect(state).toMatchObject({
|
|
171
|
+
...expected,
|
|
172
|
+
selected: false,
|
|
173
|
+
focused: outsideDays === "enabled",
|
|
174
|
+
isTabTarget: outsideDays === "enabled",
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe("dayCellStateEqual", () => {
|
|
180
|
+
const state = () => computeDayCellState(d("2026-03-12"), march, inputs());
|
|
181
|
+
|
|
182
|
+
it.each([
|
|
183
|
+
{ description: "identical states", change: {}, expected: true },
|
|
184
|
+
{
|
|
185
|
+
description: "root/orientation differ",
|
|
186
|
+
change: { orientation: "vertical" as const },
|
|
187
|
+
expected: true,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
description: "selected differs",
|
|
191
|
+
change: { selected: true },
|
|
192
|
+
expected: false,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
description: "tab target differs",
|
|
196
|
+
change: { isTabTarget: true },
|
|
197
|
+
expected: false,
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
description: "range index differs",
|
|
201
|
+
change: { rangeIndex: 2 },
|
|
202
|
+
expected: false,
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
description: "preview length differs",
|
|
206
|
+
change: { rangePreviewLength: 4 },
|
|
207
|
+
expected: false,
|
|
208
|
+
},
|
|
209
|
+
])("$description", ({ change, expected }) => {
|
|
210
|
+
expect(dayCellStateEqual(state(), { ...state(), ...change })).toBe(
|
|
211
|
+
expected,
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
});
|