2019-09-21 19:29:58 +03:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
|
2019-09-22 11:41:31 +03:00
|
|
|
import { range, max, min } from 'ramda';
|
2019-09-22 12:14:08 +03:00
|
|
|
import './SimplePaginator.scss';
|
2019-09-21 19:29:58 +03:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
pagesCount: PropTypes.number.isRequired,
|
|
|
|
currentPage: PropTypes.number.isRequired,
|
|
|
|
setCurrentPage: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-03-28 19:19:33 +03:00
|
|
|
export const ELLIPSIS = '...';
|
2019-09-22 12:14:08 +03:00
|
|
|
|
2020-03-28 19:19:33 +03:00
|
|
|
export const progressivePagination = (currentPage, pageCount) => {
|
2019-09-22 11:41:31 +03:00
|
|
|
const delta = 2;
|
|
|
|
const pages = range(
|
|
|
|
max(delta, currentPage - delta),
|
|
|
|
min(pageCount - 1, currentPage + delta) + 1
|
|
|
|
);
|
|
|
|
|
|
|
|
if (currentPage - delta > delta) {
|
2020-03-28 19:19:33 +03:00
|
|
|
pages.unshift(ELLIPSIS);
|
2019-09-22 11:41:31 +03:00
|
|
|
}
|
|
|
|
if (currentPage + delta < pageCount - 1) {
|
2020-03-28 19:19:33 +03:00
|
|
|
pages.push(ELLIPSIS);
|
2019-09-22 11:41:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pages.unshift(1);
|
|
|
|
pages.push(pageCount);
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
const SimplePaginator = ({ pagesCount, currentPage, setCurrentPage }) => {
|
|
|
|
if (pagesCount < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onClick = (page) => () => setCurrentPage(page);
|
|
|
|
|
|
|
|
return (
|
2019-09-22 12:14:08 +03:00
|
|
|
<Pagination listClassName="flex-wrap justify-content-center mb-0 simple-paginator">
|
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:19:33 +03:00
|
|
|
{progressivePagination(currentPage, pagesCount).map((page, index) => (
|
2019-09-22 11:41:31 +03:00
|
|
|
<PaginationItem
|
2020-03-28 19:19:33 +03:00
|
|
|
key={page !== ELLIPSIS ? page : `${page}_${index}`}
|
2019-09-22 11:41:31 +03:00
|
|
|
active={page === currentPage}
|
2020-03-28 19:19:33 +03:00
|
|
|
disabled={page === ELLIPSIS}
|
2019-09-22 11:41:31 +03:00
|
|
|
>
|
|
|
|
<PaginationLink tag="span" onClick={onClick(page)}>{page}</PaginationLink>
|
|
|
|
</PaginationItem>
|
|
|
|
))}
|
|
|
|
<PaginationItem disabled={currentPage >= pagesCount}>
|
|
|
|
<PaginationLink next tag="span" onClick={onClick(currentPage + 1)} />
|
|
|
|
</PaginationItem>
|
|
|
|
</Pagination>
|
|
|
|
);
|
|
|
|
};
|
2019-09-21 19:29:58 +03:00
|
|
|
|
|
|
|
SimplePaginator.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default SimplePaginator;
|