|
@@ -0,0 +1,113 @@
|
|
1
|
+import React from 'react';
|
|
2
|
+import { Upload, Icon, Modal } from 'antd';
|
|
3
|
+import './style.less';
|
|
4
|
+import { uploaderProps } from '../../utils/upload';
|
|
5
|
+import Item from './Item'
|
|
6
|
+
|
|
7
|
+/**
|
|
8
|
+ * unlimited 属性表示上传图片无限制
|
|
9
|
+ * 例子: <ImageListUpload unlimited/>
|
|
10
|
+ */
|
|
11
|
+class ImageListUpload extends React.Component {
|
|
12
|
+ state = {
|
|
13
|
+ previewVisible: false,
|
|
14
|
+ previewImage: '',
|
|
15
|
+ loadding: false,
|
|
16
|
+ };
|
|
17
|
+
|
|
18
|
+ getFileList = () => {
|
|
19
|
+ return (this.props.value || []).map((img, inx) => ({ uid: inx, url: img, status: 'done' }))
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ handleCancel = () => this.setState({ previewVisible: false });
|
|
23
|
+
|
|
24
|
+ handlePreview = async file => {
|
|
25
|
+ this.setState({
|
|
26
|
+ previewImage: file.url ,
|
|
27
|
+ previewVisible: true,
|
|
28
|
+ });
|
|
29
|
+ };
|
|
30
|
+
|
|
31
|
+ handleSticky = ({ url }) => {
|
|
32
|
+ const fileList = this.props.value || [];
|
|
33
|
+ this.props.onChange([url].concat(fileList.filter(x => x != url)))
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ handleDelete = ({ url }) => {
|
|
37
|
+ const fileList = this.props.value || [];
|
|
38
|
+ this.props.onChange(fileList.filter(x => x != url))
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ handleChange = (e) => {
|
|
42
|
+ if (e.file.status === "uploading") {
|
|
43
|
+ this.setState({ loading: true });
|
|
44
|
+ return;
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ const fileList = (this.props.value || []).filter(x => x != e.file.url);
|
|
48
|
+ this.props.onChange(fileList);
|
|
49
|
+
|
|
50
|
+ // console.log('删除图片触发了', e.file)
|
|
51
|
+ // if (e.file.state === 'removed') {
|
|
52
|
+ // const fileList = (this.props.value || []).filter(x => x != e.file.url);
|
|
53
|
+ // this.props.onChange(fileList);
|
|
54
|
+ // }
|
|
55
|
+
|
|
56
|
+ // if (e.file.status === "done") {
|
|
57
|
+ // this.setState({
|
|
58
|
+ // loading: false,
|
|
59
|
+ // })
|
|
60
|
+
|
|
61
|
+ // if (e.file.response && e.file.response.url) {
|
|
62
|
+ // if (typeof this.props.onChange === 'function') {
|
|
63
|
+ // const fileList = this.getFileList()
|
|
64
|
+ // this.props.onChange([...fileList || [], e.file.response.url]);
|
|
65
|
+ // }
|
|
66
|
+ // }
|
|
67
|
+ // }
|
|
68
|
+ };
|
|
69
|
+
|
|
70
|
+ handleUploadSucess = (url) => {
|
|
71
|
+ this.setState({ loading: false });
|
|
72
|
+ if (typeof this.props.onChange === 'function') {
|
|
73
|
+ const fileList = this.props.value || [];
|
|
74
|
+ this.props.onChange([...fileList, url]);
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ render() {
|
|
79
|
+ const { previewVisible, previewImage } = this.state;
|
|
80
|
+ const fileList = this.getFileList();
|
|
81
|
+
|
|
82
|
+ const uploadButton = (
|
|
83
|
+ <div>
|
|
84
|
+ <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"} />
|
|
85
|
+ </div>
|
|
86
|
+ );
|
|
87
|
+ return (
|
|
88
|
+ <div className="clearfix x-dargable-image-upload-list">
|
|
89
|
+ <div className="ant-upload-list ant-upload-list-picture-card">
|
|
90
|
+ {fileList.map(img => <Item key={img.url} dataset={img} onPreview={this.handlePreview} onSticky={this.handleSticky} onDelete={this.handleDelete}/>)}
|
|
91
|
+ </div>
|
|
92
|
+
|
|
93
|
+ <Upload
|
|
94
|
+ listType="picture-card"
|
|
95
|
+ showUploadList={false}
|
|
96
|
+ onPreview={this.handlePreview}
|
|
97
|
+ onChange={this.handleChange}
|
|
98
|
+ { ...uploaderProps }
|
|
99
|
+ onSuccess={this.handleUploadSucess}
|
|
100
|
+ >
|
|
101
|
+
|
|
102
|
+ {/* unlimited 表示上传无限制数量 */}
|
|
103
|
+ {(this.props.unlimited && uploadButton) || ((fileList || images).length >= 8 ? null : uploadButton)}
|
|
104
|
+ </Upload>
|
|
105
|
+ <Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
|
|
106
|
+ <img alt="example" style={{ width: '100%' }} src={previewImage} />
|
|
107
|
+ </Modal>
|
|
108
|
+ </div>
|
|
109
|
+ );
|
|
110
|
+ }
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+export default ImageListUpload;
|