export const Pagination: React.FC<{
currentPage: number,
totalPages: number,
paginate: any
}> = (props) => {
const pageNumbers = []; // 페이지 번호 - 현재를 기준으로 앞뒤 2개씩 보여줌
if (props.currentPage === 1) { // 현재 페이지가 1일 때
pageNumbers.push(props.currentPage);
if (props.totalPages >= props.currentPage + 1) {
pageNumbers.push(props.currentPage + 1);
}
if (props.totalPages >= props.currentPage + 2) {
pageNumbers.push(props.currentPage + 2);
}
} else if (props.currentPage > 1) { // 현재 페이지가 1보다 클 때
if (props.currentPage >= 3) { // 현재 페이지가 3보다 클 때
pageNumbers.push(props.currentPage - 2);
pageNumbers.push(props.currentPage - 1);
} else {
pageNumbers.push(props.currentPage - 1);
}
pageNumbers.push(props.currentPage);
if (props.totalPages >= props.currentPage + 1) {
pageNumbers.push(props.currentPage + 1);
}
if (props.totalPages >= props.currentPage + 2) {
pageNumbers.push(props.currentPage + 2);
}
}
return (
<nav aria-label="...">
<ul className='pagination'>
<li className='page-item' xxxxonClick={() => props.paginate(1)}>
<button className='page-link'>
First Page
</button>
</li>
{pageNumbers.map(number => (
<li key={number} xxxxonClick={() => props.paginate(number)}
className={'page-item ' + (props.currentPage === number ? 'active' : '')}>
<button className='page-link'>
{number}
</button>
</li>
))}
<li className='page-item' xxxxonClick={() => props.paginate(props.totalPages)}>
<button className='page-link'>
Last Page
</button>
</li>
</ul>
</nav>
);
}