import { View, Image as TaroImage, ImageProps as TaroImageProps } from '@tarojs/components' import { cn } from '@/utils/cn' import { useState } from 'react' export interface ImageProps extends Omit { /** * 图片地址 */ src: string /** * 替代文本 */ alt?: string /** * 图片模式 * @default "aspectFill" */ mode?: TaroImageProps['mode'] /** * 是否懒加载 * @default true */ lazyLoad?: boolean /** * 是否显示加载占位 * @default true */ showLoading?: boolean /** * 是否显示错误占位 * @default true */ showError?: boolean /** * 圆角大小 */ rounded?: 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full' /** * 自定义样式类 */ className?: string /** * 图片加载失败时的回调 */ onError?: () => void /** * 图片加载成功的回调 */ onLoad?: () => void } const roundedMap = { none: '', sm: 'rounded-sm', md: 'rounded-md', lg: 'rounded-lg', xl: 'rounded-xl', full: 'rounded-full' } export function Image({ src, alt = '图片', mode = 'aspectFill', lazyLoad = true, showLoading = true, showError = true, rounded = 'none', className, onError, onLoad, ...props }: ImageProps) { const [loading, setLoading] = useState(true) const [error, setError] = useState(false) const handleLoad = () => { setLoading(false) setError(false) onLoad?.() } const handleError = () => { setLoading(false) setError(true) onError?.() } const renderPlaceholder = () => { if (loading && showLoading) { return ( ) } if (error && showError) { return ( ) } return null } return ( {renderPlaceholder()} ) }