2018-06-26 14:59:35 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { IconButton, SelectControl } from '@wordpress/components';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-07-31 16:38:24 +00:00
|
|
|
import { isFinite, noop, uniqueId } from 'lodash';
|
2018-06-26 14:59:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
const PER_PAGE_OPTIONS = [ 25, 50, 75, 100 ];
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Use `Pagination` to allow navigation between pages that represent a collection of items.
|
|
|
|
* The component allows for selecting a new page and items per page options.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
class Pagination extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
|
|
|
|
this.previousPage = this.previousPage.bind( this );
|
|
|
|
this.nextPage = this.nextPage.bind( this );
|
|
|
|
this.onPageValueChange = this.onPageValueChange.bind( this );
|
|
|
|
this.perPageChange = this.perPageChange.bind( this );
|
2018-07-31 16:38:24 +00:00
|
|
|
this.selectInputValue = this.selectInputValue.bind( this );
|
2018-06-26 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
previousPage( event ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
const { page, onPageChange } = this.props;
|
|
|
|
if ( page - 1 < 1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onPageChange( page - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage( event ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
const { page, onPageChange } = this.props;
|
|
|
|
if ( page + 1 > this.pageCount ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onPageChange( page + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
perPageChange( perPage ) {
|
|
|
|
const { onPerPageChange, onPageChange, total, page } = this.props;
|
2018-09-03 15:25:38 +00:00
|
|
|
|
2018-06-26 14:59:35 +00:00
|
|
|
onPerPageChange( parseInt( perPage ) );
|
|
|
|
const newMaxPage = Math.ceil( total / parseInt( perPage ) );
|
|
|
|
if ( page > newMaxPage ) {
|
|
|
|
onPageChange( newMaxPage );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPageValueChange( event ) {
|
2018-07-30 23:08:13 +00:00
|
|
|
const { onPageChange } = this.props;
|
2018-07-31 16:38:24 +00:00
|
|
|
const newPage = parseInt( event.target.value, 10 );
|
|
|
|
|
2018-08-01 18:51:11 +00:00
|
|
|
if ( isFinite( newPage ) && this.pageCount && this.pageCount >= newPage ) {
|
2018-07-31 16:38:24 +00:00
|
|
|
onPageChange( newPage );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectInputValue( event ) {
|
|
|
|
event.target.select();
|
2018-06-26 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderPageArrows() {
|
|
|
|
const { page } = this.props;
|
|
|
|
|
|
|
|
if ( this.pageCount <= 1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const previousLinkClass = classNames( 'woocommerce-pagination__link', {
|
|
|
|
'is-active': page > 1,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const nextLinkClass = classNames( 'woocommerce-pagination__link', {
|
|
|
|
'is-active': page < this.pageCount,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-pagination__page-arrows">
|
|
|
|
<span
|
|
|
|
className="woocommerce-pagination__page-arrows-label"
|
|
|
|
role="status"
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
2018-07-10 12:48:06 +00:00
|
|
|
{ sprintf( __( 'Page %d of %d', 'wc-admin' ), page, this.pageCount ) }
|
2018-06-26 14:59:35 +00:00
|
|
|
</span>
|
|
|
|
<div className="woocommerce-pagination__page-arrows-buttons">
|
|
|
|
<IconButton
|
|
|
|
className={ previousLinkClass }
|
|
|
|
disabled={ ! ( page > 1 ) }
|
|
|
|
onClick={ this.previousPage }
|
|
|
|
icon="arrow-left-alt2"
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Previous Page', 'wc-admin' ) }
|
2018-06-26 14:59:35 +00:00
|
|
|
size={ 18 }
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
className={ nextLinkClass }
|
|
|
|
disabled={ ! ( page < this.pageCount ) }
|
|
|
|
onClick={ this.nextPage }
|
|
|
|
icon="arrow-right-alt2"
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Next Page', 'wc-admin' ) }
|
2018-06-26 14:59:35 +00:00
|
|
|
size={ 18 }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPagePicker() {
|
2018-07-30 23:08:13 +00:00
|
|
|
const { page } = this.props;
|
|
|
|
const isError = page < 1 || page > this.pageCount;
|
2018-06-26 14:59:35 +00:00
|
|
|
const inputClass = classNames( 'woocommerce-pagination__page-picker-input', {
|
|
|
|
'has-error': isError,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const instanceId = uniqueId( 'woocommerce-pagination-page-picker-' );
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-pagination__page-picker">
|
|
|
|
<label htmlFor={ instanceId } className="woocommerce-pagination__page-picker-label">
|
2018-07-10 12:48:06 +00:00
|
|
|
{ __( 'Go to page', 'wc-admin' ) }
|
2018-06-26 14:59:35 +00:00
|
|
|
<input
|
|
|
|
id={ instanceId }
|
|
|
|
className={ inputClass }
|
|
|
|
aria-invalid={ isError }
|
|
|
|
type="number"
|
2018-07-31 16:38:24 +00:00
|
|
|
onClick={ this.selectInputValue }
|
2018-06-26 14:59:35 +00:00
|
|
|
onChange={ this.onPageValueChange }
|
2018-07-30 23:08:13 +00:00
|
|
|
value={ page }
|
2018-06-26 14:59:35 +00:00
|
|
|
min={ 1 }
|
|
|
|
max={ this.pageCount }
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPerPagePicker() {
|
|
|
|
// TODO Replace this with a styleized Select drop-down/control?
|
2018-09-03 15:25:38 +00:00
|
|
|
const pickerOptions = PER_PAGE_OPTIONS.map( option => {
|
|
|
|
return { value: option, label: option };
|
|
|
|
} );
|
|
|
|
|
2018-06-26 14:59:35 +00:00
|
|
|
return (
|
|
|
|
<div className="woocommerce-pagination__per-page-picker">
|
|
|
|
<SelectControl
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Rows per page', 'wc-admin' ) }
|
2018-06-26 14:59:35 +00:00
|
|
|
value={ this.props.perPage }
|
|
|
|
onChange={ this.perPageChange }
|
2018-09-03 15:25:38 +00:00
|
|
|
options={ pickerOptions }
|
2018-06-26 14:59:35 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { total, perPage, className } = this.props;
|
|
|
|
this.pageCount = Math.ceil( total / perPage );
|
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
const classes = classNames( 'woocommerce-pagination', className );
|
|
|
|
|
2018-06-26 14:59:35 +00:00
|
|
|
if ( this.pageCount <= 1 ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
return (
|
|
|
|
( total > PER_PAGE_OPTIONS[ 0 ] && (
|
|
|
|
<div className={ classes }>{ this.renderPerPagePicker() }</div>
|
|
|
|
) ) ||
|
|
|
|
null
|
|
|
|
);
|
2018-06-26 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ classes }>
|
|
|
|
{ this.renderPageArrows() }
|
|
|
|
{ this.renderPagePicker() }
|
|
|
|
{ this.renderPerPagePicker() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Pagination.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The current page of the collection.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
page: PropTypes.number.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A function to execute when the page is changed.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
onPageChange: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The amount of results that are being displayed per page.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
perPage: PropTypes.number.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A function to execute when the per page option is changed.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
onPerPageChange: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The total number of results.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
total: PropTypes.number.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Additional classNames.
|
|
|
|
*/
|
2018-06-26 14:59:35 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
Pagination.defaultProps = {
|
|
|
|
onPageChange: noop,
|
|
|
|
onPerPageChange: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Pagination;
|