@tanstack/react-table 8.0.0-alpha.1 → 8.0.0-alpha.11

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 (96) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -78
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  3. package/build/cjs/index.js +67 -12
  4. package/build/cjs/index.js.map +1 -1
  5. package/build/esm/index.js +30 -3346
  6. package/build/esm/index.js.map +1 -1
  7. package/build/stats-html.html +1 -1
  8. package/build/stats-react.json +28 -632
  9. package/build/types/index.d.ts +3 -7
  10. package/build/umd/index.development.js +37 -3348
  11. package/build/umd/index.development.js.map +1 -1
  12. package/build/umd/index.production.js +1 -1
  13. package/build/umd/index.production.js.map +1 -1
  14. package/package.json +4 -1
  15. package/src/index.tsx +56 -7
  16. package/build/cjs/aggregationTypes.js +0 -130
  17. package/build/cjs/aggregationTypes.js.map +0 -1
  18. package/build/cjs/core.js +0 -493
  19. package/build/cjs/core.js.map +0 -1
  20. package/build/cjs/createTable.js +0 -103
  21. package/build/cjs/createTable.js.map +0 -1
  22. package/build/cjs/features/Expanding.js +0 -234
  23. package/build/cjs/features/Expanding.js.map +0 -1
  24. package/build/cjs/features/Filters.js +0 -396
  25. package/build/cjs/features/Filters.js.map +0 -1
  26. package/build/cjs/features/Grouping.js +0 -228
  27. package/build/cjs/features/Grouping.js.map +0 -1
  28. package/build/cjs/features/Headers.js +0 -483
  29. package/build/cjs/features/Headers.js.map +0 -1
  30. package/build/cjs/features/Ordering.js +0 -83
  31. package/build/cjs/features/Ordering.js.map +0 -1
  32. package/build/cjs/features/Pinning.js +0 -163
  33. package/build/cjs/features/Pinning.js.map +0 -1
  34. package/build/cjs/features/Sorting.js +0 -269
  35. package/build/cjs/features/Sorting.js.map +0 -1
  36. package/build/cjs/features/Visibility.js +0 -160
  37. package/build/cjs/features/Visibility.js.map +0 -1
  38. package/build/cjs/filterTypes.js +0 -172
  39. package/build/cjs/filterTypes.js.map +0 -1
  40. package/build/cjs/sortTypes.js +0 -121
  41. package/build/cjs/sortTypes.js.map +0 -1
  42. package/build/cjs/utils/columnFilterRowsFn.js +0 -130
  43. package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
  44. package/build/cjs/utils/expandRowsFn.js +0 -38
  45. package/build/cjs/utils/expandRowsFn.js.map +0 -1
  46. package/build/cjs/utils/globalFilterRowsFn.js +0 -100
  47. package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
  48. package/build/cjs/utils/groupRowsFn.js +0 -154
  49. package/build/cjs/utils/groupRowsFn.js.map +0 -1
  50. package/build/cjs/utils/sortRowsFn.js +0 -93
  51. package/build/cjs/utils/sortRowsFn.js.map +0 -1
  52. package/build/cjs/utils.js +0 -143
  53. package/build/cjs/utils.js.map +0 -1
  54. package/build/types/aggregationTypes.d.ts +0 -22
  55. package/build/types/core.d.ts +0 -122
  56. package/build/types/createTable.d.ts +0 -35
  57. package/build/types/features/Expanding.d.ts +0 -52
  58. package/build/types/features/Filters.d.ts +0 -93
  59. package/build/types/features/Grouping.d.ts +0 -82
  60. package/build/types/features/Headers.d.ts +0 -41
  61. package/build/types/features/Ordering.d.ts +0 -19
  62. package/build/types/features/Pinning.d.ts +0 -39
  63. package/build/types/features/Sorting.d.ts +0 -75
  64. package/build/types/features/Visibility.d.ts +0 -47
  65. package/build/types/filterTypes.d.ts +0 -50
  66. package/build/types/sortTypes.d.ts +0 -17
  67. package/build/types/types.d.ts +0 -122
  68. package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
  69. package/build/types/utils/expandRowsFn.d.ts +0 -2
  70. package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
  71. package/build/types/utils/groupRowsFn.d.ts +0 -2
  72. package/build/types/utils/sortRowsFn.d.ts +0 -2
  73. package/build/types/utils.d.ts +0 -24
  74. package/src/aggregationTypes.ts +0 -115
  75. package/src/core.tsx +0 -1194
  76. package/src/createTable.tsx +0 -181
  77. package/src/features/Expanding.ts +0 -388
  78. package/src/features/Filters.ts +0 -707
  79. package/src/features/Grouping.ts +0 -451
  80. package/src/features/Headers.ts +0 -907
  81. package/src/features/Ordering.ts +0 -134
  82. package/src/features/Pinning.ts +0 -213
  83. package/src/features/Sorting.ts +0 -487
  84. package/src/features/Visibility.ts +0 -281
  85. package/src/features/withColumnResizing.oldts +0 -281
  86. package/src/features/withPagination.oldts +0 -208
  87. package/src/features/withRowSelection.oldts +0 -467
  88. package/src/filterTypes.ts +0 -251
  89. package/src/sortTypes.ts +0 -159
  90. package/src/types.ts +0 -285
  91. package/src/utils/columnFilterRowsFn.ts +0 -162
  92. package/src/utils/expandRowsFn.ts +0 -53
  93. package/src/utils/globalFilterRowsFn.ts +0 -129
  94. package/src/utils/groupRowsFn.ts +0 -196
  95. package/src/utils/sortRowsFn.ts +0 -115
  96. package/src/utils.tsx +0 -243
@@ -1,487 +0,0 @@
1
- import { MouseEvent, TouchEvent } from 'react'
2
- import { RowModel } from '..'
3
- import { BuiltInSortType, sortTypes } from '../sortTypes'
4
-
5
- import {
6
- Column,
7
- Getter,
8
- OnChangeFn,
9
- PropGetterValue,
10
- ReactTable,
11
- Row,
12
- Updater,
13
- } from '../types'
14
-
15
- import {
16
- functionalUpdate,
17
- isFunction,
18
- makeStateUpdater,
19
- memo,
20
- propGetter,
21
- } from '../utils'
22
-
23
- export type ColumnSort = {
24
- id: string
25
- desc: boolean
26
- }
27
-
28
- export type SortingState = ColumnSort[]
29
-
30
- export type SortingFn<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> =
31
- {
32
- (
33
- rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
34
- rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
35
- columnId: string
36
- ): number
37
- }
38
-
39
- export type SortingTableState = {
40
- sorting: SortingState
41
- }
42
-
43
- export type SortType<TSortingFns> =
44
- | 'auto'
45
- | BuiltInSortType
46
- | keyof TSortingFns
47
- | SortingFn<any, any, any, TSortingFns, any>
48
-
49
- export type SortingColumnDef<TFilterFns> = {
50
- sortType?: SortType<TFilterFns>
51
- sortDescFirst?: boolean
52
- enableSorting?: boolean
53
- enableMultiSort?: boolean
54
- defaultCanSort?: boolean
55
- invertSorting?: boolean
56
- sortUndefined?: false | -1 | 1
57
- }
58
-
59
- export type SortingColumn<
60
- _TData,
61
- _TValue,
62
- TFilterFns,
63
- _TSortingFns,
64
- _TAggregationFns
65
- > = {
66
- sortType: SortType<TFilterFns>
67
- getCanSort: () => boolean
68
- getCanMultiSort: () => boolean
69
- getSortIndex: () => number
70
- getIsSorted: () => false | 'asc' | 'desc'
71
- toggleSorting: (desc?: boolean, isMulti?: boolean) => void
72
- getToggleSortingProps: <TGetter extends Getter<ToggleSortingProps>>(
73
- userProps?: TGetter
74
- ) => undefined | PropGetterValue<ToggleSortingProps, TGetter>
75
- }
76
-
77
- export type SortingOptions<
78
- TData,
79
- TValue,
80
- TFilterFns,
81
- TSortingFns,
82
- TAggregationFns
83
- > = {
84
- sortTypes?: TSortingFns
85
- onSortingChange?: OnChangeFn<SortingState>
86
- autoResetSorting?: boolean
87
- enableSorting?: boolean
88
- enableSortingRemoval?: boolean
89
- enableMultiRemove?: boolean
90
- enableMultiSort?: boolean
91
- sortDescFirst?: boolean
92
- sortRowsFn?: (
93
- instance: ReactTable<
94
- TData,
95
- TValue,
96
- TFilterFns,
97
- TSortingFns,
98
- TAggregationFns
99
- >,
100
- sortingState: SortingState,
101
- globalFilteredRowModel: RowModel<
102
- TData,
103
- TValue,
104
- TFilterFns,
105
- TSortingFns,
106
- TAggregationFns
107
- >
108
- ) => RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
109
- maxMultiSortColCount?: number
110
- isMultiSortEvent?: (e: MouseEvent | TouchEvent) => boolean
111
- }
112
-
113
- export type ToggleSortingProps = {
114
- title?: string
115
- onClick?: (event: MouseEvent | TouchEvent) => void
116
- }
117
-
118
- export type SortingInstance<
119
- TData,
120
- TValue,
121
- TFilterFns,
122
- TSortingFns,
123
- TAggregationFns
124
- > = {
125
- getColumnAutoSortingFn: (
126
- columnId: string
127
- ) => SortingFn<any, any, any, any, any> | undefined
128
-
129
- getColumnSortingFn: (
130
- columnId: string
131
- ) => SortingFn<any, any, any, any, any> | undefined
132
-
133
- setSorting: (updater: Updater<SortingState>) => void
134
- toggleColumnSorting: (
135
- columnId: string,
136
- desc?: boolean,
137
- multi?: boolean
138
- ) => void
139
- resetSorting: () => void
140
- getColumnCanSort: (columnId: string) => boolean
141
- getColumnCanMultiSort: (columnId: string) => boolean
142
- getColumnIsSorted: (columnId: string) => false | 'asc' | 'desc'
143
- getColumnSortIndex: (columnId: string) => number
144
- getToggleSortingProps: <TGetter extends Getter<ToggleSortingProps>>(
145
- columnId: string,
146
- userProps?: TGetter
147
- ) => undefined | PropGetterValue<ToggleSortingProps, TGetter>
148
- getSortedRowModel: () => RowModel<
149
- TData,
150
- TValue,
151
- TFilterFns,
152
- TSortingFns,
153
- TAggregationFns
154
- >
155
- getPreSortedRows: () => Row<
156
- TData,
157
- TValue,
158
- TFilterFns,
159
- TSortingFns,
160
- TAggregationFns
161
- >[]
162
- getPreSortedFlatRows: () => Row<
163
- TData,
164
- TValue,
165
- TFilterFns,
166
- TSortingFns,
167
- TAggregationFns
168
- >[]
169
- getPreSortedRowsById: () => Record<
170
- string,
171
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
172
- >
173
- getSortedRows: () => Row<
174
- TData,
175
- TValue,
176
- TFilterFns,
177
- TSortingFns,
178
- TAggregationFns
179
- >[]
180
- getSortedFlatRows: () => Row<
181
- TData,
182
- TValue,
183
- TFilterFns,
184
- TSortingFns,
185
- TAggregationFns
186
- >[]
187
- getSortedRowsById: () => Record<
188
- string,
189
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
190
- >
191
- }
192
-
193
- //
194
-
195
- export function getDefaultColumn<TFilterFns>(): SortingColumnDef<TFilterFns> {
196
- return {
197
- sortType: 'auto',
198
- }
199
- }
200
-
201
- export function getInitialState(): SortingTableState {
202
- return {
203
- sorting: [],
204
- }
205
- }
206
-
207
- export function getDefaultOptions<
208
- TData,
209
- TValue,
210
- TFilterFns,
211
- TSortingFns,
212
- TAggregationFns
213
- >(
214
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
215
- ): SortingOptions<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
216
- return {
217
- onSortingChange: makeStateUpdater('sorting', instance),
218
- autoResetSorting: true,
219
- }
220
- }
221
-
222
- export function createColumn<
223
- TData,
224
- TValue,
225
- TFilterFns,
226
- TSortingFns,
227
- TAggregationFns
228
- >(
229
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
230
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
231
- ): SortingColumn<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
232
- return {
233
- sortType: column.sortType,
234
- getCanSort: () => instance.getColumnCanSort(column.id),
235
- getCanMultiSort: () => instance.getColumnCanMultiSort(column.id),
236
- getSortIndex: () => instance.getColumnSortIndex(column.id),
237
- getIsSorted: () => instance.getColumnIsSorted(column.id),
238
- toggleSorting: (desc, isMulti) =>
239
- instance.toggleColumnSorting(column.id, desc, isMulti),
240
- getToggleSortingProps: userProps =>
241
- instance.getToggleSortingProps(column.id, userProps),
242
- }
243
- }
244
-
245
- export function getInstance<
246
- TData,
247
- TValue,
248
- TFilterFns,
249
- TSortingFns,
250
- TAggregationFns
251
- >(
252
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
253
- ): SortingInstance<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
254
- return {
255
- getColumnAutoSortingFn: columnId => {
256
- const firstRow = instance.getGlobalFilteredRowModel().flatRows[0]
257
-
258
- const value = firstRow?.values[columnId]
259
-
260
- if (typeof value === 'string') {
261
- return sortTypes.alphanumeric
262
- }
263
-
264
- if (Object.prototype.toString.call(value) === '[object Date]') {
265
- return sortTypes.datetime
266
- }
267
-
268
- return sortTypes.basic
269
- },
270
- getColumnSortingFn: columnId => {
271
- const column = instance.getColumn(columnId)
272
- const userSortTypes = instance.options.sortTypes
273
-
274
- if (!column) {
275
- throw new Error()
276
- }
277
-
278
- return isFunction(column.sortType)
279
- ? column.sortType
280
- : column.sortType === 'auto'
281
- ? instance.getColumnAutoFilterFn(columnId)
282
- : (userSortTypes as Record<string, any>)?.[column.sortType as string] ??
283
- (sortTypes[column.sortType as BuiltInSortType] as SortingFn<
284
- TData,
285
- TValue,
286
- TFilterFns,
287
- TSortingFns,
288
- TAggregationFns
289
- >)
290
- },
291
-
292
- setSorting: updater =>
293
- instance.options.onSortingChange?.(
294
- updater,
295
- functionalUpdate(updater, instance.getState().sorting)
296
- ),
297
-
298
- toggleColumnSorting: (columnId, desc, multi) => {
299
- const column = instance.getColumn(columnId)
300
-
301
- if (!column) {
302
- throw new Error()
303
- }
304
-
305
- instance.setSorting(old => {
306
- // Find any existing sorting for this column
307
- const existingSorting = old?.find(d => d.id === columnId)
308
- const existingIndex = old?.findIndex(d => d.id === columnId)
309
- const hasDescDefined = typeof desc !== 'undefined' && desc !== null
310
-
311
- let newSorting: SortingState = []
312
-
313
- // What should we do with this sort action?
314
- let sortAction
315
-
316
- if (!column.getCanMultiSort() && multi) {
317
- if (existingSorting) {
318
- sortAction = 'toggle'
319
- } else {
320
- sortAction = 'add'
321
- }
322
- } else {
323
- // Normal mode
324
- if (old?.length && existingIndex !== old.length - 1) {
325
- sortAction = 'replace'
326
- } else if (existingSorting) {
327
- sortAction = 'toggle'
328
- } else {
329
- sortAction = 'replace'
330
- }
331
- }
332
-
333
- const sortDescFirst =
334
- column.sortDescFirst ?? instance.options.sortDescFirst
335
-
336
- // Handle toggle states that will remove the sorting
337
- if (
338
- sortAction === 'toggle' && // Must be toggling
339
- (instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
340
- !hasDescDefined && // Must not be setting desc
341
- (multi ? instance.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
342
- (existingSorting?.desc // Finally, detect if it should indeed be removed
343
- ? !sortDescFirst
344
- : sortDescFirst)
345
- ) {
346
- sortAction = 'remove'
347
- }
348
-
349
- if (sortAction === 'replace') {
350
- newSorting = [
351
- {
352
- id: columnId,
353
- desc: hasDescDefined ? desc! : !!sortDescFirst,
354
- },
355
- ]
356
- } else if (sortAction === 'add' && old?.length) {
357
- newSorting = [
358
- ...old,
359
- {
360
- id: columnId,
361
- desc: hasDescDefined ? desc! : !!sortDescFirst,
362
- },
363
- ]
364
- // Take latest n columns
365
- newSorting.splice(
366
- 0,
367
- newSorting.length -
368
- (instance.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
369
- )
370
- } else if (sortAction === 'toggle' && old?.length) {
371
- // This flips (or sets) the
372
- newSorting = old.map(d => {
373
- if (d.id === columnId) {
374
- return {
375
- ...d,
376
- desc: hasDescDefined ? desc! : !existingSorting?.desc,
377
- }
378
- }
379
- return d
380
- })
381
- } else if (sortAction === 'remove' && old?.length) {
382
- newSorting = old.filter(d => d.id !== columnId)
383
- }
384
-
385
- return newSorting
386
- })
387
- },
388
-
389
- getColumnCanSort: columnId => {
390
- const column = instance.getColumn(columnId)
391
-
392
- if (!column) {
393
- throw new Error()
394
- }
395
-
396
- return (
397
- column.enableSorting ??
398
- instance.options.enableSorting ??
399
- column.defaultCanSort ??
400
- !!column.accessorFn
401
- )
402
- },
403
-
404
- getColumnCanMultiSort: columnId => {
405
- const column = instance.getColumn(columnId)
406
-
407
- if (!column) {
408
- throw new Error()
409
- }
410
-
411
- return (
412
- column.enableMultiSort ??
413
- instance.options.enableMultiSort ??
414
- !!column.accessorFn
415
- )
416
- },
417
-
418
- getColumnIsSorted: columnId => {
419
- const columnSort = instance
420
- .getState()
421
- .sorting?.find(d => d.id === columnId)
422
-
423
- return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'
424
- },
425
-
426
- getColumnSortIndex: columnId =>
427
- instance.getState().sorting?.findIndex(d => d.id === columnId) ?? -1,
428
-
429
- resetSorting: () => {
430
- instance.setSorting(instance.options?.initialState?.sorting ?? [])
431
- },
432
-
433
- getToggleSortingProps: (columnId, userProps) => {
434
- const column = instance.getColumn(columnId)
435
-
436
- if (!column) {
437
- throw new Error()
438
- }
439
-
440
- const canSort = column.getCanSort()
441
-
442
- const initialProps: ToggleSortingProps = {
443
- title: canSort ? 'Toggle Sorting' : undefined,
444
- onClick: canSort
445
- ? (e: MouseEvent | TouchEvent) => {
446
- e.persist()
447
- column.toggleSorting?.(
448
- undefined,
449
- column.getCanMultiSort()
450
- ? instance.options.isMultiSortEvent?.(e)
451
- : false
452
- )
453
- }
454
- : undefined,
455
- }
456
-
457
- return propGetter(initialProps, userProps)
458
- },
459
-
460
- getSortedRowModel: memo(
461
- () => [
462
- instance.getState().sorting,
463
- instance.getGlobalFilteredRowModel(),
464
- instance.options.sortRowsFn,
465
- ],
466
- (sorting, rowModel, sortingFn) => {
467
- if (!sortingFn || !sorting?.length) {
468
- return rowModel
469
- }
470
-
471
- if (process.env.NODE_ENV !== 'production' && instance.options.debug)
472
- console.info('Sorting...')
473
-
474
- return sortingFn(instance, sorting, rowModel)
475
- },
476
- 'getSortedRowModel',
477
- instance.options.debug
478
- ),
479
-
480
- getPreSortedRows: () => instance.getGlobalFilteredRowModel().rows,
481
- getPreSortedFlatRows: () => instance.getGlobalFilteredRowModel().flatRows,
482
- getPreSortedRowsById: () => instance.getGlobalFilteredRowModel().rowsById,
483
- getSortedRows: () => instance.getSortedRowModel().rows,
484
- getSortedFlatRows: () => instance.getSortedRowModel().flatRows,
485
- getSortedRowsById: () => instance.getSortedRowModel().rowsById,
486
- }
487
- }