2020-08-28 21:05:01 +03:00
|
|
|
import React, { FC } from 'react';
|
2020-04-04 00:00:57 +03:00
|
|
|
import classNames from 'classnames';
|
2019-09-21 19:29:58 +03:00
|
|
|
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
|
2020-09-13 11:03:02 +03:00
|
|
|
import {
|
|
|
|
pageIsEllipsis,
|
|
|
|
keyForPage,
|
|
|
|
NumberOrEllipsis,
|
|
|
|
progressivePagination,
|
|
|
|
prettifyPageNumber,
|
|
|
|
} from '../utils/helpers/pagination';
|
2019-09-22 12:14:08 +03:00
|
|
|
import './SimplePaginator.scss';
|
2019-09-21 19:29:58 +03:00
|
|
|
|
2020-08-28 21:05:01 +03:00
|
|
|
interface SimplePaginatorProps {
|
|
|
|
pagesCount: number;
|
|
|
|
currentPage: number;
|
|
|
|
setCurrentPage: (currentPage: number) => void;
|
|
|
|
centered?: boolean;
|
|
|
|
}
|
2019-09-21 19:29:58 +03:00
|
|
|
|
2020-08-28 21:05:01 +03:00
|
|
|
const SimplePaginator: FC<SimplePaginatorProps> = ({ pagesCount, currentPage, setCurrentPage, centered = true }) => {
|
2019-09-22 11:41:31 +03:00
|
|
|
if (pagesCount < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-28 21:05:01 +03:00
|
|
|
const onClick = (page: NumberOrEllipsis) => () => !pageIsEllipsis(page) && setCurrentPage(page);
|
2019-09-22 11:41:31 +03:00
|
|
|
|
|
|
|
return (
|
2020-04-04 00:00:57 +03:00
|
|
|
<Pagination listClassName={classNames('flex-wrap mb-0 simple-paginator', { 'justify-content-center': centered })}>
|
2019-09-22 11:41:31 +03:00
|
|
|
<PaginationItem disabled={currentPage <= 1}>
|
|
|
|
<PaginationLink previous tag="span" onClick={onClick(currentPage - 1)} />
|
2019-09-21 19:29:58 +03:00
|
|
|
</PaginationItem>
|
2020-03-28 19:43:09 +03:00
|
|
|
{progressivePagination(currentPage, pagesCount).map((pageNumber, index) => (
|
2019-09-22 11:41:31 +03:00
|
|
|
<PaginationItem
|
2020-03-28 19:43:09 +03:00
|
|
|
key={keyForPage(pageNumber, index)}
|
2020-08-28 21:05:01 +03:00
|
|
|
disabled={pageIsEllipsis(pageNumber)}
|
2020-03-28 19:43:09 +03:00
|
|
|
active={currentPage === pageNumber}
|
2019-09-22 11:41:31 +03:00
|
|
|
>
|
2020-09-13 11:03:02 +03:00
|
|
|
<PaginationLink tag="span" onClick={onClick(pageNumber)}>{prettifyPageNumber(pageNumber)}</PaginationLink>
|
2019-09-22 11:41:31 +03:00
|
|
|
</PaginationItem>
|
|
|
|
))}
|
|
|
|
<PaginationItem disabled={currentPage >= pagesCount}>
|
|
|
|
<PaginationLink next tag="span" onClick={onClick(currentPage + 1)} />
|
|
|
|
</PaginationItem>
|
|
|
|
</Pagination>
|
|
|
|
);
|
|
|
|
};
|
2019-09-21 19:29:58 +03:00
|
|
|
|
|
|
|
export default SimplePaginator;
|