import React, { useState } from "react"; import classnames from 'classnames' import { globalProps } from '../utilities/globalProps' import { buildAriaProps, buildCss, buildDataProps, buildHtmlProps } from '../utilities/props' import Icon from '../pb_icon/_icon'; type PaginationProps = { aria?: { [key: string]: string }, className?: string, data?: { [key: string]: string }, htmlOptions?: {[key: string]: string | number | boolean | (() => void)}, id?: string, current?: number; onChange?: (pageNumber: number) => void; range?: number; total?: number; }; const Pagination = ( props: PaginationProps) => { const { aria = {}, className, data = {}, htmlOptions = {}, id, current = 1, onChange, range = 5, total = 1, } = props const [currentPage, setCurrentPage] = useState(current); const handlePageChange = (pageNumber: number) => { if (pageNumber >= 1 && pageNumber <= total) { setCurrentPage(pageNumber); if (onChange) { onChange(pageNumber); } } }; const renderPageButtons = (): JSX.Element[] => { const buttons: JSX.Element[] = []; // Calculate pagination range with let let rangeStart = Math.max(1, currentPage - Math.floor(range / 2)); let rangeEnd = Math.min(total, rangeStart + range - 1); // Adjust range if it's too short to fit the range if (rangeEnd - rangeStart + 1 < range) { if (rangeStart > 1) { rangeStart = Math.max(1, rangeEnd - range + 1); } else { rangeEnd = Math.min(total, rangeStart + range - 1); } } // Always display the first page button if (rangeStart > 1) { buttons.push(