diff --git a/.changeset/pagination-empty-next.md b/.changeset/pagination-empty-next.md
new file mode 100644
index 00000000000..1b1e37f5dfb
--- /dev/null
+++ b/.changeset/pagination-empty-next.md
@@ -0,0 +1,5 @@
+---
+'@primer/react': patch
+---
+
+Pagination: Disable Next for empty results when page numbers are hidden.
diff --git a/packages/react/src/Pagination/Pagination.test.tsx b/packages/react/src/Pagination/Pagination.test.tsx
index 0f53b57f37b..7cfcccfc3b0 100644
--- a/packages/react/src/Pagination/Pagination.test.tsx
+++ b/packages/react/src/Pagination/Pagination.test.tsx
@@ -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 => , classes.PaginationContainer)
+ it.each([true, false])('disables Next when there are no pages with showPages=%s', async showPages => {
+ HTMLRender()
+
+ 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(
, 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')
@@ -24,8 +24,8 @@ describe('Pagination model', () => {
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')
diff --git a/packages/react/src/Pagination/model.tsx b/packages/react/src/Pagination/model.tsx
index 368e87f6ea7..b009fdb8327 100644
--- a/packages/react/src/Pagination/model.tsx
+++ b/packages/react/src/Pagination/model.tsx
@@ -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