import type { HTMLAttributes } from 'react'; import { cx } from '../utils/cx'; import { Icon } from './Icon'; export interface PaginationProps extends Omit, 'children' | 'onChange'> { page: number; pageCount: number; onPageChange: (page: number) => void; siblingCount?: number; size?: 'sm' | 'md'; label?: string; } const ELLIPSIS = 'ellipsis'; function buildRange(page: number, pageCount: number, siblingCount: number): (number | typeof ELLIPSIS)[] { const total = siblingCount * 2 + 5; if (pageCount <= total) return Array.from({ length: pageCount }, (_, index) => index + 1); const left = Math.max(page - siblingCount, 2); const right = Math.min(page + siblingCount, pageCount - 1); const range: (number | typeof ELLIPSIS)[] = [1]; if (left > 2) range.push(ELLIPSIS); for (let index = left; index <= right; index += 1) range.push(index); if (right < pageCount - 1) range.push(ELLIPSIS); range.push(pageCount); return range; } export function Pagination({ page, pageCount, onPageChange, siblingCount = 1, size = 'md', label = 'Pagination', className, ...rest }: PaginationProps) { const pages = buildRange(page, pageCount, siblingCount); const go = (next: number) => onPageChange(Math.min(Math.max(1, next), pageCount)); return ( ); }