'use client' import React, { useEffect, useRef, forwardRef } from 'react' interface LoadMoreTriggerProps { hasMore: boolean isLoading: boolean disabled?: boolean onLoadMore: () => void themeColor?: string displayedCount?: number // 当前显示数量 totalCount?: number // 已加载总数量 showEndMessage?: boolean // 是否显示到底提示 } /** * 加载更多触发器组件 * 使用 IntersectionObserver 实现无限滚动 */ const LoadMoreTrigger = forwardRef(function LoadMoreTrigger({ hasMore, isLoading, disabled = false, onLoadMore, themeColor = '#1f2937', displayedCount, totalCount, showEndMessage = true, }, ref) { const internalRef = useRef(null) const loadMoreRef = ref || internalRef // 判断是否需要显示加载更多 const shouldShowLoadMore = hasMore || (displayedCount !== undefined && totalCount !== undefined && displayedCount < totalCount) useEffect(() => { const element = typeof loadMoreRef === 'function' ? null : loadMoreRef.current if (!element) return if (disabled) return if (isLoading) return if (!shouldShowLoadMore) return const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting && !isLoading) { onLoadMore() } }, { threshold: 0.1, rootMargin: '100px' } ) observer.observe(element) return () => observer.disconnect() }, [loadMoreRef, hasMore, isLoading, disabled, onLoadMore, shouldShowLoadMore]) // 显示加载更多 if (shouldShowLoadMore) { return (
{isLoading ? (
加载中...
) : (

向下滚动加载更多

)}
) } // 显示到底提示 if (showEndMessage && !hasMore && (displayedCount === undefined || displayedCount >= (totalCount || 0))) { return (
已经到底啦~
) } return null }) export default LoadMoreTrigger