diff --git a/src/components/Paginate.jsx b/src/components/Paginate.jsx
index c3b4460..ec66265 100644
--- a/src/components/Paginate.jsx
+++ b/src/components/Paginate.jsx
@@ -22,41 +22,48 @@ const range = (from, to, step = 1) => {
export class Paginate extends Component {
constructor(props) {
super(props);
- const {totalRecords = null, pageLimit = 30, pageNeighbours = 0} = props;
+ const {totalRecords = 0, pageLimit = 30, pageNeighbours = 0, currentPage = 1} = props;
this.pageLimit = typeof pageLimit === 'number' ? pageLimit : 30;
this.totalRecords = typeof totalRecords === 'number' ? totalRecords : 0;
this.pageNeighbours = typeof pageNeighbours === 'number' ? Math.max(0, Math.min(pageNeighbours, 2)) : 0;
this.totalPages = Math.ceil(this.totalRecords / this.pageLimit);
- this.state = {currentPage: 1};
+ this.currentPage = typeof currentPage === 'number' ? currentPage : 1
+
+ this.state = {currentPage: this.currentPage};
+
+ this.gotoPage = this.gotoPage.bind(this);
+ this.handleClick = this.handleClick.bind(this);
+ this.handleMoveLeft = this.handleMoveLeft.bind(this);
+ this.handleMoveRight = this.handleMoveRight.bind(this);
+ this.makePageLink = this.makePageLink.bind(this);
+ this.fetchPageNumbers = this.fetchPageNumbers.bind(this);
}
- componentDidMount() {
- //this.gotoPage(1);
- }
-
- gotoPage = page => {
+ gotoPage(page) {
const {onPageChanged = f => f} = this.props;
const currentPage = Math.max(0, Math.min(page, this.totalPages));
- this.setState({currentPage}, () => onPageChanged(this.makePageLink(currentPage)));
+ this.setState({currentPage: currentPage}, () => onPageChanged(this.makePageLink(currentPage)));
}
- handleClick = page => evt => {
+ handleClick(page) {
+ return (evt) => {
+ evt.preventDefault();
+ this.gotoPage(page);
+ }
+ }
+
+ handleMoveLeft(evt) {
evt.preventDefault();
- this.gotoPage(page);
+ this.gotoPage(this.state.currentPage - 1);
}
- handleMoveLeft = evt => {
+ handleMoveRight(evt) {
evt.preventDefault();
- this.gotoPage(this.state.currentPage - (this.pageNeighbours * 2) - 1);
+ this.gotoPage(this.state.currentPage + 1);
}
- handleMoveRight = evt => {
- evt.preventDefault();
- this.gotoPage(this.state.currentPage + (this.pageNeighbours * 2) + 1);
- }
-
- makePageLink = page => {
+ makePageLink(page) {
const {makeLink = f => f} = this.props;
return makeLink(page);
}
@@ -72,7 +79,7 @@ export class Paginate extends Component {
* [x] => represents current page
* {...x} => represents page neighbours
*/
- fetchPageNumbers = () => {
+ fetchPageNumbers() {
const totalPages = this.totalPages;
const currentPage = this.state.currentPage;
const pageNeighbours = this.pageNeighbours;
@@ -134,41 +141,35 @@ export class Paginate extends Component {
render() {
if (!this.totalRecords || this.totalPages === 1) return null;
- const {currentPage} = this.state;
+ const currentPage = this.state.currentPage;
const pages = this.fetchPageNumbers();
return (