Skip to content

Commit

Permalink
fix: handled 0 total itams in pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
riddhybansal committed Jun 27, 2024
1 parent b28d8be commit b5b28d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/react/src/components/Pagination/Pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,18 @@ describe('Pagination', () => {
document.querySelector('.cds--select__page-number')
).not.toBeInTheDocument();
});

it('should handle zero total items', () => {
render(
<Pagination
totalItems={0}
pageSizes={[10, 20]}
pageSize={10}
page={1}
/>
);

expect(screen.getByText('0–0 of 0 items')).toBeInTheDocument();
});
});
});
6 changes: 4 additions & 2 deletions packages/react/src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ const Pagination = React.forwardRef(function Pagination(
});
const totalPages = totalItems
? Math.max(Math.ceil(totalItems / pageSize), 1)
: NaN;
: 1;
const backButtonDisabled = disabled || page === 1;
const backButtonClasses = cx({
[`${prefix}--pagination__button`]: true,
Expand Down Expand Up @@ -366,7 +366,9 @@ const Pagination = React.forwardRef(function Pagination(
<span
className={`${prefix}--pagination__text ${prefix}--pagination__items-count`}>
{pagesUnknown || !totalItems
? itemText(pageSize * (page - 1) + 1, page * pageSize)
? totalItems === 0
? itemRangeText(0, 0, 0)
: itemText(pageSize * (page - 1) + 1, page * pageSize)
: itemRangeText(
Math.min(pageSize * (page - 1) + 1, totalItems),
Math.min(page * pageSize, totalItems),
Expand Down

0 comments on commit b5b28d1

Please sign in to comment.