'use client'; import { ChevronLeft, ChevronRight } from 'lucide-react'; import { useId, useMemo, useState } from 'react'; interface PaginatedRowProps { children: React.ReactNode[]; itemsPerPage?: number; className?: string; } export default function PaginatedRow({ children, itemsPerPage = 10, className = '', }: PaginatedRowProps) { const [startIndex, setStartIndex] = useState(0); const [isHovered, setIsHovered] = useState(false); const uniqueId = useId(); // 为每个实例生成唯一ID // 获取当前显示的项目 - 支持无限向前浏览 const currentItems = useMemo(() => { const endIndex = startIndex + itemsPerPage; // 如果超出范围,循环显示 if (endIndex <= children.length) { return children.slice(startIndex, endIndex); } else { // 当超出范围时,从头开始循环 const firstPart = children.slice(startIndex); const secondPart = children.slice(0, endIndex - children.length); return [...firstPart, ...secondPart]; } }, [children, startIndex, itemsPerPage]); // 向前翻页 - 只有不在第一页时才能向前 const handlePrevPage = () => { if (startIndex > 0) { setStartIndex((prev) => Math.max(0, prev - itemsPerPage)); } }; // 向后翻页 - 支持无限浏览,到达末尾时循环到开头 const handleNextPage = () => { setStartIndex((prev) => { const newIndex = prev + itemsPerPage; // 当超出总长度时,从头开始,实现无限循环 return newIndex >= children.length ? 0 : newIndex; }); }; // 检查是否可以向前翻页 const canGoPrev = startIndex > 0; // 检查是否需要显示翻页按钮 const needsPagination = children.length > itemsPerPage; return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} > {/* 内容区域 - 移除group类以避免悬停效果冲突 */}
{currentItems} {/* 改进的导航按钮 - 仅在容器悬停时显示 */} {needsPagination && ( <> {/* 左箭头按钮 - 只有不在第一页时才显示 */} {canGoPrev && ( )} {/* 右箭头按钮 - 始终显示 */} )}
); }