@knitui/dates 0.5.0 → 0.6.0

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.
Files changed (143) hide show
  1. package/lib/commonjs/DatesProvider/DatesProvider.js +33 -4
  2. package/lib/commonjs/DatesProvider/DatesProvider.js.map +1 -1
  3. package/lib/commonjs/Day/Day.js +45 -7
  4. package/lib/commonjs/Day/Day.js.map +1 -1
  5. package/lib/commonjs/Month/Month.js +266 -80
  6. package/lib/commonjs/Month/Month.js.map +1 -1
  7. package/lib/commonjs/MonthLevel/MonthLevel.js +19 -3
  8. package/lib/commonjs/MonthLevel/MonthLevel.js.map +1 -1
  9. package/lib/commonjs/MonthsList/MonthsList.js +160 -60
  10. package/lib/commonjs/MonthsList/MonthsList.js.map +1 -1
  11. package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
  12. package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
  13. package/lib/commonjs/TimeControlsList/TimeControl.js +15 -1
  14. package/lib/commonjs/TimeControlsList/TimeControl.js.map +1 -1
  15. package/lib/commonjs/TimeControlsList/TimeControlsList.js +16 -3
  16. package/lib/commonjs/TimeControlsList/TimeControlsList.js.map +1 -1
  17. package/lib/commonjs/WeekdaysRow/WeekdaysRow.js +23 -4
  18. package/lib/commonjs/WeekdaysRow/WeekdaysRow.js.map +1 -1
  19. package/lib/commonjs/YearsList/YearsList.js +155 -60
  20. package/lib/commonjs/YearsList/YearsList.js.map +1 -1
  21. package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
  22. package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
  23. package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
  24. package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
  25. package/lib/commonjs/hooks/use-dates-state/use-dates-state.js +25 -7
  26. package/lib/commonjs/hooks/use-dates-state/use-dates-state.js.map +1 -1
  27. package/lib/commonjs/internal/are-cell-props-equal.js +64 -0
  28. package/lib/commonjs/internal/are-cell-props-equal.js.map +1 -0
  29. package/lib/commonjs/internal/date-string-fast-path.js +98 -0
  30. package/lib/commonjs/internal/date-string-fast-path.js.map +1 -0
  31. package/lib/commonjs/internal/memoize-control-props.js +54 -0
  32. package/lib/commonjs/internal/memoize-control-props.js.map +1 -0
  33. package/lib/commonjs/internal/month-days-cache.js +71 -0
  34. package/lib/commonjs/internal/month-days-cache.js.map +1 -0
  35. package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
  36. package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
  37. package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js +14 -1
  38. package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js.map +1 -1
  39. package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js +14 -1
  40. package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js.map +1 -1
  41. package/lib/commonjs/utils/is-same-month/is-same-month.js +17 -1
  42. package/lib/commonjs/utils/is-same-month/is-same-month.js.map +1 -1
  43. package/lib/module/DatesProvider/DatesProvider.js +34 -5
  44. package/lib/module/DatesProvider/DatesProvider.js.map +1 -1
  45. package/lib/module/Day/Day.js +46 -8
  46. package/lib/module/Day/Day.js.map +1 -1
  47. package/lib/module/Month/Month.js +267 -82
  48. package/lib/module/Month/Month.js.map +1 -1
  49. package/lib/module/MonthLevel/MonthLevel.js +19 -3
  50. package/lib/module/MonthLevel/MonthLevel.js.map +1 -1
  51. package/lib/module/MonthsList/MonthsList.js +159 -60
  52. package/lib/module/MonthsList/MonthsList.js.map +1 -1
  53. package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
  54. package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
  55. package/lib/module/TimeControlsList/TimeControl.js +15 -1
  56. package/lib/module/TimeControlsList/TimeControl.js.map +1 -1
  57. package/lib/module/TimeControlsList/TimeControlsList.js +16 -3
  58. package/lib/module/TimeControlsList/TimeControlsList.js.map +1 -1
  59. package/lib/module/WeekdaysRow/WeekdaysRow.js +23 -4
  60. package/lib/module/WeekdaysRow/WeekdaysRow.js.map +1 -1
  61. package/lib/module/YearsList/YearsList.js +154 -60
  62. package/lib/module/YearsList/YearsList.js.map +1 -1
  63. package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
  64. package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
  65. package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
  66. package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
  67. package/lib/module/hooks/use-dates-state/use-dates-state.js +25 -7
  68. package/lib/module/hooks/use-dates-state/use-dates-state.js.map +1 -1
  69. package/lib/module/internal/are-cell-props-equal.js +60 -0
  70. package/lib/module/internal/are-cell-props-equal.js.map +1 -0
  71. package/lib/module/internal/date-string-fast-path.js +91 -0
  72. package/lib/module/internal/date-string-fast-path.js.map +1 -0
  73. package/lib/module/internal/memoize-control-props.js +50 -0
  74. package/lib/module/internal/memoize-control-props.js.map +1 -0
  75. package/lib/module/internal/month-days-cache.js +68 -0
  76. package/lib/module/internal/month-days-cache.js.map +1 -0
  77. package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
  78. package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
  79. package/lib/module/utils/is-after-min-date/is-after-min-date.js +14 -1
  80. package/lib/module/utils/is-after-min-date/is-after-min-date.js.map +1 -1
  81. package/lib/module/utils/is-before-max-date/is-before-max-date.js +14 -1
  82. package/lib/module/utils/is-before-max-date/is-before-max-date.js.map +1 -1
  83. package/lib/module/utils/is-same-month/is-same-month.js +17 -1
  84. package/lib/module/utils/is-same-month/is-same-month.js.map +1 -1
  85. package/lib/typescript/DatesProvider/DatesProvider.d.ts.map +1 -1
  86. package/lib/typescript/Day/Day.d.ts +7 -7
  87. package/lib/typescript/Day/Day.d.ts.map +1 -1
  88. package/lib/typescript/Month/Month.d.ts +3 -2
  89. package/lib/typescript/Month/Month.d.ts.map +1 -1
  90. package/lib/typescript/MonthLevel/MonthLevel.d.ts.map +1 -1
  91. package/lib/typescript/MonthsList/MonthsList.d.ts +3 -2
  92. package/lib/typescript/MonthsList/MonthsList.d.ts.map +1 -1
  93. package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts +6 -0
  94. package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts.map +1 -1
  95. package/lib/typescript/TimeControlsList/TimeControl.d.ts +13 -0
  96. package/lib/typescript/TimeControlsList/TimeControl.d.ts.map +1 -1
  97. package/lib/typescript/TimeControlsList/TimeControlsList.d.ts.map +1 -1
  98. package/lib/typescript/WeekdaysRow/WeekdaysRow.d.ts.map +1 -1
  99. package/lib/typescript/YearsList/YearsList.d.ts +3 -2
  100. package/lib/typescript/YearsList/YearsList.d.ts.map +1 -1
  101. package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts +6 -0
  102. package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts.map +1 -1
  103. package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts +12 -0
  104. package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts.map +1 -1
  105. package/lib/typescript/hooks/use-dates-state/use-dates-state.d.ts.map +1 -1
  106. package/lib/typescript/internal/are-cell-props-equal.d.ts +21 -0
  107. package/lib/typescript/internal/are-cell-props-equal.d.ts.map +1 -0
  108. package/lib/typescript/internal/date-string-fast-path.d.ts +35 -0
  109. package/lib/typescript/internal/date-string-fast-path.d.ts.map +1 -0
  110. package/lib/typescript/internal/memoize-control-props.d.ts +12 -0
  111. package/lib/typescript/internal/memoize-control-props.d.ts.map +1 -0
  112. package/lib/typescript/internal/month-days-cache.d.ts +19 -0
  113. package/lib/typescript/internal/month-days-cache.d.ts.map +1 -0
  114. package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts +9 -1
  115. package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts.map +1 -1
  116. package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts +6 -0
  117. package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts.map +1 -1
  118. package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts +6 -0
  119. package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts.map +1 -1
  120. package/lib/typescript/utils/is-same-month/is-same-month.d.ts +13 -1
  121. package/lib/typescript/utils/is-same-month/is-same-month.d.ts.map +1 -1
  122. package/package.json +4 -4
  123. package/src/DatesProvider/DatesProvider.tsx +27 -5
  124. package/src/Day/Day.tsx +51 -10
  125. package/src/Month/Month.tsx +357 -80
  126. package/src/MonthLevel/MonthLevel.tsx +35 -16
  127. package/src/MonthsList/MonthsList.tsx +201 -55
  128. package/src/MonthsList/get-month-in-tab-order/get-month-in-tab-order.ts +12 -1
  129. package/src/TimeControlsList/TimeControl.tsx +16 -1
  130. package/src/TimeControlsList/TimeControlsList.tsx +15 -3
  131. package/src/WeekdaysRow/WeekdaysRow.tsx +29 -5
  132. package/src/YearsList/YearsList.tsx +196 -55
  133. package/src/YearsList/get-year-in-tab-order/get-year-in-tab-order.ts +12 -1
  134. package/src/hooks/use-dates-state/is-in-range/is-in-range.ts +22 -0
  135. package/src/hooks/use-dates-state/use-dates-state.ts +27 -11
  136. package/src/internal/are-cell-props-equal.ts +63 -0
  137. package/src/internal/date-string-fast-path.ts +94 -0
  138. package/src/internal/memoize-control-props.ts +54 -0
  139. package/src/internal/month-days-cache.ts +77 -0
  140. package/src/utils/get-date-in-tab-order/get-date-in-tab-order.ts +17 -2
  141. package/src/utils/is-after-min-date/is-after-min-date.ts +16 -1
  142. package/src/utils/is-before-max-date/is-before-max-date.ts +16 -1
  143. package/src/utils/is-same-month/is-same-month.ts +18 -1
@@ -17,6 +17,9 @@
17
17
  // prop (checklist #15): `fullWidth` is a boolean VARIANT on the frame/row/cell,
18
18
  // not a per-render spread the optimiser could fold onto a whole cell.
19
19
  // ───────────────────────────────────────────────────────────────────────────
20
+ import * as React from "react";
21
+ import { useMemo } from "react";
22
+
20
23
  import dayjs from "dayjs";
21
24
 
22
25
  import { Box } from "@knitui/components";
@@ -28,11 +31,14 @@ import {
28
31
  styled,
29
32
  withStaticProperties,
30
33
  } from "@knitui/core";
34
+ import { useCallbackRef } from "@knitui/hooks";
31
35
 
32
36
  import { type CalendarSize, CELL_SPACING } from "../cell-metrics";
33
37
  import { useDatesContext } from "../DatesProvider";
38
+ import { areCellPropsEqual } from "../internal/are-cell-props-equal";
34
39
  import { focusElement } from "../internal/focus-element";
35
40
  import { hasPreventDefault } from "../internal/has-prevent-default";
41
+ import { memoizeByDate } from "../internal/memoize-control-props";
36
42
  import { PickerControl, type PickerControlProps } from "../PickerControl";
37
43
  import type {
38
44
  ControlKeyboardEvent,
@@ -120,6 +126,134 @@ const MonthsListCell = styled(Box, {
120
126
  } as const,
121
127
  });
122
128
 
129
+ /** Props for the memoized month cell — see {@link MonthsListControlCell}. */
130
+ interface MonthsListControlCellProps {
131
+ /** The cell's month, `YYYY-MM-DD`. */
132
+ month: DateStringValue;
133
+ /** Localized month label (resolved once per year in the grid memo). */
134
+ label: string;
135
+ /** Resolved disabled state (bounds ∪ the per-month getter's own flag). */
136
+ disabled: boolean | undefined;
137
+
138
+ /** Grid position, used for roving focus. */
139
+ rowIndex: number;
140
+ cellIndex: number;
141
+
142
+ /** Roving tabindex for this cell. */
143
+ tabIndex: number;
144
+
145
+ size: CalendarSize;
146
+ fullWidth: boolean;
147
+ preventFocus: boolean | undefined;
148
+
149
+ /** `styles.cell` props for the wrapper. */
150
+ cellProps: GetProps<typeof MonthsListCell> | undefined;
151
+
152
+ /** The raw `getMonthControlProps(month)` result — source of the consumer's handlers. */
153
+ controlProps: Partial<PickerControlProps> | undefined;
154
+
155
+ /** `styles.control` merged UNDER `controlProps` — spread onto the leaf. */
156
+ mergedControlProps: Partial<PickerControlProps>;
157
+
158
+ /**
159
+ * The `__`-prefixed grid callbacks, pre-stabilised by `MonthsList` with
160
+ * `useCallbackRef` — an unstable identity here would defeat the memo for every
161
+ * cell on every render.
162
+ */
163
+ onControlMouseEnter: (event: unknown, date: DateStringValue) => void;
164
+ onControlClick: (event: ControlPressEvent, date: DateStringValue) => void;
165
+ onControlKeyDown: (
166
+ event: ControlKeyboardEvent,
167
+ payload: { rowIndex: number; cellIndex: number; date: DateStringValue },
168
+ ) => void;
169
+ getControlRef: (
170
+ rowIndex: number,
171
+ cellIndex: number,
172
+ control: { focus: () => void; disabled: boolean | undefined },
173
+ ) => void;
174
+ }
175
+
176
+ /**
177
+ * One month cell, memoized — the `MonthsList` twin of `Month`'s `MonthDayCell`.
178
+ *
179
+ * The grid re-renders for reasons that concern only one or two cells. Most sharply,
180
+ * a RANGE month picker updates `hoveredDate` on every web hover move, which
181
+ * re-renders the whole picker subtree; without a memo boundary that meant all 12
182
+ * `PickerControl` leaves re-rendering (and 12 refs detaching/reattaching, re-running
183
+ * `__getControlRef` and rebuilding the refs matrix) for a change that moved two
184
+ * cells' background colour.
185
+ *
186
+ * The boundary is deliberately an INTERNAL cell, not the public `PickerControl`,
187
+ * whose props contract stays exactly as it was. Tamagui's styled-context and theme
188
+ * reads happen inside the leaf and still propagate past a memo, so cells keep
189
+ * responding to theme/size changes.
190
+ */
191
+ const MonthsListControlCell = React.memo(function MonthsListControlCell({
192
+ month,
193
+ label,
194
+ disabled,
195
+ rowIndex,
196
+ cellIndex,
197
+ tabIndex,
198
+ size,
199
+ fullWidth,
200
+ preventFocus,
201
+ cellProps,
202
+ controlProps,
203
+ mergedControlProps,
204
+ onControlMouseEnter,
205
+ onControlClick,
206
+ onControlKeyDown,
207
+ getControlRef,
208
+ }: MonthsListControlCellProps) {
209
+ // Tamagui hover handlers are not part of `PickerControl`'s public prop type;
210
+ // attach them via a precisely-typed object spread, the kit's pattern for web-only
211
+ // affordances (see `Month`). Native never fires it.
212
+ const hoverHandlers: { onHoverIn?: (event: unknown) => void } = {
213
+ onHoverIn: (event) => onControlMouseEnter(event, month),
214
+ };
215
+
216
+ return (
217
+ <MonthsListCell role="cell" fullWidth={fullWidth} {...cellProps}>
218
+ <PickerControl
219
+ size={size}
220
+ fullWidth={fullWidth}
221
+ // explicit beats sugar: the `control` slot sits UNDER per-month
222
+ // `getMonthControlProps`, and the consumer's own handlers are
223
+ // preserved (called before ours).
224
+ {...mergedControlProps}
225
+ disabled={disabled}
226
+ ref={(node) => {
227
+ if (node) {
228
+ getControlRef(rowIndex, cellIndex, {
229
+ focus: () => focusElement(node),
230
+ disabled,
231
+ });
232
+ }
233
+ }}
234
+ onKeyDown={(event: ControlKeyboardEvent) => {
235
+ controlProps?.onKeyDown?.(event);
236
+ onControlKeyDown(event, { rowIndex, cellIndex, date: month });
237
+ }}
238
+ onPress={(event: ControlPressEvent) => {
239
+ controlProps?.onPress?.(event);
240
+ onControlClick(event, month);
241
+ }}
242
+ onPressIn={(event: ControlPressInEvent) => {
243
+ controlProps?.onPressIn?.(event);
244
+ if (preventFocus && hasPreventDefault(event)) {
245
+ event.preventDefault();
246
+ }
247
+ }}
248
+ tabIndex={tabIndex}
249
+ {...hoverHandlers}
250
+ >
251
+ {controlProps?.children ?? label}
252
+ </PickerControl>
253
+ </MonthsListCell>
254
+ );
255
+ }, areCellPropsEqual);
256
+
123
257
  // ── 7. Per-slot `styles` sugar + per-item passthrough ───────────────────────────
124
258
  // The kit's ONE styling model is props on the parts. `styles` is thin sugar over
125
259
  // that: a map from named slot → that part's props, resolved through
@@ -243,71 +377,83 @@ const MonthsListComponent = MonthsListFrame.styleable<MonthsListProps>(
243
377
  // 7. Typed per-slot accessor (dev-warns unknown keys against the known set).
244
378
  const s = slotStyles<MonthsListStyles>(styles, MONTHS_LIST_SLOT_KEYS, "MonthsList");
245
379
 
246
- const months = getMonthsData(year);
380
+ const resolvedLocale = ctx.getLocale(locale);
381
+
382
+ // The 12 cells' grid, bounds and LABELS depend only on the year, the locale,
383
+ // the format and the min/max bounds — never on the selection. They were
384
+ // nonetheless rebuilt on every render: `getMonthsData` costs ~12 dayjs
385
+ // instances plus 12 `format` calls, and each cell then paid another
386
+ // `dayjs(month).locale(…).format(monthsListFormat)` — a parse, a locale clone
387
+ // and a locale-table format — for a label that is fixed for the whole year.
388
+ const monthsInfo = useMemo(() => {
389
+ const grid = getMonthsData(year);
390
+ return {
391
+ grid,
392
+ cells: grid.map((row) =>
393
+ row.map((month) => ({
394
+ month,
395
+ disabled: isMonthDisabled({ month, minDate: minDateString, maxDate: maxDateString }),
396
+ label: dayjs(month).locale(resolvedLocale).format(monthsListFormat),
397
+ })),
398
+ ),
399
+ };
400
+ }, [year, minDateString, maxDateString, resolvedLocale, monthsListFormat]);
401
+
402
+ // ONE per-render cache shared by `getMonthInTabOrder` (which consults the
403
+ // getter twice per month) and the cell loop below (a third time). Not memoized
404
+ // across renders — the getter closes over live selection state.
405
+ const resolveControlProps = memoizeByDate(getMonthControlProps);
247
406
 
248
407
  const monthInTabOrder = getMonthInTabOrder({
249
- months,
408
+ months: monthsInfo.grid,
250
409
  minDate: minDateString,
251
410
  maxDate: maxDateString,
252
- getMonthControlProps,
411
+ getMonthControlProps: resolveControlProps,
253
412
  });
254
413
 
255
414
  const cellGap = withCellSpacing ? CELL_SPACING : 0;
256
415
 
257
- const rows = months.map((monthsRow, rowIndex) => {
258
- const cells = monthsRow.map((month, cellIndex) => {
259
- const controlProps = getMonthControlProps?.(month);
260
- const isMonthInTabOrder = dayjs(month).isSame(monthInTabOrder, "month");
261
- const disabled =
262
- isMonthDisabled({ month, minDate: minDateString, maxDate: maxDateString }) ||
263
- controlProps?.disabled;
264
-
265
- // Tamagui hover handlers are not part of `PickerControl`'s public prop
266
- // type; attach them via a precisely-typed object spread, the kit's pattern
267
- // for web-only affordances (see `Month`). Native never fires it.
268
- const hoverHandlers: { onHoverIn?: (event: unknown) => void } = {
269
- onHoverIn: (event) => __onControlMouseEnter?.(event, month),
270
- };
416
+ // The cell memo can only bail out if every function prop it receives keeps a
417
+ // stable identity. All four of these arrive freshly built each render (the level
418
+ // group builds them per year, `MonthPicker` per render), so they are wrapped
419
+ // once here rather than being stabilised at each of their many call sites.
420
+ const onControlMouseEnter = useCallbackRef(__onControlMouseEnter);
421
+ const onControlClick = useCallbackRef(__onControlClick);
422
+ const onControlKeyDown = useCallbackRef(__onControlKeyDown);
423
+ const getControlRef = useCallbackRef(__getControlRef);
424
+
425
+ const cellSlotProps = s.get("cell");
426
+
427
+ const rows = monthsInfo.cells.map((monthsRow, rowIndex) => {
428
+ const cells = monthsRow.map(({ month, disabled: outOfBounds, label }, cellIndex) => {
429
+ const controlProps = resolveControlProps?.(month);
430
+ // Both sides come from `getMonthsData`'s canonical `YYYY-MM-DD`, so the
431
+ // `YYYY-MM` prefix compare is exactly the month-granularity `isSame` it
432
+ // replaces — without a dayjs instance per cell.
433
+ const isMonthInTabOrder =
434
+ monthInTabOrder !== undefined && month.slice(0, 7) === monthInTabOrder.slice(0, 7);
435
+ const disabled = outOfBounds || controlProps?.disabled;
271
436
 
272
437
  return (
273
- <MonthsListCell key={month} role="cell" fullWidth={fullWidth} {...s.get("cell")}>
274
- <PickerControl
275
- size={size}
276
- fullWidth={fullWidth}
277
- // explicit beats sugar: the `control` slot sits UNDER per-month
278
- // `getMonthControlProps`, and the consumer's own handlers are
279
- // preserved (called before ours).
280
- {...s.merge("control", controlProps)}
281
- disabled={disabled}
282
- ref={(node) => {
283
- if (node) {
284
- __getControlRef?.(rowIndex, cellIndex, {
285
- focus: () => focusElement(node),
286
- disabled,
287
- });
288
- }
289
- }}
290
- onKeyDown={(event: ControlKeyboardEvent) => {
291
- controlProps?.onKeyDown?.(event);
292
- __onControlKeyDown?.(event, { rowIndex, cellIndex, date: month });
293
- }}
294
- onPress={(event: ControlPressEvent) => {
295
- controlProps?.onPress?.(event);
296
- __onControlClick?.(event, month);
297
- }}
298
- onPressIn={(event: ControlPressInEvent) => {
299
- controlProps?.onPressIn?.(event);
300
- if (__preventFocus && hasPreventDefault(event)) {
301
- event.preventDefault();
302
- }
303
- }}
304
- tabIndex={__preventFocus || !isMonthInTabOrder ? -1 : 0}
305
- {...hoverHandlers}
306
- >
307
- {controlProps?.children ??
308
- dayjs(month).locale(ctx.getLocale(locale)).format(monthsListFormat)}
309
- </PickerControl>
310
- </MonthsListCell>
438
+ <MonthsListControlCell
439
+ key={month}
440
+ month={month}
441
+ label={label}
442
+ disabled={disabled}
443
+ rowIndex={rowIndex}
444
+ cellIndex={cellIndex}
445
+ tabIndex={__preventFocus || !isMonthInTabOrder ? -1 : 0}
446
+ size={size}
447
+ fullWidth={fullWidth}
448
+ preventFocus={__preventFocus}
449
+ cellProps={cellSlotProps}
450
+ controlProps={controlProps}
451
+ mergedControlProps={s.merge("control", controlProps)}
452
+ onControlMouseEnter={onControlMouseEnter}
453
+ onControlClick={onControlClick}
454
+ onControlKeyDown={onControlKeyDown}
455
+ getControlRef={getControlRef}
456
+ />
311
457
  );
312
458
  });
313
459
 
@@ -8,6 +8,12 @@ interface GetMonthInTabOrderInput {
8
8
  months: DateStringValue[][];
9
9
  minDate: DateStringValue | undefined;
10
10
  maxDate: DateStringValue | undefined;
11
+ /**
12
+ * Consulted TWICE per month below (once for `disabled`, once for `selected`),
13
+ * so callers should pass a getter wrapped in `memoizeByDate` — the same wrapper
14
+ * they use for their own cell loop, so the whole render resolves each month's
15
+ * props exactly once. See `internal/memoize-control-props.ts`.
16
+ */
11
17
  getMonthControlProps: ((month: DateStringValue) => Partial<PickerControlProps>) | undefined;
12
18
  }
13
19
 
@@ -34,7 +40,12 @@ export function getMonthInTabOrder({
34
40
  return selectedMonth;
35
41
  }
36
42
 
37
- const currentMonth = enabledMonths.find((month) => dayjs().isSame(month, "month"));
43
+ // One formatted "today" instead of a fresh `dayjs()` (plus its `startOf`/`endOf`
44
+ // clones) per candidate. `months` always comes from `getMonthsData`, which emits
45
+ // zero-padded `YYYY-MM-DD`, so comparing the `YYYY-MM` prefix is exactly the
46
+ // month-granularity `isSame` this replaces.
47
+ const currentMonthPrefix = dayjs().format("YYYY-MM");
48
+ const currentMonth = enabledMonths.find((month) => month.slice(0, 7) === currentMonthPrefix);
38
49
 
39
50
  if (currentMonth) {
40
51
  return currentMonth;
@@ -121,6 +121,21 @@ function TimeControlImpl<Value extends number | string>(
121
121
  );
122
122
  }
123
123
 
124
- export const TimeControl = React.forwardRef(TimeControlImpl) as <Value extends number | string>(
124
+ /**
125
+ * `React.memo` matters here more than anywhere else in the time picker: a `24h`
126
+ * dropdown with seconds renders 24 + 60 + 60 = 144 of these, and EVERY keystroke
127
+ * in the segment inputs updates `controller.values`, re-rendering `TimePicker` and
128
+ * with it all three columns. Only the two controls whose `active` flips actually
129
+ * need to re-render.
130
+ *
131
+ * All the props are primitives (`value`, `active`, `size`, `role`) except
132
+ * `onSelect`, which `TimeControlsList` stabilises with `useCallbackRef` — so the
133
+ * shallow compare is sound. The `ref` (which `memo` does not compare) is only ever
134
+ * attached to the ACTIVE control, and `active` is itself a compared prop, so a ref
135
+ * change never happens without a prop change to drive the re-render.
136
+ */
137
+ export const TimeControl = React.memo(React.forwardRef(TimeControlImpl)) as <
138
+ Value extends number | string,
139
+ >(
125
140
  props: TimeControlProps<Value> & React.RefAttributes<TamaguiElement>,
126
141
  ) => React.ReactElement | null;
@@ -37,6 +37,7 @@ import {
37
37
  type TamaguiElement,
38
38
  withStaticProperties,
39
39
  } from "@knitui/core";
40
+ import { useCallbackRef } from "@knitui/hooks";
40
41
 
41
42
  import { type CalendarSize, CELL_SPACING } from "../cell-metrics";
42
43
  import { TimeControl, type TimeControlProps } from "./TimeControl";
@@ -204,8 +205,19 @@ function TimeControlsListComponent({
204
205
  "TimeControlsList",
205
206
  );
206
207
 
207
- const range = getValuesRange(min, max, step);
208
- const ordered = reversed ? [...range].reverse() : range;
208
+ // The column contents are a pure function of four primitives, but `TimePicker`
209
+ // re-renders on EVERY keystroke in the segment inputs (each one updates
210
+ // `controller.values`), which rebuilt all three columns' arrays — 24 + 60 + 60
211
+ // entries plus a `[...range].reverse()` copy — for a list that never changed.
212
+ const ordered = React.useMemo(() => {
213
+ const range = getValuesRange(min, max, step);
214
+ return reversed ? range.reverse() : range;
215
+ }, [min, max, step, reversed]);
216
+
217
+ // A stable `onSelect` is what makes the `React.memo` on `TimeControl` effective:
218
+ // the controller hands us a fresh arrow every render, which would otherwise fail
219
+ // the shallow prop compare for all 144 controls on every keystroke.
220
+ const stableOnSelect = useCallbackRef(onSelect);
209
221
 
210
222
  React.useEffect(() => {
211
223
  if (value === null) {
@@ -260,7 +272,7 @@ function TimeControlsListComponent({
260
272
  ref={active ? activeRef : undefined}
261
273
  value={control}
262
274
  active={active}
263
- onSelect={onSelect}
275
+ onSelect={stableOnSelect}
264
276
  size={size}
265
277
  role="option"
266
278
  {...s.merge("control", controlProps)}
@@ -15,6 +15,8 @@
15
15
  // fold (#15). a11y is set on both axes (#11): web `role="row"`/`role="columnheader"`
16
16
  // plus a native `accessibilityRole` counterpart.
17
17
  // ───────────────────────────────────────────────────────────────────────────
18
+ import { useMemo } from "react";
19
+
18
20
  import { Box, Text } from "@knitui/components";
19
21
  import {
20
22
  createStyledContext,
@@ -183,11 +185,33 @@ const WeekdaysRowComponent = WeekdaysRowFrame.styleable<WeekdaysRowProps>(
183
185
  // 9. Locale + `firstDayOfWeek` come from `DatesProvider` (consumer props win).
184
186
  const ctx = useDatesContext();
185
187
 
186
- const weekdays = getWeekdayNames({
187
- locale: ctx.getLocale(locale),
188
- format: weekdayFormat,
189
- firstDayOfWeek: ctx.getFirstDayOfWeek(firstDayOfWeek),
190
- });
188
+ const resolvedLocale = ctx.getLocale(locale);
189
+ const resolvedFirstDayOfWeek = ctx.getFirstDayOfWeek(firstDayOfWeek);
190
+
191
+ // The 7 labels are a pure function of these three inputs, and building them is
192
+ // not cheap: `getWeekdayNames` spends ~22 dayjs instances (a base date plus, per
193
+ // day, an `add` → `locale` → `format` clone chain) and 7 locale-table lookups.
194
+ // `Month` renders this row unconditionally, so unmemoized it was rebuilt on
195
+ // every grid render — including the ones caused by nothing but a web hover
196
+ // moving one cell over (`hoveredDate` lives above the whole calendar), ×
197
+ // `numberOfColumns`. Memoized on the resolved primitives, a hover frame reuses
198
+ // the labels and only a real locale/format/first-day change rebuilds them.
199
+ //
200
+ // Deliberately a per-instance `useMemo` and NOT a module-level cache (the
201
+ // pattern `../internal/month-days-cache.ts` uses for the day grid): the labels
202
+ // are only pure for a WEEKDAY-shaped format. `getWeekdayNames` derives its base
203
+ // date from `dayjs()` — today — so a date-bearing `weekdayFormat` (or a function
204
+ // renderer, whose identity is not a usable cache key) would produce output that
205
+ // a process-lifetime cache could serve stale across a midnight rollover.
206
+ const weekdays = useMemo(
207
+ () =>
208
+ getWeekdayNames({
209
+ locale: resolvedLocale,
210
+ format: weekdayFormat,
211
+ firstDayOfWeek: resolvedFirstDayOfWeek,
212
+ }),
213
+ [resolvedLocale, weekdayFormat, resolvedFirstDayOfWeek],
214
+ );
191
215
 
192
216
  // 7. Typed per-slot accessor (dev-warns unknown keys against the known set).
193
217
  const s = slotStyles<WeekdaysRowStyles>(styles, WEEKDAYS_ROW_SLOT_KEYS, "WeekdaysRow");