index.js
111 lines
| 1 | import PropTypes from 'prop-types'; |
| 2 | |
| 3 | import { __ } from '@wordpress/i18n' |
| 4 | |
| 5 | const Pagination = ({currentPage, totalPages, disabled, setPage}) => { |
| 6 | if (1 >= totalPages) { |
| 7 | return false; |
| 8 | } |
| 9 | |
| 10 | const nextPage = parseInt(currentPage) + 1; |
| 11 | const previousPage = parseInt(currentPage) - 1; |
| 12 | |
| 13 | return ( |
| 14 | <div className="tablenav bottom"> |
| 15 | <div className="tablenav-pages"> |
| 16 | <div className="pagination-links"> |
| 17 | {previousPage > 0 ? ( |
| 18 | <> |
| 19 | <a |
| 20 | href="#" |
| 21 | className="tablenav-pages-navspan button" |
| 22 | onClick={(e) => { |
| 23 | e.preventDefault(); |
| 24 | if (!disabled) { |
| 25 | setPage(1); |
| 26 | } |
| 27 | }} |
| 28 | > |
| 29 | « |
| 30 | </a>{' '} |
| 31 | <a |
| 32 | href="#" |
| 33 | className="tablenav-pages-navspan button" |
| 34 | onClick={(e) => { |
| 35 | e.preventDefault(); |
| 36 | if (!disabled) { |
| 37 | setPage(parseInt(currentPage) - 1); |
| 38 | } |
| 39 | }} |
| 40 | > |
| 41 | ‹ |
| 42 | </a> |
| 43 | </> |
| 44 | ) : ( |
| 45 | <span className="tablenav-pages-navspan button disabled">‹</span> |
| 46 | )} |
| 47 | |
| 48 | <span className="screen-reader-text">{__('Current Page', 'give')}</span> |
| 49 | <span id="table-paging" className="paging-input"> |
| 50 | <span className="tablenav-paging-text"> |
| 51 | {' '} |
| 52 | {currentPage} {__('of', 'give')} <span className="total-pages">{totalPages}</span>{' '} |
| 53 | </span> |
| 54 | </span> |
| 55 | |
| 56 | {nextPage <= totalPages ? ( |
| 57 | <> |
| 58 | <a |
| 59 | href="#" |
| 60 | className="tablenav-pages-navspan button" |
| 61 | onClick={(e) => { |
| 62 | e.preventDefault(); |
| 63 | if (!disabled) { |
| 64 | setPage(parseInt(currentPage) + 1); |
| 65 | } |
| 66 | }} |
| 67 | > |
| 68 | › |
| 69 | </a>{' '} |
| 70 | <a |
| 71 | href="#" |
| 72 | className="tablenav-pages-navspan button" |
| 73 | onClick={(e) => { |
| 74 | e.preventDefault(); |
| 75 | if (!disabled) { |
| 76 | setPage(totalPages); |
| 77 | } |
| 78 | }} |
| 79 | > |
| 80 | » |
| 81 | </a> |
| 82 | </> |
| 83 | ) : ( |
| 84 | <span className="tablenav-pages-navspan button disabled">›</span> |
| 85 | )} |
| 86 | </div> |
| 87 | </div> |
| 88 | </div> |
| 89 | ); |
| 90 | }; |
| 91 | |
| 92 | Pagination.propTypes = { |
| 93 | // Current page |
| 94 | currentPage: PropTypes.number.isRequired, |
| 95 | // Total number of pages |
| 96 | totalPages: PropTypes.number.isRequired, |
| 97 | // Function to set the next/previous page |
| 98 | setPage: PropTypes.func.isRequired, |
| 99 | // Is pagination disabled |
| 100 | disabled: PropTypes.bool.isRequired, |
| 101 | }; |
| 102 | |
| 103 | Pagination.defaultProps = { |
| 104 | currentPage: 1, |
| 105 | totalPages: 0, |
| 106 | setPage: () => {}, |
| 107 | disabled: false, |
| 108 | }; |
| 109 | |
| 110 | export default Pagination; |
| 111 |