// @flow import React from 'react'; import { withRouter } from 'react-router'; import { Form, FormField } from 'component/common/form'; import ReactPaginate from 'react-paginate'; import useIsMobile from 'effects/use-is-mobile'; const PAGINATE_PARAM = 'page'; type Props = { totalPages: number, location: { search: string }, history: { push: string => void }, onPageChange?: number => void, }; function Paginate(props: Props) { const { totalPages = 1, location, history, onPageChange } = props; const { search } = location; const [textValue, setTextValue] = React.useState(''); const urlParams = new URLSearchParams(search); const currentPage = Number(urlParams.get(PAGINATE_PARAM)) || 1; const isMobile = useIsMobile(); function handleChangePage(newPageNumber: number) { if (onPageChange) { onPageChange(newPageNumber); } if (currentPage !== newPageNumber) { const params = new URLSearchParams(search); params.set(PAGINATE_PARAM, newPageNumber.toString()); history.push('?' + params.toString()); } } function handlePaginateKeyUp() { const newPage = Number(textValue); if (newPage && newPage > 0 && newPage <= totalPages) { handleChangePage(newPage); } } return ( // Hide the paginate controls if we are loading or there is only one page // It should still be rendered to trigger the onPageChange callback
handleChangePage(e.selected + 1)} forcePage={currentPage - 1} initialPage={currentPage - 1} containerClassName="pagination" /> {!isMobile && ( setTextValue(e.target.value)} className="paginate-channel" label={__('Go to page:')} type="text" name="paginate-file" /> )}
); } export default withRouter(Paginate);