-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Feature: Allow Manual Override of Pagination canNextPage/canPreviousPage #6385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: beta
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,7 +251,14 @@ export function table_getCanPreviousPage< | |
| TFeatures extends TableFeatures, | ||
| TData extends RowData, | ||
| >(table: Table_Internal<TFeatures, TData>) { | ||
| return (table.atoms.pagination?.get()?.pageIndex ?? 0) > 0 | ||
| const pagination = table.atoms.pagination?.get() | ||
| const { canPreviousPage, pageIndex } = pagination ?? {} | ||
|
Comment on lines
+254
to
+255
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Destructuring before any return condition ensures reactivity for lazy eval frameworks are maintained. This is important in the case where |
||
|
|
||
| if (canPreviousPage !== undefined) { | ||
| return canPreviousPage | ||
| } | ||
|
|
||
| return (pageIndex ?? defaultPageIndex) > 0 | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -269,10 +276,16 @@ export function table_getCanNextPage< | |
| TFeatures extends TableFeatures, | ||
| TData extends RowData, | ||
| >(table: Table_Internal<TFeatures, TData>) { | ||
| const pageIndex = table.atoms.pagination?.get()?.pageIndex ?? defaultPageIndex | ||
|
|
||
| const pagination = table.atoms.pagination?.get() | ||
| const pageIndex = pagination?.pageIndex ?? defaultPageIndex | ||
| // Read `pageCount` before any early return so the reactivity system always | ||
| // subscribes to its dependencies (pageSize atom + pre-paginated row model). | ||
| const pageCount = table_getPageCount(table) | ||
|
|
||
| if (pagination?.canNextPage !== undefined) { | ||
| return pagination.canNextPage | ||
| } | ||
|
|
||
| if (pageCount === -1) { | ||
| return true | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Net new unit tests. |
||
| import { | ||
| constructTable, | ||
| coreFeatures, | ||
| createPaginatedRowModel, | ||
| rowPaginationFeature, | ||
| tableFeatures, | ||
| } from '../../../../src' | ||
| import { storeReactivityBindings } from '../../../../src/store-reactivity-bindings' | ||
| import { generateTestColumnDefs } from '../../../fixtures/data/generateTestColumnDefs' | ||
| import { generateTestData } from '../../../fixtures/data/generateTestData' | ||
| import type { PaginationState } from '../../../../src' | ||
| import type { Person } from '../../../fixtures/data/types' | ||
|
|
||
| const features = tableFeatures({ | ||
| ...coreFeatures, | ||
| rowPaginationFeature, | ||
| paginatedRowModel: createPaginatedRowModel(), | ||
| }) | ||
|
|
||
| function createPaginationTable(options?: { | ||
| rowLength?: number | ||
| pagination?: Partial<PaginationState> | ||
| pageCount?: number | ||
| rowCount?: number | ||
| manualPagination?: boolean | ||
| }) { | ||
| const data = generateTestData(options?.rowLength ?? 30) | ||
| const columns = generateTestColumnDefs<typeof features>(data) | ||
|
|
||
| return constructTable<typeof features, Person>({ | ||
| data, | ||
| columns, | ||
| getSubRows: (row) => row.subRows, | ||
| manualPagination: options?.manualPagination, | ||
| pageCount: options?.pageCount, | ||
| rowCount: options?.rowCount, | ||
| initialState: { | ||
| pagination: { | ||
| pageIndex: 0, | ||
| pageSize: 10, | ||
| ...options?.pagination, | ||
| }, | ||
| }, | ||
| features: { | ||
| ...features, | ||
| coreReactivityFeature: storeReactivityBindings(), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| describe('table_getCanPreviousPage', () => { | ||
| it('returns false on the first page (pageIndex 0)', () => { | ||
| const table = createPaginationTable({ pagination: { pageIndex: 0 } }) | ||
| expect(table.getCanPreviousPage()).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true when past the first page', () => { | ||
| const table = createPaginationTable({ pagination: { pageIndex: 1 } }) | ||
| expect(table.getCanPreviousPage()).toBe(true) | ||
| }) | ||
|
|
||
| it('honors an explicit canPreviousPage override on the first page', () => { | ||
| const table = createPaginationTable({ | ||
| pagination: { pageIndex: 0, canPreviousPage: true }, | ||
| }) | ||
| expect(table.getCanPreviousPage()).toBe(true) | ||
| }) | ||
|
|
||
| it('honors an explicit canPreviousPage=false override past the first page', () => { | ||
| const table = createPaginationTable({ | ||
| pagination: { pageIndex: 1, canPreviousPage: false }, | ||
| }) | ||
| expect(table.getCanPreviousPage()).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('table_getCanNextPage', () => { | ||
| it('returns true when there are more pages', () => { | ||
| // 30 rows / pageSize 10 => 3 pages, currently on page 0 | ||
| const table = createPaginationTable({ pagination: { pageIndex: 0 } }) | ||
| expect(table.getCanNextPage()).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false on the last page', () => { | ||
| const table = createPaginationTable({ pagination: { pageIndex: 2 } }) | ||
| expect(table.getCanNextPage()).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true for an unknown page count (pageCount -1)', () => { | ||
| const table = createPaginationTable({ | ||
| manualPagination: true, | ||
| pageCount: -1, | ||
| }) | ||
| expect(table.getCanNextPage()).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false for an empty page count (pageCount 0)', () => { | ||
| const table = createPaginationTable({ | ||
| manualPagination: true, | ||
| pageCount: 0, | ||
| }) | ||
| expect(table.getCanNextPage()).toBe(false) | ||
| }) | ||
|
|
||
| it('honors canNextPage=false override even when pageCount is unknown (-1)', () => { | ||
| const table = createPaginationTable({ | ||
| manualPagination: true, | ||
| pageCount: -1, | ||
| pagination: { canNextPage: false }, | ||
| }) | ||
| expect(table.getCanNextPage()).toBe(false) | ||
| }) | ||
|
|
||
| it('honors canNextPage=true override even when pageCount is 0', () => { | ||
| const table = createPaginationTable({ | ||
| manualPagination: true, | ||
| pageCount: 0, | ||
| pagination: { canNextPage: true }, | ||
| }) | ||
| expect(table.getCanNextPage()).toBe(true) | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously this used
?? 0. However this is now updated to use the samedefaultPageIndexconst astable_getCanNextPagefor consistency