@knitui/dates 0.5.0 → 0.6.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/lib/commonjs/DatesProvider/DatesProvider.js +33 -4
- package/lib/commonjs/DatesProvider/DatesProvider.js.map +1 -1
- package/lib/commonjs/Day/Day.js +45 -7
- package/lib/commonjs/Day/Day.js.map +1 -1
- package/lib/commonjs/Month/Month.js +266 -80
- package/lib/commonjs/Month/Month.js.map +1 -1
- package/lib/commonjs/MonthLevel/MonthLevel.js +19 -3
- package/lib/commonjs/MonthLevel/MonthLevel.js.map +1 -1
- package/lib/commonjs/MonthsList/MonthsList.js +160 -60
- package/lib/commonjs/MonthsList/MonthsList.js.map +1 -1
- package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
- package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
- package/lib/commonjs/TimeControlsList/TimeControl.js +15 -1
- package/lib/commonjs/TimeControlsList/TimeControl.js.map +1 -1
- package/lib/commonjs/TimeControlsList/TimeControlsList.js +16 -3
- package/lib/commonjs/TimeControlsList/TimeControlsList.js.map +1 -1
- package/lib/commonjs/WeekdaysRow/WeekdaysRow.js +23 -4
- package/lib/commonjs/WeekdaysRow/WeekdaysRow.js.map +1 -1
- package/lib/commonjs/YearsList/YearsList.js +155 -60
- package/lib/commonjs/YearsList/YearsList.js.map +1 -1
- package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
- package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
- package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
- package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
- package/lib/commonjs/hooks/use-dates-state/use-dates-state.js +25 -7
- package/lib/commonjs/hooks/use-dates-state/use-dates-state.js.map +1 -1
- package/lib/commonjs/internal/are-cell-props-equal.js +64 -0
- package/lib/commonjs/internal/are-cell-props-equal.js.map +1 -0
- package/lib/commonjs/internal/date-string-fast-path.js +98 -0
- package/lib/commonjs/internal/date-string-fast-path.js.map +1 -0
- package/lib/commonjs/internal/memoize-control-props.js +54 -0
- package/lib/commonjs/internal/memoize-control-props.js.map +1 -0
- package/lib/commonjs/internal/month-days-cache.js +71 -0
- package/lib/commonjs/internal/month-days-cache.js.map +1 -0
- package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
- package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
- package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js +14 -1
- package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js.map +1 -1
- package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js +14 -1
- package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js.map +1 -1
- package/lib/commonjs/utils/is-same-month/is-same-month.js +17 -1
- package/lib/commonjs/utils/is-same-month/is-same-month.js.map +1 -1
- package/lib/module/DatesProvider/DatesProvider.js +34 -5
- package/lib/module/DatesProvider/DatesProvider.js.map +1 -1
- package/lib/module/Day/Day.js +46 -8
- package/lib/module/Day/Day.js.map +1 -1
- package/lib/module/Month/Month.js +267 -82
- package/lib/module/Month/Month.js.map +1 -1
- package/lib/module/MonthLevel/MonthLevel.js +19 -3
- package/lib/module/MonthLevel/MonthLevel.js.map +1 -1
- package/lib/module/MonthsList/MonthsList.js +159 -60
- package/lib/module/MonthsList/MonthsList.js.map +1 -1
- package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
- package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
- package/lib/module/TimeControlsList/TimeControl.js +15 -1
- package/lib/module/TimeControlsList/TimeControl.js.map +1 -1
- package/lib/module/TimeControlsList/TimeControlsList.js +16 -3
- package/lib/module/TimeControlsList/TimeControlsList.js.map +1 -1
- package/lib/module/WeekdaysRow/WeekdaysRow.js +23 -4
- package/lib/module/WeekdaysRow/WeekdaysRow.js.map +1 -1
- package/lib/module/YearsList/YearsList.js +154 -60
- package/lib/module/YearsList/YearsList.js.map +1 -1
- package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
- package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
- package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
- package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
- package/lib/module/hooks/use-dates-state/use-dates-state.js +25 -7
- package/lib/module/hooks/use-dates-state/use-dates-state.js.map +1 -1
- package/lib/module/internal/are-cell-props-equal.js +60 -0
- package/lib/module/internal/are-cell-props-equal.js.map +1 -0
- package/lib/module/internal/date-string-fast-path.js +91 -0
- package/lib/module/internal/date-string-fast-path.js.map +1 -0
- package/lib/module/internal/memoize-control-props.js +50 -0
- package/lib/module/internal/memoize-control-props.js.map +1 -0
- package/lib/module/internal/month-days-cache.js +68 -0
- package/lib/module/internal/month-days-cache.js.map +1 -0
- package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
- package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
- package/lib/module/utils/is-after-min-date/is-after-min-date.js +14 -1
- package/lib/module/utils/is-after-min-date/is-after-min-date.js.map +1 -1
- package/lib/module/utils/is-before-max-date/is-before-max-date.js +14 -1
- package/lib/module/utils/is-before-max-date/is-before-max-date.js.map +1 -1
- package/lib/module/utils/is-same-month/is-same-month.js +17 -1
- package/lib/module/utils/is-same-month/is-same-month.js.map +1 -1
- package/lib/typescript/Calendar/pick-calendar-props/pick-calendar-props.d.ts +1 -1
- package/lib/typescript/CalendarHeader/CalendarHeader.d.ts +14 -14
- package/lib/typescript/DatesProvider/DatesProvider.d.ts.map +1 -1
- package/lib/typescript/Day/Day.d.ts +31 -31
- package/lib/typescript/Day/Day.d.ts.map +1 -1
- package/lib/typescript/DecadeLevel/DecadeLevel.d.ts +7 -7
- package/lib/typescript/DecadeLevelGroup/DecadeLevelGroup.d.ts +7 -7
- package/lib/typescript/InlineDateTimePicker/InlineDateTimePicker.d.ts +4 -4
- package/lib/typescript/MiniCalendar/MiniCalendar.d.ts +18 -18
- package/lib/typescript/Month/Month.d.ts +9 -8
- package/lib/typescript/Month/Month.d.ts.map +1 -1
- package/lib/typescript/MonthLevel/MonthLevel.d.ts +10 -10
- package/lib/typescript/MonthLevel/MonthLevel.d.ts.map +1 -1
- package/lib/typescript/MonthLevelGroup/MonthLevelGroup.d.ts +10 -10
- package/lib/typescript/MonthsList/MonthsList.d.ts +3 -2
- package/lib/typescript/MonthsList/MonthsList.d.ts.map +1 -1
- package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts +6 -0
- package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts.map +1 -1
- package/lib/typescript/PickerControl/PickerControl.d.ts +10 -10
- package/lib/typescript/TimeControlsList/TimeControl.d.ts +13 -0
- package/lib/typescript/TimeControlsList/TimeControl.d.ts.map +1 -1
- package/lib/typescript/TimeControlsList/TimeControlsList.d.ts.map +1 -1
- package/lib/typescript/TimeGrid/TimeGrid.d.ts +7 -7
- package/lib/typescript/TimePicker/TimePicker.d.ts +4 -4
- package/lib/typescript/TimePresets/TimePresetGroup.d.ts +4 -4
- package/lib/typescript/TimePresets/TimePresets.d.ts +6 -4
- package/lib/typescript/TimePresets/TimePresets.d.ts.map +1 -1
- package/lib/typescript/WeekdaysRow/WeekdaysRow.d.ts +6 -6
- package/lib/typescript/WeekdaysRow/WeekdaysRow.d.ts.map +1 -1
- package/lib/typescript/YearLevel/YearLevel.d.ts +7 -7
- package/lib/typescript/YearLevelGroup/YearLevelGroup.d.ts +7 -7
- package/lib/typescript/YearsList/YearsList.d.ts +3 -2
- package/lib/typescript/YearsList/YearsList.d.ts.map +1 -1
- package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts +6 -0
- package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts.map +1 -1
- package/lib/typescript/_reference/ExampleControl.d.ts +8 -8
- package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts +12 -0
- package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts.map +1 -1
- package/lib/typescript/hooks/use-dates-state/use-dates-state.d.ts.map +1 -1
- package/lib/typescript/internal/are-cell-props-equal.d.ts +21 -0
- package/lib/typescript/internal/are-cell-props-equal.d.ts.map +1 -0
- package/lib/typescript/internal/date-string-fast-path.d.ts +35 -0
- package/lib/typescript/internal/date-string-fast-path.d.ts.map +1 -0
- package/lib/typescript/internal/memoize-control-props.d.ts +12 -0
- package/lib/typescript/internal/memoize-control-props.d.ts.map +1 -0
- package/lib/typescript/internal/month-days-cache.d.ts +19 -0
- package/lib/typescript/internal/month-days-cache.d.ts.map +1 -0
- package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts +9 -1
- package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts.map +1 -1
- package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts +6 -0
- package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts.map +1 -1
- package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts +6 -0
- package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts.map +1 -1
- package/lib/typescript/utils/is-same-month/is-same-month.d.ts +13 -1
- package/lib/typescript/utils/is-same-month/is-same-month.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/DatesProvider/DatesProvider.tsx +27 -5
- package/src/Day/Day.tsx +51 -10
- package/src/Month/Month.tsx +357 -80
- package/src/MonthLevel/MonthLevel.tsx +35 -16
- package/src/MonthsList/MonthsList.tsx +201 -55
- package/src/MonthsList/get-month-in-tab-order/get-month-in-tab-order.ts +12 -1
- package/src/TimeControlsList/TimeControl.tsx +16 -1
- package/src/TimeControlsList/TimeControlsList.tsx +15 -3
- package/src/WeekdaysRow/WeekdaysRow.tsx +29 -5
- package/src/YearsList/YearsList.tsx +196 -55
- package/src/YearsList/get-year-in-tab-order/get-year-in-tab-order.ts +12 -1
- package/src/hooks/use-dates-state/is-in-range/is-in-range.ts +22 -0
- package/src/hooks/use-dates-state/use-dates-state.ts +27 -11
- package/src/internal/are-cell-props-equal.ts +63 -0
- package/src/internal/date-string-fast-path.ts +94 -0
- package/src/internal/memoize-control-props.ts +54 -0
- package/src/internal/month-days-cache.ts +77 -0
- package/src/utils/get-date-in-tab-order/get-date-in-tab-order.ts +17 -2
- package/src/utils/is-after-min-date/is-after-min-date.ts +16 -1
- package/src/utils/is-before-max-date/is-before-max-date.ts +16 -1
- package/src/utils/is-same-month/is-same-month.ts +18 -1
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
// frame/row/cell, not a per-render spread the optimiser could fold onto a whole
|
|
19
19
|
// cell.
|
|
20
20
|
// ───────────────────────────────────────────────────────────────────────────
|
|
21
|
+
import * as React from "react";
|
|
22
|
+
import { useMemo } from "react";
|
|
23
|
+
|
|
21
24
|
import dayjs from "dayjs";
|
|
22
25
|
|
|
23
26
|
import { Box } from "@knitui/components";
|
|
@@ -29,11 +32,14 @@ import {
|
|
|
29
32
|
styled,
|
|
30
33
|
withStaticProperties,
|
|
31
34
|
} from "@knitui/core";
|
|
35
|
+
import { useCallbackRef } from "@knitui/hooks";
|
|
32
36
|
|
|
33
37
|
import { type CalendarSize, CELL_SPACING } from "../cell-metrics";
|
|
34
38
|
import { useDatesContext } from "../DatesProvider";
|
|
39
|
+
import { areCellPropsEqual } from "../internal/are-cell-props-equal";
|
|
35
40
|
import { focusElement } from "../internal/focus-element";
|
|
36
41
|
import { hasPreventDefault } from "../internal/has-prevent-default";
|
|
42
|
+
import { memoizeByDate } from "../internal/memoize-control-props";
|
|
37
43
|
import { PickerControl, type PickerControlProps } from "../PickerControl";
|
|
38
44
|
import type {
|
|
39
45
|
ControlKeyboardEvent,
|
|
@@ -121,6 +127,132 @@ const YearsListCell = styled(Box, {
|
|
|
121
127
|
} as const,
|
|
122
128
|
});
|
|
123
129
|
|
|
130
|
+
/** Props for the memoized year cell — see {@link YearsListControlCell}. */
|
|
131
|
+
interface YearsListControlCellProps {
|
|
132
|
+
/** The cell's year, `YYYY-MM-DD`. */
|
|
133
|
+
year: DateStringValue;
|
|
134
|
+
/** Localized year label (resolved once per decade in the grid memo). */
|
|
135
|
+
label: string;
|
|
136
|
+
/** Resolved disabled state (bounds ∪ the per-year getter's own flag). */
|
|
137
|
+
disabled: boolean | undefined;
|
|
138
|
+
|
|
139
|
+
/** Grid position, used for roving focus. */
|
|
140
|
+
rowIndex: number;
|
|
141
|
+
cellIndex: number;
|
|
142
|
+
|
|
143
|
+
/** Roving tabindex for this cell. */
|
|
144
|
+
tabIndex: number;
|
|
145
|
+
|
|
146
|
+
size: CalendarSize;
|
|
147
|
+
fullWidth: boolean;
|
|
148
|
+
preventFocus: boolean | undefined;
|
|
149
|
+
|
|
150
|
+
/** `styles.cell` props for the wrapper. */
|
|
151
|
+
cellProps: GetProps<typeof YearsListCell> | undefined;
|
|
152
|
+
|
|
153
|
+
/** The raw `getYearControlProps(year)` result — source of the consumer's handlers. */
|
|
154
|
+
controlProps: Partial<PickerControlProps> | undefined;
|
|
155
|
+
|
|
156
|
+
/** `styles.control` merged UNDER `controlProps` — spread onto the leaf. */
|
|
157
|
+
mergedControlProps: Partial<PickerControlProps>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The `__`-prefixed grid callbacks, pre-stabilised by `YearsList` with
|
|
161
|
+
* `useCallbackRef` — an unstable identity here would defeat the memo for every
|
|
162
|
+
* cell on every render.
|
|
163
|
+
*/
|
|
164
|
+
onControlMouseEnter: (event: unknown, date: DateStringValue) => void;
|
|
165
|
+
onControlClick: (event: ControlPressEvent, date: DateStringValue) => void;
|
|
166
|
+
onControlKeyDown: (
|
|
167
|
+
event: ControlKeyboardEvent,
|
|
168
|
+
payload: { rowIndex: number; cellIndex: number; date: DateStringValue },
|
|
169
|
+
) => void;
|
|
170
|
+
getControlRef: (
|
|
171
|
+
rowIndex: number,
|
|
172
|
+
cellIndex: number,
|
|
173
|
+
control: { focus: () => void; disabled: boolean | undefined },
|
|
174
|
+
) => void;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* One year cell, memoized — the `YearsList` twin of `Month`'s `MonthDayCell` and
|
|
179
|
+
* `MonthsList`'s `MonthsListControlCell`.
|
|
180
|
+
*
|
|
181
|
+
* A RANGE year picker updates `hoveredDate` on every web hover move, re-rendering
|
|
182
|
+
* the whole picker subtree; without this boundary all 10 `PickerControl` leaves
|
|
183
|
+
* re-rendered (and 10 refs detached/reattached, re-running `__getControlRef`) for a
|
|
184
|
+
* change that moved two cells' background colour.
|
|
185
|
+
*
|
|
186
|
+
* The boundary is deliberately an INTERNAL cell, not the public `PickerControl`.
|
|
187
|
+
* Tamagui's styled-context and theme reads happen inside the leaf and still
|
|
188
|
+
* propagate past a memo, so cells keep responding to theme/size changes.
|
|
189
|
+
*/
|
|
190
|
+
const YearsListControlCell = React.memo(function YearsListControlCell({
|
|
191
|
+
year,
|
|
192
|
+
label,
|
|
193
|
+
disabled,
|
|
194
|
+
rowIndex,
|
|
195
|
+
cellIndex,
|
|
196
|
+
tabIndex,
|
|
197
|
+
size,
|
|
198
|
+
fullWidth,
|
|
199
|
+
preventFocus,
|
|
200
|
+
cellProps,
|
|
201
|
+
controlProps,
|
|
202
|
+
mergedControlProps,
|
|
203
|
+
onControlMouseEnter,
|
|
204
|
+
onControlClick,
|
|
205
|
+
onControlKeyDown,
|
|
206
|
+
getControlRef,
|
|
207
|
+
}: YearsListControlCellProps) {
|
|
208
|
+
// Tamagui hover handlers are not part of `PickerControl`'s public prop type;
|
|
209
|
+
// attach them via a precisely-typed object spread, the kit's pattern for web-only
|
|
210
|
+
// affordances (see `Month`). Native never fires it.
|
|
211
|
+
const hoverHandlers: { onHoverIn?: (event: unknown) => void } = {
|
|
212
|
+
onHoverIn: (event) => onControlMouseEnter(event, year),
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
return (
|
|
216
|
+
<YearsListCell role="cell" fullWidth={fullWidth} {...cellProps}>
|
|
217
|
+
<PickerControl
|
|
218
|
+
size={size}
|
|
219
|
+
fullWidth={fullWidth}
|
|
220
|
+
// explicit beats sugar: the `control` slot sits UNDER per-year
|
|
221
|
+
// `getYearControlProps`, and the consumer's own handlers are
|
|
222
|
+
// preserved (called before ours).
|
|
223
|
+
{...mergedControlProps}
|
|
224
|
+
disabled={disabled}
|
|
225
|
+
ref={(node) => {
|
|
226
|
+
if (node) {
|
|
227
|
+
getControlRef(rowIndex, cellIndex, {
|
|
228
|
+
focus: () => focusElement(node),
|
|
229
|
+
disabled,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}}
|
|
233
|
+
onKeyDown={(event: ControlKeyboardEvent) => {
|
|
234
|
+
controlProps?.onKeyDown?.(event);
|
|
235
|
+
onControlKeyDown(event, { rowIndex, cellIndex, date: year });
|
|
236
|
+
}}
|
|
237
|
+
onPress={(event: ControlPressEvent) => {
|
|
238
|
+
controlProps?.onPress?.(event);
|
|
239
|
+
onControlClick(event, year);
|
|
240
|
+
}}
|
|
241
|
+
onPressIn={(event: ControlPressInEvent) => {
|
|
242
|
+
controlProps?.onPressIn?.(event);
|
|
243
|
+
if (preventFocus && hasPreventDefault(event)) {
|
|
244
|
+
event.preventDefault();
|
|
245
|
+
}
|
|
246
|
+
}}
|
|
247
|
+
tabIndex={tabIndex}
|
|
248
|
+
{...hoverHandlers}
|
|
249
|
+
>
|
|
250
|
+
{controlProps?.children ?? label}
|
|
251
|
+
</PickerControl>
|
|
252
|
+
</YearsListCell>
|
|
253
|
+
);
|
|
254
|
+
}, areCellPropsEqual);
|
|
255
|
+
|
|
124
256
|
// ── 7. Per-slot `styles` sugar + per-item passthrough ───────────────────────────
|
|
125
257
|
// The kit's ONE styling model is props on the parts. `styles` is thin sugar over
|
|
126
258
|
// that: a map from named slot → that part's props, resolved through
|
|
@@ -241,71 +373,80 @@ const YearsListComponent = YearsListFrame.styleable<YearsListProps>(function Yea
|
|
|
241
373
|
// 7. Typed per-slot accessor (dev-warns unknown keys against the known set).
|
|
242
374
|
const s = slotStyles<YearsListStyles>(styles, YEARS_LIST_SLOT_KEYS, "YearsList");
|
|
243
375
|
|
|
244
|
-
const
|
|
376
|
+
const resolvedLocale = ctx.getLocale(locale);
|
|
377
|
+
|
|
378
|
+
// The 10 cells' grid, bounds and LABELS depend only on the decade, the locale,
|
|
379
|
+
// the format and the min/max bounds — never on the selection — yet they were
|
|
380
|
+
// rebuilt every render, each cell paying a `dayjs(year).locale(…).format(…)`
|
|
381
|
+
// (parse + locale clone + locale-table format) for a label fixed for the decade.
|
|
382
|
+
const yearsInfo = useMemo(() => {
|
|
383
|
+
const grid = getYearsData(decade);
|
|
384
|
+
return {
|
|
385
|
+
grid,
|
|
386
|
+
cells: grid.map((row) =>
|
|
387
|
+
row.map((year) => ({
|
|
388
|
+
year,
|
|
389
|
+
disabled: isYearDisabled({ year, minDate: minDateString, maxDate: maxDateString }),
|
|
390
|
+
label: dayjs(year).locale(resolvedLocale).format(yearsListFormat),
|
|
391
|
+
})),
|
|
392
|
+
),
|
|
393
|
+
};
|
|
394
|
+
}, [decade, minDateString, maxDateString, resolvedLocale, yearsListFormat]);
|
|
395
|
+
|
|
396
|
+
// ONE per-render cache shared by `getYearInTabOrder` (which consults the getter
|
|
397
|
+
// twice per year) and the cell loop below (a third time). Not memoized across
|
|
398
|
+
// renders — the getter closes over live selection state.
|
|
399
|
+
const resolveControlProps = memoizeByDate(getYearControlProps);
|
|
245
400
|
|
|
246
401
|
const yearInTabOrder = getYearInTabOrder({
|
|
247
|
-
years,
|
|
402
|
+
years: yearsInfo.grid,
|
|
248
403
|
minDate: minDateString,
|
|
249
404
|
maxDate: maxDateString,
|
|
250
|
-
getYearControlProps,
|
|
405
|
+
getYearControlProps: resolveControlProps,
|
|
251
406
|
});
|
|
252
407
|
|
|
253
408
|
const cellGap = withCellSpacing ? CELL_SPACING : 0;
|
|
254
409
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
410
|
+
// The cell memo can only bail out if every function prop it receives keeps a
|
|
411
|
+
// stable identity. All four of these arrive freshly built each render (the level
|
|
412
|
+
// group builds them per decade, `YearPicker` per render), so they are wrapped once
|
|
413
|
+
// here rather than being stabilised at each of their many call sites.
|
|
414
|
+
const onControlMouseEnter = useCallbackRef(__onControlMouseEnter);
|
|
415
|
+
const onControlClick = useCallbackRef(__onControlClick);
|
|
416
|
+
const onControlKeyDown = useCallbackRef(__onControlKeyDown);
|
|
417
|
+
const getControlRef = useCallbackRef(__getControlRef);
|
|
418
|
+
|
|
419
|
+
const cellSlotProps = s.get("cell");
|
|
420
|
+
|
|
421
|
+
const rows = yearsInfo.cells.map((yearsRow, rowIndex) => {
|
|
422
|
+
const cells = yearsRow.map(({ year, disabled: outOfBounds, label }, cellIndex) => {
|
|
423
|
+
const controlProps = resolveControlProps?.(year);
|
|
424
|
+
// Both sides come from `getYearsData`'s canonical `YYYY-MM-DD`, so the `YYYY`
|
|
425
|
+
// prefix compare is exactly the year-granularity `isSame` it replaces.
|
|
426
|
+
const isYearInTabOrder =
|
|
427
|
+
yearInTabOrder !== undefined && year.slice(0, 4) === yearInTabOrder.slice(0, 4);
|
|
428
|
+
const disabled = outOfBounds || controlProps?.disabled;
|
|
269
429
|
|
|
270
430
|
return (
|
|
271
|
-
<
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
__onControlKeyDown?.(event, { rowIndex, cellIndex, date: year });
|
|
291
|
-
}}
|
|
292
|
-
onPress={(event: ControlPressEvent) => {
|
|
293
|
-
controlProps?.onPress?.(event);
|
|
294
|
-
__onControlClick?.(event, year);
|
|
295
|
-
}}
|
|
296
|
-
onPressIn={(event: ControlPressInEvent) => {
|
|
297
|
-
controlProps?.onPressIn?.(event);
|
|
298
|
-
if (__preventFocus && hasPreventDefault(event)) {
|
|
299
|
-
event.preventDefault();
|
|
300
|
-
}
|
|
301
|
-
}}
|
|
302
|
-
tabIndex={__preventFocus || !isYearInTabOrder ? -1 : 0}
|
|
303
|
-
{...hoverHandlers}
|
|
304
|
-
>
|
|
305
|
-
{controlProps?.children ??
|
|
306
|
-
dayjs(year).locale(ctx.getLocale(locale)).format(yearsListFormat)}
|
|
307
|
-
</PickerControl>
|
|
308
|
-
</YearsListCell>
|
|
431
|
+
<YearsListControlCell
|
|
432
|
+
key={year}
|
|
433
|
+
year={year}
|
|
434
|
+
label={label}
|
|
435
|
+
disabled={disabled}
|
|
436
|
+
rowIndex={rowIndex}
|
|
437
|
+
cellIndex={cellIndex}
|
|
438
|
+
tabIndex={__preventFocus || !isYearInTabOrder ? -1 : 0}
|
|
439
|
+
size={size}
|
|
440
|
+
fullWidth={fullWidth}
|
|
441
|
+
preventFocus={__preventFocus}
|
|
442
|
+
cellProps={cellSlotProps}
|
|
443
|
+
controlProps={controlProps}
|
|
444
|
+
mergedControlProps={s.merge("control", controlProps)}
|
|
445
|
+
onControlMouseEnter={onControlMouseEnter}
|
|
446
|
+
onControlClick={onControlClick}
|
|
447
|
+
onControlKeyDown={onControlKeyDown}
|
|
448
|
+
getControlRef={getControlRef}
|
|
449
|
+
/>
|
|
309
450
|
);
|
|
310
451
|
});
|
|
311
452
|
|
|
@@ -8,6 +8,12 @@ interface GetYearInTabOrderInput {
|
|
|
8
8
|
years: DateStringValue[][];
|
|
9
9
|
minDate: DateStringValue | undefined;
|
|
10
10
|
maxDate: DateStringValue | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Consulted TWICE per year below (once for `disabled`, once for `selected`), so
|
|
13
|
+
* callers should pass a getter wrapped in `memoizeByDate` — the same wrapper they
|
|
14
|
+
* use for their own cell loop, so the whole render resolves each year's props
|
|
15
|
+
* exactly once. See `internal/memoize-control-props.ts`.
|
|
16
|
+
*/
|
|
11
17
|
getYearControlProps: ((year: DateStringValue) => Partial<PickerControlProps>) | undefined;
|
|
12
18
|
}
|
|
13
19
|
|
|
@@ -34,7 +40,12 @@ export function getYearInTabOrder({
|
|
|
34
40
|
return selectedYear;
|
|
35
41
|
}
|
|
36
42
|
|
|
37
|
-
|
|
43
|
+
// One formatted "today" instead of a fresh `dayjs()` (plus its `startOf`/`endOf`
|
|
44
|
+
// clones) per candidate. `years` always comes from `getYearsData`, which emits
|
|
45
|
+
// zero-padded `YYYY-MM-DD`, so comparing the `YYYY` prefix is exactly the
|
|
46
|
+
// year-granularity `isSame` this replaces.
|
|
47
|
+
const currentYearPrefix = dayjs().format("YYYY");
|
|
48
|
+
const currentYear = enabledYears.find((year) => year.slice(0, 4) === currentYearPrefix);
|
|
38
49
|
|
|
39
50
|
if (currentYear) {
|
|
40
51
|
return currentYear;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import dayjs from "dayjs";
|
|
2
2
|
|
|
3
|
+
import { isCanonicalDateString } from "../../../internal/date-string-fast-path";
|
|
3
4
|
import type { DateStringValue } from "../../../types";
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -7,11 +8,32 @@ import type { DateStringValue } from "../../../types";
|
|
|
7
8
|
* Mantine's `is-in-range`. The pair is sorted first (so callers may pass the
|
|
8
9
|
* endpoints in either order), then the bounds are widened by 1ms to the day's
|
|
9
10
|
* edges so the comparison is inclusive of both endpoints. Pure dayjs — no DOM.
|
|
11
|
+
*
|
|
12
|
+
* The Mantine spelling is the most expensive per-cell call in a range picker:
|
|
13
|
+
* `[...range].sort()` allocates an array and builds 2 dayjs instances inside the
|
|
14
|
+
* comparator, then each bound costs an instance plus `startOf`/`endOf` and
|
|
15
|
+
* `subtract`/`add` clones — ~11 instances per call, and `getControlProps` calls
|
|
16
|
+
* it for every cell (up to 3× per cell across the range/first/last checks).
|
|
17
|
+
*
|
|
18
|
+
* The widened bounds exist purely to make the endpoint comparison inclusive at
|
|
19
|
+
* DAY granularity, so for canonical zero-padded `YYYY-MM-DD` strings the whole
|
|
20
|
+
* thing reduces to `min <= date <= max` on the date prefix — exact, and with no
|
|
21
|
+
* sort, no clones and no allocations. Any non-canonical endpoint falls back to
|
|
22
|
+
* the original dayjs path.
|
|
10
23
|
*/
|
|
11
24
|
export function isInRange(
|
|
12
25
|
date: DateStringValue,
|
|
13
26
|
range: [DateStringValue, DateStringValue],
|
|
14
27
|
): boolean {
|
|
28
|
+
const [start, end] = range;
|
|
29
|
+
|
|
30
|
+
if (isCanonicalDateString(date) && isCanonicalDateString(start) && isCanonicalDateString(end)) {
|
|
31
|
+
const value = date.slice(0, 10);
|
|
32
|
+
const from = start.slice(0, 10);
|
|
33
|
+
const to = end.slice(0, 10);
|
|
34
|
+
return from <= to ? value >= from && value <= to : value >= to && value <= from;
|
|
35
|
+
}
|
|
36
|
+
|
|
15
37
|
const _range = [...range].sort((a, b) => (dayjs(a).isAfter(dayjs(b)) ? 1 : -1));
|
|
16
38
|
return (
|
|
17
39
|
dayjs(_range[0]).startOf("day").subtract(1, "ms").isBefore(date) &&
|
|
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
|
|
|
2
2
|
|
|
3
3
|
import dayjs from "dayjs";
|
|
4
4
|
|
|
5
|
+
import { isBeforeByPrefix, isSameByPrefix } from "../../internal/date-string-fast-path";
|
|
5
6
|
import type {
|
|
6
7
|
DatePickerType,
|
|
7
8
|
DatePickerValue,
|
|
@@ -184,13 +185,32 @@ export function useDatesState<Type extends DatePickerType = "default">({
|
|
|
184
185
|
}
|
|
185
186
|
: onMouseLeave;
|
|
186
187
|
|
|
188
|
+
// ── The per-cell comparison helpers ────────────────────────────────────────
|
|
189
|
+
// `getControlProps` below is called for EVERY cell of the grid (42 for a day
|
|
190
|
+
// picker, ×2 for two columns), and it consults these three times per cell — so
|
|
191
|
+
// `dayjs(a).isSame(b, level)`, at ~4 allocations a call (the instance, plus
|
|
192
|
+
// dayjs' internal `startOf`/`endOf` clones), dominated the render. Both sides
|
|
193
|
+
// are canonical `YYYY-MM-DD` in every picker, so a prefix compare at the
|
|
194
|
+
// `level`'s granularity is exact and allocation-free; `isSameByPrefix` /
|
|
195
|
+
// `isBeforeByPrefix` return `null` for anything non-canonical, which falls back
|
|
196
|
+
// to the original dayjs comparison.
|
|
197
|
+
const isSameAtLevel = (a: DateStringValue | null, b: DateStringValue | null): boolean => {
|
|
198
|
+
if (a == null || b == null) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
return isSameByPrefix(a, b, level) ?? dayjs(a).isSame(b, level);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const isStrictlyBefore = (a: DateStringValue, b: DateStringValue): boolean =>
|
|
205
|
+
isBeforeByPrefix(a, b) ?? dayjs(a).isBefore(b);
|
|
206
|
+
|
|
187
207
|
const isFirstInRange = (date: DateStringValue) => {
|
|
188
208
|
if (!rangeView[0]) {
|
|
189
209
|
return false;
|
|
190
210
|
}
|
|
191
211
|
|
|
192
|
-
if (
|
|
193
|
-
return !(hoveredDate &&
|
|
212
|
+
if (isSameAtLevel(date, rangeView[0])) {
|
|
213
|
+
return !(hoveredDate && isStrictlyBefore(hoveredDate, rangeView[0]));
|
|
194
214
|
}
|
|
195
215
|
|
|
196
216
|
return false;
|
|
@@ -198,14 +218,14 @@ export function useDatesState<Type extends DatePickerType = "default">({
|
|
|
198
218
|
|
|
199
219
|
const isLastInRange = (date: DateStringValue) => {
|
|
200
220
|
if (rangeView[1]) {
|
|
201
|
-
return
|
|
221
|
+
return isSameAtLevel(date, rangeView[1]);
|
|
202
222
|
}
|
|
203
223
|
|
|
204
224
|
if (!rangeView[0] || !hoveredDate) {
|
|
205
225
|
return false;
|
|
206
226
|
}
|
|
207
227
|
|
|
208
|
-
return
|
|
228
|
+
return isStrictlyBefore(hoveredDate, rangeView[0]) && isSameAtLevel(date, rangeView[0]);
|
|
209
229
|
};
|
|
210
230
|
|
|
211
231
|
// NOTE: Mantine also returns a `data-autofocus` web-DOM attribute here; it is
|
|
@@ -213,9 +233,7 @@ export function useDatesState<Type extends DatePickerType = "default">({
|
|
|
213
233
|
const getControlProps = (date: DateStringValue): RangeControlProps | SelectedControlProps => {
|
|
214
234
|
if (type === "range") {
|
|
215
235
|
return {
|
|
216
|
-
selected: rangeView.some(
|
|
217
|
-
(selection) => selection !== null && dayjs(selection).isSame(date, level),
|
|
218
|
-
),
|
|
236
|
+
selected: rangeView.some((selection) => isSameAtLevel(selection, date)),
|
|
219
237
|
inRange: isDateInRange(date),
|
|
220
238
|
firstInRange: isFirstInRange(date),
|
|
221
239
|
lastInRange: isLastInRange(date),
|
|
@@ -224,14 +242,12 @@ export function useDatesState<Type extends DatePickerType = "default">({
|
|
|
224
242
|
|
|
225
243
|
if (type === "multiple") {
|
|
226
244
|
return {
|
|
227
|
-
selected: storedArray.some(
|
|
228
|
-
(selection) => selection !== null && dayjs(selection).isSame(date, level),
|
|
229
|
-
),
|
|
245
|
+
selected: storedArray.some((selection) => isSameAtLevel(selection, date)),
|
|
230
246
|
};
|
|
231
247
|
}
|
|
232
248
|
|
|
233
249
|
const single = Array.isArray(stored) ? null : stored;
|
|
234
|
-
return { selected:
|
|
250
|
+
return { selected: isSameAtLevel(single, date) };
|
|
235
251
|
};
|
|
236
252
|
|
|
237
253
|
const onHoveredDateChange = (date: DateStringValue) => {
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Internal cross-platform helper — NOT exported from the public `src/index.ts`
|
|
2
|
+
// barrel.
|
|
3
|
+
//
|
|
4
|
+
// The shared `React.memo` comparator for the grid CELL components of `Month`,
|
|
5
|
+
// `MonthsList` and `YearsList`. All three grids have the same shape of problem, so
|
|
6
|
+
// they share one implementation rather than each carrying a copy.
|
|
7
|
+
|
|
8
|
+
/** A plain (non-array, non-null) object — the shape {@link areCellPropsEqual} looks one level into. */
|
|
9
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
10
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* One-level-deep `React.memo` comparator for a memoized grid cell.
|
|
15
|
+
*
|
|
16
|
+
* A plain shallow compare is not enough for these grids: a cell's per-item inputs
|
|
17
|
+
* arrive as FRESHLY BUILT objects on every render — the per-item prop getter
|
|
18
|
+
* (`getDayProps` / `getMonthControlProps` / `getYearControlProps`, which is
|
|
19
|
+
* `useDatesState`'s `getControlProps`) returns a new object by design, and
|
|
20
|
+
* `slotStyles().get()/merge()` likewise. Their CONTENTS, though, are stable
|
|
21
|
+
* booleans for all but the two or three cells whose state actually moved.
|
|
22
|
+
*
|
|
23
|
+
* So: compare props shallowly, and where both sides are plain objects, compare one
|
|
24
|
+
* level deeper. That is exactly deep enough to see "same selection state, new
|
|
25
|
+
* object" and shallow enough to stay O(props) — and it degrades safely, since any
|
|
26
|
+
* value it cannot prove equal (a nested object, a fresh function) reads as changed
|
|
27
|
+
* and simply re-renders the cell.
|
|
28
|
+
*
|
|
29
|
+
* Generic over the cell's props so each grid keeps its own precise props type; the
|
|
30
|
+
* comparator itself only ever reads keys and compares values.
|
|
31
|
+
*/
|
|
32
|
+
export function areCellPropsEqual<P extends object>(prev: P, next: P): boolean {
|
|
33
|
+
const keys = Object.keys(prev) as (keyof P)[];
|
|
34
|
+
|
|
35
|
+
if (keys.length !== Object.keys(next).length) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
const a: unknown = prev[key];
|
|
41
|
+
const b: unknown = next[key];
|
|
42
|
+
|
|
43
|
+
if (a === b) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!isPlainObject(a) || !isPlainObject(b)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const innerKeys = Object.keys(a);
|
|
52
|
+
if (innerKeys.length !== Object.keys(b).length) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
for (const innerKey of innerKeys) {
|
|
56
|
+
if (a[innerKey] !== b[innerKey]) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Internal cross-platform helper — NOT exported from the public `src/index.ts`
|
|
2
|
+
// barrel.
|
|
3
|
+
//
|
|
4
|
+
// Most date comparisons in this package run over values that are ALREADY
|
|
5
|
+
// zero-padded `YYYY-MM-DD` strings — that is the package's canonical value shape
|
|
6
|
+
// (`DateStringValue`), and `getMonthDays` / `toDateString` emit nothing else. For
|
|
7
|
+
// those inputs a lexicographic string compare is EXACT: fixed-width, zero-padded,
|
|
8
|
+
// big-endian date strings sort in chronological order, so `a <= b` on the
|
|
9
|
+
// `YYYY-MM-DD` prefix is equivalent to a day-granularity dayjs comparison, and
|
|
10
|
+
// equality on the `YYYY-MM` prefix is equivalent to a month-granularity one.
|
|
11
|
+
//
|
|
12
|
+
// The dayjs spellings cost 2+ instances (each a `new Date` plus an object) and,
|
|
13
|
+
// for the month case, 2 `format()` calls that hit the locale table and run a
|
|
14
|
+
// regex replace — 42+ times per month grid render. The string spellings allocate
|
|
15
|
+
// nothing.
|
|
16
|
+
//
|
|
17
|
+
// The comparisons that use this all keep their dayjs path as a fallback for
|
|
18
|
+
// non-canonical input (`Date` objects, unpadded strings, other formats), so the
|
|
19
|
+
// fast path is a pure optimisation and never a narrowing of the accepted input.
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Matches a leading zero-padded `YYYY-MM-DD`, the package's canonical date
|
|
23
|
+
* string. Anchored at the start and tolerant of a trailing time component, so
|
|
24
|
+
* `DateTimeStringValue` (`YYYY-MM-DD HH:mm:ss`) also qualifies — its date part
|
|
25
|
+
* is what every day-granularity comparison looks at.
|
|
26
|
+
*/
|
|
27
|
+
const CANONICAL_DATE = /^\d{4}-\d{2}-\d{2}/;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Matches a leading zero-padded `YYYY-MM`. Accepts a bare year-month as well as
|
|
31
|
+
* a full date, since month-granularity comparisons only read that prefix. An
|
|
32
|
+
* UNPADDED month (`2026-6-15`) fails to match — `\d{2}` cannot consume `6-` — and
|
|
33
|
+
* so falls through to the dayjs path, which is the point of the guard.
|
|
34
|
+
*/
|
|
35
|
+
const CANONICAL_YEAR_MONTH = /^\d{4}-\d{2}/;
|
|
36
|
+
|
|
37
|
+
/** Whether `value` is a canonical `YYYY-MM-DD…` string safe to compare as text. */
|
|
38
|
+
export function isCanonicalDateString(value: unknown): value is string {
|
|
39
|
+
return typeof value === "string" && CANONICAL_DATE.test(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether `value` starts with a canonical zero-padded `YYYY-MM`. */
|
|
43
|
+
export function isCanonicalYearMonthString(value: unknown): value is string {
|
|
44
|
+
return typeof value === "string" && CANONICAL_YEAR_MONTH.test(value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Prefix length of a canonical date string that carries each granularity. */
|
|
48
|
+
const PREFIX_LENGTH = { year: 4, month: 7, day: 10 } as const;
|
|
49
|
+
|
|
50
|
+
/** The comparison granularities the pickers use (`useDatesState`'s `level`). */
|
|
51
|
+
export type DateGranularity = keyof typeof PREFIX_LENGTH;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `dayjs(a).isSame(b, level)` for canonical date strings, as a prefix compare.
|
|
55
|
+
*
|
|
56
|
+
* Returns `null` when either side is NOT canonical, so callers can fall back to
|
|
57
|
+
* the dayjs comparison rather than silently changing semantics — the same
|
|
58
|
+
* belt-and-braces shape as the predicates above.
|
|
59
|
+
*
|
|
60
|
+
* This is the single hottest comparison in a range picker: `getControlProps`
|
|
61
|
+
* runs it for `selected`, `firstInRange` and `lastInRange` on EVERY cell, so a
|
|
62
|
+
* 42-cell grid pays for it well over a hundred times per render — and a web
|
|
63
|
+
* hover move triggers a full re-render.
|
|
64
|
+
*/
|
|
65
|
+
export function isSameByPrefix(
|
|
66
|
+
a: string | null | undefined,
|
|
67
|
+
b: string | null | undefined,
|
|
68
|
+
level: DateGranularity,
|
|
69
|
+
): boolean | null {
|
|
70
|
+
if (!isCanonicalDateString(a) || !isCanonicalDateString(b)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const length = PREFIX_LENGTH[level];
|
|
75
|
+
return a.slice(0, length) === b.slice(0, length);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `dayjs(a).isBefore(b)` for canonical date strings, as a prefix compare.
|
|
80
|
+
*
|
|
81
|
+
* Both sides parse to local midnight, so comparing the `YYYY-MM-DD` prefix
|
|
82
|
+
* lexicographically is equivalent to dayjs' millisecond comparison. Returns
|
|
83
|
+
* `null` when either side is not canonical.
|
|
84
|
+
*/
|
|
85
|
+
export function isBeforeByPrefix(
|
|
86
|
+
a: string | null | undefined,
|
|
87
|
+
b: string | null | undefined,
|
|
88
|
+
): boolean | null {
|
|
89
|
+
if (!isCanonicalDateString(a) || !isCanonicalDateString(b)) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return a.slice(0, 10) < b.slice(0, 10);
|
|
94
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Internal cross-platform helper — NOT exported from the public `src/index.ts`
|
|
2
|
+
// barrel.
|
|
3
|
+
//
|
|
4
|
+
// The grid components (`Month`, `MonthsList`, `YearsList`) each consult their
|
|
5
|
+
// per-item prop getter (`getDayProps` / `getMonthControlProps` /
|
|
6
|
+
// `getYearControlProps`) from MORE THAN ONE place in a single render: once from
|
|
7
|
+
// the `…InTabOrder` helper's "which cell is enabled" filter, again from its
|
|
8
|
+
// "which cell is selected" scan, and a third time in the cell loop itself.
|
|
9
|
+
//
|
|
10
|
+
// Those getters are NOT cheap. `DatePicker` wires `getDayProps` to
|
|
11
|
+
// `useDatesState`'s `getControlProps`, which for `type="range"` runs
|
|
12
|
+
// `rangeView.some(isSame)` + `isDateInRange` + `isFirstInRange` +
|
|
13
|
+
// `isLastInRange` — on the order of 20 dayjs instances and 2 array allocations
|
|
14
|
+
// per call. Three calls × 42 cells is ~2,500 dayjs instances spent re-deriving
|
|
15
|
+
// the SAME answer.
|
|
16
|
+
//
|
|
17
|
+
// Wrapping the getter in a per-render `Map` collapses that to one real call per
|
|
18
|
+
// date. The cache MUST be per render (created during render, discarded with it):
|
|
19
|
+
// the getters close over live selection/hover state and their identity changes
|
|
20
|
+
// every render, so a cache that outlived the render would serve stale props.
|
|
21
|
+
|
|
22
|
+
import type { DateStringValue } from "../types";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Wrap a per-date prop getter so each distinct date is resolved at most once.
|
|
26
|
+
*
|
|
27
|
+
* Returns `undefined` when `getter` is `undefined`, preserving the optionality
|
|
28
|
+
* of the original prop so call sites keep their `getter?.(date)` shape and their
|
|
29
|
+
* result stays `T | undefined`. Call this ONCE PER RENDER and share the result
|
|
30
|
+
* with every consumer in that render — see the note above on why the cache must
|
|
31
|
+
* not be memoized across renders.
|
|
32
|
+
*/
|
|
33
|
+
export function memoizeByDate<T>(
|
|
34
|
+
getter: ((date: DateStringValue) => T) | undefined,
|
|
35
|
+
): ((date: DateStringValue) => T) | undefined {
|
|
36
|
+
if (!getter) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const cache = new Map<DateStringValue, T>();
|
|
41
|
+
|
|
42
|
+
return (date: DateStringValue): T => {
|
|
43
|
+
const cached = cache.get(date);
|
|
44
|
+
// `has` (not a truthiness test) so a getter legitimately returning
|
|
45
|
+
// `undefined`/`false` is still cached rather than recomputed every hit.
|
|
46
|
+
if (cached !== undefined || cache.has(date)) {
|
|
47
|
+
return cached as T;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const value = getter(date);
|
|
51
|
+
cache.set(date, value);
|
|
52
|
+
return value;
|
|
53
|
+
};
|
|
54
|
+
}
|