ant-design/components/upload/demo/picture-card.md

130 lines
3.2 KiB
Markdown
Raw Normal View History

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
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
import { Upload, Modal } from 'antd';
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: '',
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',
},
{
uid: '-5',
name: 'image.png',
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,
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() {
const { previewVisible, previewImage, fileList, previewTitle } = this.state;
2016-11-25 15:00:28 +08:00
const uploadButton = (
<div>
<PlusOutlined />
2016-11-25 15:00:28 +08:00
<div className="ant-upload-text">Upload</div>
</div>
);
2016-04-05 12:30:14 +08:00
return (
<div className="clearfix">
2016-11-25 15:00:28 +08:00
<Upload
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>
<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>
</div>
);
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
```
2016-01-15 16:44:07 +08:00
2019-05-07 14:57:32 +08:00
```css
2016-08-25 09:46:19 +08:00
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
2016-01-15 16:44:07 +08:00
color: #999;
font-size: 32px;
2016-01-15 16:44:07 +08:00
}
.ant-upload-select-picture-card .ant-upload-text {
2016-01-15 16:44:07 +08:00
margin-top: 8px;
color: #666;
}
2019-05-07 14:57:32 +08:00
```