2019-05-07 04:35:04 +02:00
|
|
|
|
// @flow
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { withRouter } from 'react-router';
|
|
|
|
|
import { Form, FormField } from 'component/common/form';
|
|
|
|
|
import ReactPaginate from 'react-paginate';
|
2020-08-10 22:47:39 +02:00
|
|
|
|
import { useIsMobile } from 'effects/use-screensize';
|
2019-05-07 04:35:04 +02:00
|
|
|
|
|
|
|
|
|
const PAGINATE_PARAM = 'page';
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
totalPages: number,
|
|
|
|
|
location: { search: string },
|
2021-09-10 17:36:08 +02:00
|
|
|
|
history: { push: (string) => void },
|
|
|
|
|
onPageChange?: (number) => void,
|
|
|
|
|
disableHistory?: boolean, // Disables the use of '&page=' param and history stack.
|
2019-05-07 04:35:04 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function Paginate(props: Props) {
|
2021-09-10 17:36:08 +02:00
|
|
|
|
const { totalPages = 1, location, history, onPageChange, disableHistory } = props;
|
2019-05-07 04:35:04 +02:00
|
|
|
|
const { search } = location;
|
2019-09-26 17:38:48 +02:00
|
|
|
|
const [textValue, setTextValue] = React.useState('');
|
2019-05-07 04:35:04 +02:00
|
|
|
|
const urlParams = new URLSearchParams(search);
|
2021-11-05 20:09:06 +01:00
|
|
|
|
const urlParamPage = Number(urlParams.get(PAGINATE_PARAM));
|
|
|
|
|
const initialPage = disableHistory ? 1 : urlParamPage || 1;
|
2021-09-10 17:36:08 +02:00
|
|
|
|
const [currentPage, setCurrentPage] = React.useState(initialPage);
|
2020-01-08 20:20:30 +01:00
|
|
|
|
const isMobile = useIsMobile();
|
2019-05-07 04:35:04 +02:00
|
|
|
|
|
|
|
|
|
function handleChangePage(newPageNumber: number) {
|
|
|
|
|
if (onPageChange) {
|
|
|
|
|
onPageChange(newPageNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentPage !== newPageNumber) {
|
2021-09-10 17:36:08 +02:00
|
|
|
|
setCurrentPage(newPageNumber);
|
|
|
|
|
|
|
|
|
|
if (!disableHistory) {
|
|
|
|
|
const params = new URLSearchParams(search);
|
|
|
|
|
params.set(PAGINATE_PARAM, newPageNumber.toString());
|
|
|
|
|
history.push('?' + params.toString());
|
|
|
|
|
}
|
2019-05-07 04:35:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 17:38:48 +02:00
|
|
|
|
function handlePaginateKeyUp() {
|
|
|
|
|
const newPage = Number(textValue);
|
|
|
|
|
if (newPage && newPage > 0 && newPage <= totalPages) {
|
2019-05-07 04:35:04 +02:00
|
|
|
|
handleChangePage(newPage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 20:09:06 +01:00
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (urlParamPage) {
|
|
|
|
|
setCurrentPage(urlParamPage);
|
|
|
|
|
}
|
|
|
|
|
}, [urlParamPage, setCurrentPage]);
|
|
|
|
|
|
2019-05-07 04:35:04 +02:00
|
|
|
|
return (
|
2019-05-08 03:29:30 +02:00
|
|
|
|
// Hide the paginate controls if we are loading or there is only one page
|
2019-05-09 06:11:22 +02:00
|
|
|
|
// It should still be rendered to trigger the onPageChange callback
|
2020-05-21 17:38:28 +02:00
|
|
|
|
<Form style={totalPages <= 1 ? { display: 'none' } : null} onSubmit={handlePaginateKeyUp}>
|
2019-05-07 04:35:04 +02:00
|
|
|
|
<fieldset-group class="fieldset-group--smushed fieldgroup--paginate">
|
|
|
|
|
<fieldset-section>
|
|
|
|
|
<ReactPaginate
|
|
|
|
|
pageCount={totalPages}
|
|
|
|
|
pageRangeDisplayed={2}
|
|
|
|
|
previousLabel="‹"
|
|
|
|
|
nextLabel="›"
|
|
|
|
|
activeClassName="pagination__item--selected"
|
|
|
|
|
pageClassName="pagination__item"
|
|
|
|
|
previousClassName="pagination__item pagination__item--previous"
|
|
|
|
|
nextClassName="pagination__item pagination__item--next"
|
|
|
|
|
breakClassName="pagination__item pagination__item--break"
|
|
|
|
|
marginPagesDisplayed={2}
|
2021-09-10 17:36:08 +02:00
|
|
|
|
onPageChange={(e) => handleChangePage(e.selected + 1)}
|
2019-05-07 04:35:04 +02:00
|
|
|
|
forcePage={currentPage - 1}
|
|
|
|
|
initialPage={currentPage - 1}
|
|
|
|
|
containerClassName="pagination"
|
|
|
|
|
/>
|
|
|
|
|
</fieldset-section>
|
2020-01-08 20:20:30 +01:00
|
|
|
|
{!isMobile && (
|
|
|
|
|
<FormField
|
|
|
|
|
value={textValue}
|
2021-09-10 17:36:08 +02:00
|
|
|
|
onChange={(e) => setTextValue(e.target.value)}
|
2022-04-17 19:04:56 +02:00
|
|
|
|
className="paginate-goto"
|
|
|
|
|
aria-label={__('Go to page:')}
|
|
|
|
|
placeholder={__('Go to')}
|
2020-01-08 20:20:30 +01:00
|
|
|
|
type="text"
|
|
|
|
|
name="paginate-file"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2019-05-07 04:35:04 +02:00
|
|
|
|
</fieldset-group>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default withRouter(Paginate);
|