2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-11-25 15:00:28 +08:00
|
|
|
order: 3
|
2016-09-18 11:08:54 +08:00
|
|
|
title:
|
2016-11-25 15:00:28 +08:00
|
|
|
zh-CN: 照片墙
|
|
|
|
en-US: Pictures Wall
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-01-15 16:44:07 +08:00
|
|
|
|
2016-08-25 09:46:19 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-11-25 15:00:28 +08:00
|
|
|
用户可以上传图片并在列表中显示缩略图。当上传照片数到达限制后,上传按钮消失。
|
2016-01-15 16:44:07 +08:00
|
|
|
|
2016-08-25 09:46:19 +08:00
|
|
|
## en-US
|
|
|
|
|
2016-11-25 15:00:28 +08:00
|
|
|
After users upload picture, the thumbnail will be shown in list. The upload button will disappear when count meets limitation.
|
2016-08-25 09:46:19 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2019-08-13 14:07:17 +08:00
|
|
|
import { Upload, Modal } from 'antd';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { PlusOutlined } from '@ant-design/icons';
|
2016-01-15 16:44:07 +08:00
|
|
|
|
2019-05-24 16:36:09 +08:00
|
|
|
function getBase64(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
reader.onload = () => resolve(reader.result);
|
|
|
|
reader.onerror = error => reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-25 15:00:28 +08:00
|
|
|
class PicturesWall extends React.Component {
|
|
|
|
state = {
|
|
|
|
previewVisible: false,
|
|
|
|
previewImage: '',
|
2020-04-17 14:28:18 +08:00
|
|
|
previewTitle: '',
|
2019-05-07 14:57:32 +08:00
|
|
|
fileList: [
|
|
|
|
{
|
|
|
|
uid: '-1',
|
2019-07-22 19:18:47 +08:00
|
|
|
name: 'image.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uid: '-2',
|
|
|
|
name: 'image.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uid: '-3',
|
|
|
|
name: 'image.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uid: '-4',
|
|
|
|
name: 'image.png',
|
|
|
|
status: 'done',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
2020-08-24 16:33:24 +08:00
|
|
|
{
|
|
|
|
uid: '-xxx',
|
|
|
|
percent: 50,
|
|
|
|
name: 'image.png',
|
|
|
|
status: 'uploading',
|
|
|
|
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
|
},
|
2019-07-22 19:18:47 +08:00
|
|
|
{
|
|
|
|
uid: '-5',
|
|
|
|
name: 'image.png',
|
2019-11-12 21:45:32 +08:00
|
|
|
status: 'error',
|
2019-05-07 14:57:32 +08:00
|
|
|
},
|
|
|
|
],
|
2016-11-25 15:00:28 +08:00
|
|
|
};
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
handleCancel = () => this.setState({ previewVisible: false });
|
2016-11-25 15:00:28 +08:00
|
|
|
|
2019-05-24 16:36:09 +08:00
|
|
|
handlePreview = async file => {
|
|
|
|
if (!file.url && !file.preview) {
|
|
|
|
file.preview = await getBase64(file.originFileObj);
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:30:14 +08:00
|
|
|
this.setState({
|
2019-05-24 16:36:09 +08:00
|
|
|
previewImage: file.url || file.preview,
|
2016-11-25 15:00:28 +08:00
|
|
|
previewVisible: true,
|
2020-04-17 14:28:18 +08:00
|
|
|
previewTitle: file.name || file.url.substring(file.url.lastIndexOf('/') + 1),
|
2016-04-05 12:30:14 +08:00
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-25 15:00:28 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
handleChange = ({ fileList }) => this.setState({ fileList });
|
2016-11-25 15:00:28 +08:00
|
|
|
|
2016-04-05 12:30:14 +08:00
|
|
|
render() {
|
2020-04-17 14:28:18 +08:00
|
|
|
const { previewVisible, previewImage, fileList, previewTitle } = this.state;
|
2016-11-25 15:00:28 +08:00
|
|
|
const uploadButton = (
|
|
|
|
<div>
|
2019-11-28 12:34:33 +08:00
|
|
|
<PlusOutlined />
|
2020-08-24 16:33:24 +08:00
|
|
|
<div style={{ marginTop: 8 }}>Upload</div>
|
2016-11-25 15:00:28 +08:00
|
|
|
</div>
|
|
|
|
);
|
2016-04-05 12:30:14 +08:00
|
|
|
return (
|
2020-08-24 16:33:24 +08:00
|
|
|
<>
|
2016-11-25 15:00:28 +08:00
|
|
|
<Upload
|
2019-04-30 16:10:00 +08:00
|
|
|
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
|
2016-11-25 15:00:28 +08:00
|
|
|
listType="picture-card"
|
|
|
|
fileList={fileList}
|
|
|
|
onPreview={this.handlePreview}
|
|
|
|
onChange={this.handleChange}
|
2016-11-22 14:21:42 +08:00
|
|
|
>
|
2019-07-22 19:18:47 +08:00
|
|
|
{fileList.length >= 8 ? null : uploadButton}
|
2016-11-25 15:00:28 +08:00
|
|
|
</Upload>
|
2020-04-17 14:28:18 +08:00
|
|
|
<Modal
|
|
|
|
visible={previewVisible}
|
|
|
|
title={previewTitle}
|
|
|
|
footer={null}
|
|
|
|
onCancel={this.handleCancel}
|
|
|
|
>
|
2016-11-25 15:00:28 +08:00
|
|
|
<img alt="example" style={{ width: '100%' }} src={previewImage} />
|
2016-04-05 12:30:14 +08:00
|
|
|
</Modal>
|
2020-08-24 16:33:24 +08:00
|
|
|
</>
|
2016-04-05 12:30:14 +08:00
|
|
|
);
|
2016-11-25 15:00:28 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-15 16:44:07 +08:00
|
|
|
|
2016-11-25 15:00:28 +08:00
|
|
|
ReactDOM.render(<PicturesWall />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|