2019-09-03 14:41:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-10-06 12:36:15 +00:00
|
|
|
import Label from '@woocommerce/base-components/label';
|
2019-09-03 14:41:05 +00:00
|
|
|
import { getIndexes } from './utils.js';
|
|
|
|
import './style.scss';
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
const Pagination = ( {
|
|
|
|
currentPage,
|
|
|
|
displayFirstAndLastPages,
|
|
|
|
displayNextAndPreviousArrows,
|
|
|
|
pagesToDisplay,
|
|
|
|
onPageChange,
|
|
|
|
totalPages,
|
|
|
|
} ) => {
|
2019-10-28 13:53:09 +00:00
|
|
|
let { minIndex, maxIndex } = getIndexes(
|
2019-09-05 15:09:31 +00:00
|
|
|
pagesToDisplay,
|
|
|
|
currentPage,
|
|
|
|
totalPages
|
|
|
|
);
|
2019-09-03 14:41:05 +00:00
|
|
|
const showFirstPage = displayFirstAndLastPages && Boolean( minIndex !== 1 );
|
2019-09-05 15:09:31 +00:00
|
|
|
const showLastPage =
|
|
|
|
displayFirstAndLastPages && Boolean( maxIndex !== totalPages );
|
|
|
|
const showFirstPageEllipsis =
|
2019-10-28 13:53:09 +00:00
|
|
|
displayFirstAndLastPages && Boolean( minIndex > 3 );
|
2019-09-05 15:09:31 +00:00
|
|
|
const showLastPageEllipsis =
|
2019-10-28 13:53:09 +00:00
|
|
|
displayFirstAndLastPages && Boolean( maxIndex < totalPages - 2 );
|
|
|
|
|
|
|
|
// Handle the cases where there would be an ellipsis replacing one single page
|
|
|
|
if ( showFirstPage && minIndex === 3 ) {
|
|
|
|
minIndex = minIndex - 1;
|
|
|
|
}
|
|
|
|
if ( showLastPage && maxIndex === totalPages - 2 ) {
|
|
|
|
maxIndex = maxIndex + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pages = [];
|
|
|
|
if ( minIndex && maxIndex ) {
|
|
|
|
for ( let i = minIndex; i <= maxIndex; i++ ) {
|
|
|
|
pages.push( i );
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 14:41:05 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="wc-block-pagination">
|
|
|
|
<Label
|
2019-09-05 15:09:31 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Navigate to another page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
/>
|
2019-10-28 13:53:09 +00:00
|
|
|
{ displayNextAndPreviousArrows && (
|
2019-09-03 14:41:05 +00:00
|
|
|
<button
|
|
|
|
className="wc-block-pagination-page"
|
|
|
|
onClick={ () => onPageChange( currentPage - 1 ) }
|
2019-09-09 10:52:48 +00:00
|
|
|
title={ __(
|
|
|
|
'Previous page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage <= 1 }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
<Label
|
|
|
|
label="<"
|
2019-09-05 15:09:31 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Previous page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
{ showFirstPage && (
|
|
|
|
<button
|
2019-10-28 13:53:09 +00:00
|
|
|
className={ classNames( 'wc-block-pagination-page', {
|
|
|
|
'wc-block-pagination-page--active': currentPage === 1,
|
|
|
|
} ) }
|
2019-09-03 14:41:05 +00:00
|
|
|
onClick={ () => onPageChange( 1 ) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage === 1 }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
1
|
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
{ showFirstPageEllipsis && (
|
2019-09-09 10:52:48 +00:00
|
|
|
<span
|
|
|
|
className="wc-block-pagination-ellipsis"
|
|
|
|
aria-hidden="true"
|
|
|
|
>
|
2019-09-03 14:41:05 +00:00
|
|
|
{ __( '…', 'woo-gutenberg-products-block' ) }
|
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
{ pages.map( ( page ) => {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
key={ page }
|
|
|
|
className={ classNames( 'wc-block-pagination-page', {
|
2019-09-09 10:52:48 +00:00
|
|
|
'wc-block-pagination-page--active':
|
|
|
|
currentPage === page,
|
2019-09-03 14:41:05 +00:00
|
|
|
} ) }
|
2019-09-09 10:52:48 +00:00
|
|
|
onClick={
|
|
|
|
currentPage === page
|
|
|
|
? null
|
|
|
|
: () => onPageChange( page )
|
|
|
|
}
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage === page }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
{ page }
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
{ showLastPageEllipsis && (
|
2019-09-09 10:52:48 +00:00
|
|
|
<span
|
|
|
|
className="wc-block-pagination-ellipsis"
|
|
|
|
aria-hidden="true"
|
|
|
|
>
|
2019-09-03 14:41:05 +00:00
|
|
|
{ __( '…', 'woo-gutenberg-products-block' ) }
|
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
{ showLastPage && (
|
|
|
|
<button
|
2019-10-28 13:53:09 +00:00
|
|
|
className={ classNames( 'wc-block-pagination-page', {
|
|
|
|
'wc-block-pagination-page--active':
|
|
|
|
currentPage === totalPages,
|
|
|
|
} ) }
|
2019-09-03 14:41:05 +00:00
|
|
|
onClick={ () => onPageChange( totalPages ) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage === totalPages }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
{ totalPages }
|
|
|
|
</button>
|
|
|
|
) }
|
2019-10-28 13:53:09 +00:00
|
|
|
{ displayNextAndPreviousArrows && (
|
2019-09-03 14:41:05 +00:00
|
|
|
<button
|
|
|
|
className="wc-block-pagination-page"
|
|
|
|
onClick={ () => onPageChange( currentPage + 1 ) }
|
|
|
|
title={ __( 'Next page', 'woo-gutenberg-products-block' ) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage >= totalPages }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
<Label
|
|
|
|
label=">"
|
2019-09-05 15:09:31 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Next page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Pagination.propTypes = {
|
|
|
|
/**
|
|
|
|
* Number of the page currently being displayed.
|
|
|
|
*/
|
|
|
|
currentPage: PropTypes.number.isRequired,
|
|
|
|
/**
|
|
|
|
* Total number of pages.
|
|
|
|
*/
|
|
|
|
totalPages: PropTypes.number.isRequired,
|
|
|
|
/**
|
|
|
|
* Displays first and last pages if they are not in the current range of pages displayed.
|
|
|
|
*/
|
|
|
|
displayFirstAndLastPages: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Displays arrows to navigate to the previous and next pages.
|
|
|
|
*/
|
|
|
|
displayNextAndPreviousArrows: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Callback function called when the user triggers a page change.
|
|
|
|
*/
|
|
|
|
onPageChange: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* Number of pages to display at the same time, including the active page
|
|
|
|
* and the pages displayed before and after it. It doesn't include the first
|
|
|
|
* and last pages.
|
|
|
|
*/
|
|
|
|
pagesToDisplay: PropTypes.number,
|
|
|
|
};
|
|
|
|
|
|
|
|
Pagination.defaultProps = {
|
|
|
|
displayFirstAndLastPages: true,
|
|
|
|
displayNextAndPreviousArrows: true,
|
|
|
|
pagesToDisplay: 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Pagination;
|