2022-09-02 15:30:34 +00:00
|
|
|
import './pagination-controls.css';
|
|
|
|
|
2022-01-04 12:16:09 +00:00
|
|
|
import { generatePagesArray } from './generatePagesArray';
|
|
|
|
import { PageButton } from './PageButton';
|
2022-05-23 07:57:22 +00:00
|
|
|
import { PageInput } from './PageInput';
|
2022-01-04 12:16:09 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
boundaryLinks?: boolean;
|
|
|
|
currentPage: number;
|
|
|
|
directionLinks?: boolean;
|
|
|
|
itemsPerPage: number;
|
|
|
|
onPageChange(page: number): void;
|
|
|
|
totalCount: number;
|
|
|
|
maxSize: number;
|
2022-05-23 07:57:22 +00:00
|
|
|
isInputVisible?: boolean;
|
2022-01-04 12:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function PageSelector({
|
|
|
|
currentPage,
|
|
|
|
totalCount,
|
|
|
|
itemsPerPage,
|
|
|
|
onPageChange,
|
|
|
|
maxSize = 5,
|
|
|
|
directionLinks = true,
|
|
|
|
boundaryLinks = false,
|
2022-05-23 07:57:22 +00:00
|
|
|
isInputVisible = false,
|
2022-01-04 12:16:09 +00:00
|
|
|
}: Props) {
|
|
|
|
const pages = generatePagesArray(
|
|
|
|
currentPage,
|
|
|
|
totalCount,
|
|
|
|
itemsPerPage,
|
|
|
|
maxSize
|
|
|
|
);
|
|
|
|
const last = pages[pages.length - 1];
|
|
|
|
|
|
|
|
if (pages.length <= 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-05-23 07:57:22 +00:00
|
|
|
<>
|
|
|
|
{isInputVisible && (
|
|
|
|
<PageInput
|
|
|
|
onChange={(page) => onPageChange(page)}
|
|
|
|
totalPages={Math.ceil(totalCount / itemsPerPage)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<ul className="pagination">
|
|
|
|
{boundaryLinks ? (
|
|
|
|
<PageButton
|
|
|
|
onPageChange={onPageChange}
|
|
|
|
page={1}
|
|
|
|
disabled={currentPage === 1}
|
|
|
|
>
|
|
|
|
«
|
|
|
|
</PageButton>
|
|
|
|
) : null}
|
|
|
|
{directionLinks ? (
|
|
|
|
<PageButton
|
|
|
|
onPageChange={onPageChange}
|
|
|
|
page={currentPage - 1}
|
|
|
|
disabled={currentPage === 1}
|
|
|
|
>
|
|
|
|
‹
|
|
|
|
</PageButton>
|
|
|
|
) : null}
|
|
|
|
{pages.map((pageNumber, index) => (
|
|
|
|
<PageButton
|
|
|
|
onPageChange={onPageChange}
|
|
|
|
page={pageNumber}
|
|
|
|
disabled={pageNumber === '...'}
|
|
|
|
active={currentPage === pageNumber}
|
|
|
|
key={index}
|
|
|
|
>
|
|
|
|
{pageNumber}
|
|
|
|
</PageButton>
|
|
|
|
))}
|
2022-01-04 12:16:09 +00:00
|
|
|
|
2022-05-23 07:57:22 +00:00
|
|
|
{directionLinks ? (
|
|
|
|
<PageButton
|
|
|
|
onPageChange={onPageChange}
|
|
|
|
page={currentPage + 1}
|
|
|
|
disabled={currentPage === last}
|
|
|
|
>
|
|
|
|
›
|
|
|
|
</PageButton>
|
|
|
|
) : null}
|
|
|
|
{boundaryLinks ? (
|
|
|
|
<PageButton
|
|
|
|
disabled={currentPage === last}
|
|
|
|
onPageChange={onPageChange}
|
|
|
|
page={last}
|
|
|
|
>
|
|
|
|
»
|
|
|
|
</PageButton>
|
|
|
|
) : null}
|
|
|
|
</ul>
|
|
|
|
</>
|
2022-01-04 12:16:09 +00:00
|
|
|
);
|
|
|
|
}
|