react-dropzone 4.2.13 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/index.js CHANGED
@@ -16,7 +16,7 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
16
16
 
17
17
  import React from 'react';
18
18
  import PropTypes from 'prop-types';
19
- import { supportMultiple, fileAccepted, allFilesAccepted, fileMatchSize, onDocumentDragOver, getDataTransferItems, isIeOrEdge } from './utils';
19
+ import { supportMultiple, fileAccepted, allFilesAccepted, fileMatchSize, onDocumentDragOver, getDataTransferItems as defaultGetDataTransferItem, isIeOrEdge } from './utils';
20
20
  import styles from './utils/styles';
21
21
 
22
22
  var Dropzone = function (_React$Component) {
@@ -119,6 +119,8 @@ var Dropzone = function (_React$Component) {
119
119
  }, {
120
120
  key: 'onDragEnter',
121
121
  value: function onDragEnter(evt) {
122
+ var _this2 = this;
123
+
122
124
  evt.preventDefault();
123
125
 
124
126
  // Count the dropzone and any children that are entered.
@@ -126,11 +128,12 @@ var Dropzone = function (_React$Component) {
126
128
  this.dragTargets.push(evt.target);
127
129
  }
128
130
 
129
- this.setState({
130
- isDragActive: true, // Do not rely on files for the drag state. It doesn't work in Safari.
131
- draggedFiles: getDataTransferItems(evt)
131
+ Promise.resolve(this.props.getDataTransferItems(evt)).then(function (draggedFiles) {
132
+ _this2.setState({
133
+ isDragActive: true, // Do not rely on files for the drag state. It doesn't work in Safari.
134
+ draggedFiles: draggedFiles
135
+ });
132
136
  });
133
-
134
137
  if (this.props.onDragEnter) {
135
138
  this.props.onDragEnter.call(this, evt);
136
139
  }
@@ -158,13 +161,13 @@ var Dropzone = function (_React$Component) {
158
161
  }, {
159
162
  key: 'onDragLeave',
160
163
  value: function onDragLeave(evt) {
161
- var _this2 = this;
164
+ var _this3 = this;
162
165
 
163
166
  evt.preventDefault();
164
167
 
165
168
  // Only deactivate once the dropzone and all children have been left.
166
169
  this.dragTargets = this.dragTargets.filter(function (el) {
167
- return el !== evt.target && _this2.node.contains(el);
170
+ return el !== evt.target && _this3.node.contains(el);
168
171
  });
169
172
  if (this.dragTargets.length > 0) {
170
173
  return;
@@ -183,7 +186,7 @@ var Dropzone = function (_React$Component) {
183
186
  }, {
184
187
  key: 'onDrop',
185
188
  value: function onDrop(evt) {
186
- var _this3 = this;
189
+ var _this4 = this;
187
190
 
188
191
  var _props = this.props,
189
192
  onDrop = _props.onDrop,
@@ -191,64 +194,65 @@ var Dropzone = function (_React$Component) {
191
194
  onDropRejected = _props.onDropRejected,
192
195
  multiple = _props.multiple,
193
196
  disablePreview = _props.disablePreview,
194
- accept = _props.accept;
195
-
196
- var fileList = getDataTransferItems(evt);
197
- var acceptedFiles = [];
198
- var rejectedFiles = [];
197
+ accept = _props.accept,
198
+ getDataTransferItems = _props.getDataTransferItems;
199
199
 
200
200
  // Stop default browser behavior
201
+
201
202
  evt.preventDefault();
202
203
 
203
204
  // Reset the counter along with the drag on a drop.
204
205
  this.dragTargets = [];
205
206
  this.isFileDialogActive = false;
206
207
 
207
- fileList.forEach(function (file) {
208
- if (!disablePreview) {
209
- try {
210
- file.preview = window.URL.createObjectURL(file); // eslint-disable-line no-param-reassign
211
- } catch (err) {
212
- if (process.env.NODE_ENV !== 'production') {
213
- console.error('Failed to generate preview for file', file, err); // eslint-disable-line no-console
214
- }
215
- }
216
- }
208
+ // Clear files value
209
+ this.draggedFiles = null;
217
210
 
218
- if (fileAccepted(file, accept) && fileMatchSize(file, _this3.props.maxSize, _this3.props.minSize)) {
219
- acceptedFiles.push(file);
220
- } else {
221
- rejectedFiles.push(file);
222
- }
211
+ // Reset drag state
212
+ this.setState({
213
+ isDragActive: false,
214
+ draggedFiles: []
223
215
  });
224
216
 
225
- if (!multiple) {
226
- // if not in multi mode add any extra accepted files to rejected.
227
- // This will allow end users to easily ignore a multi file drop in "single" mode.
228
- rejectedFiles.push.apply(rejectedFiles, _toConsumableArray(acceptedFiles.splice(1)));
229
- }
217
+ Promise.resolve(getDataTransferItems(evt)).then(function (fileList) {
218
+ var acceptedFiles = [];
219
+ var rejectedFiles = [];
220
+
221
+ fileList.forEach(function (file) {
222
+ if (!disablePreview) {
223
+ try {
224
+ file.preview = window.URL.createObjectURL(file); // eslint-disable-line no-param-reassign
225
+ } catch (err) {
226
+ if (process.env.NODE_ENV !== 'production') {
227
+ console.error('Failed to generate preview for file', file, err); // eslint-disable-line no-console
228
+ }
229
+ }
230
+ }
230
231
 
231
- if (onDrop) {
232
- onDrop.call(this, acceptedFiles, rejectedFiles, evt);
233
- }
232
+ if (fileAccepted(file, accept) && fileMatchSize(file, _this4.props.maxSize, _this4.props.minSize)) {
233
+ acceptedFiles.push(file);
234
+ } else {
235
+ rejectedFiles.push(file);
236
+ }
237
+ });
234
238
 
235
- if (rejectedFiles.length > 0 && onDropRejected) {
236
- onDropRejected.call(this, rejectedFiles, evt);
237
- }
239
+ if (!multiple) {
240
+ // if not in multi mode add any extra accepted files to rejected.
241
+ // This will allow end users to easily ignore a multi file drop in "single" mode.
242
+ rejectedFiles.push.apply(rejectedFiles, _toConsumableArray(acceptedFiles.splice(1)));
243
+ }
238
244
 
239
- if (acceptedFiles.length > 0 && onDropAccepted) {
240
- onDropAccepted.call(this, acceptedFiles, evt);
241
- }
245
+ if (onDrop) {
246
+ onDrop.call(_this4, acceptedFiles, rejectedFiles, evt);
247
+ }
242
248
 
243
- // Clear files value
244
- this.draggedFiles = null;
249
+ if (rejectedFiles.length > 0 && onDropRejected) {
250
+ onDropRejected.call(_this4, rejectedFiles, evt);
251
+ }
245
252
 
246
- // Reset drag state
247
- this.setState({
248
- isDragActive: false,
249
- draggedFiles: [],
250
- acceptedFiles: acceptedFiles,
251
- rejectedFiles: rejectedFiles
253
+ if (acceptedFiles.length > 0 && onDropAccepted) {
254
+ onDropAccepted.call(_this4, acceptedFiles, evt);
255
+ }
252
256
  });
253
257
  }
254
258
  }, {
@@ -286,7 +290,7 @@ var Dropzone = function (_React$Component) {
286
290
  }, {
287
291
  key: 'onFileDialogCancel',
288
292
  value: function onFileDialogCancel() {
289
- var _this4 = this;
293
+ var _this5 = this;
290
294
 
291
295
  // timeout will not recognize context of this method
292
296
  var onFileDialogCancel = this.props.onFileDialogCancel;
@@ -294,13 +298,13 @@ var Dropzone = function (_React$Component) {
294
298
 
295
299
  if (this.isFileDialogActive) {
296
300
  setTimeout(function () {
297
- if (_this4.fileInputEl != null) {
301
+ if (_this5.fileInputEl != null) {
298
302
  // Returns an object as FileList
299
- var files = _this4.fileInputEl.files;
303
+ var files = _this5.fileInputEl.files;
300
304
 
301
305
 
302
306
  if (!files.length) {
303
- _this4.isFileDialogActive = false;
307
+ _this5.isFileDialogActive = false;
304
308
  }
305
309
  }
306
310
 
@@ -437,7 +441,8 @@ var Dropzone = function (_React$Component) {
437
441
  onFileDialogCancel = props.onFileDialogCancel,
438
442
  maxSize = props.maxSize,
439
443
  minSize = props.minSize,
440
- divProps = _objectWithoutProperties(props, ['acceptedFiles', 'preventDropOnDocument', 'disablePreview', 'disableClick', 'onDropAccepted', 'onDropRejected', 'onFileDialogCancel', 'maxSize', 'minSize']);
444
+ getDataTransferItems = props.getDataTransferItems,
445
+ divProps = _objectWithoutProperties(props, ['acceptedFiles', 'preventDropOnDocument', 'disablePreview', 'disableClick', 'onDropAccepted', 'onDropRejected', 'onFileDialogCancel', 'maxSize', 'minSize', 'getDataTransferItems']);
441
446
 
442
447
  return React.createElement(
443
448
  'div',
@@ -575,6 +580,13 @@ Dropzone.propTypes = {
575
580
  */
576
581
  disabledStyle: PropTypes.object,
577
582
 
583
+ /**
584
+ * getDataTransferItems handler
585
+ * @param {Event} event
586
+ * @returns {Array} array of File objects
587
+ */
588
+ getDataTransferItems: PropTypes.func,
589
+
578
590
  /**
579
591
  * onClick callback
580
592
  * @param {Event} event
@@ -630,5 +642,6 @@ Dropzone.defaultProps = {
630
642
  inputProps: {},
631
643
  multiple: true,
632
644
  maxSize: Infinity,
633
- minSize: 0
645
+ minSize: 0,
646
+ getDataTransferItems: defaultGetDataTransferItem
634
647
  };
@@ -6,6 +6,7 @@ export function getDataTransferItems(event) {
6
6
  var dataTransferItemsList = [];
7
7
  if (event.dataTransfer) {
8
8
  var dt = event.dataTransfer;
9
+
9
10
  if (dt.files && dt.files.length) {
10
11
  dataTransferItemsList = dt.files;
11
12
  } else if (dt.items && dt.items.length) {
@@ -16,6 +17,7 @@ export function getDataTransferItems(event) {
16
17
  } else if (event.target && event.target.files) {
17
18
  dataTransferItemsList = event.target.files;
18
19
  }
20
+
19
21
  // Convert from DataTransferItemsList to the native Array
20
22
  return Array.prototype.slice.call(dataTransferItemsList);
21
23
  }