index.tsx
128 lines
| 1 | import PropTypes from 'prop-types'; |
| 2 | import {useState, useEffect} from 'react'; |
| 3 | import styles from './Pagination.module.scss'; |
| 4 | import cx from 'classnames'; |
| 5 | import {__, sprintf} from '@wordpress/i18n'; |
| 6 | |
| 7 | const Pagination = ({currentPage, totalPages, totalItems = -1, disabled, setPage, singleName, pluralName}) => { |
| 8 | const [pageInput, setPageInput] = useState(1); |
| 9 | |
| 10 | useEffect(() => { |
| 11 | setPageInput(currentPage); |
| 12 | }, [currentPage]); |
| 13 | |
| 14 | const nextPage = parseInt(currentPage) + 1; |
| 15 | const previousPage = parseInt(currentPage) - 1; |
| 16 | |
| 17 | return ( |
| 18 | <nav aria-label={sprintf(__('%s table', 'give'), pluralName)} className={styles.container}> |
| 19 | {totalItems >= 1 && ( |
| 20 | <span> |
| 21 | {totalItems.toString() + ' '} |
| 22 | {totalItems == 1 ? singleName : pluralName} |
| 23 | </span> |
| 24 | )} |
| 25 | {1 < totalPages && ( |
| 26 | <> |
| 27 | <button |
| 28 | className={cx(styles.navDirection, styles.navElement)} |
| 29 | aria-disabled={previousPage <= 1} |
| 30 | aria-label={__('first page')} |
| 31 | onClick={(e) => { |
| 32 | if (e.currentTarget.getAttribute('aria-disabled') === 'false') { |
| 33 | setPage(1); |
| 34 | } |
| 35 | }} |
| 36 | > |
| 37 | <span aria-hidden={true}>«</span> |
| 38 | </button> |
| 39 | <button |
| 40 | className={cx(styles.navDirection, styles.navElement)} |
| 41 | aria-disabled={previousPage <= 0} |
| 42 | aria-label={__('previous page')} |
| 43 | onClick={(e) => { |
| 44 | if (e.currentTarget.getAttribute('aria-disabled') === 'false') { |
| 45 | setPage(parseInt(currentPage) - 1); |
| 46 | } |
| 47 | }} |
| 48 | > |
| 49 | <span aria-hidden={true}>‹</span> |
| 50 | </button> |
| 51 | <span> |
| 52 | <label htmlFor={styles.currentPage} className={styles.visuallyHidden}> |
| 53 | {__('Current Page', 'give')} |
| 54 | </label> |
| 55 | <input |
| 56 | className={styles.navElement} |
| 57 | id={styles.currentPage} |
| 58 | name={'currentPageSelector'} |
| 59 | type="number" |
| 60 | min={1} |
| 61 | max={totalPages} |
| 62 | value={pageInput} |
| 63 | onChange={(e) => { |
| 64 | const cleanValue = parseInt(e.target.value.replace(/[^0-9]/, '')); |
| 65 | const page = Number(cleanValue); |
| 66 | setPageInput(cleanValue); |
| 67 | if (totalPages >= page && page > 0) { |
| 68 | setPage(page); |
| 69 | } |
| 70 | }} |
| 71 | /> |
| 72 | <span> |
| 73 | {' '} |
| 74 | {__('of', 'give')} <span>{totalPages}</span>{' '} |
| 75 | </span> |
| 76 | </span> |
| 77 | <button |
| 78 | className={cx(styles.navDirection, styles.navElement)} |
| 79 | aria-disabled={nextPage > totalPages} |
| 80 | aria-label={__('next page')} |
| 81 | onClick={(e) => { |
| 82 | if (e.currentTarget.getAttribute('aria-disabled') === 'false') { |
| 83 | setPage(parseInt(currentPage) + 1); |
| 84 | } |
| 85 | }} |
| 86 | > |
| 87 | <span aria-hidden={true}>›</span> |
| 88 | </button> |
| 89 | <button |
| 90 | className={cx(styles.navDirection, styles.navElement)} |
| 91 | aria-disabled={nextPage > totalPages - 1} |
| 92 | aria-label={__('final page')} |
| 93 | onClick={(e) => { |
| 94 | if (e.currentTarget.getAttribute('aria-disabled') === 'false') { |
| 95 | setPage(totalPages); |
| 96 | } |
| 97 | }} |
| 98 | > |
| 99 | <span aria-hidden={true}>»</span> |
| 100 | </button> |
| 101 | </> |
| 102 | )} |
| 103 | </nav> |
| 104 | ); |
| 105 | }; |
| 106 | |
| 107 | Pagination.propTypes = { |
| 108 | // Current page |
| 109 | currentPage: PropTypes.number.isRequired, |
| 110 | // Total number of pages |
| 111 | totalPages: PropTypes.number.isRequired, |
| 112 | // Total number of items |
| 113 | totalItems: PropTypes.number, |
| 114 | // Function to set the next/previous page |
| 115 | setPage: PropTypes.func.isRequired, |
| 116 | // Is pagination disabled |
| 117 | disabled: PropTypes.bool.isRequired, |
| 118 | }; |
| 119 | |
| 120 | Pagination.defaultProps = { |
| 121 | currentPage: 1, |
| 122 | totalPages: 0, |
| 123 | setPage: () => {}, |
| 124 | disabled: false, |
| 125 | }; |
| 126 | |
| 127 | export default Pagination; |
| 128 |