import React, { useState } from "react"; import Icon from '../pb_icon/_icon'; type PaginationProps = { defaultPage?: number; onPageChange?: any; pageRange?: number; totalPages?: number; }; const Pagination = ({ defaultPage = 1, onPageChange, pageRange = 5, totalPages = 1, }: PaginationProps) => { const [currentPage, setCurrentPage] = useState(defaultPage); const handlePageChange = (pageNumber: number) => { if (pageNumber >= 1 && pageNumber <= totalPages) { setCurrentPage(pageNumber); onPageChange(pageNumber); } }; const renderPageButtons = () => { const buttons = []; // Calculate pagination range with let let rangeStart = Math.max(1, currentPage - Math.floor(pageRange / 2)); let rangeEnd = Math.min(totalPages, rangeStart + pageRange - 1); // Adjust range if it's too short to fit the pageRange if (rangeEnd - rangeStart + 1 < pageRange) { if (rangeStart > 1) { rangeStart = Math.max(1, rangeEnd - pageRange + 1); } else { rangeEnd = Math.min(totalPages, rangeStart + pageRange - 1); } } // Always display the first page button if (rangeStart > 1) { buttons.push( ); } // Always display the second page button if (rangeStart > 2) { buttons.push( ); } // Display page buttons within the calculated range for (let i = rangeStart; i <= rangeEnd; i++) { buttons.push( ); } // Always display the second-to-last page button if (rangeEnd < totalPages - 1) { buttons.push( ); } // Always display the last page button if (rangeEnd < totalPages) { buttons.push( ); } return buttons; }; return (