@tanstack/table-core 9.0.0-beta.45 → 9.0.0-beta.47

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 (116) hide show
  1. package/dist/features/column-filtering/columnFilteringFeature.types.d.cts +43 -3
  2. package/dist/features/column-filtering/columnFilteringFeature.types.d.ts +43 -3
  3. package/dist/features/column-filtering/columnFilteringFeature.utils.cjs +18 -9
  4. package/dist/features/column-filtering/columnFilteringFeature.utils.cjs.map +1 -1
  5. package/dist/features/column-filtering/columnFilteringFeature.utils.d.cts +6 -2
  6. package/dist/features/column-filtering/columnFilteringFeature.utils.d.ts +6 -2
  7. package/dist/features/column-filtering/columnFilteringFeature.utils.js +18 -9
  8. package/dist/features/column-filtering/columnFilteringFeature.utils.js.map +1 -1
  9. package/dist/features/column-filtering/createFilteredRowModel.cjs +7 -2
  10. package/dist/features/column-filtering/createFilteredRowModel.cjs.map +1 -1
  11. package/dist/features/column-filtering/createFilteredRowModel.d.cts +6 -2
  12. package/dist/features/column-filtering/createFilteredRowModel.d.ts +6 -2
  13. package/dist/features/column-filtering/createFilteredRowModel.js +7 -2
  14. package/dist/features/column-filtering/createFilteredRowModel.js.map +1 -1
  15. package/dist/features/column-grouping/columnGroupingFeature.types.d.cts +41 -3
  16. package/dist/features/column-grouping/columnGroupingFeature.types.d.ts +41 -3
  17. package/dist/features/column-grouping/columnGroupingFeature.utils.cjs +17 -3
  18. package/dist/features/column-grouping/columnGroupingFeature.utils.cjs.map +1 -1
  19. package/dist/features/column-grouping/columnGroupingFeature.utils.d.cts +5 -0
  20. package/dist/features/column-grouping/columnGroupingFeature.utils.d.ts +5 -0
  21. package/dist/features/column-grouping/columnGroupingFeature.utils.js +17 -3
  22. package/dist/features/column-grouping/columnGroupingFeature.utils.js.map +1 -1
  23. package/dist/features/column-grouping/createGroupedRowModel.cjs +6 -3
  24. package/dist/features/column-grouping/createGroupedRowModel.cjs.map +1 -1
  25. package/dist/features/column-grouping/createGroupedRowModel.d.cts +6 -3
  26. package/dist/features/column-grouping/createGroupedRowModel.d.ts +6 -3
  27. package/dist/features/column-grouping/createGroupedRowModel.js +6 -3
  28. package/dist/features/column-grouping/createGroupedRowModel.js.map +1 -1
  29. package/dist/features/global-filtering/globalFilteringFeature.utils.cjs +3 -1
  30. package/dist/features/global-filtering/globalFilteringFeature.utils.cjs.map +1 -1
  31. package/dist/features/global-filtering/globalFilteringFeature.utils.d.cts +2 -6
  32. package/dist/features/global-filtering/globalFilteringFeature.utils.d.ts +2 -6
  33. package/dist/features/global-filtering/globalFilteringFeature.utils.js +3 -1
  34. package/dist/features/global-filtering/globalFilteringFeature.utils.js.map +1 -1
  35. package/dist/features/row-sorting/createSortedRowModel.cjs +6 -2
  36. package/dist/features/row-sorting/createSortedRowModel.cjs.map +1 -1
  37. package/dist/features/row-sorting/createSortedRowModel.d.cts +6 -2
  38. package/dist/features/row-sorting/createSortedRowModel.d.ts +6 -2
  39. package/dist/features/row-sorting/createSortedRowModel.js +6 -2
  40. package/dist/features/row-sorting/createSortedRowModel.js.map +1 -1
  41. package/dist/features/row-sorting/rowSortingFeature.types.d.cts +31 -2
  42. package/dist/features/row-sorting/rowSortingFeature.types.d.ts +31 -2
  43. package/dist/features/row-sorting/rowSortingFeature.utils.cjs +19 -4
  44. package/dist/features/row-sorting/rowSortingFeature.utils.cjs.map +1 -1
  45. package/dist/features/row-sorting/rowSortingFeature.utils.js +19 -4
  46. package/dist/features/row-sorting/rowSortingFeature.utils.js.map +1 -1
  47. package/dist/fns/aggregationFns.cjs +128 -57
  48. package/dist/fns/aggregationFns.cjs.map +1 -1
  49. package/dist/fns/aggregationFns.d.cts +72 -18
  50. package/dist/fns/aggregationFns.d.ts +72 -18
  51. package/dist/fns/aggregationFns.js +126 -58
  52. package/dist/fns/aggregationFns.js.map +1 -1
  53. package/dist/fns/filterFns.cjs +254 -97
  54. package/dist/fns/filterFns.cjs.map +1 -1
  55. package/dist/fns/filterFns.d.cts +126 -94
  56. package/dist/fns/filterFns.d.ts +126 -94
  57. package/dist/fns/filterFns.js +247 -98
  58. package/dist/fns/filterFns.js.map +1 -1
  59. package/dist/fns/sortFns.cjs +68 -21
  60. package/dist/fns/sortFns.cjs.map +1 -1
  61. package/dist/fns/sortFns.d.cts +48 -15
  62. package/dist/fns/sortFns.d.ts +48 -15
  63. package/dist/fns/sortFns.js +68 -22
  64. package/dist/fns/sortFns.js.map +1 -1
  65. package/dist/helpers/tableFeatures.cjs +5 -4
  66. package/dist/helpers/tableFeatures.cjs.map +1 -1
  67. package/dist/helpers/tableFeatures.d.cts +5 -4
  68. package/dist/helpers/tableFeatures.d.ts +5 -4
  69. package/dist/helpers/tableFeatures.js +5 -4
  70. package/dist/helpers/tableFeatures.js.map +1 -1
  71. package/dist/index.cjs +12 -0
  72. package/dist/index.d.cts +8 -8
  73. package/dist/index.d.ts +8 -8
  74. package/dist/index.js +4 -4
  75. package/dist/types/TableFeatures.d.cts +15 -7
  76. package/dist/types/TableFeatures.d.ts +15 -7
  77. package/dist/types/type-utils.d.cts +8 -1
  78. package/dist/types/type-utils.d.ts +8 -1
  79. package/package.json +1 -1
  80. package/skills/api-not-found/SKILL.md +1 -1
  81. package/skills/client-vs-server/SKILL.md +1 -1
  82. package/skills/column-faceting/SKILL.md +11 -5
  83. package/skills/column-filtering/SKILL.md +8 -3
  84. package/skills/column-ordering/SKILL.md +1 -1
  85. package/skills/column-pinning/SKILL.md +1 -1
  86. package/skills/column-resizing/SKILL.md +1 -1
  87. package/skills/column-sizing/SKILL.md +1 -1
  88. package/skills/column-visibility/SKILL.md +1 -1
  89. package/skills/core/SKILL.md +1 -1
  90. package/skills/custom-features/SKILL.md +1 -1
  91. package/skills/expanding/SKILL.md +1 -1
  92. package/skills/global-filtering/SKILL.md +3 -3
  93. package/skills/grouping/SKILL.md +8 -3
  94. package/skills/migrate-v8-to-v9/SKILL.md +7 -7
  95. package/skills/pagination/SKILL.md +1 -1
  96. package/skills/row-pinning/SKILL.md +1 -1
  97. package/skills/row-selection/SKILL.md +1 -1
  98. package/skills/sorting/SKILL.md +9 -3
  99. package/skills/table-features/SKILL.md +16 -13
  100. package/skills/typescript/SKILL.md +1 -1
  101. package/src/features/column-filtering/columnFilteringFeature.types.ts +60 -0
  102. package/src/features/column-filtering/columnFilteringFeature.utils.ts +33 -19
  103. package/src/features/column-filtering/createFilteredRowModel.ts +13 -3
  104. package/src/features/column-grouping/columnGroupingFeature.types.ts +63 -8
  105. package/src/features/column-grouping/columnGroupingFeature.utils.ts +43 -9
  106. package/src/features/column-grouping/createGroupedRowModel.ts +6 -3
  107. package/src/features/global-filtering/globalFilteringFeature.utils.ts +8 -0
  108. package/src/features/row-sorting/createSortedRowModel.ts +6 -2
  109. package/src/features/row-sorting/rowSortingFeature.types.ts +53 -1
  110. package/src/features/row-sorting/rowSortingFeature.utils.ts +45 -13
  111. package/src/fns/aggregationFns.ts +204 -146
  112. package/src/fns/filterFns.ts +361 -280
  113. package/src/fns/sortFns.ts +95 -78
  114. package/src/helpers/tableFeatures.ts +5 -4
  115. package/src/types/TableFeatures.ts +15 -7
  116. package/src/types/type-utils.ts +8 -0
@@ -1,3 +1,7 @@
1
+ import type {
2
+ CreatedSortFn,
3
+ SortFnDef,
4
+ } from '../features/row-sorting/rowSortingFeature.types'
1
5
  import type { RowData } from '../types/type-utils'
2
6
  import type { TableFeatures } from '../types/TableFeatures'
3
7
  import type { Row } from '../types/Row'
@@ -11,122 +15,127 @@ import type { Row } from '../types/Row'
11
15
  export const reSplitAlphaNumeric = /([0-9]+)/gm
12
16
 
13
17
  /**
14
- * Sorts rows with the built-in alphanumeric strategy.
18
+ * Builds a `SortFn` from a value-level comparator plus an optional
19
+ * `resolveDataValue` normalizer.
15
20
  *
16
- * This comparator returns ascending-order results; descending order is applied by the sorting row model.
21
+ * The `sort` comparator receives both rows' data values, each already passed
22
+ * through `resolveDataValue` when one is defined. Keeping normalization in the
23
+ * resolver means a variant of an existing sorting function only has to swap
24
+ * the resolver, not re-implement the comparison.
25
+ *
26
+ * The definition is attached to the returned function, so a variant can be
27
+ * created by spreading a built-in sorting function and overriding what
28
+ * differs:
29
+ *
30
+ * ```ts
31
+ * const stripDiacritics = (value: string) =>
32
+ * value.normalize('NFD').replace(/\p{Diacritic}/gu, '')
33
+ *
34
+ * const alphanumericIgnoreDiacritics = constructSortFn({
35
+ * ...sortFn_alphanumeric,
36
+ * resolveDataValue: (value) =>
37
+ * stripDiacritics(sortFn_alphanumeric.resolveDataValue!(value)),
38
+ * })
39
+ * ```
17
40
  */
18
- export const sortFn_alphanumeric = <
19
- TFeatures extends TableFeatures,
20
- TData extends RowData,
21
- >(
22
- rowA: Row<TFeatures, TData>,
23
- rowB: Row<TFeatures, TData>,
24
- columnId: string,
25
- ) => {
26
- return compareAlphanumeric(
27
- toString(rowA.getValue(columnId)).toLowerCase(),
28
- toString(rowB.getValue(columnId)).toLowerCase(),
41
+ export function constructSortFn<
42
+ TFeatures extends TableFeatures = any,
43
+ TData extends RowData = any,
44
+ >(def: SortFnDef<TFeatures, TData>): CreatedSortFn<TFeatures, TData> {
45
+ const sortFn: CreatedSortFn<TFeatures, TData> = Object.assign(
46
+ <TRowFeatures extends TableFeatures, TRowData extends RowData>(
47
+ rowA: Row<TRowFeatures, TRowData>,
48
+ rowB: Row<TRowFeatures, TRowData>,
49
+ columnId: string,
50
+ ): number => {
51
+ let dataValueA = rowA.getValue(columnId)
52
+ let dataValueB = rowB.getValue(columnId)
53
+ const resolveDataValue = sortFn.resolveDataValue
54
+ if (resolveDataValue) {
55
+ dataValueA = resolveDataValue(dataValueA)
56
+ dataValueB = resolveDataValue(dataValueB)
57
+ }
58
+ // The def's comparator is typed against the factory's TFeatures/TData;
59
+ // the caller's row generics are erased at this boundary.
60
+ return sortFn.sort(
61
+ dataValueA,
62
+ dataValueB,
63
+ rowA as any,
64
+ rowB as any,
65
+ columnId,
66
+ )
67
+ },
68
+ def,
29
69
  )
70
+ return sortFn
30
71
  }
31
72
 
73
+ /**
74
+ * Sorts rows with the built-in alphanumeric strategy.
75
+ *
76
+ * This comparator returns ascending-order results; descending order is applied by the sorting row model.
77
+ */
78
+ export const sortFn_alphanumeric = constructSortFn({
79
+ resolveDataValue: (dataValue) => toString(dataValue).toLowerCase(),
80
+ sort: (dataValueA, dataValueB) => compareAlphanumeric(dataValueA, dataValueB),
81
+ })
82
+
32
83
  /**
33
84
  * Sorts rows with the built-in alphanumeric case sensitive strategy.
34
85
  *
35
86
  * This comparator returns ascending-order results; descending order is applied by the sorting row model.
36
87
  */
37
- export const sortFn_alphanumericCaseSensitive = <
38
- TFeatures extends TableFeatures,
39
- TData extends RowData,
40
- >(
41
- rowA: Row<TFeatures, TData>,
42
- rowB: Row<TFeatures, TData>,
43
- columnId: string,
44
- ) => {
45
- return compareAlphanumeric(
46
- toString(rowA.getValue(columnId)),
47
- toString(rowB.getValue(columnId)),
48
- )
49
- }
88
+ export const sortFn_alphanumericCaseSensitive = constructSortFn({
89
+ resolveDataValue: (dataValue) => toString(dataValue),
90
+ sort: (dataValueA, dataValueB) => compareAlphanumeric(dataValueA, dataValueB),
91
+ })
50
92
 
51
- // The text filter is more basic (less numeric support)
93
+ // The text sort is more basic (less numeric support)
52
94
  // but is much faster
53
95
  /**
54
96
  * Sorts rows with the built-in text strategy.
55
97
  *
56
98
  * This comparator returns ascending-order results; descending order is applied by the sorting row model.
57
99
  */
58
- export const sortFn_text = <
59
- TFeatures extends TableFeatures,
60
- TData extends RowData,
61
- >(
62
- rowA: Row<TFeatures, TData>,
63
- rowB: Row<TFeatures, TData>,
64
- columnId: string,
65
- ) => {
66
- return compareBasic(
67
- toString(rowA.getValue(columnId)).toLowerCase(),
68
- toString(rowB.getValue(columnId)).toLowerCase(),
69
- )
70
- }
100
+ export const sortFn_text = constructSortFn({
101
+ resolveDataValue: (dataValue) => toString(dataValue).toLowerCase(),
102
+ sort: (dataValueA, dataValueB) => compareBasic(dataValueA, dataValueB),
103
+ })
71
104
 
72
- // The text filter is more basic (less numeric support)
105
+ // The text sort is more basic (less numeric support)
73
106
  // but is much faster
74
107
  /**
75
108
  * Sorts rows with the built-in text case sensitive strategy.
76
109
  *
77
110
  * This comparator returns ascending-order results; descending order is applied by the sorting row model.
78
111
  */
79
- export const sortFn_textCaseSensitive = <
80
- TFeatures extends TableFeatures,
81
- TData extends RowData,
82
- >(
83
- rowA: Row<TFeatures, TData>,
84
- rowB: Row<TFeatures, TData>,
85
- columnId: string,
86
- ) => {
87
- return compareBasic(
88
- toString(rowA.getValue(columnId)),
89
- toString(rowB.getValue(columnId)),
90
- )
91
- }
112
+ export const sortFn_textCaseSensitive = constructSortFn({
113
+ resolveDataValue: (dataValue) => toString(dataValue),
114
+ sort: (dataValueA, dataValueB) => compareBasic(dataValueA, dataValueB),
115
+ })
92
116
 
93
117
  /**
94
118
  * Sorts rows with the built-in datetime strategy.
95
119
  *
96
120
  * This comparator returns ascending-order results; descending order is applied by the sorting row model.
97
121
  */
98
- export const sortFn_datetime = <
99
- TFeatures extends TableFeatures,
100
- TData extends RowData,
101
- >(
102
- rowA: Row<TFeatures, TData>,
103
- rowB: Row<TFeatures, TData>,
104
- columnId: string,
105
- ) => {
106
- const a = toDateSortValue(rowA.getValue(columnId))
107
- const b = toDateSortValue(rowB.getValue(columnId))
108
-
122
+ export const sortFn_datetime = constructSortFn({
123
+ resolveDataValue: (dataValue) => toDateSortValue(dataValue),
109
124
  // Can handle nullish values
110
125
  // Use > and < because == (and ===) doesn't work with
111
126
  // Date objects (would require calling getTime()).
112
- return a > b ? 1 : a < b ? -1 : 0
113
- }
127
+ sort: (dataValueA, dataValueB) =>
128
+ dataValueA > dataValueB ? 1 : dataValueA < dataValueB ? -1 : 0,
129
+ })
114
130
 
115
131
  /**
116
132
  * Sorts rows with the built-in basic strategy.
117
133
  *
118
134
  * This comparator returns ascending-order results; descending order is applied by the sorting row model.
119
135
  */
120
- export const sortFn_basic = <
121
- TFeatures extends TableFeatures,
122
- TData extends RowData,
123
- >(
124
- rowA: Row<TFeatures, TData>,
125
- rowB: Row<TFeatures, TData>,
126
- columnId: string,
127
- ) => {
128
- return compareBasic(rowA.getValue(columnId), rowB.getValue(columnId))
129
- }
136
+ export const sortFn_basic = constructSortFn({
137
+ sort: (dataValueA, dataValueB) => compareBasic(dataValueA, dataValueB),
138
+ })
130
139
 
131
140
  // Utils
132
141
 
@@ -338,7 +347,15 @@ function countRemainingChunks(str: string, start: number) {
338
347
  /**
339
348
  * The built-in sorting function registry.
340
349
  *
341
- * Pass this object to sorted row model creation or extend it with custom sorting functions.
350
+ * Registering this full object opts out of tree-shaking: every built-in
351
+ * sorting function ends up in your bundle. Prefer importing the `sortFn_*`
352
+ * functions you actually use and registering just those in the `sortFns`
353
+ * slot, or passing them directly to the `sortFn` column option.
354
+ *
355
+ * @deprecated Import individual `sortFn_*` functions instead for a smaller
356
+ * bundle. This export still works and is not going away in v9, but built-in
357
+ * name resolution (including `sortFn: 'auto'`) only finds functions you
358
+ * register yourself.
342
359
  */
343
360
  export const sortFns = {
344
361
  alphanumeric: sortFn_alphanumeric,
@@ -24,9 +24,10 @@ import type {
24
24
  * columnFilteringFeature,
25
25
  * createFilteredRowModel,
26
26
  * createSortedRowModel,
27
- * filterFns,
27
+ * filterFn_includesString,
28
28
  * rowSortingFeature,
29
- * sortFns,
29
+ * sortFn_alphanumeric,
30
+ * sortFn_text,
30
31
  * tableFeatures,
31
32
  * } from '@tanstack/react-table'
32
33
  * const features = tableFeatures({
@@ -34,8 +35,8 @@ import type {
34
35
  * rowSortingFeature,
35
36
  * filteredRowModel: createFilteredRowModel(),
36
37
  * sortedRowModel: createSortedRowModel(),
37
- * filterFns: { ...filterFns, myCustomFilterFn },
38
- * sortFns,
38
+ * filterFns: { includesString: filterFn_includesString, myCustomFilterFn },
39
+ * sortFns: { alphanumeric: sortFn_alphanumeric, text: sortFn_text },
39
40
  * tableMeta: {} as { updateData: (rowIndex: number, columnId: string, value: unknown) => void },
40
41
  * columnMeta: {} as { align?: 'left' | 'right' },
41
42
  * });
@@ -184,9 +184,11 @@ export interface TableFeatures
184
184
  * Registry of aggregation functions available to this table by name.
185
185
  *
186
186
  * Keys registered here become the valid string values for `aggregationFn` on
187
- * column definitions, with full inference. Spread the exported
188
- * `aggregationFns` to register the built-in aggregation functions:
189
- * `aggregationFns: { ...aggregationFns, myCustomAggregationFn }`.
187
+ * column definitions, with full inference. Import the built-in aggregation
188
+ * functions you use individually and register them by their conventional
189
+ * names: `aggregationFns: { sum: aggregationFn_sum, myCustomAggregationFn }`.
190
+ * Spreading the exported `aggregationFns` registry also works, but puts
191
+ * every built-in aggregation function in your bundle.
190
192
  */
191
193
  aggregationFns?: Record<string, AggregationFn<any, any>>
192
194
  /**
@@ -243,8 +245,11 @@ export interface TableFeatures
243
245
  *
244
246
  * Keys registered here become the valid string values for `filterFn` on
245
247
  * column definitions and the `globalFilterFn` option, with full inference.
246
- * Spread the exported `filterFns` to register the built-in filter functions:
247
- * `filterFns: { ...filterFns, myCustomFilterFn }`.
248
+ * Import the built-in filter functions you use individually and register
249
+ * them by their conventional names:
250
+ * `filterFns: { includesString: filterFn_includesString, myCustomFilterFn }`.
251
+ * Spreading the exported `filterFns` registry also works, but puts every
252
+ * built-in filter function in your bundle.
248
253
  */
249
254
  filterFns?: Record<string, FilterFn<any, any>>
250
255
  /**
@@ -281,8 +286,11 @@ export interface TableFeatures
281
286
  * Registry of sorting functions available to this table by name.
282
287
  *
283
288
  * Keys registered here become the valid string values for `sortFn` on column
284
- * definitions, with full inference. Spread the exported `sortFns` to register
285
- * the built-in sorting functions: `sortFns: { ...sortFns, myCustomSortFn }`.
289
+ * definitions, with full inference. Import the built-in sorting functions
290
+ * you use individually and register them by their conventional names:
291
+ * `sortFns: { alphanumeric: sortFn_alphanumeric, myCustomSortFn }`. Spreading
292
+ * the exported `sortFns` registry also works, but puts every built-in
293
+ * sorting function in your bundle.
286
294
  */
287
295
  sortFns?: Record<string, SortFn<any, any>>
288
296
  /**
@@ -6,6 +6,14 @@ export type RowData = Record<string, any> | Array<any>
6
6
 
7
7
  export type CellData = unknown
8
8
 
9
+ /**
10
+ * Normalizes a row's value before a filter or sort comparator sees it.
11
+ *
12
+ * Attach as `resolveDataValue` on filter/sort functions built with
13
+ * `constructFilterFn`/`constructSortFn` (e.g. to lowercase or strip diacritics).
14
+ */
15
+ export type TransformDataValueFn = (dataValue: any) => any
16
+
9
17
  export type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
10
18
 
11
19
  export type RequiredKeys<T, K extends keyof T> = Omit<T, K> &