|
@@ -3,63 +3,77 @@ import { Upload, Icon, Modal } from 'antd';
|
3
|
3
|
import './style.less';
|
4
|
4
|
import { uploaderProps } from '../../utils/upload';
|
5
|
5
|
|
6
|
|
-function getBase64(file) {
|
7
|
|
- return new Promise((resolve, reject) => {
|
8
|
|
- const reader = new FileReader();
|
9
|
|
- reader.readAsDataURL(file);
|
10
|
|
- reader.onload = () => resolve(reader.result);
|
11
|
|
- reader.onerror = error => reject(error);
|
12
|
|
- });
|
13
|
|
-}
|
14
|
|
-
|
15
|
6
|
class ImageListUpload extends React.Component {
|
16
|
7
|
state = {
|
17
|
8
|
previewVisible: false,
|
18
|
9
|
previewImage: '',
|
19
|
|
- fileList: [],
|
|
10
|
+ loadding: false,
|
20
|
11
|
};
|
21
|
12
|
|
|
13
|
+ getFileList = () => {
|
|
14
|
+ return (this.props.value || []).map((img, inx) => ({ uid: inx, url: img, status: 'done' }))
|
|
15
|
+ }
|
|
16
|
+
|
22
|
17
|
handleCancel = () => this.setState({ previewVisible: false });
|
23
|
18
|
|
24
|
19
|
handlePreview = async file => {
|
25
|
|
- if (!file.url && !file.preview) {
|
26
|
|
- file.preview = await getBase64(file.originFileObj);
|
27
|
|
- }
|
28
|
|
-
|
29
|
20
|
this.setState({
|
30
|
|
- previewImage: file.url || file.preview,
|
|
21
|
+ previewImage: file.url ,
|
31
|
22
|
previewVisible: true,
|
32
|
23
|
});
|
33
|
24
|
};
|
34
|
25
|
|
35
|
|
- handleChange = ({ fileList }) => {
|
36
|
|
- this.setState({ fileList })
|
37
|
|
- if (typeof this.props.onChange === 'function') {
|
38
|
|
- // return item.response.url
|
39
|
|
- const images = fileList.filter(item => item.status === 'done')
|
40
|
|
- this.props.onChange(images.map(item => item.response.url))
|
|
26
|
+ handleChange = (e) => {
|
|
27
|
+ if (e.file.status === "uploading") {
|
|
28
|
+ this.setState({ loading: true });
|
|
29
|
+ return;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ if (e.file.state === 'removed') {
|
|
33
|
+ const fileList = (this.props.value || []).filter(x => x != e.file.url);
|
|
34
|
+ this.props.onChange(fileList);
|
41
|
35
|
}
|
|
36
|
+
|
|
37
|
+ // if (e.file.status === "done") {
|
|
38
|
+ // this.setState({
|
|
39
|
+ // loading: false,
|
|
40
|
+ // })
|
|
41
|
+
|
|
42
|
+ // if (e.file.response && e.file.response.url) {
|
|
43
|
+ // if (typeof this.props.onChange === 'function') {
|
|
44
|
+ // const fileList = this.getFileList()
|
|
45
|
+ // this.props.onChange([...fileList || [], e.file.response.url]);
|
|
46
|
+ // }
|
|
47
|
+ // }
|
|
48
|
+ // }
|
42
|
49
|
};
|
43
|
50
|
|
44
|
|
- render() {
|
45
|
|
- const { previewVisible, previewImage, fileList } = this.state;
|
|
51
|
+ handleUploadSucess = (url) => {
|
|
52
|
+ this.setState({ loading: false });
|
|
53
|
+ if (typeof this.props.onChange === 'function') {
|
|
54
|
+ const fileList = this.props.value || [];
|
|
55
|
+ this.props.onChange([...fileList, url]);
|
|
56
|
+ }
|
|
57
|
+ }
|
46
|
58
|
|
47
|
|
- const { value } = this.props
|
|
59
|
+ render() {
|
|
60
|
+ const { previewVisible, previewImage } = this.state;
|
|
61
|
+ const fileList = this.getFileList();
|
48
|
62
|
|
49
|
63
|
const uploadButton = (
|
50
|
64
|
<div>
|
51
|
|
- <Icon type="plus" />
|
52
|
|
- <div className="ant-upload-text">Upload</div>
|
|
65
|
+ <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"} />
|
53
|
66
|
</div>
|
54
|
67
|
);
|
55
|
68
|
return (
|
56
|
69
|
<div className="clearfix">
|
57
|
70
|
<Upload
|
58
|
71
|
listType="picture-card"
|
59
|
|
- fileList={fileList || value}
|
|
72
|
+ fileList={fileList}
|
60
|
73
|
onPreview={this.handlePreview}
|
61
|
74
|
onChange={this.handleChange}
|
62
|
75
|
{ ...uploaderProps }
|
|
76
|
+ onSuccess={this.handleUploadSucess}
|
63
|
77
|
>
|
64
|
78
|
{fileList.length >= 8 ? null : uploadButton}
|
65
|
79
|
</Upload>
|