mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-01 03:29:39 +08:00
6ff094efba
* 💄 STYLE: add **picture-circle** to `listType` * 📖 DOC: update API table * Update components/upload/index.en-US.md Co-authored-by: lijianan <574980606@qq.com> * Update components/upload/index.zh-CN.md Co-authored-by: lijianan <574980606@qq.com> * Update components/upload/Upload.tsx Co-authored-by: Amumu <yoyo837@hotmail.com> * 👌 IMPROVE: classNames * 👌 IMPROVE: add missing className * 📖 DOC: update Upload demo for icons * 👌 IMPROVE: classNames order * 🧪 TEST: update snapshots * 👌 IMPROVE: Upload list classNames & snapshots * 📖 DOC: add new demo & revert previous ones * Update components/upload/index.zh-CN.md Co-authored-by: Amumu <yoyo837@hotmail.com> * Update components/upload/index.en-US.md Co-authored-by: Amumu <yoyo837@hotmail.com> * Update components/upload/index.zh-CN.md Co-authored-by: Amumu <yoyo837@hotmail.com> --------- Co-authored-by: lijianan <574980606@qq.com> Co-authored-by: Amumu <yoyo837@hotmail.com>
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { PlusOutlined } from '@ant-design/icons';
|
|
import { Modal, Upload } from 'antd';
|
|
import type { RcFile, UploadProps } from 'antd/es/upload';
|
|
import type { UploadFile } from 'antd/es/upload/interface';
|
|
|
|
const getBase64 = (file: RcFile): Promise<string> =>
|
|
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 [previewTitle, setPreviewTitle] = useState('');
|
|
const [fileList, setFileList] = useState<UploadFile[]>([
|
|
{
|
|
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 handleCancel = () => setPreviewOpen(false);
|
|
|
|
const handlePreview = async (file: UploadFile) => {
|
|
if (!file.url && !file.preview) {
|
|
file.preview = await getBase64(file.originFileObj as RcFile);
|
|
}
|
|
|
|
setPreviewImage(file.url || (file.preview as string));
|
|
setPreviewOpen(true);
|
|
setPreviewTitle(file.name || file.url!.substring(file.url!.lastIndexOf('/') + 1));
|
|
};
|
|
|
|
const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) =>
|
|
setFileList(newFileList);
|
|
|
|
const uploadButton = (
|
|
<div>
|
|
<PlusOutlined />
|
|
<div style={{ marginTop: 8 }}>Upload</div>
|
|
</div>
|
|
);
|
|
return (
|
|
<>
|
|
<Upload
|
|
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
|
|
listType="picture-circle"
|
|
fileList={fileList}
|
|
onPreview={handlePreview}
|
|
onChange={handleChange}
|
|
>
|
|
{fileList.length >= 8 ? null : uploadButton}
|
|
</Upload>
|
|
<Modal open={previewOpen} title={previewTitle} footer={null} onCancel={handleCancel}>
|
|
<img alt="example" style={{ width: '100%' }} src={previewImage} />
|
|
</Modal>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default App;
|