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';
|
|
|
|
|
|
|
|
|
|
const PAGINATE_PARAM = 'page';
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
loading: boolean,
|
|
|
|
|
totalPages: number,
|
|
|
|
|
location: { search: string },
|
|
|
|
|
history: { push: string => void },
|
|
|
|
|
onPageChange?: number => void,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function Paginate(props: Props) {
|
2019-05-13 08:37:34 +02:00
|
|
|
|
const { totalPages = 1, loading, location, history, onPageChange } = 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);
|
|
|
|
|
const currentPage = Number(urlParams.get(PAGINATE_PARAM)) || 1;
|
|
|
|
|
|
|
|
|
|
function handleChangePage(newPageNumber: number) {
|
|
|
|
|
if (onPageChange) {
|
|
|
|
|
onPageChange(newPageNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentPage !== newPageNumber) {
|
|
|
|
|
history.push(`?${PAGINATE_PARAM}=${newPageNumber}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2019-09-26 17:38:48 +02:00
|
|
|
|
<Form style={totalPages <= 1 || loading ? { 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}
|
|
|
|
|
onPageChange={e => handleChangePage(e.selected + 1)}
|
|
|
|
|
forcePage={currentPage - 1}
|
|
|
|
|
initialPage={currentPage - 1}
|
|
|
|
|
containerClassName="pagination"
|
|
|
|
|
/>
|
|
|
|
|
</fieldset-section>
|
|
|
|
|
<FormField
|
2019-09-26 17:38:48 +02:00
|
|
|
|
value={textValue}
|
|
|
|
|
onChange={e => setTextValue(e.target.value)}
|
2019-05-07 04:35:04 +02:00
|
|
|
|
className="paginate-channel"
|
|
|
|
|
label={__('Go to page:')}
|
|
|
|
|
type="text"
|
|
|
|
|
name="paginate-file"
|
|
|
|
|
/>
|
|
|
|
|
</fieldset-group>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default withRouter(Paginate);
|