Skip to content

Commit

Permalink
fix(Pagination): fix total page calculation (#1237)
Browse files Browse the repository at this point in the history
Co-authored-by: Evgenij Shangin <[email protected]>
  • Loading branch information
maeshchenko and jhoncool authored Jan 10, 2024
1 parent 5bd5edd commit 8f1af66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './components';
import {usePagination} from './hooks/usePagination';
import type {PaginationProps} from './types';
import {getResultPage, getResultTotal} from './utils';

import './Pagination.scss';

Expand All @@ -35,7 +36,19 @@ export const Pagination = ({
const size = mobile ? 'l' : 'm';
const compact = mobile ? true : propCompact;

const {items, numberOfPages} = usePagination({page, pageSize, total, mobile});
const resultTotal = getResultTotal(total);
const resultPage = getResultPage({
page,
total: resultTotal,
pageSize,
});

const {items, numberOfPages} = usePagination({
page: resultPage,
pageSize,
total: resultTotal,
mobile,
});

const pagination = items
.map((item) => {
Expand Down Expand Up @@ -79,7 +92,7 @@ export const Pagination = ({
key={item.action}
size={size}
item={item}
page={page}
page={resultPage}
pageSize={pageSize}
onUpdate={onUpdate}
compact={compact}
Expand Down Expand Up @@ -107,11 +120,11 @@ export const Pagination = ({
{pageSizeOptions && (
<PaginationPageSizer
onUpdate={onUpdate}
page={page}
page={resultPage}
pageSize={pageSize}
pageSizeOptions={pageSizeOptions}
size={size}
total={total}
total={resultTotal}
className={b('page-sizer')}
/>
)}
Expand Down
18 changes: 18 additions & 0 deletions src/components/Pagination/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ function getDesktopNumerationList(page: number, numberOfPages: number) {
export function getNumberOfPages(pageSize: number, total = 0) {
return Math.floor((total - 1) / pageSize) + 1;
}

export function getResultTotal(total: number | undefined) {
return total === undefined || total > 0 ? total : 1;
}

export function getResultPage({
page,
total,
pageSize,
}: {
page: number;
total: number | undefined;
pageSize: number;
}) {
return page > 0 && (total === undefined || page <= getNumberOfPages(pageSize, total))
? page
: 1;
}

0 comments on commit 8f1af66

Please sign in to comment.