@tanstack/table-core 9.0.0-beta.46 → 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 +253 -96
  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 +246 -97
  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 -283
  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
@@ -141,6 +141,11 @@ export function column_getToggleGroupingHandler<
141
141
  * Numeric columns default to `sum`, date-like values default to `extent`, and
142
142
  * other value types leave aggregation unspecified.
143
143
  *
144
+ * The chosen aggregation function is looked up in the table's
145
+ * `aggregationFns` registry. When it is not registered there, this returns
146
+ * `undefined` and warns in development instead of substituting a different
147
+ * aggregation function.
148
+ *
144
149
  * @example
145
150
  * ```ts
146
151
  * const aggregationFn = column_getAutoAggregationFn(column)
@@ -159,15 +164,27 @@ export function column_getAutoAggregationFn<
159
164
 
160
165
  const value = firstRow?.getValue(column.id)
161
166
 
167
+ let aggregationFnName: string | undefined
168
+
162
169
  if (typeof value === 'number') {
163
- return aggregationFns?.sum
170
+ aggregationFnName = 'sum'
171
+ } else if (Object.prototype.toString.call(value) === '[object Date]') {
172
+ aggregationFnName = 'extent'
164
173
  }
165
174
 
166
- if (Object.prototype.toString.call(value) === '[object Date]') {
167
- return aggregationFns?.extent
175
+ if (!aggregationFnName) {
176
+ return undefined
168
177
  }
169
178
 
170
- return undefined
179
+ const aggregationFn = aggregationFns?.[aggregationFnName]
180
+
181
+ if (process.env.NODE_ENV === 'development' && !aggregationFn) {
182
+ console.warn(
183
+ `aggregationFn '${aggregationFnName}' (auto) for column '${column.id}' is not registered`,
184
+ )
185
+ }
186
+
187
+ return aggregationFn
171
188
  }
172
189
 
173
190
  /**
@@ -191,11 +208,28 @@ export function column_getAggregationFn<
191
208
  | Record<string, AggregationFn<TFeatures, TData>>
192
209
  | undefined = column.table._rowModelFns.aggregationFns
193
210
 
194
- return isFunction(column.columnDef.aggregationFn)
195
- ? column.columnDef.aggregationFn
196
- : column.columnDef.aggregationFn === 'auto'
197
- ? column_getAutoAggregationFn(column)
198
- : aggregationFns?.[column.columnDef.aggregationFn as string]
211
+ if (isFunction(column.columnDef.aggregationFn)) {
212
+ return column.columnDef.aggregationFn
213
+ }
214
+
215
+ if (column.columnDef.aggregationFn === 'auto') {
216
+ return column_getAutoAggregationFn(column)
217
+ }
218
+
219
+ const aggregationFn =
220
+ aggregationFns?.[column.columnDef.aggregationFn as string]
221
+
222
+ if (
223
+ process.env.NODE_ENV === 'development' &&
224
+ !aggregationFn &&
225
+ column.columnDef.aggregationFn != null
226
+ ) {
227
+ console.warn(
228
+ `aggregationFn '${String(column.columnDef.aggregationFn)}' for column '${column.id}' is not registered`,
229
+ )
230
+ }
231
+
232
+ return aggregationFn
199
233
  }
200
234
 
201
235
  /**
@@ -20,9 +20,12 @@ import type { RowData } from '../../types/type-utils'
20
20
  *
21
21
  * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.
22
22
  *
23
- * Register aggregation functions with the `aggregationFns` slot on the
24
- * `features` option:
25
- * `tableFeatures({ columnGroupingFeature, groupedRowModel: createGroupedRowModel(), aggregationFns })`.
23
+ * Register the aggregation functions you use with the `aggregationFns` slot
24
+ * on the `features` option:
25
+ * `tableFeatures({ columnGroupingFeature, groupedRowModel: createGroupedRowModel(), aggregationFns: { sum: aggregationFn_sum } })`.
26
+ * Importing individual `aggregationFn_*` functions keeps unused built-ins out
27
+ * of your bundle; aggregation functions passed directly to the
28
+ * `aggregationFn` column option need no registration at all.
26
29
  */
27
30
  export function createGroupedRowModel<
28
31
  TFeatures extends TableFeatures,
@@ -74,6 +74,14 @@ export function table_getGlobalFilterFn<
74
74
  ? table_getGlobalAutoFilterFn()
75
75
  : filterFns?.[globalFilterFn as string]
76
76
 
77
+ if (
78
+ process.env.NODE_ENV === 'development' &&
79
+ !filterFn &&
80
+ globalFilterFn != null
81
+ ) {
82
+ console.warn(`globalFilterFn '${String(globalFilterFn)}' is not registered`)
83
+ }
84
+
77
85
  return filterFn
78
86
  }
79
87
 
@@ -14,8 +14,12 @@ import type { RowData } from '../../types/type-utils'
14
14
  *
15
15
  * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.
16
16
  *
17
- * Register sorting functions with the `sortFns` slot on the `features` option:
18
- * `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns })`.
17
+ * Register the sorting functions you use with the `sortFns` slot on the
18
+ * `features` option:
19
+ * `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.
20
+ * Importing individual `sortFn_*` functions keeps unused built-ins out of
21
+ * your bundle; sorting functions passed directly to the `sortFn` column
22
+ * option need no registration at all.
19
23
  */
20
24
  export function createSortedRowModel<
21
25
  TFeatures extends TableFeatures,
@@ -1,6 +1,11 @@
1
1
  import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'
2
2
  import type { BuiltInSortFn } from '../../fns/sortFns'
3
- import type { OnChangeFn, RowData, Updater } from '../../types/type-utils'
3
+ import type {
4
+ OnChangeFn,
5
+ RowData,
6
+ TransformDataValueFn,
7
+ Updater,
8
+ } from '../../types/type-utils'
4
9
  import type { IsAny, TableFeatures } from '../../types/TableFeatures'
5
10
  import type { Row } from '../../types/Row'
6
11
 
@@ -35,6 +40,53 @@ export interface SortFn<
35
40
  rowB: Row<TFeatures, TData>,
36
41
  columnId: string,
37
42
  ): number
43
+ /**
44
+ * Normalizes each row's value before the two sides are compared. Only
45
+ * honored by sorting functions built with `constructSortFn` (including all
46
+ * built-in sorting functions).
47
+ */
48
+ resolveDataValue?: TransformDataValueFn
49
+ }
50
+
51
+ /**
52
+ * The definition object accepted by `constructSortFn`.
53
+ *
54
+ * `sort` is a value-level comparator: it receives both rows' (resolved) data
55
+ * values instead of the whole rows, so normalization concerns stay in
56
+ * `resolveDataValue`.
57
+ */
58
+ export interface SortFnDef<
59
+ in out TFeatures extends TableFeatures,
60
+ in out TData extends RowData,
61
+ > {
62
+ sort: (
63
+ dataValueA: any,
64
+ dataValueB: any,
65
+ rowA: Row<TFeatures, TData>,
66
+ rowB: Row<TFeatures, TData>,
67
+ columnId: string,
68
+ ) => number
69
+ resolveDataValue?: TransformDataValueFn
70
+ }
71
+
72
+ /**
73
+ * The shape returned by `constructSortFn`: a `SortFn` with its definition
74
+ * attached, so variants can be built by spreading it into another
75
+ * `constructSortFn` call.
76
+ *
77
+ * The call signature is generic so a created sorting function can be used
78
+ * with any table's rows, regardless of the feature set it was defined
79
+ * against.
80
+ */
81
+ export interface CreatedSortFn<
82
+ in out TFeatures extends TableFeatures,
83
+ in out TData extends RowData,
84
+ > extends SortFnDef<TFeatures, TData> {
85
+ <TRowFeatures extends TableFeatures, TRowData extends RowData>(
86
+ rowA: Row<TRowFeatures, TRowData>,
87
+ rowB: Row<TRowFeatures, TRowData>,
88
+ columnId: string,
89
+ ): number
38
90
  }
39
91
 
40
92
  export type CustomSortFns<
@@ -91,30 +91,50 @@ export function column_getAutoSortFn<
91
91
 
92
92
  const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10)
93
93
 
94
+ let sortFnName: string | undefined
94
95
  let isString = false
95
96
 
96
97
  for (let i = 0; i < firstRows.length; i++) {
97
98
  const value = firstRows[i]!.getValue(column.id)
98
99
 
99
100
  if (Object.prototype.toString.call(value) === '[object Date]') {
100
- if (sortFns?.datetime) {
101
- return sortFns.datetime
102
- }
101
+ sortFnName = 'datetime'
102
+ break
103
103
  }
104
104
 
105
105
  if (typeof value === 'string') {
106
106
  isString = true
107
107
 
108
108
  if (value.split(reSplitAlphaNumeric).length > 1) {
109
- if (sortFns?.alphanumeric) {
110
- return sortFns.alphanumeric
111
- }
109
+ sortFnName = 'alphanumeric'
110
+ break
112
111
  }
113
112
  }
114
113
  }
115
114
 
116
- if (isString) {
117
- return sortFns?.text ?? sortFn_basic
115
+ if (!sortFnName && isString) {
116
+ sortFnName = 'text'
117
+ }
118
+
119
+ if (sortFnName) {
120
+ let sortFn = sortFns?.[sortFnName]
121
+
122
+ if (!sortFn) {
123
+ if (process.env.NODE_ENV === 'development') {
124
+ console.warn(
125
+ `sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`,
126
+ )
127
+ }
128
+
129
+ // String columns degrade to a registered text sort before basic
130
+ if (sortFnName === 'alphanumeric') {
131
+ sortFn = sortFns?.text
132
+ }
133
+ }
134
+
135
+ if (sortFn) {
136
+ return sortFn
137
+ }
118
138
  }
119
139
 
120
140
  return sortFn_basic
@@ -167,11 +187,23 @@ export function column_getSortFn<
167
187
  const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =
168
188
  column.table._rowModelFns.sortFns
169
189
 
170
- return isFunction(column.columnDef.sortFn)
171
- ? column.columnDef.sortFn
172
- : column.columnDef.sortFn === 'auto'
173
- ? column_getAutoSortFn(column)
174
- : (sortFns?.[column.columnDef.sortFn as string] ?? sortFn_basic)
190
+ if (isFunction(column.columnDef.sortFn)) {
191
+ return column.columnDef.sortFn
192
+ }
193
+
194
+ if (column.columnDef.sortFn === 'auto') {
195
+ return column_getAutoSortFn(column)
196
+ }
197
+
198
+ const sortFn = sortFns?.[column.columnDef.sortFn as string]
199
+
200
+ if (process.env.NODE_ENV === 'development' && !sortFn) {
201
+ console.warn(
202
+ `sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`,
203
+ )
204
+ }
205
+
206
+ return sortFn ?? sortFn_basic
175
207
  }
176
208
 
177
209
  /**