Skip to content

Commit b926c08

Browse files
AAugustinefrancinelucca
authored andcommitted
Update data table pagination pageEnd to handle zero based index (#5913)
Co-authored-by: Marie Lucca <[email protected]>
1 parent 3d06689 commit b926c08

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

.changeset/weak-spiders-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
Update data table pagination pageEnd to handle zero based index

packages/react/src/DataTable/Pagination.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ type RangeProps = {
367367

368368
function Range({pageStart, pageEnd, totalCount}: RangeProps) {
369369
const start = pageStart + 1
370-
const end = pageEnd === totalCount - 1 ? totalCount : pageEnd
370+
const end = pageEnd
371371
return (
372372
<>
373373
<Message value={`Showing ${start} through ${end} of ${totalCount}`} />
@@ -524,7 +524,7 @@ function usePagination(config: PaginationConfig): PaginationResult {
524524
onChange?.({pageIndex: defaultPageIndex})
525525
}
526526
const pageStart = pageIndex * pageSize
527-
const pageEnd = Math.min(pageIndex * pageSize + pageSize, totalCount - 1)
527+
const pageEnd = Math.min((pageIndex + 1) * pageSize, totalCount)
528528
const hasNextPage = pageIndex + 1 < pageCount
529529
const hasPreviousPage = pageIndex > 0
530530

packages/react/src/DataTable/__tests__/Pagination.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ describe('Table.Pagination', () => {
116116
expect(getPageRange()).toEqual('1 through 25 of 50')
117117
})
118118

119+
it('should display two pages with correct range when totalCount is one plus pageSize', () => {
120+
render(<Pagination aria-label="Test label" pageSize={25} totalCount={26} />)
121+
122+
expect(getPages()).toHaveLength(2)
123+
expect(getCurrentPage()).toEqual(getPage(0))
124+
expect(getPageRange()).toEqual('1 through 25 of 26')
125+
})
126+
119127
it('should call `onChange` when clicking on pages', async () => {
120128
const user = userEvent.setup()
121129
const onChange = jest.fn()

0 commit comments

Comments
 (0)