@tanstack/table-core 8.9.7 → 8.9.8

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 (54) hide show
  1. package/build/lib/core/cell.js +1 -1
  2. package/build/lib/core/cell.js.map +1 -1
  3. package/build/lib/core/column.js +3 -3
  4. package/build/lib/core/column.js.map +1 -1
  5. package/build/lib/core/headers.js +182 -181
  6. package/build/lib/core/headers.js.map +1 -1
  7. package/build/lib/core/row.js +1 -1
  8. package/build/lib/core/row.js.map +1 -1
  9. package/build/lib/core/table.js +4 -3
  10. package/build/lib/core/table.js.map +1 -1
  11. package/build/lib/features/ColumnSizing.js +173 -179
  12. package/build/lib/features/ColumnSizing.js.map +1 -1
  13. package/build/lib/features/Expanding.js +119 -123
  14. package/build/lib/features/Expanding.js.map +1 -1
  15. package/build/lib/features/Filters.js +157 -165
  16. package/build/lib/features/Filters.js.map +1 -1
  17. package/build/lib/features/Grouping.js +71 -79
  18. package/build/lib/features/Grouping.js.map +1 -1
  19. package/build/lib/features/Ordering.js +32 -34
  20. package/build/lib/features/Ordering.js.map +1 -1
  21. package/build/lib/features/Pagination.js +112 -114
  22. package/build/lib/features/Pagination.js.map +1 -1
  23. package/build/lib/features/Pinning.js +120 -126
  24. package/build/lib/features/Pinning.js.map +1 -1
  25. package/build/lib/features/RowSelection.js +245 -247
  26. package/build/lib/features/RowSelection.js.map +1 -1
  27. package/build/lib/features/Sorting.js +163 -167
  28. package/build/lib/features/Sorting.js.map +1 -1
  29. package/build/lib/features/Visibility.js +60 -66
  30. package/build/lib/features/Visibility.js.map +1 -1
  31. package/build/lib/index.esm.js +1469 -1515
  32. package/build/lib/index.esm.js.map +1 -1
  33. package/build/lib/index.mjs +1469 -1515
  34. package/build/lib/index.mjs.map +1 -1
  35. package/build/umd/index.development.js +1469 -1515
  36. package/build/umd/index.development.js.map +1 -1
  37. package/build/umd/index.production.js +1 -1
  38. package/build/umd/index.production.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/core/cell.ts +5 -8
  41. package/src/core/column.ts +3 -3
  42. package/src/core/headers.ts +264 -280
  43. package/src/core/row.ts +1 -1
  44. package/src/core/table.ts +4 -3
  45. package/src/features/ColumnSizing.ts +220 -231
  46. package/src/features/Expanding.ts +132 -140
  47. package/src/features/Filters.ts +193 -206
  48. package/src/features/Grouping.ts +94 -110
  49. package/src/features/Ordering.ts +48 -51
  50. package/src/features/Pagination.ts +150 -154
  51. package/src/features/Pinning.ts +158 -178
  52. package/src/features/RowSelection.ts +280 -286
  53. package/src/features/Sorting.ts +196 -206
  54. package/src/features/Visibility.ts +98 -107
@@ -134,249 +134,239 @@ export const Sorting: TableFeature = {
134
134
  createColumn: <TData extends RowData, TValue>(
135
135
  column: Column<TData, TValue>,
136
136
  table: Table<TData>
137
- ): SortingColumn<TData> => {
138
- return {
139
- getAutoSortingFn: () => {
140
- const firstRows = table.getFilteredRowModel().flatRows.slice(10)
137
+ ): void => {
138
+ column.getAutoSortingFn = () => {
139
+ const firstRows = table.getFilteredRowModel().flatRows.slice(10)
141
140
 
142
- let isString = false
141
+ let isString = false
143
142
 
144
- for (const row of firstRows) {
145
- const value = row?.getValue(column.id)
143
+ for (const row of firstRows) {
144
+ const value = row?.getValue(column.id)
146
145
 
147
- if (Object.prototype.toString.call(value) === '[object Date]') {
148
- return sortingFns.datetime
149
- }
146
+ if (Object.prototype.toString.call(value) === '[object Date]') {
147
+ return sortingFns.datetime
148
+ }
150
149
 
151
- if (typeof value === 'string') {
152
- isString = true
150
+ if (typeof value === 'string') {
151
+ isString = true
153
152
 
154
- if (value.split(reSplitAlphaNumeric).length > 1) {
155
- return sortingFns.alphanumeric
156
- }
153
+ if (value.split(reSplitAlphaNumeric).length > 1) {
154
+ return sortingFns.alphanumeric
157
155
  }
158
156
  }
157
+ }
159
158
 
160
- if (isString) {
161
- return sortingFns.text
162
- }
163
-
164
- return sortingFns.basic
165
- },
166
- getAutoSortDir: () => {
167
- const firstRow = table.getFilteredRowModel().flatRows[0]
159
+ if (isString) {
160
+ return sortingFns.text
161
+ }
168
162
 
169
- const value = firstRow?.getValue(column.id)
163
+ return sortingFns.basic
164
+ }
165
+ column.getAutoSortDir = () => {
166
+ const firstRow = table.getFilteredRowModel().flatRows[0]
170
167
 
171
- if (typeof value === 'string') {
172
- return 'asc'
173
- }
168
+ const value = firstRow?.getValue(column.id)
174
169
 
175
- return 'desc'
176
- },
177
- getSortingFn: () => {
178
- if (!column) {
179
- throw new Error()
180
- }
170
+ if (typeof value === 'string') {
171
+ return 'asc'
172
+ }
181
173
 
182
- return isFunction(column.columnDef.sortingFn)
183
- ? column.columnDef.sortingFn
184
- : column.columnDef.sortingFn === 'auto'
185
- ? column.getAutoSortingFn()
186
- : table.options.sortingFns?.[column.columnDef.sortingFn as string] ??
187
- sortingFns[column.columnDef.sortingFn as BuiltInSortingFn]
188
- },
189
- toggleSorting: (desc, multi) => {
190
- // if (column.columns.length) {
191
- // column.columns.forEach((c, i) => {
192
- // if (c.id) {
193
- // table.toggleColumnSorting(c.id, undefined, multi || !!i)
194
- // }
195
- // })
196
- // return
197
- // }
198
-
199
- // this needs to be outside of table.setSorting to be in sync with rerender
200
- const nextSortingOrder = column.getNextSortingOrder()
201
- const hasManualValue = typeof desc !== 'undefined' && desc !== null
202
-
203
- table.setSorting(old => {
204
- // Find any existing sorting for this column
205
- const existingSorting = old?.find(d => d.id === column.id)
206
- const existingIndex = old?.findIndex(d => d.id === column.id)
207
-
208
- let newSorting: SortingState = []
209
-
210
- // What should we do with this sort action?
211
- let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
212
- let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'
213
-
214
- // Multi-mode
215
- if (old?.length && column.getCanMultiSort() && multi) {
216
- if (existingSorting) {
217
- sortAction = 'toggle'
218
- } else {
219
- sortAction = 'add'
220
- }
174
+ return 'desc'
175
+ }
176
+ column.getSortingFn = () => {
177
+ if (!column) {
178
+ throw new Error()
179
+ }
180
+
181
+ return isFunction(column.columnDef.sortingFn)
182
+ ? column.columnDef.sortingFn
183
+ : column.columnDef.sortingFn === 'auto'
184
+ ? column.getAutoSortingFn()
185
+ : table.options.sortingFns?.[column.columnDef.sortingFn as string] ??
186
+ sortingFns[column.columnDef.sortingFn as BuiltInSortingFn]
187
+ }
188
+ column.toggleSorting = (desc, multi) => {
189
+ // if (column.columns.length) {
190
+ // column.columns.forEach((c, i) => {
191
+ // if (c.id) {
192
+ // table.toggleColumnSorting(c.id, undefined, multi || !!i)
193
+ // }
194
+ // })
195
+ // return
196
+ // }
197
+
198
+ // this needs to be outside of table.setSorting to be in sync with rerender
199
+ const nextSortingOrder = column.getNextSortingOrder()
200
+ const hasManualValue = typeof desc !== 'undefined' && desc !== null
201
+
202
+ table.setSorting(old => {
203
+ // Find any existing sorting for this column
204
+ const existingSorting = old?.find(d => d.id === column.id)
205
+ const existingIndex = old?.findIndex(d => d.id === column.id)
206
+
207
+ let newSorting: SortingState = []
208
+
209
+ // What should we do with this sort action?
210
+ let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
211
+ let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'
212
+
213
+ // Multi-mode
214
+ if (old?.length && column.getCanMultiSort() && multi) {
215
+ if (existingSorting) {
216
+ sortAction = 'toggle'
221
217
  } else {
222
- // Normal mode
223
- if (old?.length && existingIndex !== old.length - 1) {
224
- sortAction = 'replace'
225
- } else if (existingSorting) {
226
- sortAction = 'toggle'
227
- } else {
228
- sortAction = 'replace'
229
- }
218
+ sortAction = 'add'
230
219
  }
220
+ } else {
221
+ // Normal mode
222
+ if (old?.length && existingIndex !== old.length - 1) {
223
+ sortAction = 'replace'
224
+ } else if (existingSorting) {
225
+ sortAction = 'toggle'
226
+ } else {
227
+ sortAction = 'replace'
228
+ }
229
+ }
231
230
 
232
- // Handle toggle states that will remove the sorting
233
- if (sortAction === 'toggle') {
234
- // If we are "actually" toggling (not a manual set value), should we remove the sorting?
235
- if (!hasManualValue) {
236
- // Is our intention to remove?
237
- if (!nextSortingOrder) {
238
- sortAction = 'remove'
239
- }
231
+ // Handle toggle states that will remove the sorting
232
+ if (sortAction === 'toggle') {
233
+ // If we are "actually" toggling (not a manual set value), should we remove the sorting?
234
+ if (!hasManualValue) {
235
+ // Is our intention to remove?
236
+ if (!nextSortingOrder) {
237
+ sortAction = 'remove'
240
238
  }
241
239
  }
240
+ }
242
241
 
243
- if (sortAction === 'add') {
244
- newSorting = [
245
- ...old,
246
- {
247
- id: column.id,
242
+ if (sortAction === 'add') {
243
+ newSorting = [
244
+ ...old,
245
+ {
246
+ id: column.id,
247
+ desc: nextDesc,
248
+ },
249
+ ]
250
+ // Take latest n columns
251
+ newSorting.splice(
252
+ 0,
253
+ newSorting.length -
254
+ (table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
255
+ )
256
+ } else if (sortAction === 'toggle') {
257
+ // This flips (or sets) the
258
+ newSorting = old.map(d => {
259
+ if (d.id === column.id) {
260
+ return {
261
+ ...d,
248
262
  desc: nextDesc,
249
- },
250
- ]
251
- // Take latest n columns
252
- newSorting.splice(
253
- 0,
254
- newSorting.length -
255
- (table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
256
- )
257
- } else if (sortAction === 'toggle') {
258
- // This flips (or sets) the
259
- newSorting = old.map(d => {
260
- if (d.id === column.id) {
261
- return {
262
- ...d,
263
- desc: nextDesc,
264
- }
265
263
  }
266
- return d
267
- })
268
- } else if (sortAction === 'remove') {
269
- newSorting = old.filter(d => d.id !== column.id)
270
- } else {
271
- newSorting = [
272
- {
273
- id: column.id,
274
- desc: nextDesc,
275
- },
276
- ]
277
- }
264
+ }
265
+ return d
266
+ })
267
+ } else if (sortAction === 'remove') {
268
+ newSorting = old.filter(d => d.id !== column.id)
269
+ } else {
270
+ newSorting = [
271
+ {
272
+ id: column.id,
273
+ desc: nextDesc,
274
+ },
275
+ ]
276
+ }
278
277
 
279
- return newSorting
280
- })
281
- },
278
+ return newSorting
279
+ })
280
+ }
282
281
 
283
- getFirstSortDir: () => {
284
- const sortDescFirst =
285
- column.columnDef.sortDescFirst ??
286
- table.options.sortDescFirst ??
287
- column.getAutoSortDir() === 'desc'
288
- return sortDescFirst ? 'desc' : 'asc'
289
- },
282
+ column.getFirstSortDir = () => {
283
+ const sortDescFirst =
284
+ column.columnDef.sortDescFirst ??
285
+ table.options.sortDescFirst ??
286
+ column.getAutoSortDir() === 'desc'
287
+ return sortDescFirst ? 'desc' : 'asc'
288
+ }
290
289
 
291
- getNextSortingOrder: (multi?: boolean) => {
292
- const firstSortDirection = column.getFirstSortDir()
293
- const isSorted = column.getIsSorted()
290
+ column.getNextSortingOrder = (multi?: boolean) => {
291
+ const firstSortDirection = column.getFirstSortDir()
292
+ const isSorted = column.getIsSorted()
293
+
294
+ if (!isSorted) {
295
+ return firstSortDirection
296
+ }
297
+
298
+ if (
299
+ isSorted !== firstSortDirection &&
300
+ (table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
301
+ (multi ? table.options.enableMultiRemove ?? true : true) // If multi, don't allow if enableMultiRemove))
302
+ ) {
303
+ return false
304
+ }
305
+ return isSorted === 'desc' ? 'asc' : 'desc'
306
+ }
294
307
 
295
- if (!isSorted) {
296
- return firstSortDirection
297
- }
308
+ column.getCanSort = () => {
309
+ return (
310
+ (column.columnDef.enableSorting ?? true) &&
311
+ (table.options.enableSorting ?? true) &&
312
+ !!column.accessorFn
313
+ )
314
+ }
298
315
 
299
- if (
300
- isSorted !== firstSortDirection &&
301
- (table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
302
- (multi ? table.options.enableMultiRemove ?? true : true) // If multi, don't allow if enableMultiRemove))
303
- ) {
304
- return false
305
- }
306
- return isSorted === 'desc' ? 'asc' : 'desc'
307
- },
316
+ column.getCanMultiSort = () => {
317
+ return (
318
+ column.columnDef.enableMultiSort ??
319
+ table.options.enableMultiSort ??
320
+ !!column.accessorFn
321
+ )
322
+ }
308
323
 
309
- getCanSort: () => {
310
- return (
311
- (column.columnDef.enableSorting ?? true) &&
312
- (table.options.enableSorting ?? true) &&
313
- !!column.accessorFn
314
- )
315
- },
324
+ column.getIsSorted = () => {
325
+ const columnSort = table.getState().sorting?.find(d => d.id === column.id)
316
326
 
317
- getCanMultiSort: () => {
318
- return (
319
- column.columnDef.enableMultiSort ??
320
- table.options.enableMultiSort ??
321
- !!column.accessorFn
322
- )
323
- },
327
+ return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'
328
+ }
324
329
 
325
- getIsSorted: () => {
326
- const columnSort = table
327
- .getState()
328
- .sorting?.find(d => d.id === column.id)
330
+ column.getSortIndex = () =>
331
+ table.getState().sorting?.findIndex(d => d.id === column.id) ?? -1
329
332
 
330
- return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'
331
- },
333
+ column.clearSorting = () => {
334
+ //clear sorting for just 1 column
335
+ table.setSorting(old =>
336
+ old?.length ? old.filter(d => d.id !== column.id) : []
337
+ )
338
+ }
332
339
 
333
- getSortIndex: () =>
334
- table.getState().sorting?.findIndex(d => d.id === column.id) ?? -1,
340
+ column.getToggleSortingHandler = () => {
341
+ const canSort = column.getCanSort()
335
342
 
336
- clearSorting: () => {
337
- //clear sorting for just 1 column
338
- table.setSorting(old =>
339
- old?.length ? old.filter(d => d.id !== column.id) : []
343
+ return (e: unknown) => {
344
+ if (!canSort) return
345
+ ;(e as any).persist?.()
346
+ column.toggleSorting?.(
347
+ undefined,
348
+ column.getCanMultiSort() ? table.options.isMultiSortEvent?.(e) : false
340
349
  )
341
- },
342
-
343
- getToggleSortingHandler: () => {
344
- const canSort = column.getCanSort()
345
-
346
- return (e: unknown) => {
347
- if (!canSort) return
348
- ;(e as any).persist?.()
349
- column.toggleSorting?.(
350
- undefined,
351
- column.getCanMultiSort()
352
- ? table.options.isMultiSortEvent?.(e)
353
- : false
354
- )
355
- }
356
- },
350
+ }
357
351
  }
358
352
  },
359
353
 
360
- createTable: <TData extends RowData>(
361
- table: Table<TData>
362
- ): SortingInstance<TData> => {
363
- return {
364
- setSorting: updater => table.options.onSortingChange?.(updater),
365
- resetSorting: defaultState => {
366
- table.setSorting(defaultState ? [] : table.initialState?.sorting ?? [])
367
- },
368
- getPreSortedRowModel: () => table.getGroupedRowModel(),
369
- getSortedRowModel: () => {
370
- if (!table._getSortedRowModel && table.options.getSortedRowModel) {
371
- table._getSortedRowModel = table.options.getSortedRowModel(table)
372
- }
354
+ createTable: <TData extends RowData>(table: Table<TData>): void => {
355
+ table.setSorting = updater => table.options.onSortingChange?.(updater)
356
+ table.resetSorting = defaultState => {
357
+ table.setSorting(defaultState ? [] : table.initialState?.sorting ?? [])
358
+ }
359
+ table.getPreSortedRowModel = () => table.getGroupedRowModel()
360
+ table.getSortedRowModel = () => {
361
+ if (!table._getSortedRowModel && table.options.getSortedRowModel) {
362
+ table._getSortedRowModel = table.options.getSortedRowModel(table)
363
+ }
373
364
 
374
- if (table.options.manualSorting || !table._getSortedRowModel) {
375
- return table.getPreSortedRowModel()
376
- }
365
+ if (table.options.manualSorting || !table._getSortedRowModel) {
366
+ return table.getPreSortedRowModel()
367
+ }
377
368
 
378
- return table._getSortedRowModel()
379
- },
369
+ return table._getSortedRowModel()
380
370
  }
381
371
  },
382
372
  }