forked from react-dropzone/react-dropzone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·197 lines (166 loc) · 4.45 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var React = require('react');
var accept = require('attr-accept');
var Dropzone = React.createClass({
getDefaultProps: function() {
return {
disableClick: false,
multiple: true
};
},
getInitialState: function() {
return {
isDragActive: false
};
},
propTypes: {
onDrop: React.PropTypes.func,
onDropAccepted: React.PropTypes.func,
onDropRejected: React.PropTypes.func,
onDragEnter: React.PropTypes.func,
onDragLeave: React.PropTypes.func,
style: React.PropTypes.object,
activeStyle: React.PropTypes.object,
className: React.PropTypes.string,
activeClassName: React.PropTypes.string,
rejectClassName: React.PropTypes.string,
disableClick: React.PropTypes.bool,
multiple: React.PropTypes.bool,
accept: React.PropTypes.string,
},
allFilesAccepted: function(files) {
return files.every(file => accept(file, this.props.accept))
},
onDragEnter: function(e) {
e.preventDefault();
// This is tricky. During the drag even the dataTransfer.files is null
// But Chrome implements some drag store, which is accesible via dataTransfer.items
var dataTransferItems = e.dataTransfer && e.dataTransfer.items ? e.dataTransfer.items : [];
// Now we need to convert the DataTransferList to Array
var itemsArray = Array.prototype.slice.call(dataTransferItems);
var allFilesAccepted = this.allFilesAccepted(itemsArray);
this.setState({
isDragActive: allFilesAccepted,
isDragReject: !allFilesAccepted
});
if (this.props.onDragEnter) {
this.props.onDragEnter(e);
}
},
onDragOver: function (e) {
e.preventDefault();
},
onDragLeave: function(e) {
e.preventDefault();
this.setState({
isDragActive: false,
isDragReject: false
});
if (this.props.onDragLeave) {
this.props.onDragLeave(e);
}
},
onDrop: function(e) {
e.preventDefault();
this.setState({
isDragActive: false,
isDragReject: false
});
var droppedFiles = e.dataTransfer ? e.dataTransfer.files : e.target.files;
var max = this.props.multiple ? droppedFiles.length : 1;
var files = [];
for (var i = 0; i < max; i++) {
var file = droppedFiles[i];
file.preview = URL.createObjectURL(file);
files.push(file);
}
if (this.props.onDrop) {
this.props.onDrop(files, e);
}
if (this.allFilesAccepted(files)) {
if (this.props.onDropAccepted) {
this.props.onDropAccepted(files, e);
}
} else {
if (this.props.onDropRejected) {
this.props.onDropRejected(files, e);
}
}
},
onClick: function () {
if (!this.props.disableClick) {
this.open();
}
},
open: function() {
var fileInput = React.findDOMNode(this.refs.fileInput);
fileInput.value = null;
fileInput.click();
},
render: function() {
var className;
if (this.props.className) {
className = this.props.className;
if (this.state.isDragActive) {
className += ' ' + this.props.activeClassName;
};
if (this.state.isDragReject) {
className += ' ' + this.props.rejectClassName;
};
};
var style, activeStyle;
if (this.props.style || this.props.activeStyle) {
if (this.props.style) {
style = this.props.style;
}
if (this.props.activeStyle) {
activeStyle = this.props.activeStyle;
}
} else if (!className) {
style = {
width: 200,
height: 200,
borderWidth: 2,
borderColor: '#666',
borderStyle: 'dashed',
borderRadius: 5,
};
activeStyle = {
borderStyle: 'solid',
backgroundColor: '#eee'
};
}
var appliedStyle;
if (activeStyle && this.state.isDragActive) {
appliedStyle = {
...style,
...activeStyle
};
} else {
appliedStyle = {
...style
};
};
return (
<div
className={className}
style={appliedStyle}
onClick={this.onClick}
onDragEnter={this.onDragEnter}
onDragOver={this.onDragOver}
onDragLeave={this.onDragLeave}
onDrop={this.onDrop}
>
{this.props.children}
<input
type='file'
ref='fileInput'
style={{ display: 'none' }}
multiple={this.props.multiple}
accept={this.props.accept}
onChange={this.onDrop}
/>
</div>
);
}
});
module.exports = Dropzone;