import React, { useState } from 'react'; import { PlusOutlined } from '@ant-design/icons'; import { Image, Upload } from 'antd'; import type { GetProp, UploadFile, UploadProps } from 'antd'; type FileType = Parameters>[0]; const getBase64 = (file: FileType): Promise => new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result as string); reader.onerror = (error) => reject(error); }); const App: React.FC = () => { const [previewOpen, setPreviewOpen] = useState(false); const [previewImage, setPreviewImage] = useState(''); const [fileList, setFileList] = useState([ { uid: '-1', name: 'image.png', status: 'done', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-xxx', percent: 50, name: 'image.png', status: 'uploading', url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', }, { uid: '-5', name: 'image.png', status: 'error', }, ]); const handlePreview = async (file: UploadFile) => { if (!file.url && !file.preview) { file.preview = await getBase64(file.originFileObj as FileType); } setPreviewImage(file.url || (file.preview as string)); setPreviewOpen(true); }; const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => setFileList(newFileList); const uploadButton = ( ); return ( <> {fileList.length >= 8 ? null : uploadButton} {previewImage && ( setPreviewOpen(visible), afterOpenChange: (visible) => !visible && setPreviewImage(''), }} src={previewImage} /> )} ); }; export default App;