@klinking/colander 3.1.0-alpha.1 → 3.1.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/formats/date.cjs +2 -3
- package/dist/formats/date.d.cts +10 -14
- package/dist/formats/date.d.mts +10 -14
- package/dist/formats/date.mjs +2 -3
- package/dist/formats/object.cjs +2 -3
- package/dist/formats/object.d.cts +10 -14
- package/dist/formats/object.d.mts +10 -14
- package/dist/formats/object.mjs +2 -3
- package/dist/formats/plain-date-time.cjs +2 -3
- package/dist/formats/plain-date-time.d.cts +10 -14
- package/dist/formats/plain-date-time.d.mts +10 -14
- package/dist/formats/plain-date-time.mjs +2 -3
- package/dist/formats/plain-date.cjs +2 -3
- package/dist/formats/plain-date.d.cts +10 -14
- package/dist/formats/plain-date.d.mts +10 -14
- package/dist/formats/plain-date.mjs +2 -3
- package/dist/formats/plain-month-day.cjs +2 -3
- package/dist/formats/plain-month-day.d.cts +10 -14
- package/dist/formats/plain-month-day.d.mts +10 -14
- package/dist/formats/plain-month-day.mjs +2 -3
- package/dist/formats/plain-year-month.cjs +2 -3
- package/dist/formats/plain-year-month.d.cts +10 -14
- package/dist/formats/plain-year-month.d.mts +10 -14
- package/dist/formats/plain-year-month.mjs +2 -3
- package/dist/formats/zoned-date-time.cjs +2 -3
- package/dist/formats/zoned-date-time.d.cts +10 -14
- package/dist/formats/zoned-date-time.d.mts +10 -14
- package/dist/formats/zoned-date-time.mjs +2 -3
- package/dist/{weeks-view-context-CNAq2Hiu.d.cts → index-K1_zOIpa.d.mts} +198 -114
- package/dist/{weeks-view-context-CsP3PBiu.d.mts → index-bRoYX03d.d.cts} +198 -114
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -11
- package/dist/index.d.mts +2 -11
- package/dist/index.mjs +1 -2
- package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-3UTRiTUP.cjs} +1102 -952
- package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-BoHqK5fq.mjs} +1102 -951
- package/package.json +21 -18
- package/src/calendar-provider.test.tsx +88 -1
- package/src/calendar-provider.tsx +89 -73
- package/src/calendar-types.ts +2 -0
- package/src/compute-weeks-in-window.test.ts +1 -1
- package/src/day-cell-state.test.ts +214 -0
- package/src/day-cell-state.ts +244 -0
- package/src/day-cell-store.test.tsx +273 -0
- package/src/day-cell-store.tsx +204 -0
- package/src/day-cell.test.tsx +73 -90
- package/src/day-cell.tsx +106 -444
- package/src/drag-handle.tsx +2 -2
- package/src/formats/date.ts +2 -0
- package/src/formats/formats.test.ts +1 -1
- package/src/formats/object.ts +2 -0
- package/src/formats/plain-date-time.ts +2 -0
- package/src/formats/plain-date.ts +2 -0
- package/src/formats/plain-month-day.ts +2 -0
- package/src/formats/plain-year-month.ts +2 -0
- package/src/formats/zoned-date-time.ts +2 -0
- package/src/grid-header.tsx +3 -3
- package/src/grid.event-perf.test.tsx +7 -1
- package/src/grid.perf.test.tsx +7 -1
- package/src/grid.tsx +38 -77
- package/src/index.ts +2 -0
- package/src/keyboard.test.ts +1 -1
- package/src/month-separator.test.tsx +1 -1
- package/src/month-view-navigation.ts +180 -0
- package/src/month-view-types.ts +3 -0
- package/src/month-view.bounds.test.tsx +136 -0
- package/src/month-view.test.tsx +89 -1
- package/src/month-view.tsx +74 -217
- package/src/month-window.test.ts +207 -0
- package/src/month-window.ts +93 -0
- package/src/navigation.tsx +18 -76
- package/src/out-of-range.test.ts +34 -54
- package/src/out-of-range.ts +47 -123
- package/src/range-preview.test.ts +120 -0
- package/src/range-preview.tsx +3 -5
- package/src/resolve-first-week.test.ts +1 -1
- package/src/root-selection.ts +99 -97
- package/src/selected-range.tsx +3 -5
- package/src/store.ts +72 -0
- package/src/today.test.tsx +105 -0
- package/src/types.ts +24 -1
- package/src/utils.bench.ts +158 -106
- package/src/utils.test.ts +26 -1
- package/src/utils.ts +40 -9
- package/src/view-context.test.tsx +126 -0
- package/src/view-context.ts +45 -0
- package/src/week-number.test.tsx +76 -0
- package/src/week-number.tsx +48 -19
- package/src/weeks-grid.tsx +20 -10
- package/src/weeks-keyboard.test.ts +1 -1
- package/src/weeks-navigation.tsx +15 -31
- package/src/weeks-view-state.ts +67 -101
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-view.bounds.test.tsx +246 -0
- package/src/weeks-view.test.tsx +116 -14
- package/src/weeks-view.tsx +24 -52
- package/src/weeks-window.test.ts +131 -0
- package/src/weeks-window.ts +102 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { GridOrientation } from "./context";
|
|
3
|
+
import type {
|
|
4
|
+
DayCellTemplateState,
|
|
5
|
+
OutsideDays,
|
|
6
|
+
TemporalNamespace,
|
|
7
|
+
} from "./types";
|
|
8
|
+
import { isInRange as isInRangeUtil } from "./utils";
|
|
9
|
+
|
|
10
|
+
/** A day cell's state: the public {@link DayCellTemplateState} plus the roving-tabindex flag. */
|
|
11
|
+
export type DayCellState = DayCellTemplateState & { isTabTarget: boolean };
|
|
12
|
+
|
|
13
|
+
type RangeState = Pick<
|
|
14
|
+
DayCellTemplateState,
|
|
15
|
+
| "rangeStart"
|
|
16
|
+
| "rangeEnd"
|
|
17
|
+
| "rangeBoundary"
|
|
18
|
+
| "inRange"
|
|
19
|
+
| "rangeIndex"
|
|
20
|
+
| "rangeLength"
|
|
21
|
+
| "rangeHasStart"
|
|
22
|
+
| "rangeHasEnd"
|
|
23
|
+
| "rangePreviewStart"
|
|
24
|
+
| "rangePreviewEnd"
|
|
25
|
+
| "rangePreviewBoundary"
|
|
26
|
+
| "rangePreviewInRange"
|
|
27
|
+
| "rangePreviewIndex"
|
|
28
|
+
| "rangePreviewLength"
|
|
29
|
+
| "rangePreviewHasStart"
|
|
30
|
+
| "rangePreviewHasEnd"
|
|
31
|
+
>;
|
|
32
|
+
|
|
33
|
+
const NO_RANGE: RangeState = {
|
|
34
|
+
rangeStart: false,
|
|
35
|
+
rangeEnd: false,
|
|
36
|
+
rangeBoundary: false,
|
|
37
|
+
inRange: false,
|
|
38
|
+
rangeIndex: false,
|
|
39
|
+
rangeLength: false,
|
|
40
|
+
rangeHasStart: false,
|
|
41
|
+
rangeHasEnd: false,
|
|
42
|
+
rangePreviewStart: false,
|
|
43
|
+
rangePreviewEnd: false,
|
|
44
|
+
rangePreviewBoundary: false,
|
|
45
|
+
rangePreviewInRange: false,
|
|
46
|
+
rangePreviewIndex: false,
|
|
47
|
+
rangePreviewLength: false,
|
|
48
|
+
rangePreviewHasStart: false,
|
|
49
|
+
rangePreviewHasEnd: false,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Everything that is the same for every cell of one render. */
|
|
53
|
+
export interface DayCellInputs {
|
|
54
|
+
T: TemporalNamespace;
|
|
55
|
+
today: Temporal.PlainDate;
|
|
56
|
+
orientation: GridOrientation;
|
|
57
|
+
rootState: DayCellTemplateState["root"];
|
|
58
|
+
selectionMode: "single" | "range" | "multiple";
|
|
59
|
+
selectedDates: Temporal.PlainDate[];
|
|
60
|
+
rangeStart: Temporal.PlainDate | undefined;
|
|
61
|
+
rangeEnd: Temporal.PlainDate | undefined;
|
|
62
|
+
previewStart: Temporal.PlainDate | undefined;
|
|
63
|
+
previewEnd: Temporal.PlainDate | undefined;
|
|
64
|
+
disabled: boolean;
|
|
65
|
+
isDateDisabled: ((date: Temporal.PlainDate) => boolean) | undefined;
|
|
66
|
+
focusedDate: Temporal.PlainDate;
|
|
67
|
+
tabTargetDate: Temporal.PlainDate;
|
|
68
|
+
outsideDays: OutsideDays;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Where one cell sits: its column and the month the grid it is in belongs to. */
|
|
72
|
+
export interface DayCellPosition {
|
|
73
|
+
columnIndex: number;
|
|
74
|
+
month: { year: number; month: number };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function computeRangeState(
|
|
78
|
+
date: Temporal.PlainDate,
|
|
79
|
+
inputs: DayCellInputs,
|
|
80
|
+
): RangeState {
|
|
81
|
+
const { T, selectionMode, rangeStart, rangeEnd, previewStart, previewEnd } =
|
|
82
|
+
inputs;
|
|
83
|
+
if (selectionMode !== "range") return NO_RANGE;
|
|
84
|
+
|
|
85
|
+
const isRangeStart = rangeStart
|
|
86
|
+
? T.PlainDate.compare(date, rangeStart) === 0
|
|
87
|
+
: false;
|
|
88
|
+
const isRangeEnd = rangeEnd
|
|
89
|
+
? T.PlainDate.compare(date, rangeEnd) === 0
|
|
90
|
+
: false;
|
|
91
|
+
const inRange = isInRangeUtil(date, rangeStart, rangeEnd, T) !== false;
|
|
92
|
+
const effectiveStart = rangeStart ?? rangeEnd;
|
|
93
|
+
const effectiveEnd = rangeEnd ?? rangeStart;
|
|
94
|
+
|
|
95
|
+
const isPreviewStart = previewStart
|
|
96
|
+
? T.PlainDate.compare(date, previewStart) === 0
|
|
97
|
+
: false;
|
|
98
|
+
const isPreviewEnd = previewEnd
|
|
99
|
+
? T.PlainDate.compare(date, previewEnd) === 0
|
|
100
|
+
: false;
|
|
101
|
+
const inPreview =
|
|
102
|
+
previewStart && previewEnd
|
|
103
|
+
? isInRangeUtil(date, previewStart, previewEnd, T) !== false
|
|
104
|
+
: false;
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
rangeStart: isRangeStart,
|
|
108
|
+
rangeEnd: isRangeEnd,
|
|
109
|
+
rangeBoundary: isRangeStart || isRangeEnd,
|
|
110
|
+
inRange,
|
|
111
|
+
rangeIndex:
|
|
112
|
+
inRange && effectiveStart ? date.since(effectiveStart).days : false,
|
|
113
|
+
rangeLength:
|
|
114
|
+
inRange && effectiveStart && effectiveEnd
|
|
115
|
+
? effectiveEnd.since(effectiveStart).days + 1
|
|
116
|
+
: false,
|
|
117
|
+
rangeHasStart: inRange && rangeStart !== undefined,
|
|
118
|
+
rangeHasEnd: inRange && rangeEnd !== undefined,
|
|
119
|
+
rangePreviewStart: isPreviewStart,
|
|
120
|
+
rangePreviewEnd: isPreviewEnd,
|
|
121
|
+
rangePreviewBoundary: isPreviewStart || isPreviewEnd,
|
|
122
|
+
rangePreviewInRange: inPreview,
|
|
123
|
+
rangePreviewIndex:
|
|
124
|
+
inPreview && previewStart ? date.since(previewStart).days : false,
|
|
125
|
+
rangePreviewLength:
|
|
126
|
+
inPreview && previewStart && previewEnd
|
|
127
|
+
? previewEnd.since(previewStart).days + 1
|
|
128
|
+
: false,
|
|
129
|
+
rangePreviewHasStart: inPreview && previewStart !== undefined,
|
|
130
|
+
rangePreviewHasEnd: inPreview && previewEnd !== undefined,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Derives the state of one day cell. Pure function, no hooks. */
|
|
135
|
+
export function computeDayCellState(
|
|
136
|
+
date: Temporal.PlainDate,
|
|
137
|
+
position: DayCellPosition,
|
|
138
|
+
inputs: DayCellInputs,
|
|
139
|
+
): DayCellState {
|
|
140
|
+
const { T, outsideDays } = inputs;
|
|
141
|
+
const { columnIndex, month } = position;
|
|
142
|
+
const isCurrentMonth = date.year === month.year && date.month === month.month;
|
|
143
|
+
const base = {
|
|
144
|
+
root: inputs.rootState,
|
|
145
|
+
date,
|
|
146
|
+
columnIndex,
|
|
147
|
+
orientation: inputs.orientation,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
if (!isCurrentMonth && outsideDays === "hidden") {
|
|
151
|
+
return {
|
|
152
|
+
...base,
|
|
153
|
+
...NO_RANGE,
|
|
154
|
+
outsideMonth: true,
|
|
155
|
+
hidden: true,
|
|
156
|
+
selected: false,
|
|
157
|
+
today: false,
|
|
158
|
+
disabled: true,
|
|
159
|
+
focused: false,
|
|
160
|
+
isTabTarget: false,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const isToday = T.PlainDate.compare(date, inputs.today) === 0;
|
|
165
|
+
|
|
166
|
+
if (!isCurrentMonth && outsideDays !== "enabled") {
|
|
167
|
+
// `readOnly` outside days keep their range state; `disabled` ones don't.
|
|
168
|
+
const suppressRange = outsideDays === "disabled";
|
|
169
|
+
return {
|
|
170
|
+
...base,
|
|
171
|
+
...(suppressRange ? NO_RANGE : computeRangeState(date, inputs)),
|
|
172
|
+
outsideMonth: true,
|
|
173
|
+
hidden: false,
|
|
174
|
+
selected: false,
|
|
175
|
+
today: isToday,
|
|
176
|
+
disabled: true,
|
|
177
|
+
focused: false,
|
|
178
|
+
isTabTarget: false,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
...base,
|
|
184
|
+
...computeRangeState(date, inputs),
|
|
185
|
+
outsideMonth: !isCurrentMonth,
|
|
186
|
+
hidden: false,
|
|
187
|
+
selected:
|
|
188
|
+
inputs.selectionMode !== "range"
|
|
189
|
+
? inputs.selectedDates.some((d) => T.PlainDate.compare(d, date) === 0)
|
|
190
|
+
: false,
|
|
191
|
+
today: isToday,
|
|
192
|
+
disabled: inputs.disabled || (inputs.isDateDisabled?.(date) ?? false),
|
|
193
|
+
focused: T.PlainDate.compare(date, inputs.focusedDate) === 0,
|
|
194
|
+
isTabTarget: T.PlainDate.compare(date, inputs.tabTargetDate) === 0,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Every field of {@link DayCellState} that decides whether a cell re-renders.
|
|
200
|
+
* `root`, `date`, `columnIndex` and `orientation` are not listed: the memo
|
|
201
|
+
* compares `date`/`columnIndex` as props, and the other two never change a
|
|
202
|
+
* cell on their own. Adding a field to the state fails to compile until it is
|
|
203
|
+
* listed here.
|
|
204
|
+
*/
|
|
205
|
+
const COMPARED_STATE: Record<
|
|
206
|
+
Exclude<keyof DayCellState, "root" | "date" | "columnIndex" | "orientation">,
|
|
207
|
+
true
|
|
208
|
+
> = {
|
|
209
|
+
outsideMonth: true,
|
|
210
|
+
hidden: true,
|
|
211
|
+
selected: true,
|
|
212
|
+
today: true,
|
|
213
|
+
disabled: true,
|
|
214
|
+
focused: true,
|
|
215
|
+
rangeStart: true,
|
|
216
|
+
rangeEnd: true,
|
|
217
|
+
rangeBoundary: true,
|
|
218
|
+
inRange: true,
|
|
219
|
+
rangeIndex: true,
|
|
220
|
+
rangeLength: true,
|
|
221
|
+
rangeHasStart: true,
|
|
222
|
+
rangeHasEnd: true,
|
|
223
|
+
rangePreviewStart: true,
|
|
224
|
+
rangePreviewEnd: true,
|
|
225
|
+
rangePreviewBoundary: true,
|
|
226
|
+
rangePreviewInRange: true,
|
|
227
|
+
rangePreviewIndex: true,
|
|
228
|
+
rangePreviewLength: true,
|
|
229
|
+
rangePreviewHasStart: true,
|
|
230
|
+
rangePreviewHasEnd: true,
|
|
231
|
+
isTabTarget: true,
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const COMPARED_KEYS = Object.keys(
|
|
235
|
+
COMPARED_STATE,
|
|
236
|
+
) as (keyof typeof COMPARED_STATE)[];
|
|
237
|
+
|
|
238
|
+
/** Whether two cell states render the same cell. */
|
|
239
|
+
export function dayCellStateEqual(a: DayCellState, b: DayCellState): boolean {
|
|
240
|
+
for (const key of COMPARED_KEYS) {
|
|
241
|
+
if (a[key] !== b[key]) return false;
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
+
import { act, fireEvent, render } from "@testing-library/react";
|
|
4
|
+
import { Suspense, startTransition, useState, type ReactNode } from "react";
|
|
5
|
+
import { describe, expect, it } from "vite-plus/test";
|
|
6
|
+
import { DayButton, DayCellTemplate } from "./day-cell";
|
|
7
|
+
import { Grid, GridBody, WeekTemplate } from "./grid";
|
|
8
|
+
import { MonthView } from "./month-view";
|
|
9
|
+
import type { DayCellTemplateState } from "./types";
|
|
10
|
+
|
|
11
|
+
const temporal = {
|
|
12
|
+
Now: Temporal.Now,
|
|
13
|
+
PlainDate: Temporal.PlainDate,
|
|
14
|
+
PlainDateTime: Temporal.PlainDateTime,
|
|
15
|
+
PlainMonthDay: Temporal.PlainMonthDay,
|
|
16
|
+
PlainYearMonth: Temporal.PlainYearMonth,
|
|
17
|
+
};
|
|
18
|
+
const base = {
|
|
19
|
+
temporal,
|
|
20
|
+
locale: "en-US",
|
|
21
|
+
timeZone: "America/New_York",
|
|
22
|
+
format: "PlainDate" as const,
|
|
23
|
+
};
|
|
24
|
+
const d = (s: string) => Temporal.PlainDate.from(s);
|
|
25
|
+
|
|
26
|
+
/** March 2026 without fixed weeks renders 5 rows of 7 cells. */
|
|
27
|
+
const MARCH_CELLS = 35;
|
|
28
|
+
|
|
29
|
+
/** Counts render-function calls; cleared between the setup and the action. */
|
|
30
|
+
let renders = 0;
|
|
31
|
+
|
|
32
|
+
const readsNothing = (props: object) => {
|
|
33
|
+
renders++;
|
|
34
|
+
return <td {...props} />;
|
|
35
|
+
};
|
|
36
|
+
const readsRoot = (props: object, state: DayCellTemplateState) => {
|
|
37
|
+
renders++;
|
|
38
|
+
return <td {...props} data-root-selected={String(state.root.selected)} />;
|
|
39
|
+
};
|
|
40
|
+
const buttonReadsNothing = (props: object) => {
|
|
41
|
+
renders++;
|
|
42
|
+
return <button {...props} />;
|
|
43
|
+
};
|
|
44
|
+
const buttonReadsRoot = (props: object, state: DayCellTemplateState) => {
|
|
45
|
+
renders++;
|
|
46
|
+
return <button {...props} data-root-selected={String(state.root.selected)} />;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function Cells(props: { children: ReactNode }) {
|
|
50
|
+
return (
|
|
51
|
+
<Grid>
|
|
52
|
+
<GridBody>
|
|
53
|
+
<WeekTemplate>{props.children}</WeekTemplate>
|
|
54
|
+
</GridBody>
|
|
55
|
+
</Grid>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function rootValues(container: HTMLElement) {
|
|
60
|
+
return new Set(
|
|
61
|
+
[...container.querySelectorAll("[data-root-selected]")].map((el) =>
|
|
62
|
+
el.getAttribute("data-root-selected"),
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe("day cells subscribe to the store per date", () => {
|
|
68
|
+
it.each([
|
|
69
|
+
{
|
|
70
|
+
description:
|
|
71
|
+
"a cell render that ignores root re-renders the two cells that changed",
|
|
72
|
+
cells: <DayCellTemplate render={readsNothing} />,
|
|
73
|
+
expected: { renders: 2, rootValues: [] },
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
description:
|
|
77
|
+
"a cell render that reads root re-renders every cell, none stale",
|
|
78
|
+
cells: <DayCellTemplate render={readsRoot} />,
|
|
79
|
+
expected: { renders: MARCH_CELLS, rootValues: ["2026-03-20"] },
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
description:
|
|
83
|
+
"a DayButton child that ignores root re-renders the two buttons that changed",
|
|
84
|
+
cells: (
|
|
85
|
+
<DayCellTemplate>
|
|
86
|
+
<DayButton render={buttonReadsNothing} />
|
|
87
|
+
</DayCellTemplate>
|
|
88
|
+
),
|
|
89
|
+
expected: { renders: 2, rootValues: [] },
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
description:
|
|
93
|
+
"a DayButton child that reads root re-renders every button, none stale",
|
|
94
|
+
cells: (
|
|
95
|
+
<DayCellTemplate>
|
|
96
|
+
<DayButton render={buttonReadsRoot} />
|
|
97
|
+
</DayCellTemplate>
|
|
98
|
+
),
|
|
99
|
+
expected: { renders: MARCH_CELLS, rootValues: ["2026-03-20"] },
|
|
100
|
+
},
|
|
101
|
+
])("$description", ({ cells, expected }) => {
|
|
102
|
+
const ui = (value: Temporal.PlainDate) => (
|
|
103
|
+
<MonthView {...base} value={value}>
|
|
104
|
+
<Cells>{cells}</Cells>
|
|
105
|
+
</MonthView>
|
|
106
|
+
);
|
|
107
|
+
const { container, rerender } = render(ui(d("2026-03-15")));
|
|
108
|
+
renders = 0;
|
|
109
|
+
|
|
110
|
+
rerender(ui(d("2026-03-20")));
|
|
111
|
+
|
|
112
|
+
expect({
|
|
113
|
+
renders,
|
|
114
|
+
rootValues: [...rootValues(container)],
|
|
115
|
+
}).toEqual(expected);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("moving focus with the keyboard re-renders only the two buttons it moved between", () => {
|
|
119
|
+
const { container } = render(
|
|
120
|
+
<MonthView {...base} defaultValue={d("2026-03-15")}>
|
|
121
|
+
<Cells>
|
|
122
|
+
<DayCellTemplate>
|
|
123
|
+
<DayButton render={buttonReadsNothing} />
|
|
124
|
+
</DayCellTemplate>
|
|
125
|
+
</Cells>
|
|
126
|
+
</MonthView>,
|
|
127
|
+
);
|
|
128
|
+
renders = 0;
|
|
129
|
+
|
|
130
|
+
const grid = container.querySelector('[role="grid"]')!;
|
|
131
|
+
act(() => {
|
|
132
|
+
grid.dispatchEvent(
|
|
133
|
+
new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }),
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
expect({
|
|
138
|
+
renders,
|
|
139
|
+
focused: container
|
|
140
|
+
.querySelector("button[data-focused]")
|
|
141
|
+
?.getAttribute("data-date"),
|
|
142
|
+
}).toEqual({ renders: 2, focused: "2026-03-16" });
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("a hover preview re-renders only the cells entering the preview", () => {
|
|
146
|
+
const { container } = render(
|
|
147
|
+
<MonthView
|
|
148
|
+
{...base}
|
|
149
|
+
selectionMode="range"
|
|
150
|
+
month={Temporal.PlainYearMonth.from("2026-03")}
|
|
151
|
+
>
|
|
152
|
+
<Cells>
|
|
153
|
+
<DayCellTemplate>
|
|
154
|
+
<DayButton render={buttonReadsNothing} />
|
|
155
|
+
</DayCellTemplate>
|
|
156
|
+
</Cells>
|
|
157
|
+
</MonthView>,
|
|
158
|
+
);
|
|
159
|
+
const button = (date: string) =>
|
|
160
|
+
container.querySelector(`button[data-date="${date}"]`)!;
|
|
161
|
+
fireEvent.click(button("2026-03-10"));
|
|
162
|
+
renders = 0;
|
|
163
|
+
|
|
164
|
+
fireEvent.pointerOver(button("2026-03-14"));
|
|
165
|
+
|
|
166
|
+
expect({
|
|
167
|
+
renders,
|
|
168
|
+
previewed: container.querySelectorAll(
|
|
169
|
+
"button[data-range-preview-in-range]",
|
|
170
|
+
).length,
|
|
171
|
+
}).toEqual({ renders: 5, previewed: 5 });
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("cells mounted by a month change see that month's state on first render", () => {
|
|
175
|
+
const { container } = render(
|
|
176
|
+
<MonthView {...base} defaultValue={d("2026-03-31")}>
|
|
177
|
+
<Cells>
|
|
178
|
+
<DayCellTemplate>
|
|
179
|
+
<DayButton />
|
|
180
|
+
</DayCellTemplate>
|
|
181
|
+
</Cells>
|
|
182
|
+
</MonthView>,
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const grid = container.querySelector('[role="grid"]')!;
|
|
186
|
+
act(() => {
|
|
187
|
+
grid.dispatchEvent(
|
|
188
|
+
new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }),
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
act(() => {
|
|
192
|
+
grid.dispatchEvent(
|
|
193
|
+
new KeyboardEvent("keydown", { key: "PageDown", bubbles: true }),
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect({
|
|
198
|
+
focused: container
|
|
199
|
+
.querySelector("button[data-focused]")
|
|
200
|
+
?.getAttribute("data-date"),
|
|
201
|
+
firstInMonth: container
|
|
202
|
+
.querySelector("td:not([data-outside-month])")
|
|
203
|
+
?.getAttribute("data-date"),
|
|
204
|
+
}).toEqual({ focused: "2026-05-01", firstInMonth: "2026-05-01" });
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("a standalone DayButton tracks its own date's state", () => {
|
|
208
|
+
const ui = (value: Temporal.PlainDate) => (
|
|
209
|
+
<MonthView {...base} value={value}>
|
|
210
|
+
<DayButton date={d("2026-03-20")} />
|
|
211
|
+
</MonthView>
|
|
212
|
+
);
|
|
213
|
+
const { container, rerender } = render(ui(d("2026-03-15")));
|
|
214
|
+
const button = () => container.querySelector("button")!;
|
|
215
|
+
const before = button().hasAttribute("data-selected");
|
|
216
|
+
|
|
217
|
+
rerender(ui(d("2026-03-20")));
|
|
218
|
+
|
|
219
|
+
expect({ before, after: button().hasAttribute("data-selected") }).toEqual({
|
|
220
|
+
before: false,
|
|
221
|
+
after: true,
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("a suspended transition's value stays out of cells that re-render meanwhile", async () => {
|
|
226
|
+
const never = new Promise(() => {});
|
|
227
|
+
function SuspendOn20(props: { value: Temporal.PlainDate }) {
|
|
228
|
+
if (props.value.day === 20) throw never;
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
let setValue!: (value: Temporal.PlainDate) => void;
|
|
232
|
+
let setClassName!: (className: string) => void;
|
|
233
|
+
function UserCells() {
|
|
234
|
+
const [className, set] = useState("a");
|
|
235
|
+
setClassName = set;
|
|
236
|
+
return (
|
|
237
|
+
<Cells>
|
|
238
|
+
<DayCellTemplate className={className}>
|
|
239
|
+
<DayButton />
|
|
240
|
+
</DayCellTemplate>
|
|
241
|
+
</Cells>
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
function App() {
|
|
245
|
+
const [value, set] = useState(d("2026-03-15"));
|
|
246
|
+
setValue = set;
|
|
247
|
+
return (
|
|
248
|
+
<>
|
|
249
|
+
<MonthView {...base} value={value}>
|
|
250
|
+
<UserCells />
|
|
251
|
+
</MonthView>
|
|
252
|
+
<Suspense fallback={null}>
|
|
253
|
+
<SuspendOn20 value={value} />
|
|
254
|
+
</Suspense>
|
|
255
|
+
</>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const { container } = render(<App />);
|
|
259
|
+
|
|
260
|
+
// The transition renders the calendar with 03-20, then suspends, so it
|
|
261
|
+
// never commits. A re-render of the cells alone must still show 03-15.
|
|
262
|
+
await act(async () => {
|
|
263
|
+
startTransition(() => setValue(d("2026-03-20")));
|
|
264
|
+
});
|
|
265
|
+
act(() => setClassName("b"));
|
|
266
|
+
|
|
267
|
+
expect(
|
|
268
|
+
[...container.querySelectorAll("button[data-selected]")].map((b) =>
|
|
269
|
+
b.getAttribute("data-date"),
|
|
270
|
+
),
|
|
271
|
+
).toEqual(["2026-03-15"]);
|
|
272
|
+
});
|
|
273
|
+
});
|