Skip to content

Commit

Permalink
fix corner case
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Oct 4, 2024
1 parent b5e9179 commit 7bf3b6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/react/src/DataTable/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,15 @@ function getDefaultOffsetStartIndex(pageIndex: number, pageCount: number, trunca
// When the current page is closer to the end of the list than the beginning
if (pageIndex > pageCount - 1 - pageIndex) {
if (pageCount - 1 - pageIndex >= truncatedPageCount) {
return pageIndex - 3
return Math.max(pageIndex - 3, 1)
}
return pageCount - 1 - truncatedPageCount
return Math.max(pageCount - 1 - truncatedPageCount, 1)
}

// When the current page is closer to the beginning of the list than the end
if (pageIndex < pageCount - 1 - pageIndex) {
if (pageIndex >= truncatedPageCount) {
return pageIndex - 3
return Math.max(pageIndex - 3, 1)
}
return 1
}
Expand All @@ -356,7 +356,7 @@ function getDefaultOffsetStartIndex(pageIndex: number, pageCount: number, trunca
if (pageIndex < truncatedPageCount) {
return pageIndex
}
return pageIndex - 3
return Math.max(pageIndex - 3, 1)
}

type RangeProps = {
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/DataTable/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ describe('Table.Pagination', () => {
pageIndex: 0,
})
})

it('when rendering 3 pages and the second page is selected we should render a page number not ...', async () => {
const onChange = jest.fn()
render(<Pagination aria-label="Test label" onChange={onChange} defaultPageIndex={1} pageSize={2} totalCount={6} />)
expect(getPageRange()).toEqual('3 through 4 of 6')
expect(getCurrentPage()).toEqual(getPage(1))
expect(getInvalidPages()).toHaveLength(0)
})
})

function getPages() {
Expand Down

0 comments on commit 7bf3b6d

Please sign in to comment.