@klinking/colander 3.1.0-alpha.8 → 3.1.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/formats/date.cjs +1 -1
- package/dist/formats/date.d.cts +1 -1
- package/dist/formats/date.d.mts +1 -1
- package/dist/formats/date.mjs +1 -1
- package/dist/formats/object.cjs +1 -1
- package/dist/formats/object.d.cts +1 -1
- package/dist/formats/object.d.mts +1 -1
- package/dist/formats/object.mjs +1 -1
- package/dist/formats/plain-date-time.cjs +1 -1
- package/dist/formats/plain-date-time.d.cts +1 -1
- package/dist/formats/plain-date-time.d.mts +1 -1
- package/dist/formats/plain-date-time.mjs +1 -1
- package/dist/formats/plain-date.cjs +1 -1
- package/dist/formats/plain-date.d.cts +1 -1
- package/dist/formats/plain-date.d.mts +1 -1
- package/dist/formats/plain-date.mjs +1 -1
- package/dist/formats/plain-month-day.cjs +1 -1
- package/dist/formats/plain-month-day.d.cts +1 -1
- package/dist/formats/plain-month-day.d.mts +1 -1
- package/dist/formats/plain-month-day.mjs +1 -1
- package/dist/formats/plain-year-month.cjs +1 -1
- package/dist/formats/plain-year-month.d.cts +1 -1
- package/dist/formats/plain-year-month.d.mts +1 -1
- package/dist/formats/plain-year-month.mjs +1 -1
- package/dist/formats/zoned-date-time.cjs +1 -1
- package/dist/formats/zoned-date-time.d.cts +1 -1
- package/dist/formats/zoned-date-time.d.mts +1 -1
- package/dist/formats/zoned-date-time.mjs +1 -1
- package/dist/{index-BqzrxYgn.d.cts → index-B5VJ_WF2.d.mts} +2 -8
- package/dist/{index-CkhYFmf4.d.mts → index-BMsxaq1s.d.cts} +2 -8
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{weeks-navigation-CatYJIKb.cjs → weeks-navigation-CUQApLMk.cjs} +438 -290
- package/dist/{weeks-navigation-BWB5OvqW.mjs → weeks-navigation-CWNpF_6M.mjs} +440 -292
- package/package.json +5 -1
- package/src/calendar-provider.tsx +8 -2
- package/src/day-cell-state.ts +0 -70
- package/src/day-cell-store.test.tsx +273 -0
- package/src/day-cell-store.tsx +204 -0
- package/src/day-cell.test.tsx +72 -89
- package/src/day-cell.tsx +93 -78
- package/src/grid.tsx +17 -15
- package/src/month-view.tsx +3 -2
- package/src/store.ts +72 -0
- package/src/view-context.ts +2 -0
- package/src/weeks-grid.tsx +20 -10
- package/src/weeks-view-state.ts +1 -1
- package/src/weeks-view.tsx +2 -1
package/src/day-cell.test.tsx
CHANGED
|
@@ -6,104 +6,87 @@ import {
|
|
|
6
6
|
} from "./day-cell";
|
|
7
7
|
|
|
8
8
|
const date = Temporal.PlainDate.from("2026-03-15");
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
root: {},
|
|
14
|
-
date,
|
|
15
|
-
columnIndex: 0,
|
|
16
|
-
orientation: "horizontal",
|
|
17
|
-
outsideMonth: false,
|
|
18
|
-
hidden: false,
|
|
19
|
-
selected: false,
|
|
20
|
-
today: false,
|
|
21
|
-
disabled: false,
|
|
22
|
-
focused: false,
|
|
23
|
-
rangeStart: false,
|
|
24
|
-
rangeEnd: false,
|
|
25
|
-
rangeBoundary: false,
|
|
26
|
-
inRange: false,
|
|
27
|
-
rangeIndex: false,
|
|
28
|
-
rangeLength: false,
|
|
29
|
-
rangeHasStart: false,
|
|
30
|
-
rangeHasEnd: false,
|
|
31
|
-
rangePreviewStart: false,
|
|
32
|
-
rangePreviewEnd: false,
|
|
33
|
-
rangePreviewBoundary: false,
|
|
34
|
-
rangePreviewInRange: false,
|
|
35
|
-
rangePreviewIndex: false,
|
|
36
|
-
rangePreviewLength: false,
|
|
37
|
-
rangePreviewHasStart: false,
|
|
38
|
-
rangePreviewHasEnd: false,
|
|
39
|
-
isTabTarget: false,
|
|
40
|
-
...overrides,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
9
|
+
const gridMonth = { year: 2026, month: 3 };
|
|
10
|
+
const render = () => null;
|
|
11
|
+
const onPointerDown = () => {};
|
|
43
12
|
|
|
44
13
|
describe("dayCellInstancePropsAreEqual", () => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
14
|
+
// A cell's state comes from the store, so only its props decide whether it
|
|
15
|
+
// can skip a parent re-render.
|
|
16
|
+
it.each([
|
|
17
|
+
{
|
|
18
|
+
description: "unchanged props are equal",
|
|
19
|
+
prev: { date, gridMonth, render, className: "a", onPointerDown },
|
|
20
|
+
next: { date, gridMonth, render, className: "a", onPointerDown },
|
|
21
|
+
expected: true,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
description: "children are ignored (fresh React elements every render)",
|
|
25
|
+
prev: { date, children: <span>a</span> },
|
|
26
|
+
next: { date, children: <span>b</span> },
|
|
27
|
+
expected: true,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
description: "a changed pass-through prop differs",
|
|
31
|
+
prev: { date, className: "a" },
|
|
32
|
+
next: { date, className: "b" },
|
|
33
|
+
expected: false,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
description: "a new pass-through handler identity differs",
|
|
37
|
+
prev: { date, onPointerDown: () => {} },
|
|
38
|
+
next: { date, onPointerDown: () => {} },
|
|
39
|
+
expected: false,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
description: "a new render function identity differs",
|
|
43
|
+
prev: { date, render: () => null },
|
|
44
|
+
next: { date, render: () => null },
|
|
45
|
+
expected: false,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
description: "a new gridMonth identity differs",
|
|
49
|
+
prev: { date, gridMonth: { year: 2026, month: 3 } },
|
|
50
|
+
next: { date, gridMonth: { year: 2026, month: 3 } },
|
|
51
|
+
expected: false,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
description: "a different column differs",
|
|
55
|
+
prev: { date, columnIndex: 0 },
|
|
56
|
+
next: { date, columnIndex: 1 },
|
|
57
|
+
expected: false,
|
|
58
|
+
},
|
|
59
|
+
])("$description", ({ prev, next, expected }) => {
|
|
58
60
|
expect(dayCellInstancePropsAreEqual(prev as never, next as never)).toBe(
|
|
59
|
-
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("returns true when pass-through props are unchanged", () => {
|
|
64
|
-
const _derivedState = makeDerived();
|
|
65
|
-
const onPointerDown = () => {};
|
|
66
|
-
const prev = { date, _derivedState, className: "a", onPointerDown };
|
|
67
|
-
const next = { date, _derivedState, className: "a", onPointerDown };
|
|
68
|
-
expect(dayCellInstancePropsAreEqual(prev as never, next as never)).toBe(
|
|
69
|
-
true,
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("still ignores children (fresh React elements every render)", () => {
|
|
74
|
-
const _derivedState = makeDerived();
|
|
75
|
-
const prev = { date, _derivedState, children: <span>a</span> };
|
|
76
|
-
const next = { date, _derivedState, children: <span>b</span> };
|
|
77
|
-
expect(dayCellInstancePropsAreEqual(prev as never, next as never)).toBe(
|
|
78
|
-
true,
|
|
79
|
-
);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("returns false when a derived-state field changes", () => {
|
|
83
|
-
const prev = { date, _derivedState: makeDerived({ selected: false }) };
|
|
84
|
-
const next = { date, _derivedState: makeDerived({ selected: true }) };
|
|
85
|
-
expect(dayCellInstancePropsAreEqual(prev as never, next as never)).toBe(
|
|
86
|
-
false,
|
|
61
|
+
expected,
|
|
87
62
|
);
|
|
88
63
|
});
|
|
89
64
|
});
|
|
90
65
|
|
|
91
66
|
describe("dayButtonInnerPropsAreEqual", () => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
67
|
+
const _state = {};
|
|
68
|
+
it.each([
|
|
69
|
+
{
|
|
70
|
+
description: "unchanged props and state are equal",
|
|
71
|
+
prev: { date, _state, "data-x": "1" },
|
|
72
|
+
next: { date, _state, "data-x": "1" },
|
|
73
|
+
expected: true,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
description: "a changed pass-through prop differs",
|
|
77
|
+
prev: { date, _state, "data-x": "1" },
|
|
78
|
+
next: { date, _state, "data-x": "2" },
|
|
79
|
+
expected: false,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
description: "a new cell state object differs",
|
|
83
|
+
prev: { date, _state: {} },
|
|
84
|
+
next: { date, _state: {} },
|
|
85
|
+
expected: false,
|
|
86
|
+
},
|
|
87
|
+
])("$description", ({ prev, next, expected }) => {
|
|
105
88
|
expect(dayButtonInnerPropsAreEqual(prev as never, next as never)).toBe(
|
|
106
|
-
|
|
89
|
+
expected,
|
|
107
90
|
);
|
|
108
91
|
});
|
|
109
92
|
});
|
package/src/day-cell.tsx
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
2
|
import { useRender } from "@base-ui/react/use-render";
|
|
3
3
|
import type { Temporal as TemporalPoly } from "@js-temporal/polyfill";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
forwardRef,
|
|
7
|
+
memo,
|
|
8
|
+
useContext,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useRef,
|
|
12
|
+
} from "react";
|
|
5
13
|
import { useCalendarStable } from "./calendar-context";
|
|
6
14
|
import { WeekDataContext, DayCellDataContext } from "./context";
|
|
7
|
-
import { dayCellStateEqual, useDayCellDeriver } from "./day-cell-state";
|
|
8
15
|
import type { DayCellState } from "./day-cell-state";
|
|
16
|
+
import { useDayCellState, useStandaloneDayCellState } from "./day-cell-store";
|
|
9
17
|
import { MonthViewStateContext } from "./month-view-context";
|
|
10
18
|
import type { StateAttributesMapping } from "./types";
|
|
11
19
|
import type {
|
|
@@ -14,7 +22,7 @@ import type {
|
|
|
14
22
|
DayCellTemplateState,
|
|
15
23
|
} from "./types";
|
|
16
24
|
import { shouldMoveDomFocus } from "./utils";
|
|
17
|
-
import {
|
|
25
|
+
import { useViewStable } from "./view-context";
|
|
18
26
|
|
|
19
27
|
export const dayStateAttributesMapping = {
|
|
20
28
|
root: () => null,
|
|
@@ -51,21 +59,27 @@ export const dayStateAttributesMapping = {
|
|
|
51
59
|
rangePreviewHasEnd: (v) => (v ? { "data-range-preview-has-end": "" } : null),
|
|
52
60
|
} as const satisfies StateAttributesMapping<DayCellTemplateState>;
|
|
53
61
|
|
|
62
|
+
/** @internal The enclosing day cell's state, read by its `DayButton`. */
|
|
63
|
+
const DayCellStateContext = createContext<DayCellState | null>(null);
|
|
64
|
+
|
|
65
|
+
type MonthKey = { year: number; month: number };
|
|
66
|
+
|
|
54
67
|
/** Props for the memoized DayCellInstance. */
|
|
55
68
|
interface DayCellInstanceProps {
|
|
56
69
|
render?: DayCellTemplateProps["render"];
|
|
57
70
|
date: TemporalPoly.PlainDate;
|
|
58
71
|
columnIndex?: number | undefined;
|
|
72
|
+
gridMonth?: MonthKey | undefined;
|
|
59
73
|
children?: React.ReactNode;
|
|
60
|
-
_derivedState: DayCellState;
|
|
61
74
|
[key: string]: unknown;
|
|
62
75
|
}
|
|
63
76
|
|
|
64
77
|
function DayCellInstanceFn(props: DayCellInstanceProps) {
|
|
65
|
-
const { render, date, columnIndex,
|
|
78
|
+
const { render, date, columnIndex, gridMonth, children, ...otherProps } =
|
|
66
79
|
props;
|
|
67
80
|
|
|
68
|
-
const
|
|
81
|
+
const derived = useDayCellState(date, columnIndex, gridMonth);
|
|
82
|
+
const state = derived as unknown as DayCellTemplateState;
|
|
69
83
|
|
|
70
84
|
const defaultProps: Record<string, unknown> = state.hidden
|
|
71
85
|
? {
|
|
@@ -77,7 +91,7 @@ function DayCellInstanceFn(props: DayCellInstanceProps) {
|
|
|
77
91
|
role: "gridcell",
|
|
78
92
|
"aria-selected": state.selected || undefined,
|
|
79
93
|
"aria-disabled": state.disabled || undefined,
|
|
80
|
-
children: children ?? <DayButton
|
|
94
|
+
children: children ?? <DayButton />,
|
|
81
95
|
};
|
|
82
96
|
|
|
83
97
|
const cell = useRender({
|
|
@@ -91,10 +105,16 @@ function DayCellInstanceFn(props: DayCellInstanceProps) {
|
|
|
91
105
|
|
|
92
106
|
const outsideDisabled =
|
|
93
107
|
state.hidden || (state.outsideMonth && state.disabled);
|
|
108
|
+
const cellData = useMemo(
|
|
109
|
+
() => ({ date, columnIndex, outsideDisabled }),
|
|
110
|
+
[date, columnIndex, outsideDisabled],
|
|
111
|
+
);
|
|
94
112
|
|
|
95
113
|
return (
|
|
96
|
-
<DayCellDataContext.Provider value={
|
|
97
|
-
{
|
|
114
|
+
<DayCellDataContext.Provider value={cellData}>
|
|
115
|
+
<DayCellStateContext.Provider value={derived}>
|
|
116
|
+
{cell}
|
|
117
|
+
</DayCellStateContext.Provider>
|
|
98
118
|
</DayCellDataContext.Provider>
|
|
99
119
|
);
|
|
100
120
|
}
|
|
@@ -123,33 +143,30 @@ const DAY_CELL_HANDLED_KEYS: ReadonlySet<string> = new Set([
|
|
|
123
143
|
"render",
|
|
124
144
|
"date",
|
|
125
145
|
"columnIndex",
|
|
146
|
+
"gridMonth",
|
|
126
147
|
"children",
|
|
127
|
-
"_derivedState",
|
|
128
148
|
]);
|
|
129
149
|
|
|
130
150
|
const DAY_BUTTON_HANDLED_KEYS: ReadonlySet<string> = new Set([
|
|
131
151
|
"render",
|
|
132
152
|
"date",
|
|
133
153
|
"children",
|
|
134
|
-
"
|
|
154
|
+
"_state",
|
|
135
155
|
]);
|
|
136
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Whether a day cell can skip a parent re-render. Its own state comes from
|
|
159
|
+
* the store, so only props are compared here.
|
|
160
|
+
*/
|
|
137
161
|
export function dayCellInstancePropsAreEqual(
|
|
138
162
|
prev: DayCellInstanceProps,
|
|
139
163
|
next: DayCellInstanceProps,
|
|
140
164
|
): boolean {
|
|
141
|
-
// Skip `children` — React elements are always new objects. Child components
|
|
142
|
-
// that subscribe to context (e.g. DayButton fallback) get their own
|
|
143
|
-
// re-render via context; the optimised internal DayButton reads only
|
|
144
|
-
// stable context and is separately memo'd.
|
|
145
|
-
if (
|
|
146
|
-
prev.date !== next.date ||
|
|
147
|
-
prev.render !== next.render ||
|
|
148
|
-
prev.columnIndex !== next.columnIndex
|
|
149
|
-
)
|
|
150
|
-
return false;
|
|
151
165
|
return (
|
|
152
|
-
|
|
166
|
+
prev.date === next.date &&
|
|
167
|
+
prev.render === next.render &&
|
|
168
|
+
prev.columnIndex === next.columnIndex &&
|
|
169
|
+
prev.gridMonth === next.gridMonth &&
|
|
153
170
|
passThroughPropsEqual(prev, next, DAY_CELL_HANDLED_KEYS)
|
|
154
171
|
);
|
|
155
172
|
}
|
|
@@ -159,6 +176,19 @@ const DayCellInstance = memo(
|
|
|
159
176
|
dayCellInstancePropsAreEqual,
|
|
160
177
|
) as typeof DayCellInstanceFn;
|
|
161
178
|
|
|
179
|
+
/** The cells of a whole month, for a `DayCellTemplate` outside any week row. */
|
|
180
|
+
function MonthDayCells(props: Omit<DayCellTemplateProps, "date">) {
|
|
181
|
+
const monthState = useContext(MonthViewStateContext);
|
|
182
|
+
const days = monthState?.weeks.flat() ?? [];
|
|
183
|
+
return (
|
|
184
|
+
<>
|
|
185
|
+
{days.map((day) => (
|
|
186
|
+
<DayCellInstance key={day.toString()} {...props} date={day} />
|
|
187
|
+
))}
|
|
188
|
+
</>
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
162
192
|
/**
|
|
163
193
|
* Renders one `<td role="gridcell">` per day. Exposes data-attributes for
|
|
164
194
|
* `selected`, `today`, `disabled`, `outside-month`, `focused`,
|
|
@@ -170,31 +200,28 @@ const DayCellInstance = memo(
|
|
|
170
200
|
export function DayCellTemplate(props: DayCellTemplateProps) {
|
|
171
201
|
const { date: dateProp, ...restProps } = props;
|
|
172
202
|
const weekData = useContext(WeekDataContext);
|
|
173
|
-
const monthState = useContext(MonthViewStateContext);
|
|
174
|
-
const derive = useDayCellDeriver();
|
|
175
203
|
|
|
176
204
|
if (dateProp) {
|
|
177
205
|
return (
|
|
178
206
|
<DayCellInstance
|
|
179
207
|
{...restProps}
|
|
180
208
|
date={dateProp}
|
|
181
|
-
|
|
209
|
+
gridMonth={weekData?.gridMonth}
|
|
182
210
|
/>
|
|
183
211
|
);
|
|
184
212
|
}
|
|
185
213
|
|
|
186
|
-
|
|
187
|
-
const perWeek = weekData != null;
|
|
214
|
+
if (!weekData) return <MonthDayCells {...restProps} />;
|
|
188
215
|
|
|
189
216
|
return (
|
|
190
217
|
<>
|
|
191
|
-
{days.map((day, i) => (
|
|
218
|
+
{weekData.days.map((day, i) => (
|
|
192
219
|
<DayCellInstance
|
|
193
220
|
key={day.toString()}
|
|
194
221
|
{...restProps}
|
|
195
222
|
date={day}
|
|
196
|
-
columnIndex={
|
|
197
|
-
|
|
223
|
+
columnIndex={i}
|
|
224
|
+
gridMonth={weekData.gridMonth}
|
|
198
225
|
/>
|
|
199
226
|
))}
|
|
200
227
|
</>
|
|
@@ -205,7 +232,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
|
|
|
205
232
|
interface DayButtonInnerProps {
|
|
206
233
|
render?: DayButtonProps["render"];
|
|
207
234
|
date: TemporalPoly.PlainDate;
|
|
208
|
-
|
|
235
|
+
_state: DayCellState;
|
|
209
236
|
[key: string]: unknown;
|
|
210
237
|
}
|
|
211
238
|
|
|
@@ -213,17 +240,16 @@ function DayButtonInnerFn(
|
|
|
213
240
|
props: DayButtonInnerProps,
|
|
214
241
|
ref: React.ForwardedRef<HTMLButtonElement>,
|
|
215
242
|
) {
|
|
216
|
-
const { render, date,
|
|
243
|
+
const { render, date, _state, ...otherProps } = props;
|
|
217
244
|
const { onSelect, locale, readOnly, setHoveredDate } = useCalendarStable();
|
|
218
|
-
const { setFocusedDate } =
|
|
219
|
-
const { gridFocusedRef } = useViewStable();
|
|
245
|
+
const { gridFocusedRef, setFocusedDate } = useViewStable();
|
|
220
246
|
|
|
221
247
|
const internalRef = useRef<HTMLButtonElement>(null);
|
|
222
248
|
|
|
223
|
-
const isHidden =
|
|
224
|
-
const isFocused =
|
|
225
|
-
const isTabTarget =
|
|
226
|
-
const isDisabled =
|
|
249
|
+
const isHidden = _state.hidden;
|
|
250
|
+
const isFocused = _state.focused;
|
|
251
|
+
const isTabTarget = _state.isTabTarget;
|
|
252
|
+
const isDisabled = _state.disabled;
|
|
227
253
|
|
|
228
254
|
useEffect(() => {
|
|
229
255
|
if (
|
|
@@ -234,7 +260,7 @@ function DayButtonInnerFn(
|
|
|
234
260
|
}
|
|
235
261
|
}, [isFocused, gridFocusedRef]);
|
|
236
262
|
|
|
237
|
-
const state =
|
|
263
|
+
const state = _state as unknown as DayCellTemplateState;
|
|
238
264
|
|
|
239
265
|
const defaultProps: Record<string, unknown> = isHidden
|
|
240
266
|
? {}
|
|
@@ -278,12 +304,12 @@ export function dayButtonInnerPropsAreEqual(
|
|
|
278
304
|
prev: DayButtonInnerProps,
|
|
279
305
|
next: DayButtonInnerProps,
|
|
280
306
|
): boolean {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
307
|
+
return (
|
|
308
|
+
prev.date === next.date &&
|
|
309
|
+
prev.render === next.render &&
|
|
310
|
+
prev._state === next._state &&
|
|
311
|
+
passThroughPropsEqual(prev, next, DAY_BUTTON_HANDLED_KEYS)
|
|
312
|
+
);
|
|
287
313
|
}
|
|
288
314
|
|
|
289
315
|
// The `as any` casts are needed because DayButtonInnerProps uses an index
|
|
@@ -303,30 +329,28 @@ const DayButtonInner = memo(
|
|
|
303
329
|
* Must be used inside a {@link DayCellTemplate} or given an explicit `date` prop.
|
|
304
330
|
*/
|
|
305
331
|
function DayButtonFn(
|
|
306
|
-
props: DayButtonProps
|
|
307
|
-
_derivedState?: DayCellState;
|
|
308
|
-
},
|
|
332
|
+
props: DayButtonProps,
|
|
309
333
|
ref: React.ForwardedRef<HTMLButtonElement>,
|
|
310
334
|
) {
|
|
311
|
-
const { date: dateProp,
|
|
335
|
+
const { date: dateProp, ...restProps } = props;
|
|
336
|
+
const cellState = useContext(DayCellStateContext);
|
|
312
337
|
const cellData = useContext(DayCellDataContext);
|
|
313
338
|
|
|
314
|
-
|
|
339
|
+
// Inside a cell for the same date, reuse the cell's state.
|
|
340
|
+
if (cellState && (!dateProp || dateProp.equals(cellState.date))) {
|
|
341
|
+
return (
|
|
342
|
+
<DayButtonInner
|
|
343
|
+
{...restProps}
|
|
344
|
+
ref={ref}
|
|
345
|
+
date={cellState.date}
|
|
346
|
+
_state={cellState}
|
|
347
|
+
/>
|
|
348
|
+
);
|
|
349
|
+
}
|
|
315
350
|
|
|
351
|
+
const resolvedDate = dateProp ?? cellData?.date;
|
|
316
352
|
if (resolvedDate) {
|
|
317
|
-
|
|
318
|
-
if (_derivedState) {
|
|
319
|
-
return (
|
|
320
|
-
<DayButtonInner
|
|
321
|
-
{...restProps}
|
|
322
|
-
ref={ref}
|
|
323
|
-
date={resolvedDate}
|
|
324
|
-
_derivedState={_derivedState}
|
|
325
|
-
/>
|
|
326
|
-
);
|
|
327
|
-
}
|
|
328
|
-
// Fallback: compute state (for standalone DayButton usage with explicit date)
|
|
329
|
-
return <DayButtonFallback ref={ref} {...restProps} date={resolvedDate} />;
|
|
353
|
+
return <StandaloneDayButton {...restProps} ref={ref} date={resolvedDate} />;
|
|
330
354
|
}
|
|
331
355
|
|
|
332
356
|
throw new Error(
|
|
@@ -335,30 +359,21 @@ function DayButtonFn(
|
|
|
335
359
|
}
|
|
336
360
|
|
|
337
361
|
export const DayButton = forwardRef(DayButtonFn) as (
|
|
338
|
-
props: DayButtonProps &
|
|
339
|
-
_derivedState?: DayCellState;
|
|
340
|
-
} & React.RefAttributes<HTMLButtonElement>,
|
|
362
|
+
props: DayButtonProps & React.RefAttributes<HTMLButtonElement>,
|
|
341
363
|
) => React.ReactElement | null;
|
|
342
364
|
|
|
343
|
-
/**
|
|
344
|
-
function
|
|
365
|
+
/** A `DayButton` outside any day cell: subscribes to its own date's state. */
|
|
366
|
+
function StandaloneDayButtonFn(
|
|
345
367
|
props: Omit<DayButtonProps, "date"> & { date: TemporalPoly.PlainDate },
|
|
346
368
|
ref: React.ForwardedRef<HTMLButtonElement>,
|
|
347
369
|
) {
|
|
348
370
|
const { date, ...restProps } = props;
|
|
349
|
-
const
|
|
371
|
+
const state = useStandaloneDayCellState(date);
|
|
350
372
|
|
|
351
|
-
return
|
|
352
|
-
<DayButtonInner
|
|
353
|
-
{...restProps}
|
|
354
|
-
ref={ref}
|
|
355
|
-
date={date}
|
|
356
|
-
_derivedState={derive(date)}
|
|
357
|
-
/>
|
|
358
|
-
);
|
|
373
|
+
return <DayButtonInner {...restProps} ref={ref} date={date} _state={state} />;
|
|
359
374
|
}
|
|
360
375
|
|
|
361
|
-
const
|
|
376
|
+
const StandaloneDayButton = forwardRef(StandaloneDayButtonFn) as (
|
|
362
377
|
props: Omit<DayButtonProps, "date"> & {
|
|
363
378
|
date: TemporalPoly.PlainDate;
|
|
364
379
|
} & React.RefAttributes<HTMLButtonElement>,
|
package/src/grid.tsx
CHANGED
|
@@ -571,26 +571,28 @@ export function WeekTemplate(props: WeekTemplateProps) {
|
|
|
571
571
|
: undefined,
|
|
572
572
|
[gridMonthCtx],
|
|
573
573
|
);
|
|
574
|
-
const
|
|
574
|
+
const gridRowIndex = outerWeekData?.gridRowIndex;
|
|
575
|
+
// Built once per set of weeks, so a focus or selection change that
|
|
576
|
+
// re-renders this template leaves the day cells' context untouched.
|
|
577
|
+
const weekValues = useMemo(
|
|
578
|
+
() =>
|
|
579
|
+
weeks.map((weekDays, i) => ({
|
|
580
|
+
days: weekDays,
|
|
581
|
+
weekIndex: i,
|
|
582
|
+
...(gridMonth !== undefined && { gridMonth }),
|
|
583
|
+
...(gridRowIndex !== undefined && { gridRowIndex }),
|
|
584
|
+
})),
|
|
585
|
+
[weeks, gridMonth, gridRowIndex],
|
|
586
|
+
);
|
|
575
587
|
|
|
576
588
|
return (
|
|
577
589
|
<>
|
|
578
|
-
{
|
|
579
|
-
const firstDay =
|
|
590
|
+
{weekValues.map((value) => {
|
|
591
|
+
const firstDay = value.days[0];
|
|
580
592
|
if (firstDay === undefined) return null;
|
|
581
593
|
return (
|
|
582
|
-
<WeekDataContext.Provider
|
|
583
|
-
|
|
584
|
-
value={{
|
|
585
|
-
days: weekDays,
|
|
586
|
-
weekIndex: i,
|
|
587
|
-
...(gridMonth !== undefined && { gridMonth }),
|
|
588
|
-
...(outerWeekData?.gridRowIndex !== undefined && {
|
|
589
|
-
gridRowIndex: outerWeekData.gridRowIndex,
|
|
590
|
-
}),
|
|
591
|
-
}}
|
|
592
|
-
>
|
|
593
|
-
<Instance {...props} />
|
|
594
|
+
<WeekDataContext.Provider key={firstDay.toString()} value={value}>
|
|
595
|
+
<WeekInstance {...props} />
|
|
594
596
|
</WeekDataContext.Provider>
|
|
595
597
|
);
|
|
596
598
|
})}
|
package/src/month-view.tsx
CHANGED
|
@@ -3,6 +3,7 @@ import { useCallback, useMemo, useRef, useState } from "react";
|
|
|
3
3
|
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
4
4
|
import { CalendarProvider } from "./calendar-provider";
|
|
5
5
|
import type { CalendarProviderProps } from "./calendar-types";
|
|
6
|
+
import { DayCellStoreProvider } from "./day-cell-store";
|
|
6
7
|
import {
|
|
7
8
|
MonthViewStableContext,
|
|
8
9
|
MonthViewStateContext,
|
|
@@ -265,7 +266,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
265
266
|
);
|
|
266
267
|
|
|
267
268
|
const viewStableCtx = useMemo<ViewStableContextValue>(
|
|
268
|
-
() => ({ gridFocusedRef, outsideDays }),
|
|
269
|
+
() => ({ gridFocusedRef, outsideDays, setFocusedDate }),
|
|
269
270
|
[outsideDays],
|
|
270
271
|
);
|
|
271
272
|
|
|
@@ -280,7 +281,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
280
281
|
<ViewStableContext.Provider value={viewStableCtx}>
|
|
281
282
|
<ViewStateContext.Provider value={viewStateCtx}>
|
|
282
283
|
<ViewContext.Provider value={viewCtx}>
|
|
283
|
-
{children}
|
|
284
|
+
<DayCellStoreProvider>{children}</DayCellStoreProvider>
|
|
284
285
|
</ViewContext.Provider>
|
|
285
286
|
</ViewStateContext.Provider>
|
|
286
287
|
</ViewStableContext.Provider>
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useInsertionEffect,
|
|
5
|
+
useLayoutEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
} from "react";
|
|
8
|
+
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
|
|
9
|
+
|
|
10
|
+
/** A minimal external store. `set` replaces the state and notifies. */
|
|
11
|
+
export interface Store<T> {
|
|
12
|
+
get: () => T;
|
|
13
|
+
set: (next: T) => void;
|
|
14
|
+
subscribe: (listener: () => void) => () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function createStore<T>(initial: T): Store<T> {
|
|
18
|
+
let state = initial;
|
|
19
|
+
const listeners = new Set<() => void>();
|
|
20
|
+
return {
|
|
21
|
+
get: () => state,
|
|
22
|
+
set: (next) => {
|
|
23
|
+
if (Object.is(state, next)) return;
|
|
24
|
+
state = next;
|
|
25
|
+
for (const listener of listeners) listener();
|
|
26
|
+
},
|
|
27
|
+
subscribe: (listener) => {
|
|
28
|
+
listeners.add(listener);
|
|
29
|
+
return () => listeners.delete(listener);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `useLayoutEffect` in the browser, `useEffect` on the server (no warning). */
|
|
35
|
+
export const useIsoLayoutEffect =
|
|
36
|
+
typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Subscribes to the slice of `store` that `select` picks. The component
|
|
40
|
+
* re-renders only when `isEqual` says the slice changed; otherwise the
|
|
41
|
+
* previously returned slice is handed back, keeping its identity.
|
|
42
|
+
*/
|
|
43
|
+
export function useStoreSelector<T, S>(
|
|
44
|
+
store: Store<T>,
|
|
45
|
+
select: (state: T) => S,
|
|
46
|
+
isEqual: (a: S, b: S) => boolean,
|
|
47
|
+
): S {
|
|
48
|
+
return useSyncExternalStoreWithSelector(
|
|
49
|
+
store.subscribe,
|
|
50
|
+
store.get,
|
|
51
|
+
store.get,
|
|
52
|
+
select,
|
|
53
|
+
isEqual,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns a function with a stable identity that always calls the latest
|
|
59
|
+
* `fn`, so contexts holding it don't change when its closure does. Call it
|
|
60
|
+
* from event handlers and effects, not during render.
|
|
61
|
+
*/
|
|
62
|
+
export function useStableCallback<A extends unknown[], R>(
|
|
63
|
+
fn: (...args: A) => R,
|
|
64
|
+
): (...args: A) => R {
|
|
65
|
+
const latest = useRef(fn);
|
|
66
|
+
// Insertion effects run before every layout effect, so no effect in the
|
|
67
|
+
// tree can call a stale `fn`.
|
|
68
|
+
useInsertionEffect(() => {
|
|
69
|
+
latest.current = fn;
|
|
70
|
+
});
|
|
71
|
+
return useCallback((...args: A) => latest.current(...args), []);
|
|
72
|
+
}
|
package/src/view-context.ts
CHANGED
|
@@ -24,6 +24,8 @@ export interface ViewStableContextValue {
|
|
|
24
24
|
gridFocusedRef: React.RefObject<boolean>;
|
|
25
25
|
/** How days outside the viewed month are displayed. Always `"enabled"` in WeeksView. */
|
|
26
26
|
outsideDays: OutsideDays;
|
|
27
|
+
/** Moves the logically focused date (same function as {@link ViewContextValue.setFocusedDate}). */
|
|
28
|
+
setFocusedDate: (date: Temporal.PlainDate) => void;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/** @internal Volatile values shared by both views' parts. */
|