import { ChevronLeft, ChevronRight } from "lucide-react" import type { ChangeEvent } from "react" import { Button } from "@/shared/ui/button" import { cn } from "@/shared/ui/utils" interface PaginationProps { page: number pageSize: number total: number pageSizeOptions?: readonly number[] onPageChange: (page: number) => void onPageSizeChange: (pageSize: number) => void } const DEFAULT_PAGE_SIZE_OPTIONS = [10, 20, 50] as const function clamp(value: number, min: number, max: number) { return Math.min(max, Math.max(min, value)) } function rangeLabel(page: number, pageSize: number, total: number) { if (total <= 0) return "共 0 条" const start = (page - 1) * pageSize + 1 const end = Math.min(page * pageSize, total) return `第 ${start}–${end} 条,共 ${total} 条` } function buildPageItems(page: number, pageCount: number) { if (pageCount <= 1) return [1] const items: Array = [1] const start = Math.max(2, page - 1) const end = Math.min(pageCount - 1, page + 1) if (start > 2) items.push("ellipsis") for (let p = start; p <= end; p += 1) items.push(p) if (end < pageCount - 1) items.push("ellipsis") if (pageCount > 1) items.push(pageCount) return items } export function Pagination({ page, pageSize, total, pageSizeOptions = DEFAULT_PAGE_SIZE_OPTIONS, onPageChange, onPageSizeChange, }: PaginationProps) { const pageCount = Math.max(1, Math.ceil(total / pageSize)) const currentPage = clamp(page, 1, pageCount) const hasPrev = currentPage > 1 const hasNext = currentPage < pageCount const items = buildPageItems(currentPage, pageCount) function handlePageSizeChange(event: ChangeEvent) { onPageSizeChange(Number(event.target.value)) } return ( ) }