|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | + |
| 4 | +export function Pagination({ |
| 5 | + actualPage, |
| 6 | + ariaLabel, |
| 7 | + firstLabel, |
| 8 | + lastLabel, |
| 9 | + lastPage, |
| 10 | + maxPageWindow, |
| 11 | + nextLabel, |
| 12 | + onSelect, |
| 13 | + previousLabel, |
| 14 | +}) { |
| 15 | + const pageWindow = Math.min(maxPageWindow, lastPage); |
| 16 | + const centerOfPageWindow = Math.max(Math.floor(pageWindow / 2), 2); |
| 17 | + const lastStartPage = Math.max(lastPage - pageWindow + 1, 1); |
| 18 | + const startPage = Math.max(Math.min(actualPage - centerOfPageWindow, lastStartPage), 1); |
| 19 | + const showPreviousNavigation = startPage > 1; |
| 20 | + |
| 21 | + const pages = Array.from( |
| 22 | + { |
| 23 | + length: pageWindow, |
| 24 | + }, |
| 25 | + (v, i) => startPage + i |
| 26 | + ); |
| 27 | + |
| 28 | + const select = (page) => (e) => { |
| 29 | + e.preventDefault(); |
| 30 | + e.stopPropagation(); |
| 31 | + onSelect(page); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <nav aria-label={ariaLabel}> |
| 36 | + <ul className="pagination"> |
| 37 | + {showPreviousNavigation && ( |
| 38 | + <React.Fragment> |
| 39 | + <li className="page-item"> |
| 40 | + <a className="page-link" href="" onClick={select(1)} aria-label={firstLabel}> |
| 41 | + <span aria-hidden="true">«</span> |
| 42 | + <span className="sr-only">{firstLabel}</span> |
| 43 | + </a> |
| 44 | + </li> |
| 45 | + |
| 46 | + <li className="page-item"> |
| 47 | + <a className="page-link" href="" onClick={select(actualPage - 1)} aria-label={previousLabel}> |
| 48 | + <span aria-hidden="true">‹</span> |
| 49 | + <span className="sr-only">{previousLabel}</span> |
| 50 | + </a> |
| 51 | + </li> |
| 52 | + </React.Fragment> |
| 53 | + )} |
| 54 | + |
| 55 | + {pages.map((page) => ( |
| 56 | + <li key={`pages-${page}`} className={`page-item${actualPage === page ? ' active' : ''}`}> |
| 57 | + <a className="page-link" href="" onClick={select(page)}> |
| 58 | + {page} |
| 59 | + </a> |
| 60 | + </li> |
| 61 | + ))} |
| 62 | + |
| 63 | + {startPage < lastStartPage && ( |
| 64 | + <React.Fragment> |
| 65 | + <li className="page-item"> |
| 66 | + <a className="page-link" href="" onClick={select(actualPage + 1)} aria-label={nextLabel}> |
| 67 | + <span aria-hidden="true">›</span> |
| 68 | + <span className="sr-only">{nextLabel}</span> |
| 69 | + </a> |
| 70 | + </li> |
| 71 | + |
| 72 | + <li className="page-item"> |
| 73 | + <a className="page-link" href="" onClick={select(lastPage)} aria-label={lastLabel}> |
| 74 | + <span aria-hidden="true">»</span> |
| 75 | + <span className="sr-only">{lastLabel}</span> |
| 76 | + </a> |
| 77 | + </li> |
| 78 | + </React.Fragment> |
| 79 | + )} |
| 80 | + </ul> |
| 81 | + </nav> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +Pagination.defaultProps = { |
| 86 | + actualPage: 1, |
| 87 | + ariaLabel: 'Page navigation', |
| 88 | + firstLabel: 'First', |
| 89 | + lastLabel: 'Last', |
| 90 | + maxPageWindow: 5, |
| 91 | + nextLabel: 'Next', |
| 92 | + previousLabel: 'Previous', |
| 93 | +}; |
| 94 | + |
| 95 | +Pagination.propTypes = { |
| 96 | + actualPage: PropTypes.number, |
| 97 | + ariaLabel: PropTypes.string, |
| 98 | + firstLabel: PropTypes.string, |
| 99 | + lastLabel: PropTypes.string, |
| 100 | + lastPage: PropTypes.number, |
| 101 | + maxPageWindow: PropTypes.number, |
| 102 | + nextLabel: PropTypes.string, |
| 103 | + onSelect: PropTypes.func.isRequired, |
| 104 | + previousLabel: PropTypes.string, |
| 105 | +}; |
0 commit comments