@knitui/dates 0.4.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 (147) 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/DecadeLevel/DecadeLevel.js.map +1 -1
  6. package/lib/commonjs/Month/Month.js +266 -80
  7. package/lib/commonjs/Month/Month.js.map +1 -1
  8. package/lib/commonjs/MonthLevel/MonthLevel.js +19 -3
  9. package/lib/commonjs/MonthLevel/MonthLevel.js.map +1 -1
  10. package/lib/commonjs/MonthsList/MonthsList.js +160 -60
  11. package/lib/commonjs/MonthsList/MonthsList.js.map +1 -1
  12. package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
  13. package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
  14. package/lib/commonjs/TimeControlsList/TimeControl.js +15 -1
  15. package/lib/commonjs/TimeControlsList/TimeControl.js.map +1 -1
  16. package/lib/commonjs/TimeControlsList/TimeControlsList.js +16 -3
  17. package/lib/commonjs/TimeControlsList/TimeControlsList.js.map +1 -1
  18. package/lib/commonjs/WeekdaysRow/WeekdaysRow.js +23 -4
  19. package/lib/commonjs/WeekdaysRow/WeekdaysRow.js.map +1 -1
  20. package/lib/commonjs/YearsList/YearsList.js +155 -60
  21. package/lib/commonjs/YearsList/YearsList.js.map +1 -1
  22. package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
  23. package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
  24. package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
  25. package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
  26. package/lib/commonjs/hooks/use-dates-state/use-dates-state.js +25 -7
  27. package/lib/commonjs/hooks/use-dates-state/use-dates-state.js.map +1 -1
  28. package/lib/commonjs/internal/are-cell-props-equal.js +64 -0
  29. package/lib/commonjs/internal/are-cell-props-equal.js.map +1 -0
  30. package/lib/commonjs/internal/date-string-fast-path.js +98 -0
  31. package/lib/commonjs/internal/date-string-fast-path.js.map +1 -0
  32. package/lib/commonjs/internal/memoize-control-props.js +54 -0
  33. package/lib/commonjs/internal/memoize-control-props.js.map +1 -0
  34. package/lib/commonjs/internal/month-days-cache.js +71 -0
  35. package/lib/commonjs/internal/month-days-cache.js.map +1 -0
  36. package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
  37. package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
  38. package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js +14 -1
  39. package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js.map +1 -1
  40. package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js +14 -1
  41. package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js.map +1 -1
  42. package/lib/commonjs/utils/is-same-month/is-same-month.js +17 -1
  43. package/lib/commonjs/utils/is-same-month/is-same-month.js.map +1 -1
  44. package/lib/module/DatesProvider/DatesProvider.js +34 -5
  45. package/lib/module/DatesProvider/DatesProvider.js.map +1 -1
  46. package/lib/module/Day/Day.js +46 -8
  47. package/lib/module/Day/Day.js.map +1 -1
  48. package/lib/module/DecadeLevel/DecadeLevel.js.map +1 -1
  49. package/lib/module/Month/Month.js +267 -82
  50. package/lib/module/Month/Month.js.map +1 -1
  51. package/lib/module/MonthLevel/MonthLevel.js +19 -3
  52. package/lib/module/MonthLevel/MonthLevel.js.map +1 -1
  53. package/lib/module/MonthsList/MonthsList.js +159 -60
  54. package/lib/module/MonthsList/MonthsList.js.map +1 -1
  55. package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
  56. package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
  57. package/lib/module/TimeControlsList/TimeControl.js +15 -1
  58. package/lib/module/TimeControlsList/TimeControl.js.map +1 -1
  59. package/lib/module/TimeControlsList/TimeControlsList.js +16 -3
  60. package/lib/module/TimeControlsList/TimeControlsList.js.map +1 -1
  61. package/lib/module/WeekdaysRow/WeekdaysRow.js +23 -4
  62. package/lib/module/WeekdaysRow/WeekdaysRow.js.map +1 -1
  63. package/lib/module/YearsList/YearsList.js +154 -60
  64. package/lib/module/YearsList/YearsList.js.map +1 -1
  65. package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
  66. package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
  67. package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
  68. package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
  69. package/lib/module/hooks/use-dates-state/use-dates-state.js +25 -7
  70. package/lib/module/hooks/use-dates-state/use-dates-state.js.map +1 -1
  71. package/lib/module/internal/are-cell-props-equal.js +60 -0
  72. package/lib/module/internal/are-cell-props-equal.js.map +1 -0
  73. package/lib/module/internal/date-string-fast-path.js +91 -0
  74. package/lib/module/internal/date-string-fast-path.js.map +1 -0
  75. package/lib/module/internal/memoize-control-props.js +50 -0
  76. package/lib/module/internal/memoize-control-props.js.map +1 -0
  77. package/lib/module/internal/month-days-cache.js +68 -0
  78. package/lib/module/internal/month-days-cache.js.map +1 -0
  79. package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
  80. package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
  81. package/lib/module/utils/is-after-min-date/is-after-min-date.js +14 -1
  82. package/lib/module/utils/is-after-min-date/is-after-min-date.js.map +1 -1
  83. package/lib/module/utils/is-before-max-date/is-before-max-date.js +14 -1
  84. package/lib/module/utils/is-before-max-date/is-before-max-date.js.map +1 -1
  85. package/lib/module/utils/is-same-month/is-same-month.js +17 -1
  86. package/lib/module/utils/is-same-month/is-same-month.js.map +1 -1
  87. package/lib/typescript/DatesProvider/DatesProvider.d.ts.map +1 -1
  88. package/lib/typescript/Day/Day.d.ts +7 -7
  89. package/lib/typescript/Day/Day.d.ts.map +1 -1
  90. package/lib/typescript/DecadeLevel/DecadeLevel.d.ts.map +1 -1
  91. package/lib/typescript/Month/Month.d.ts +3 -2
  92. package/lib/typescript/Month/Month.d.ts.map +1 -1
  93. package/lib/typescript/MonthLevel/MonthLevel.d.ts.map +1 -1
  94. package/lib/typescript/MonthsList/MonthsList.d.ts +3 -2
  95. package/lib/typescript/MonthsList/MonthsList.d.ts.map +1 -1
  96. package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts +6 -0
  97. package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts.map +1 -1
  98. package/lib/typescript/TimeControlsList/TimeControl.d.ts +13 -0
  99. package/lib/typescript/TimeControlsList/TimeControl.d.ts.map +1 -1
  100. package/lib/typescript/TimeControlsList/TimeControlsList.d.ts.map +1 -1
  101. package/lib/typescript/WeekdaysRow/WeekdaysRow.d.ts.map +1 -1
  102. package/lib/typescript/YearsList/YearsList.d.ts +3 -2
  103. package/lib/typescript/YearsList/YearsList.d.ts.map +1 -1
  104. package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts +6 -0
  105. package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts.map +1 -1
  106. package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts +12 -0
  107. package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts.map +1 -1
  108. package/lib/typescript/hooks/use-dates-state/use-dates-state.d.ts.map +1 -1
  109. package/lib/typescript/internal/are-cell-props-equal.d.ts +21 -0
  110. package/lib/typescript/internal/are-cell-props-equal.d.ts.map +1 -0
  111. package/lib/typescript/internal/date-string-fast-path.d.ts +35 -0
  112. package/lib/typescript/internal/date-string-fast-path.d.ts.map +1 -0
  113. package/lib/typescript/internal/memoize-control-props.d.ts +12 -0
  114. package/lib/typescript/internal/memoize-control-props.d.ts.map +1 -0
  115. package/lib/typescript/internal/month-days-cache.d.ts +19 -0
  116. package/lib/typescript/internal/month-days-cache.d.ts.map +1 -0
  117. package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts +9 -1
  118. package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts.map +1 -1
  119. package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts +6 -0
  120. package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts.map +1 -1
  121. package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts +6 -0
  122. package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts.map +1 -1
  123. package/lib/typescript/utils/is-same-month/is-same-month.d.ts +13 -1
  124. package/lib/typescript/utils/is-same-month/is-same-month.d.ts.map +1 -1
  125. package/package.json +12 -12
  126. package/src/DatesProvider/DatesProvider.tsx +27 -5
  127. package/src/Day/Day.tsx +51 -10
  128. package/src/DecadeLevel/DecadeLevel.tsx +1 -2
  129. package/src/Month/Month.tsx +357 -80
  130. package/src/MonthLevel/MonthLevel.tsx +35 -16
  131. package/src/MonthsList/MonthsList.tsx +201 -55
  132. package/src/MonthsList/get-month-in-tab-order/get-month-in-tab-order.ts +12 -1
  133. package/src/TimeControlsList/TimeControl.tsx +16 -1
  134. package/src/TimeControlsList/TimeControlsList.tsx +15 -3
  135. package/src/WeekdaysRow/WeekdaysRow.tsx +29 -5
  136. package/src/YearsList/YearsList.tsx +196 -55
  137. package/src/YearsList/get-year-in-tab-order/get-year-in-tab-order.ts +12 -1
  138. package/src/hooks/use-dates-state/is-in-range/is-in-range.ts +22 -0
  139. package/src/hooks/use-dates-state/use-dates-state.ts +28 -15
  140. package/src/internal/are-cell-props-equal.ts +63 -0
  141. package/src/internal/date-string-fast-path.ts +94 -0
  142. package/src/internal/memoize-control-props.ts +54 -0
  143. package/src/internal/month-days-cache.ts +77 -0
  144. package/src/utils/get-date-in-tab-order/get-date-in-tab-order.ts +17 -2
  145. package/src/utils/is-after-min-date/is-after-min-date.ts +16 -1
  146. package/src/utils/is-before-max-date/is-before-max-date.ts +16 -1
  147. package/src/utils/is-same-month/is-same-month.ts +18 -1
@@ -0,0 +1,77 @@
1
+ // Internal cross-platform helper — NOT exported from the public `src/index.ts`
2
+ // barrel.
3
+ //
4
+ // `getMonthDays` is the grid's enumerator, and it is not cheap: it walks the
5
+ // month day by day, so a 5-week grid costs ~70 dayjs instances (the running
6
+ // cursor plus dayjs' internal clones) and 35 `format("YYYY-MM-DD")` calls. The
7
+ // answer, though, is a PURE function of three primitives — the month, the first
8
+ // weekday, and whether the grid is padded to 6 weeks — and it is re-derived on
9
+ // every single `Month` render, including the ones triggered by nothing but a web
10
+ // hover moving one cell over.
11
+ //
12
+ // A small module-level LRU makes month navigation free after the first visit:
13
+ // stepping back and forth across a few months (or re-rendering the same month
14
+ // for any other reason) replays cached arrays instead of re-walking the calendar.
15
+ // The cap is generous enough to hold a couple of years of browsing yet bounded so
16
+ // a long-lived app cannot grow the map without limit.
17
+ //
18
+ // This deliberately wraps the public `getMonthDays` rather than caching inside
19
+ // it: the public util keeps returning a FRESH array per call, so an external
20
+ // consumer that mutates the result cannot corrupt a shared cache entry. Inside
21
+ // this package the arrays are only read (`.flat()`, `.filter()`, `.find()`,
22
+ // `.map()`), so sharing them is safe — and it additionally gives the grid a
23
+ // STABLE array identity per month, which is what lets `Month` use it directly as
24
+ // a `useMemo` dependency.
25
+
26
+ import type { DateStringValue, DayOfWeek } from "../types";
27
+ import { getMonthDays } from "../utils";
28
+
29
+ /** How many distinct month grids to retain (~2 years of navigation). */
30
+ const MAX_ENTRIES = 32;
31
+
32
+ const cache = new Map<string, DateStringValue[][]>();
33
+
34
+ interface CachedMonthDaysInput {
35
+ /** Any date within the month to enumerate, `YYYY-MM-DD`. */
36
+ month: DateStringValue;
37
+ /** First weekday of each row. */
38
+ firstDayOfWeek: DayOfWeek;
39
+ /** Pad to a fixed 6 weeks so the grid height never shifts. */
40
+ consistentWeeks: boolean | undefined;
41
+ }
42
+
43
+ /**
44
+ * `getMonthDays` memoized on its three primitive inputs.
45
+ *
46
+ * The returned weeks array is SHARED — treat it as read-only. Callers also get a
47
+ * stable identity for a given key, so it can be passed straight to `useMemo` as a
48
+ * dependency.
49
+ */
50
+ export function getCachedMonthDays({
51
+ month,
52
+ firstDayOfWeek,
53
+ consistentWeeks,
54
+ }: CachedMonthDaysInput): DateStringValue[][] {
55
+ const key = `${month}|${firstDayOfWeek}|${consistentWeeks ? 1 : 0}`;
56
+ const cached = cache.get(key);
57
+
58
+ if (cached) {
59
+ // Re-insert so the most recently used key is last, making the first map key
60
+ // the least recently used one for eviction below.
61
+ cache.delete(key);
62
+ cache.set(key, cached);
63
+ return cached;
64
+ }
65
+
66
+ const weeks = getMonthDays({ month, firstDayOfWeek, consistentWeeks });
67
+ cache.set(key, weeks);
68
+
69
+ if (cache.size > MAX_ENTRIES) {
70
+ const oldest = cache.keys().next();
71
+ if (!oldest.done) {
72
+ cache.delete(oldest.value);
73
+ }
74
+ }
75
+
76
+ return weeks;
77
+ }
@@ -13,7 +13,15 @@ interface GetDateInTabOrderInput {
13
13
  minDate: DateStringValue | undefined;
14
14
  /** Upper bound, `YYYY-MM-DD`. */
15
15
  maxDate: DateStringValue | undefined;
16
- /** Per-day prop getter — its `disabled`/`selected` participate in the choice. */
16
+ /**
17
+ * Per-day prop getter — its `disabled`/`selected` participate in the choice.
18
+ *
19
+ * It is consulted TWICE per date below (once for `disabled`, once for
20
+ * `selected`), so callers should pass a getter wrapped in
21
+ * `memoizeByDate` — the same wrapper they use for their own cell loop, so the
22
+ * whole render resolves each date's props exactly once. See
23
+ * `internal/memoize-control-props.ts`.
24
+ */
17
25
  getDayProps: ((date: DateStringValue) => Partial<DayProps>) | undefined;
18
26
  /** Predicate marking a day disabled. */
19
27
  excludeDate: ((date: DateStringValue) => boolean) | undefined;
@@ -54,7 +62,14 @@ export function getDateInTabOrder({
54
62
  return selectedDate;
55
63
  }
56
64
 
57
- const currentDate = enabledDates.find((date) => dayjs().isSame(date, "date"));
65
+ // `dayjs().isSame(date, "date")` inside the predicate built a FRESH "now"
66
+ // instance (plus dayjs' internal `startOf`/`endOf` clones) for every candidate
67
+ // — ~4 allocations × up to 42 dates, to answer a question whose left-hand side
68
+ // never changes. One formatted "today" string compares exactly instead:
69
+ // `dates` always comes from `getMonthDays`, which emits zero-padded
70
+ // `YYYY-MM-DD`, so string equality is equivalent to a day-granularity `isSame`.
71
+ const today = dayjs().format("YYYY-MM-DD");
72
+ const currentDate = enabledDates.find((date) => date.slice(0, 10) === today);
58
73
 
59
74
  if (currentDate) {
60
75
  return currentDate;
@@ -1,14 +1,29 @@
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
  /**
6
7
  * Whether `date` is on or after `minDate` (no lower bound when `minDate` is
7
8
  * absent). Mirrors @mantine/dates `isAfterMinDate`.
9
+ *
10
+ * The lower-bound twin of `isBeforeMaxDate` — the dayjs spelling
11
+ * (`isAfter(minDate - 1 day, "day")`) is a day-granularity `>=`, exactly a
12
+ * lexicographic `>=` for canonical zero-padded `YYYY-MM-DD` strings. Same hot
13
+ * path (once per cell in `Month`, again in `getDateInTabOrder`), same fallback
14
+ * for non-canonical input.
8
15
  */
9
16
  export function isAfterMinDate(
10
17
  date: DateStringValue,
11
18
  minDate: DateStringValue | undefined,
12
19
  ): boolean {
13
- return minDate ? dayjs(date).isAfter(dayjs(minDate).subtract(1, "day"), "day") : true;
20
+ if (!minDate) {
21
+ return true;
22
+ }
23
+
24
+ if (isCanonicalDateString(date) && isCanonicalDateString(minDate)) {
25
+ return date.slice(0, 10) >= minDate.slice(0, 10);
26
+ }
27
+
28
+ return dayjs(date).isAfter(dayjs(minDate).subtract(1, "day"), "day");
14
29
  }
@@ -1,14 +1,29 @@
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
  /**
6
7
  * Whether `date` is on or before `maxDate` (no upper bound when `maxDate` is
7
8
  * absent). Mirrors @mantine/dates `isBeforeMaxDate`.
9
+ *
10
+ * Called once per cell by `Month` and again by `getDateInTabOrder`. The dayjs
11
+ * spelling (`isBefore(maxDate + 1 day, "day")`) is a day-granularity `<=`, which
12
+ * for canonical zero-padded `YYYY-MM-DD` strings is exactly a lexicographic
13
+ * `<=` — same answer, no `add`, no clones, no allocations. Non-canonical input
14
+ * falls back to the original path.
8
15
  */
9
16
  export function isBeforeMaxDate(
10
17
  date: DateStringValue,
11
18
  maxDate: DateStringValue | undefined,
12
19
  ): boolean {
13
- return maxDate ? dayjs(date).isBefore(dayjs(maxDate).add(1, "day"), "day") : true;
20
+ if (!maxDate) {
21
+ return true;
22
+ }
23
+
24
+ if (isCanonicalDateString(date) && isCanonicalDateString(maxDate)) {
25
+ return date.slice(0, 10) <= maxDate.slice(0, 10);
26
+ }
27
+
28
+ return dayjs(date).isBefore(dayjs(maxDate).add(1, "day"), "day");
14
29
  }
@@ -1,11 +1,28 @@
1
1
  import dayjs from "dayjs";
2
2
 
3
+ import { isCanonicalYearMonthString } from "../../internal/date-string-fast-path";
3
4
  import type { DateStringValue } from "../../types";
4
5
 
5
- /** Whether two dates fall in the same calendar month. Mirrors @mantine/dates `isSameMonth`. */
6
+ /**
7
+ * Whether two dates fall in the same calendar month. Mirrors @mantine/dates
8
+ * `isSameMonth`.
9
+ *
10
+ * This is one of the hottest comparisons in the package — `Month` calls it once
11
+ * per cell (42×) to decide `outside`, and `getDateInTabOrder` calls it again per
12
+ * cell whenever `hideOutsideDates` is on (which `DatePicker` enables
13
+ * automatically for multiple columns). The dayjs spelling cost 2 instances plus
14
+ * 2 `format("YYYY-MM")` calls (locale lookup + regex replace) EACH TIME; for the
15
+ * canonical `YYYY-MM-DD` strings that are the actual inputs, comparing the
16
+ * `YYYY-MM` prefix is exact and allocates nothing. `Date` inputs (and any
17
+ * non-canonical string) keep the original dayjs path.
18
+ */
6
19
  export function isSameMonth(
7
20
  date: DateStringValue | Date,
8
21
  comparison: DateStringValue | Date,
9
22
  ): boolean {
23
+ if (isCanonicalYearMonthString(date) && isCanonicalYearMonthString(comparison)) {
24
+ return date.slice(0, 7) === comparison.slice(0, 7);
25
+ }
26
+
10
27
  return dayjs(date).format("YYYY-MM") === dayjs(comparison).format("YYYY-MM");
11
28
  }