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

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 (100) hide show
  1. package/build/cjs/core.js +118 -59
  2. package/build/cjs/core.js.map +1 -1
  3. package/build/cjs/createTable.js +11 -6
  4. package/build/cjs/createTable.js.map +1 -1
  5. package/build/cjs/features/ColumnSizing.js +317 -0
  6. package/build/cjs/features/ColumnSizing.js.map +1 -0
  7. package/build/cjs/features/Expanding.js +23 -2
  8. package/build/cjs/features/Expanding.js.map +1 -1
  9. package/build/cjs/features/Filters.js +54 -5
  10. package/build/cjs/features/Filters.js.map +1 -1
  11. package/build/cjs/features/Grouping.js +23 -2
  12. package/build/cjs/features/Grouping.js.map +1 -1
  13. package/build/cjs/features/Headers.js +88 -22
  14. package/build/cjs/features/Headers.js.map +1 -1
  15. package/build/cjs/features/Ordering.js +4 -1
  16. package/build/cjs/features/Ordering.js.map +1 -1
  17. package/build/cjs/features/Pagination.js +198 -0
  18. package/build/cjs/features/Pagination.js.map +1 -0
  19. package/build/cjs/features/Pinning.js +0 -14
  20. package/build/cjs/features/Pinning.js.map +1 -1
  21. package/build/cjs/features/RowSelection.js +541 -0
  22. package/build/cjs/features/RowSelection.js.map +1 -0
  23. package/build/cjs/features/Sorting.js +76 -18
  24. package/build/cjs/features/Sorting.js.map +1 -1
  25. package/build/cjs/features/Visibility.js +8 -2
  26. package/build/cjs/features/Visibility.js.map +1 -1
  27. package/build/cjs/index.js +2 -0
  28. package/build/cjs/index.js.map +1 -1
  29. package/build/cjs/sortTypes.js +1 -0
  30. package/build/cjs/sortTypes.js.map +1 -1
  31. package/build/cjs/utils/columnFilterRowsFn.js +3 -2
  32. package/build/cjs/utils/columnFilterRowsFn.js.map +1 -1
  33. package/build/cjs/utils/expandRowsFn.js +2 -2
  34. package/build/cjs/utils/expandRowsFn.js.map +1 -1
  35. package/build/cjs/utils/globalFilterRowsFn.js +3 -2
  36. package/build/cjs/utils/globalFilterRowsFn.js.map +1 -1
  37. package/build/cjs/utils/groupRowsFn.js +4 -3
  38. package/build/cjs/utils/groupRowsFn.js.map +1 -1
  39. package/build/cjs/utils/paginateRowsFn.js +44 -0
  40. package/build/cjs/utils/paginateRowsFn.js.map +1 -0
  41. package/build/cjs/utils/sortRowsFn.js +3 -2
  42. package/build/cjs/utils/sortRowsFn.js.map +1 -1
  43. package/build/cjs/utils.js +6 -3
  44. package/build/cjs/utils.js.map +1 -1
  45. package/build/esm/index.js +1613 -225
  46. package/build/esm/index.js.map +1 -1
  47. package/build/stats-html.html +1 -1
  48. package/build/stats-react.json +363 -217
  49. package/build/types/core.d.ts +14 -25
  50. package/build/types/createTable.d.ts +21 -3
  51. package/build/types/features/ColumnSizing.d.ts +67 -0
  52. package/build/types/features/Expanding.d.ts +2 -1
  53. package/build/types/features/Filters.d.ts +7 -2
  54. package/build/types/features/Grouping.d.ts +2 -2
  55. package/build/types/features/Ordering.d.ts +1 -1
  56. package/build/types/features/Pagination.d.ts +44 -0
  57. package/build/types/features/Pinning.d.ts +3 -3
  58. package/build/types/features/RowSelection.d.ts +66 -0
  59. package/build/types/features/Sorting.d.ts +5 -2
  60. package/build/types/index.d.ts +1 -0
  61. package/build/types/sortTypes.d.ts +5 -4
  62. package/build/types/types.d.ts +13 -8
  63. package/build/types/utils/columnFilterRowsFn.d.ts +2 -2
  64. package/build/types/utils/expandRowsFn.d.ts +2 -2
  65. package/build/types/utils/globalFilterRowsFn.d.ts +2 -2
  66. package/build/types/utils/groupRowsFn.d.ts +2 -2
  67. package/build/types/utils/paginateRowsFn.d.ts +2 -0
  68. package/build/types/utils/sortRowsFn.d.ts +2 -2
  69. package/build/types/utils.d.ts +7 -3
  70. package/build/umd/index.development.js +1613 -224
  71. package/build/umd/index.development.js.map +1 -1
  72. package/build/umd/index.production.js +1 -1
  73. package/build/umd/index.production.js.map +1 -1
  74. package/package.json +1 -1
  75. package/src/core.tsx +252 -265
  76. package/src/createTable.tsx +69 -9
  77. package/src/features/ColumnSizing.ts +453 -0
  78. package/src/features/Expanding.ts +27 -11
  79. package/src/features/Filters.ts +75 -20
  80. package/src/features/Grouping.ts +27 -12
  81. package/src/features/Headers.ts +55 -50
  82. package/src/features/Ordering.ts +2 -3
  83. package/src/features/Pagination.ts +327 -0
  84. package/src/features/Pinning.ts +3 -16
  85. package/src/features/RowSelection.ts +831 -0
  86. package/src/features/Sorting.ts +82 -22
  87. package/src/features/Visibility.ts +2 -4
  88. package/src/index.tsx +1 -0
  89. package/src/sortTypes.ts +1 -1
  90. package/src/types.ts +55 -9
  91. package/src/utils/columnFilterRowsFn.ts +5 -12
  92. package/src/utils/expandRowsFn.ts +2 -5
  93. package/src/utils/globalFilterRowsFn.ts +3 -10
  94. package/src/utils/groupRowsFn.ts +3 -5
  95. package/src/utils/paginateRowsFn.ts +34 -0
  96. package/src/utils/sortRowsFn.ts +5 -5
  97. package/src/utils.tsx +20 -6
  98. package/src/features/withColumnResizing.oldts +0 -281
  99. package/src/features/withPagination.oldts +0 -208
  100. package/src/features/withRowSelection.oldts +0 -467
@@ -0,0 +1,327 @@
1
+ import {
2
+ Column,
3
+ OnChangeFn,
4
+ ReactTable,
5
+ Row,
6
+ RowModel,
7
+ TableState,
8
+ Updater,
9
+ } from '../types'
10
+ import { functionalUpdate, makeStateUpdater, memo } from '../utils'
11
+
12
+ export type PageCount = undefined | null | number
13
+
14
+ export type PaginationState = {
15
+ pageIndex: number
16
+ pageSize: number
17
+ pageCount?: PageCount
18
+ }
19
+
20
+ export type PaginationTableState = {
21
+ pagination: PaginationState
22
+ }
23
+
24
+ export type PaginationOptions<
25
+ TData,
26
+ TValue,
27
+ TFilterFns,
28
+ TSortingFns,
29
+ TAggregationFns
30
+ > = {
31
+ onPaginationChange?: OnChangeFn<PaginationState>
32
+ autoResetPageIndex?: boolean
33
+ paginateRowsFn?: (
34
+ instance: ReactTable<
35
+ TData,
36
+ TValue,
37
+ TFilterFns,
38
+ TSortingFns,
39
+ TAggregationFns
40
+ >,
41
+ rowModel: RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
42
+ ) => RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
43
+ }
44
+
45
+ export type PaginationDefaultOptions = {
46
+ onPaginationChange: OnChangeFn<PaginationState>
47
+ autoResetPageIndex: boolean
48
+ }
49
+
50
+ export type PaginationInstance<
51
+ TData,
52
+ TValue,
53
+ TFilterFns,
54
+ TSortingFns,
55
+ TAggregationFns
56
+ > = {
57
+ _notifyPageIndexReset: () => void
58
+ setPagination: (updater: Updater<PaginationState>) => void
59
+ resetPagination: () => void
60
+ setPageIndex: (updater: Updater<number>) => void
61
+ resetPageIndex: () => void
62
+ setPageSize: (updater: Updater<number>) => void
63
+ resetPageSize: () => void
64
+ setPageCount: (updater: Updater<PageCount>) => void
65
+ getPageOptions: () => number[]
66
+ getCanPreviousPage: () => boolean
67
+ getCanNextPage: () => boolean
68
+ previousPage: () => void
69
+ nextPage: () => void
70
+ getPaginationRowModel: () => RowModel<
71
+ TData,
72
+ TValue,
73
+ TFilterFns,
74
+ TSortingFns,
75
+ TAggregationFns
76
+ >
77
+ getPrePaginationRows: () => Row<
78
+ TData,
79
+ TValue,
80
+ TFilterFns,
81
+ TSortingFns,
82
+ TAggregationFns
83
+ >[]
84
+ getPrePaginationFlatRows: () => Row<
85
+ TData,
86
+ TValue,
87
+ TFilterFns,
88
+ TSortingFns,
89
+ TAggregationFns
90
+ >[]
91
+ getPrePaginationRowsById: () => Record<
92
+ string,
93
+ Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
94
+ >
95
+ getPaginationRows: () => Row<
96
+ TData,
97
+ TValue,
98
+ TFilterFns,
99
+ TSortingFns,
100
+ TAggregationFns
101
+ >[]
102
+ getPaginationFlatRows: () => Row<
103
+ TData,
104
+ TValue,
105
+ TFilterFns,
106
+ TSortingFns,
107
+ TAggregationFns
108
+ >[]
109
+ getPaginationRowsById: () => Record<
110
+ string,
111
+ Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
112
+ >
113
+ }
114
+
115
+ //
116
+
117
+ export function getInitialState(): PaginationTableState {
118
+ return {
119
+ pagination: {
120
+ pageIndex: 0,
121
+ pageSize: 10,
122
+ },
123
+ }
124
+ }
125
+
126
+ export function getDefaultOptions<
127
+ TData,
128
+ TValue,
129
+ TFilterFns,
130
+ TSortingFns,
131
+ TAggregationFns
132
+ >(
133
+ instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
134
+ ): PaginationDefaultOptions {
135
+ return {
136
+ onPaginationChange: makeStateUpdater('pagination', instance),
137
+ autoResetPageIndex: true,
138
+ }
139
+ }
140
+
141
+ export function getInstance<
142
+ TData,
143
+ TValue,
144
+ TFilterFns,
145
+ TSortingFns,
146
+ TAggregationFns
147
+ >(
148
+ instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
149
+ ): PaginationInstance<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
150
+ let registered = false
151
+ return {
152
+ _notifyPageIndexReset: () => {
153
+ if (!registered) {
154
+ registered = true
155
+ return
156
+ }
157
+
158
+ if (instance.options.autoResetAll === false) {
159
+ return
160
+ }
161
+
162
+ if (
163
+ instance.options.autoResetAll === true ||
164
+ instance.options.autoResetPageIndex
165
+ ) {
166
+ instance.resetPageSize()
167
+ }
168
+ },
169
+ setPagination: updater => {
170
+ const safeUpdater: Updater<PaginationState> = old => {
171
+ let newState = functionalUpdate(updater, old)
172
+
173
+ if (instance.options.paginateRowsFn) {
174
+ newState.pageCount = instance.getPrePaginationRows()?.length
175
+ ? Math.ceil(
176
+ instance.getPrePaginationRows().length /
177
+ instance.getState().pagination.pageSize
178
+ )
179
+ : 0
180
+ }
181
+
182
+ return newState
183
+ }
184
+
185
+ return instance.options.onPaginationChange?.(
186
+ safeUpdater,
187
+ functionalUpdate(safeUpdater, instance.getState().pagination)
188
+ )
189
+ },
190
+ resetPagination: () => {
191
+ instance.setPagination(
192
+ instance.initialState.pagination ?? {
193
+ pageIndex: 0,
194
+ pageSize: 10,
195
+ pageCount: -1,
196
+ }
197
+ )
198
+ },
199
+ setPageIndex: updater => {
200
+ instance.setPagination(old => {
201
+ let pageIndex = functionalUpdate(updater, old.pageIndex)
202
+
203
+ const maxPageIndex =
204
+ old.pageCount && old.pageCount > 0
205
+ ? old.pageCount - 1
206
+ : Number.MAX_SAFE_INTEGER
207
+
208
+ pageIndex = Math.min(Math.max(0, pageIndex), maxPageIndex)
209
+
210
+ return {
211
+ ...old,
212
+ pageIndex,
213
+ }
214
+ })
215
+ },
216
+ resetPageIndex: () => {
217
+ instance.setPageIndex(0)
218
+ },
219
+ resetPageSize: () => {
220
+ instance.setPageSize(
221
+ instance.options.initialState?.pagination?.pageSize ?? 10
222
+ )
223
+ },
224
+ setPageSize: updater => {
225
+ instance.setPagination(old => {
226
+ const pageSize = Math.max(1, functionalUpdate(updater, old.pageSize))
227
+ const topRowIndex = old.pageSize * old.pageIndex!
228
+ const pageIndex = Math.floor(topRowIndex / pageSize)
229
+
230
+ return {
231
+ ...old,
232
+ pageIndex,
233
+ pageSize,
234
+ }
235
+ })
236
+ },
237
+ setPageCount: updater =>
238
+ instance.setPagination(old => {
239
+ let newPageCount = functionalUpdate(updater, old.pageCount)
240
+
241
+ if (typeof newPageCount === 'number') {
242
+ newPageCount = Math.max(-1, newPageCount)
243
+ }
244
+
245
+ return {
246
+ ...old,
247
+ pageCount: newPageCount,
248
+ }
249
+ }),
250
+
251
+ getPageOptions: memo(
252
+ () => [
253
+ instance.getState().pagination.pageSize,
254
+ instance.getState().pagination.pageCount,
255
+ ],
256
+ (pageSize, pageCount) => {
257
+ let pageOptions: number[] = []
258
+ if (pageCount && pageCount > 0) {
259
+ pageOptions = [...new Array(pageCount)].fill(null).map((_, i) => i)
260
+ }
261
+ return pageOptions
262
+ },
263
+ {
264
+ key: 'getPageOptions',
265
+ debug: instance.options.debug,
266
+ }
267
+ ),
268
+
269
+ getCanPreviousPage: () => instance.getState().pagination.pageIndex > 0,
270
+
271
+ getCanNextPage: () => {
272
+ const { pageIndex, pageCount, pageSize } = instance.getState().pagination
273
+
274
+ if (pageCount === -1) {
275
+ return true
276
+ }
277
+
278
+ if (pageCount === 0) {
279
+ return false
280
+ }
281
+
282
+ return (
283
+ pageIndex <
284
+ Math.ceil(instance.getPrePaginationRows().length / pageSize) - 1
285
+ )
286
+ },
287
+
288
+ previousPage: () => {
289
+ return instance.setPageIndex(old => old - 1)
290
+ },
291
+
292
+ nextPage: () => {
293
+ return instance.setPageIndex(old => {
294
+ return old + 1
295
+ })
296
+ },
297
+
298
+ getPaginationRowModel: memo(
299
+ () => [
300
+ instance.getState().pagination,
301
+ instance.getExpandedRowModel(),
302
+ instance.options.paginateRowsFn,
303
+ ],
304
+ (_pagination, rowModel, paginateRowsFn) => {
305
+ if (!paginateRowsFn || !rowModel.rows.length) {
306
+ return rowModel
307
+ }
308
+
309
+ if (process.env.NODE_ENV !== 'production' && instance.options.debug)
310
+ console.info('Paginating...')
311
+
312
+ return paginateRowsFn(instance, rowModel)
313
+ },
314
+ {
315
+ key: 'getPaginationRowModel',
316
+ debug: instance.options.debug,
317
+ }
318
+ ),
319
+
320
+ getPrePaginationRows: () => instance.getExpandedRowModel().rows,
321
+ getPrePaginationFlatRows: () => instance.getExpandedRowModel().flatRows,
322
+ getPrePaginationRowsById: () => instance.getExpandedRowModel().rowsById,
323
+ getPaginationRows: () => instance.getPaginationRowModel().rows,
324
+ getPaginationFlatRows: () => instance.getPaginationRowModel().flatRows,
325
+ getPaginationRowsById: () => instance.getPaginationRowModel().rowsById,
326
+ }
327
+ }
@@ -1,7 +1,7 @@
1
1
  import { OnChangeFn, Updater, ReactTable, Column } from '../types'
2
2
  import { functionalUpdate, makeStateUpdater } from '../utils'
3
3
 
4
- type ColumnPinningPosition = 'left' | 'right' | 'both'
4
+ type ColumnPinningPosition = false | 'left' | 'right'
5
5
 
6
6
  export type ColumnPinningState = {
7
7
  left?: string[]
@@ -29,7 +29,7 @@ export type ColumnPinningColumnDef = {
29
29
  export type ColumnPinningColumn = {
30
30
  getCanPin: () => boolean
31
31
  getPinnedIndex: () => number
32
- getIsPinned: () => false | ColumnPinningPosition
32
+ getIsPinned: () => ColumnPinningPosition
33
33
  pin: (position: ColumnPinningPosition) => void
34
34
  }
35
35
 
@@ -44,7 +44,7 @@ export type ColumnPinningInstance<
44
44
  resetColumnPinning: () => void
45
45
  pinColumn: (columnId: string, position: ColumnPinningPosition) => void
46
46
  getColumnCanPin: (columnId: string) => boolean
47
- getColumnIsPinned: (columnId: string) => false | ColumnPinningPosition
47
+ getColumnIsPinned: (columnId: string) => ColumnPinningPosition
48
48
  getColumnPinnedIndex: (columnId: string) => number
49
49
  }
50
50
 
@@ -186,25 +186,12 @@ export function getInstance<
186
186
  const isLeft = leafColumnIds.some(d => left?.includes(d))
187
187
  const isRight = leafColumnIds.some(d => right?.includes(d))
188
188
 
189
- if (isLeft && isRight) {
190
- return 'both'
191
- }
192
-
193
189
  return isLeft ? 'left' : isRight ? 'right' : false
194
190
  },
195
191
 
196
192
  getColumnPinnedIndex: columnId => {
197
193
  const position = instance.getColumnIsPinned(columnId)
198
194
 
199
- if (position === 'both') {
200
- if (process.env.NODE_ENV !== 'production') {
201
- console.warn(
202
- `Column ${columnId} has leaf columns that are pinned on both sides`
203
- )
204
- }
205
- throw new Error()
206
- }
207
-
208
195
  return position
209
196
  ? instance.getState().columnPinning?.[position]?.indexOf(columnId) ?? -1
210
197
  : 0