@tanstack/react-table 8.0.0-alpha.3 → 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.
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.4",
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,8 +291,7 @@ 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<
294
+ } & // instance: ReactTable< // | Renderable<{ // | string // header?: // accessorKey?: never // id: string // accessorFn: AccessorFn<TData> // | {
296
295
  // TData,
297
296
  // TValue,
298
297
  // TFilterFns,
@@ -1053,7 +1052,7 @@ export function createTableInstance<
1053
1052
  // expanded rows, which then work their way up
1054
1053
 
1055
1054
  getRowModel: () => {
1056
- return instance.getExpandedRowModel()
1055
+ return instance.getPaginationRowModel()
1057
1056
  },
1058
1057
 
1059
1058
  getRows: () => {
@@ -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
  },
@@ -232,10 +235,18 @@ export function getInstance<
232
235
  })
233
236
  },
234
237
  setPageCount: updater =>
235
- instance.setPagination(old => ({
236
- ...old,
237
- pageCount: Math.max(-1, functionalUpdate(updater, old.pageCount)),
238
- })),
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
+ }),
239
250
 
240
251
  getPageOptions: memo(
241
252
  () => [
@@ -244,7 +255,7 @@ export function getInstance<
244
255
  ],
245
256
  (pageSize, pageCount) => {
246
257
  let pageOptions: number[] = []
247
- if (pageCount > 0) {
258
+ if (pageCount && pageCount > 0) {
248
259
  pageOptions = [...new Array(pageCount)].fill(null).map((_, i) => i)
249
260
  }
250
261
  return pageOptions
@@ -274,12 +285,14 @@ export function getInstance<
274
285
  )
275
286
  },
276
287
 
277
- gotoPreviousPage: () => {
278
- return instance.setPageIndex?.(old => old! - 1)
288
+ previousPage: () => {
289
+ return instance.setPageIndex(old => old - 1)
279
290
  },
280
291
 
281
- gotoNextPage: () => {
282
- return instance.setPageIndex?.(old => old! + 1)
292
+ nextPage: () => {
293
+ return instance.setPageIndex(old => {
294
+ return old + 1
295
+ })
283
296
  },
284
297
 
285
298
  getPaginationRowModel: memo(
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 * from './utils/paginateRowsFn'
7
8
  export { createTable } from './createTable'