forked from plotly/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpload.react.js
98 lines (91 loc) · 3.18 KB
/
Upload.react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React, {Component} from 'react';
import Dropzone from 'react-dropzone';
import {propTypes} from '../components/Upload.react';
import LoadingElement from '../utils/LoadingElement';
export default class Upload extends Component {
constructor() {
super();
this.onDrop = this.onDrop.bind(this);
}
onDrop(files) {
const {multiple, setProps} = this.props;
const newProps = {
contents: [],
filename: [],
last_modified: [],
};
files.forEach(file => {
const reader = new FileReader();
reader.onload = () => {
/*
* I'm not sure if reader.onload will be executed in order.
* For example, if the 1st file is larger than the 2nd one,
* the 2nd file might load first.
*/
newProps.contents.push(reader.result);
newProps.filename.push(file.name);
// eslint-disable-next-line no-magic-numbers
newProps.last_modified.push(file.lastModified / 1000);
if (newProps.contents.length === files.length) {
if (multiple) {
setProps(newProps);
} else {
setProps({
contents: newProps.contents[0],
filename: newProps.filename[0],
last_modified: newProps.last_modified[0],
});
}
}
};
reader.readAsDataURL(file);
});
}
render() {
const {
id,
children,
accept,
disabled,
disable_click,
max_size,
min_size,
multiple,
className,
className_active,
className_reject,
className_disabled,
style,
style_active,
style_reject,
style_disabled,
} = this.props;
const activeStyle = className_active ? undefined : style_active;
const disabledStyle = className_disabled ? undefined : style_disabled;
const rejectStyle = className_reject ? undefined : style_reject;
return (
<LoadingElement id={id}>
<Dropzone
onDrop={this.onDrop}
accept={accept}
disabled={disabled}
disableClick={disable_click}
maxSize={max_size === -1 ? Infinity : max_size}
minSize={min_size}
multiple={multiple}
className={className}
activeClassName={className_active}
rejectClassName={className_reject}
disabledClassName={className_disabled}
style={style}
activeStyle={activeStyle}
rejectStyle={rejectStyle}
disabledStyle={disabledStyle}
>
{children}
</Dropzone>
</LoadingElement>
);
}
}
Upload.propTypes = propTypes;