|
@@ -1,81 +1,80 @@
|
1
|
1
|
import React from 'react';
|
2
|
|
-import { Upload, Icon, message, Modal } from 'antd';
|
|
2
|
+import { Upload, Icon, Modal } from 'antd';
|
3
|
3
|
import './style.less';
|
4
|
4
|
import { uploaderProps } from '../../utils/upload';
|
5
|
5
|
|
6
|
|
-
|
7
|
|
-
|
8
|
|
-class ImageUpload extends React.Component {
|
9
|
|
-
|
|
6
|
+/**
|
|
7
|
+ * unlimited 属性表示上传图片无限制
|
|
8
|
+ * 例子: <ImageListUpload unlimited/>
|
|
9
|
+ */
|
|
10
|
+class ImageListUpload extends React.Component {
|
10
|
11
|
state = {
|
11
|
|
- loading: false,
|
12
|
12
|
previewVisible: false,
|
13
|
13
|
previewImage: '',
|
14
|
|
- fileList: this.getPropsFileList(),
|
|
14
|
+ loadding: false,
|
15
|
15
|
};
|
16
|
16
|
|
17
|
|
- componentDidUpdate(prevProps) {
|
18
|
|
- // 直接 setState 会死循环
|
19
|
|
- if (prevProps.value !== this.props.value) {
|
20
|
|
- this.setState({ fileList: this.getPropsFileList() })
|
21
|
|
- }
|
22
|
|
- }
|
23
|
|
-
|
24
|
|
- getPropsFileList() {
|
25
|
|
- return !this.props.value ? [] : [{
|
26
|
|
- uid: '-1',
|
27
|
|
- name: 'image.png',
|
28
|
|
- status: 'done',
|
29
|
|
- url: this.props.value,
|
30
|
|
- }]
|
|
17
|
+ getFileList = () => {
|
|
18
|
+ return (this.props.value || []).map((img, inx) => ({ uid: inx, url: img, status: 'done' }))
|
31
|
19
|
}
|
32
|
20
|
|
33
|
|
-
|
34
|
|
- handleChange = ({ file, fileList }) => {
|
35
|
|
- const { status, response } = file
|
36
|
|
- this.setState({ fileList, loading: status === "uploading" })
|
37
|
|
-
|
38
|
|
- if (typeof this.props.onChange === 'function' && status != "uploading") {
|
39
|
|
- const image = status === 'done' ? response : undefined
|
40
|
|
- this.props.onChange(image);
|
41
|
|
- }
|
42
|
|
- };
|
43
|
|
-
|
44
|
21
|
handleCancel = () => this.setState({ previewVisible: false });
|
45
|
22
|
|
46
|
23
|
handlePreview = async file => {
|
47
|
|
- console.log(file)
|
48
|
24
|
this.setState({
|
49
|
|
- previewImage: file.url,
|
|
25
|
+ previewImage: file.url ,
|
50
|
26
|
previewVisible: true,
|
51
|
27
|
});
|
52
|
28
|
};
|
53
|
29
|
|
|
30
|
+ handleChange = (e) => {
|
|
31
|
+ if (e.file.status === "uploading") {
|
|
32
|
+ this.setState({ loading: true });
|
|
33
|
+ return;
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ const fileList = (this.props.value || []).filter(x => x != e.file.url);
|
|
37
|
+ this.props.onChange(fileList);
|
|
38
|
+
|
|
39
|
+ };
|
|
40
|
+
|
|
41
|
+ handleUploadSucess = (url) => {
|
|
42
|
+ this.setState({ loading: false });
|
|
43
|
+ if (typeof this.props.onChange === 'function') {
|
|
44
|
+ const fileList = this.props.value || [];
|
|
45
|
+ this.props.onChange([...fileList, url]);
|
|
46
|
+ }
|
|
47
|
+ }
|
|
48
|
+
|
54
|
49
|
render() {
|
|
50
|
+ const { previewVisible, previewImage } = this.state;
|
|
51
|
+ const fileList = this.getFileList();
|
|
52
|
+
|
55
|
53
|
const uploadButton = (
|
56
|
54
|
<div>
|
57
|
|
- <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"} />
|
|
55
|
+ <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"} />
|
58
|
56
|
</div>
|
59
|
57
|
);
|
60
|
|
-
|
61
|
58
|
return (
|
62
|
|
- <>
|
63
|
|
- <Upload
|
64
|
|
- {...uploaderProps}
|
65
|
|
- listType="picture-card"
|
66
|
|
- className="avatar-uploader"
|
67
|
|
- onChange={this.handleChange}
|
68
|
|
- fileList={this.state.fileList}
|
69
|
|
- onPreview={this.handlePreview}
|
70
|
|
- >
|
71
|
|
- {this.state.fileList.length >= 6 ? null : uploadButton}
|
72
|
|
- </Upload>
|
73
|
|
- <Modal visible={this.state.previewVisible} footer={null} onCancel={this.handleCancel}>
|
74
|
|
- <img alt="example" style={{ width: '100%' }} src={this.state.previewImage} />
|
75
|
|
- </Modal>
|
76
|
|
- </>
|
|
59
|
+ <div className="clearfix">
|
|
60
|
+ <Upload
|
|
61
|
+ listType="picture-card"
|
|
62
|
+ multiple={true}
|
|
63
|
+ fileList={fileList}
|
|
64
|
+ onPreview={this.handlePreview}
|
|
65
|
+ onChange={this.handleChange}
|
|
66
|
+ { ...uploaderProps }
|
|
67
|
+ onSuccess={this.handleUploadSucess}
|
|
68
|
+ >
|
|
69
|
+ {/* unlimited 表示上传无限制数量 */}
|
|
70
|
+ {(this.props.unlimited && uploadButton) || ((fileList || images).length >= 6 ? null : uploadButton)}
|
|
71
|
+ </Upload>
|
|
72
|
+ <Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
|
|
73
|
+ <img alt="example" style={{ width: '100%' }} src={previewImage} />
|
|
74
|
+ </Modal>
|
|
75
|
+ </div>
|
77
|
76
|
);
|
78
|
77
|
}
|
79
|
78
|
}
|
80
|
79
|
|
81
|
|
-export default ImageUpload;
|
|
80
|
+export default ImageListUpload;
|