'use client' import * as React from 'react' import {useState, useEffect} from 'react' import { ChevronLeftIcon, ChevronRightIcon, MoreHorizontalIcon, } from 'lucide-react' import {merge} from '@/lib/utils' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from './select' export interface PaginationProps { page: number size: number total: number sizeOptions?: number[] onPageChange?: (page: number) => void onSizeChange?: (size: number) => void className?: string } function Pagination({ page, size, total, sizeOptions = [10, 20, 50, 100], onPageChange, onSizeChange, className, }: PaginationProps) { const [currentPage, setCurrentPage] = useState(page) const totalPages = Math.ceil(total / size) // 同步外部 page 变化 useEffect(() => { setCurrentPage(page) }, [page]) // 分页器逻辑 const generatePaginationItems = () => { // 最多显示7个页码,其余用省略号 const SIBLINGS = 1 // 当前页左右各显示的页码数 const DOTS = -1 // 省略号标记 if (totalPages <= 7) { // 总页数少于7,全部显示 return Array.from({length: totalPages}, (_, i) => i + 1) } // 是否需要显示左边的省略号 const showLeftDots = currentPage > 2 + SIBLINGS // 是否需要显示右边的省略号 const showRightDots = currentPage < totalPages - (2 + SIBLINGS) if (showLeftDots && showRightDots) { // 两边都有省略号 const leftSiblingIndex = Math.max(currentPage - SIBLINGS, 1) const rightSiblingIndex = Math.min(currentPage + SIBLINGS, totalPages) return [1, DOTS, ...Array.from( {length: rightSiblingIndex - leftSiblingIndex + 1}, (_, i) => leftSiblingIndex + i, ), DOTS, totalPages] } if (!showLeftDots && showRightDots) { // 只有右边有省略号 return [...Array.from({length: 3 + SIBLINGS * 2}, (_, i) => i + 1), DOTS, totalPages] } if (showLeftDots && !showRightDots) { // 只有左边有省略号 return [1, DOTS, ...Array.from( {length: 3 + SIBLINGS * 2}, (_, i) => totalPages - (3 + SIBLINGS * 2) + i + 1, )] } return [] } const handlePageChange = (newPage: number) => { if (newPage < 1 || newPage > totalPages || newPage === currentPage) { return } setCurrentPage(newPage) onPageChange?.(newPage) } const handlePageSizeChange = (newSize: string) => { const parsedSize = parseInt(newSize, 10) if (onSizeChange) { onSizeChange(parsedSize) } } const paginationItems = generatePaginationItems() return (