import { CloseCircleOutlined } from '@ant-design/icons'; import { Progress, ProgressProps } from 'antd'; import classNames from 'classnames'; import { round } from 'lodash'; import ResizeObserver from 'rc-resize-observer'; import React, { useCallback } from 'react'; import AutoImage from './index'; import './single-image.less'; interface SingleImageProps { loading?: boolean; width?: number; height?: number; progress?: number; maxHeight?: number; maxWidth?: number; dataUrl: string; label?: React.ReactNode; uid: number; preview?: boolean; autoSize?: boolean; onDelete: (uid: number) => void; onClick?: (item: any) => void; autoBgColor?: boolean; editable?: boolean; style?: React.CSSProperties; loadingSize?: ProgressProps['size']; progressType?: 'line' | 'circle' | 'dashboard'; progressColor?: string; progressWidth?: number; } const SingleImage: React.FC = (props) => { const { editable, onDelete: handleOnDelete, onClick, autoSize, uid, loading, width, height, progress, maxHeight, maxWidth, dataUrl = '', label, style, autoBgColor, preview = true, loadingSize = 'default' } = props; const imgWrapper = React.useRef(null); const [imgSize, setImgSize] = React.useState({ width: width, height: height }); const thumImgWrapStyle = React.useMemo(() => { return loading ? { width: '100%', height: '100%' } : {}; }, [loading, imgSize]); const handleOnClick = useCallback(() => { onClick?.(props); }, [onClick, props]); const handleResize = useCallback( (size: { width: number; height: number }) => { if (!autoSize || !size.width || !size.height) return; const { width: containerWidth, height: containerHeight } = size; const { width: originalWidth, height: originalHeight } = props; if (!originalWidth || !originalHeight) return; const widthRatio = containerWidth / originalWidth; const heightRatio = containerHeight / originalHeight; const scale = Math.min(widthRatio, heightRatio, 1); const newWidth = originalWidth * scale; const newHeight = originalHeight * scale; if (newWidth === imgSize.width && newHeight === imgSize.height) { return; } setImgSize({ width: newWidth, height: newHeight }); }, [autoSize, props.width, props.height] ); const handleOnLoad = React.useCallback(async () => {}, []); return (
{autoBgColor && (
)} <> {label &&
{label}
} {loading ? ( ( {round(progress, 0)}% )} trailColor="var(--ant-color-fill-secondary)" /> ) : ( {progress && progress < 100 && ( ( {round(progress, 0)}% )} strokeColor="var(--color-white-secondary)" trailColor="var(--ant-color-fill-secondary)" /> )} )} {editable && ( handleOnDelete(uid)}> )}
); }; export default SingleImage;