/* global wdpI18n */ import React from 'react' export default class TablePagination extends React.Component { constructor(props) { super(props); this.state = { numPages: Math.ceil(this.props.total / this.props.perPage) } // Setup events. this.navigateTo = this.navigateTo.bind(this); this.nextPageClick = this.nextPageClick.bind(this); this.prevPageClick = this.prevPageClick.bind(this); } /** * Handle next page click event. * * Set current page to next page after validation. * * @since 4.11.6 */ nextPageClick() { let page = this.props.page + 1; // Continue only if valid. if (page < 1 || page > this.state.numPages) { return; } // Dispatch paginate event. this.dispatchPaginate(page) } /** * Handle previous page click event. * * Set current page to previous page after validation. * * @since 4.11.6 */ prevPageClick() { let page = this.props.page - 1; // Continue only if valid. if (page < 1 || page > this.state.numPages) { return; } // Dispatch paginate event. this.dispatchPaginate(page) } /** * Handle manual pagination event. * * When the page number is manually set, process pagination. * * @param {object} ev Event. * @since 4.11.6 */ navigateTo(ev) { let page = ev.target.value; // Continue only if valid. if (page < 1 || page > this.state.numPages) { return; } // Dispatch paginate event. this.dispatchPaginate(page) } /** * Dispatch pagination change event. * * @param {number} page Page no. * @since 4.11.6 */ dispatchPaginate(page) { // Dispatch paginate event. this.props.paginate({ page: page, type: this.props.type, total: this.props.total, pages: this.state.numPages, start: (page - 1) * this.props.perPage, end: page * this.props.perPage }); } render() { return (