import React, { FC, useState } from 'react'; import styled from '@emotion/styled'; import { PreviewGroupPreview } from 'rc-image/lib/PreviewGroup'; import Image from '../image'; import defaultImg from './defaultImg'; const ImgWrapper = styled.span<{ width?: number | string }>` position: relative; display: block; width: ${({ width }) => (typeof width === 'string' ? width : `${width}px`)}; min-width: 40px; max-width: 100%; overflow: hidden; background: rgb(255 255 255); border: 1px solid rgba(225, 227, 229, 1); border-radius: 4px; img { position: absolute; top: 0; right: 0; bottom: 0; left: 0; max-width: 100%; max-height: 100%; margin: auto; color: #637381; } ::after { display: block; padding-bottom: 100%; content: ''; } `; const ImageMask = styled.div` position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 99; display: flex; align-items: center; justify-content: center; color: #fff; background: rgba(0, 0, 0, 0.5); cursor: pointer; opacity: 0; transition: opacity 0.3s; :hover { opacity: 1; } `; const ImageQuantity = styled.div` position: absolute; right: 2px; bottom: 0; font-size: 12px; `; export interface ThumbnailProps extends React.HTMLAttributes { /** 图片链接 */ src?: string; /** 多张图片预览链接 */ previewList?: string[]; /** 图片宽度 */ width?: number | string; /** 预览图片继承 previewType ,为 false 时关闭预览 */ preview?: boolean | Omit; /** 图片加载之前的占位符 */ alt?: string; /** 显示预览多张图片的数字 */ showCount?: boolean; } const Thumbnail: FC = ({ alt = '...', src, width = 60, preview = true, previewList, showCount = true, ...props }) => { const [errSrc, setErrSrc] = useState(false); const [visiblePreview, setVisiblePreview] = useState(false); return ( <> { e.preventDefault(); e.stopPropagation(); props.onClick?.(e); }} {...props} > {alt} setErrSrc(true)} /> {preview && ( setVisiblePreview(true)}> {previewList?.length && showCount ? ( {previewList.length} ) : null} )} {preview && visiblePreview && ( e.stopPropagation()}> setVisiblePreview(visible), ...(typeof preview === 'object' && preview), }} > {previewList && previewList?.length > 0 ? ( previewList.map((item, index) => ( // eslint-disable-next-line react/no-array-index-key )) ) : ( setVisiblePreview(visible), }} wrapperStyle={{ display: 'none' }} /> )} )} ); }; export default Thumbnail;