@tanstack/react-table 8.0.0-alpha.3 → 8.0.0-alpha.6

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 (45) hide show
  1. package/build/cjs/core.js +1 -1
  2. package/build/cjs/core.js.map +1 -1
  3. package/build/cjs/createTable.js.map +1 -1
  4. package/build/cjs/features/Expanding.js +5 -9
  5. package/build/cjs/features/Expanding.js.map +1 -1
  6. package/build/cjs/features/Filters.js +3 -3
  7. package/build/cjs/features/Filters.js.map +1 -1
  8. package/build/cjs/features/Grouping.js +2 -2
  9. package/build/cjs/features/Grouping.js.map +1 -1
  10. package/build/cjs/features/Pagination.js +24 -20
  11. package/build/cjs/features/Pagination.js.map +1 -1
  12. package/build/cjs/features/Pinning.js +2 -2
  13. package/build/cjs/features/Pinning.js.map +1 -1
  14. package/build/cjs/features/Sorting.js +2 -2
  15. package/build/cjs/features/Sorting.js.map +1 -1
  16. package/build/cjs/index.js +2 -0
  17. package/build/cjs/index.js.map +1 -1
  18. package/build/cjs/utils/paginateRowsFn.js +44 -0
  19. package/build/cjs/utils/paginateRowsFn.js.map +1 -0
  20. package/build/cjs/utils.js +9 -14
  21. package/build/cjs/utils.js.map +1 -1
  22. package/build/esm/index.js +75 -54
  23. package/build/esm/index.js.map +1 -1
  24. package/build/stats-html.html +1 -1
  25. package/build/stats-react.json +266 -234
  26. package/build/types/core.d.ts +0 -2
  27. package/build/types/createTable.d.ts +1 -2
  28. package/build/types/features/Pagination.d.ts +5 -4
  29. package/build/types/index.d.ts +2 -1
  30. package/build/types/utils.d.ts +0 -4
  31. package/build/umd/index.development.js +75 -53
  32. package/build/umd/index.development.js.map +1 -1
  33. package/build/umd/index.production.js +1 -1
  34. package/build/umd/index.production.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/core.tsx +1 -92
  37. package/src/createTable.tsx +1 -1
  38. package/src/features/Expanding.ts +5 -3
  39. package/src/features/Filters.ts +4 -4
  40. package/src/features/Grouping.ts +1 -1
  41. package/src/features/Pagination.ts +44 -33
  42. package/src/features/Pinning.ts +1 -3
  43. package/src/features/Sorting.ts +1 -1
  44. package/src/index.tsx +2 -1
  45. package/src/utils.tsx +7 -69
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-table",
3
3
  "author": "Tanner Linsley",
4
- "version": "8.0.0-alpha.3",
4
+ "version": "8.0.0-alpha.6",
5
5
  "description": "Hooks for building lightweight, fast and extendable datagrids for React",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/tanstack/react-table#readme",
package/src/core.tsx CHANGED
@@ -291,97 +291,6 @@ export type CoreColumnDef<
291
291
  header: Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
292
292
  column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
293
293
  }>
294
- } & // | Renderable<{ // | string // header?: // accessorKey?: never // id: string // accessorFn: AccessorFn<TData> // | {
295
- // instance: ReactTable<
296
- // TData,
297
- // TValue,
298
- // TFilterFns,
299
- // TSortingFns,
300
- // TAggregationFns
301
- // >
302
- // header: Header<
303
- // TData,
304
- // TValue,
305
- // TFilterFns,
306
- // TSortingFns,
307
- // TAggregationFns
308
- // >
309
- // column: Column<
310
- // TData,
311
- // TValue,
312
- // TFilterFns,
313
- // TSortingFns,
314
- // TAggregationFns
315
- // >
316
- // }>
317
- // }
318
- // | {
319
- // accessorKey: string & keyof TData
320
- // id?: string
321
- // accessorFn?: never
322
- // header?:
323
- // | string
324
- // | Renderable<{
325
- // instance: ReactTable<
326
- // TData,
327
- // TValue,
328
- // TFilterFns,
329
- // TSortingFns,
330
- // TAggregationFns
331
- // >
332
- // header: Header<
333
- // TData,
334
- // TValue,
335
- // TFilterFns,
336
- // TSortingFns,
337
- // TAggregationFns
338
- // >
339
- // column: Column<
340
- // TData,
341
- // TValue,
342
- // TFilterFns,
343
- // TSortingFns,
344
- // TAggregationFns
345
- // >
346
- // }>
347
- // }
348
- // | {
349
- // id: string
350
- // accessorKey?: never
351
- // accessorFn?: never
352
- // header?:
353
- // | string
354
- // | Renderable<{
355
- // instance: ReactTable<
356
- // TData,
357
- // TValue,
358
- // TFilterFns,
359
- // TSortingFns,
360
- // TAggregationFns
361
- // >
362
- // header: Header<
363
- // TData,
364
- // TValue,
365
- // TFilterFns,
366
- // TSortingFns,
367
- // TAggregationFns
368
- // >
369
- // column: Column<
370
- // TData,
371
- // TValue,
372
- // TFilterFns,
373
- // TSortingFns,
374
- // TAggregationFns
375
- // >
376
- // }>
377
- // }
378
- // | {
379
- // header: string
380
- // id?: string
381
- // accessorKey?: never
382
- // accessorFn?: never
383
- // }
384
- {
385
294
  __generated: true
386
295
  width?: number
387
296
  minWidth?: number
@@ -1053,7 +962,7 @@ export function createTableInstance<
1053
962
  // expanded rows, which then work their way up
1054
963
 
1055
964
  getRowModel: () => {
1056
- return instance.getExpandedRowModel()
965
+ return instance.getPaginationRowModel()
1057
966
  },
1058
967
 
1059
968
  getRows: () => {
@@ -11,7 +11,7 @@ import {
11
11
  } from './types'
12
12
  import { Overwrite } from './utils'
13
13
 
14
- type TableHelper<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> = {
14
+ export type TableHelper<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> = {
15
15
  RowType<TTData>(): TableHelper<
16
16
  TTData,
17
17
  TValue,
@@ -192,7 +192,7 @@ export function getInstance<
192
192
  toggleRowExpanded: (rowId, expanded) => {
193
193
  if (!rowId) return
194
194
 
195
- instance.setExpanded((old = {}) => {
195
+ instance.setExpanded(old => {
196
196
  const exists = old === true ? true : !!old?.[rowId]
197
197
 
198
198
  let oldExpanded: ExpandedStateList = {}
@@ -230,7 +230,7 @@ export function getInstance<
230
230
  }
231
231
  },
232
232
  resetExpanded: () => {
233
- instance.setExpanded(instance.options?.initialState?.expanded ?? {})
233
+ instance.setExpanded(instance.initialState?.expanded ?? {})
234
234
  },
235
235
  getIsRowExpanded: rowId => {
236
236
  const row = instance.getRow(rowId)
@@ -362,7 +362,9 @@ export function getInstance<
362
362
  {
363
363
  key: 'getExpandedRowModel',
364
364
  debug: instance.options.debug,
365
- onChange: () => instance._notifyPageIndexReset(),
365
+ onChange: () => {
366
+ instance._notifyPageIndexReset()
367
+ },
366
368
  }
367
369
  ),
368
370
 
@@ -628,9 +628,7 @@ export function getInstance<
628
628
  },
629
629
 
630
630
  resetColumnFilters: () => {
631
- instance.setColumnFilters(
632
- instance.options?.initialState?.columnFilters ?? []
633
- )
631
+ instance.setColumnFilters(instance.initialState?.columnFilters ?? [])
634
632
  },
635
633
 
636
634
  getColumnFilteredRowModel: memo(
@@ -730,7 +728,9 @@ export function getInstance<
730
728
  {
731
729
  key: 'getGlobalFilteredRowModel',
732
730
  debug: instance.options.debug,
733
- onChange: () => instance._notifySortingReset(),
731
+ onChange: () => {
732
+ instance._notifySortingReset()
733
+ },
734
734
  }
735
735
  ),
736
736
 
@@ -331,7 +331,7 @@ export function getInstance<
331
331
  instance.getState().grouping?.indexOf(columnId),
332
332
 
333
333
  resetGrouping: () => {
334
- instance.setGrouping(instance.options?.initialState?.grouping ?? [])
334
+ instance.setGrouping(instance.initialState?.grouping ?? [])
335
335
  },
336
336
 
337
337
  getToggleGroupingProps: (columnId, userProps) => {
@@ -9,10 +9,12 @@ import {
9
9
  } from '../types'
10
10
  import { functionalUpdate, makeStateUpdater, memo } from '../utils'
11
11
 
12
+ export type PageCount = undefined | null | number
13
+
12
14
  export type PaginationState = {
13
15
  pageIndex: number
14
16
  pageSize: number
15
- pageCount: number
17
+ pageCount?: PageCount
16
18
  }
17
19
 
18
20
  export type PaginationTableState = {
@@ -59,12 +61,12 @@ export type PaginationInstance<
59
61
  resetPageIndex: () => void
60
62
  setPageSize: (updater: Updater<number>) => void
61
63
  resetPageSize: () => void
62
- setPageCount: (updater: Updater<number>) => void
64
+ setPageCount: (updater: Updater<PageCount>) => void
63
65
  getPageOptions: () => number[]
64
66
  getCanPreviousPage: () => boolean
65
67
  getCanNextPage: () => boolean
66
- gotoPreviousPage: () => void
67
- gotoNextPage: () => void
68
+ previousPage: () => void
69
+ nextPage: () => void
68
70
  getPaginationRowModel: () => RowModel<
69
71
  TData,
70
72
  TValue,
@@ -117,7 +119,6 @@ export function getInitialState(): PaginationTableState {
117
119
  pagination: {
118
120
  pageIndex: 0,
119
121
  pageSize: 10,
120
- pageCount: -1,
121
122
  },
122
123
  }
123
124
  }
@@ -167,21 +168,18 @@ export function getInstance<
167
168
  },
168
169
  setPagination: updater => {
169
170
  const safeUpdater: Updater<PaginationState> = old => {
170
- const newState = functionalUpdate(old, updater)
171
-
172
- if (!instance.options.paginateRowsFn) {
173
- return {
174
- ...old,
175
- pageCount: instance.getPreExpandedRows()?.length
176
- ? Math.ceil(
177
- instance.getPreExpandedRows().length /
178
- instance.getState().pagination.pageSize
179
- )
180
- : 0,
181
- }
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
182
180
  }
183
181
 
184
- return old
182
+ return newState
185
183
  }
186
184
 
187
185
  return instance.options.onPaginationChange?.(
@@ -200,13 +198,18 @@ export function getInstance<
200
198
  },
201
199
  setPageIndex: updater => {
202
200
  instance.setPagination(old => {
203
- const newPageIndex = functionalUpdate(updater, old.pageIndex)
201
+ let pageIndex = functionalUpdate(updater, old.pageIndex)
202
+
204
203
  const maxPageIndex =
205
- old.pageCount > 0 ? old.pageCount - 1 : Number.MAX_SAFE_INTEGER
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)
206
209
 
207
210
  return {
208
211
  ...old,
209
- pageIndex: Math.min(Math.max(0, newPageIndex), maxPageIndex),
212
+ pageIndex,
210
213
  }
211
214
  })
212
215
  },
@@ -214,9 +217,7 @@ export function getInstance<
214
217
  instance.setPageIndex(0)
215
218
  },
216
219
  resetPageSize: () => {
217
- instance.setPageSize(
218
- instance.options.initialState?.pagination?.pageSize ?? 10
219
- )
220
+ instance.setPageSize(instance.initialState?.pagination?.pageSize ?? 10)
220
221
  },
221
222
  setPageSize: updater => {
222
223
  instance.setPagination(old => {
@@ -232,10 +233,18 @@ export function getInstance<
232
233
  })
233
234
  },
234
235
  setPageCount: updater =>
235
- instance.setPagination(old => ({
236
- ...old,
237
- pageCount: Math.max(-1, functionalUpdate(updater, old.pageCount)),
238
- })),
236
+ instance.setPagination(old => {
237
+ let newPageCount = functionalUpdate(updater, old.pageCount)
238
+
239
+ if (typeof newPageCount === 'number') {
240
+ newPageCount = Math.max(-1, newPageCount)
241
+ }
242
+
243
+ return {
244
+ ...old,
245
+ pageCount: newPageCount,
246
+ }
247
+ }),
239
248
 
240
249
  getPageOptions: memo(
241
250
  () => [
@@ -244,7 +253,7 @@ export function getInstance<
244
253
  ],
245
254
  (pageSize, pageCount) => {
246
255
  let pageOptions: number[] = []
247
- if (pageCount > 0) {
256
+ if (pageCount && pageCount > 0) {
248
257
  pageOptions = [...new Array(pageCount)].fill(null).map((_, i) => i)
249
258
  }
250
259
  return pageOptions
@@ -274,12 +283,14 @@ export function getInstance<
274
283
  )
275
284
  },
276
285
 
277
- gotoPreviousPage: () => {
278
- return instance.setPageIndex?.(old => old! - 1)
286
+ previousPage: () => {
287
+ return instance.setPageIndex(old => old - 1)
279
288
  },
280
289
 
281
- gotoNextPage: () => {
282
- return instance.setPageIndex?.(old => old! + 1)
290
+ nextPage: () => {
291
+ return instance.setPageIndex(old => {
292
+ return old + 1
293
+ })
283
294
  },
284
295
 
285
296
  getPaginationRowModel: memo(
@@ -114,9 +114,7 @@ export function getInstance<
114
114
  ),
115
115
 
116
116
  resetColumnPinning: () =>
117
- instance.setColumnPinning(
118
- instance.options.initialState?.columnPinning ?? {}
119
- ),
117
+ instance.setColumnPinning(instance.initialState?.columnPinning ?? {}),
120
118
 
121
119
  pinColumn: (columnId, position) => {
122
120
  const column = instance.getColumn(columnId)
@@ -484,7 +484,7 @@ export function getInstance<
484
484
  instance.getState().sorting?.findIndex(d => d.id === columnId) ?? -1,
485
485
 
486
486
  resetSorting: () => {
487
- instance.setSorting(instance.options?.initialState?.sorting ?? [])
487
+ instance.setSorting(instance.initialState?.sorting ?? [])
488
488
  },
489
489
 
490
490
  getToggleSortingProps: (columnId, userProps) => {
package/src/index.tsx CHANGED
@@ -4,4 +4,5 @@ export * from './utils/globalFilterRowsFn'
4
4
  export * from './utils/sortRowsFn'
5
5
  export * from './utils/groupRowsFn'
6
6
  export * from './utils/expandRowsFn'
7
- export { createTable } from './createTable'
7
+ export * from './utils/paginateRowsFn'
8
+ export * from './createTable'
package/src/utils.tsx CHANGED
@@ -43,29 +43,6 @@ export function makeStateUpdater(key: keyof TableState, instance: unknown) {
43
43
  }
44
44
  }
45
45
 
46
- // SSR has issues with useLayoutEffect still, so use useEffect during SSR
47
- export const safeUseLayoutEffect =
48
- typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect
49
-
50
- export function useMountedLayoutEffect(fn: any, deps: any[]) {
51
- const mountedRef = React.useRef(false)
52
-
53
- safeUseLayoutEffect(() => {
54
- if (mountedRef.current) {
55
- fn()
56
- }
57
- mountedRef.current = true
58
- // eslint-disable-next-line
59
- }, deps)
60
- }
61
-
62
- export function useGetLatest<T>(obj: T): () => T {
63
- const ref = React.useRef<T>()
64
- ref.current = obj
65
-
66
- return React.useCallback(() => ref.current!, [])
67
- }
68
-
69
46
  type AnyFunction = (...args: any) => any
70
47
 
71
48
  export function isFunction<T extends AnyFunction>(d: any): d is T {
@@ -124,27 +101,19 @@ export function memo<TDeps extends readonly any[], TResult>(
124
101
 
125
102
  return () => {
126
103
  const newDeps = getDeps()
127
- const newSerializedDeps = newDeps
128
- const oldSerializedDeps = deps
129
104
 
130
105
  const depsChanged =
131
- newSerializedDeps.length !== oldSerializedDeps.length ||
132
- newSerializedDeps.some(
133
- (dep: any, index: number) => oldSerializedDeps[index] !== dep
134
- )
106
+ newDeps.length !== deps.length ||
107
+ newDeps.some((dep: any, index: number) => deps[index] !== dep)
135
108
 
136
109
  if (depsChanged) {
137
110
  if (opts?.debug) {
138
111
  console.info(opts?.key, {
139
- length: `${oldSerializedDeps.length} -> ${newSerializedDeps.length}`,
140
- ...newSerializedDeps
112
+ length: `${deps.length} -> ${newDeps.length}`,
113
+ ...newDeps
141
114
  .map((_, index) => {
142
- if (oldSerializedDeps[index] !== newSerializedDeps[index]) {
143
- return [
144
- index,
145
- oldSerializedDeps[index],
146
- newSerializedDeps[index],
147
- ]
115
+ if (deps[index] !== newDeps[index]) {
116
+ return [index, deps[index], newDeps[index]]
148
117
  }
149
118
 
150
119
  return false
@@ -163,7 +132,7 @@ export function memo<TDeps extends readonly any[], TResult>(
163
132
 
164
133
  let oldResult = result
165
134
  result = fn(...newDeps)
166
- deps = newSerializedDeps
135
+ deps = newDeps
167
136
  opts?.onChange?.(result, oldResult)
168
137
 
169
138
  oldResult = undefined
@@ -173,37 +142,6 @@ export function memo<TDeps extends readonly any[], TResult>(
173
142
  }
174
143
  }
175
144
 
176
- // Copied from: https://github.com/jonschlinkert/is-plain-object
177
- export function isPlainObject(o: any): o is Object {
178
- if (!hasObjectPrototype(o)) {
179
- return false
180
- }
181
-
182
- // If has modified constructor
183
- const ctor = o.constructor
184
- if (typeof ctor === 'undefined') {
185
- return true
186
- }
187
-
188
- // If has modified prototype
189
- const prot = ctor.prototype
190
- if (!hasObjectPrototype(prot)) {
191
- return false
192
- }
193
-
194
- // If constructor does not have an Object-specific method
195
- if (!prot.hasOwnProperty('isPrototypeOf')) {
196
- return false
197
- }
198
-
199
- // Most likely a plain Object
200
- return true
201
- }
202
-
203
- function hasObjectPrototype(o: any): boolean {
204
- return Object.prototype.toString.call(o) === '[object Object]'
205
- }
206
-
207
145
  export type Render = typeof flexRender
208
146
 
209
147
  export function flexRender<TProps extends {}>(