Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pagination-empty-next.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Pagination: Disable Next for empty results when page numbers are hidden.
9 changes: 9 additions & 0 deletions packages/react/src/Pagination/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import Pagination from '../Pagination'
import {ReactRouterLikeLink} from './mocks/ReactRouterLink'
import {implementsClassName} from '../utils/testing'
import classes from './Pagination.module.css'
import {page} from 'vitest/browser'

describe('Pagination', () => {
implementsClassName(props => <Pagination pageCount={5} currentPage={1} {...props} />, classes.PaginationContainer)

it.each([true, false])('disables Next when there are no pages with showPages=%s', async showPages => {
HTMLRender(<Pagination pageCount={0} currentPage={1} showPages={showPages} />)

const next = page.getByText('Next', {exact: true})
await expect.element(next).toHaveAttribute('aria-disabled', 'true')
await expect.element(next).not.toHaveAttribute('href')
})

it('should render links instead of anchor tags with the renderPage prop', () => {
const {container} = HTMLRender(
<Pagination
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/Pagination/PaginationModel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ function last(array: Array<any>, count = 1) {
}

describe('Pagination model', () => {
it('correctly handles negative pages', () => {
const model = buildPaginationModel(-10, 1, true, 1, 2)
it.each([true, false])('correctly handles negative pages with showPages=%s', showPages => {
const model = buildPaginationModel(-10, 1, showPages, 1, 2)
expect(first(model).type).toEqual('PREV')
expect(first(model).disabled).toBe(true)
expect(last(model).type).toEqual('NEXT')
expect(last(model).disabled).toBe(true)
expect(model.length).toBe(2)
})

it('correctly handles zero pages', () => {
const model = buildPaginationModel(0, 1, true, 1, 2)
it.each([true, false])('correctly handles zero pages with showPages=%s', showPages => {
const model = buildPaginationModel(0, 1, showPages, 1, 2)
expect(first(model).type).toEqual('PREV')
expect(first(model).disabled).toBe(true)
expect(last(model).type).toEqual('NEXT')
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/Pagination/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export function buildPaginationModel(
) {
const prev: PageType = {type: 'PREV', num: currentPage - 1, disabled: currentPage === 1}
const next: PageType = {type: 'NEXT', num: currentPage + 1, disabled: currentPage === pageCount}
if (!showPages) {
return [prev, next]
}

if (pageCount <= 0) {
return [prev, {...next, disabled: true}]
}

if (!showPages) {
return [prev, next]
}

const pages: PageType[] = []

// number of pages shown on each side of the current page
Expand Down
Loading