Skip to content

Commit

Permalink
fix(Pagination): fix total page calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
maeshchenko committed Jan 9, 2024
1 parent 4f0f099 commit 296fff4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import {
} from './components';
import {usePagination} from './hooks/usePagination';
import type {PaginationProps} from './types';
import {getResultPage, getResultTotal} from './utils';

import './Pagination.scss';

const b = blockNew('pagination');
const DEFAULT_TOTAL = 1;
const DEFAULT_PAGE = 1;

export const Pagination = ({
page,
Expand All @@ -35,7 +38,20 @@ 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, DEFAULT_TOTAL);
const resultPage = getResultPage({
page,
total: resultTotal,
pageSize,
defaultPage: DEFAULT_PAGE,
});

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

const pagination = items
.map((item) => {
Expand Down Expand Up @@ -79,7 +95,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 +123,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
20 changes: 20 additions & 0 deletions src/components/Pagination/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ 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, defaultTotal: number) {
return total === undefined || total > 0 ? total : defaultTotal;
}

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

0 comments on commit 296fff4

Please sign in to comment.