react-dropzone 4.2.10 → 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
 
@@ -384,14 +388,14 @@ var Dropzone = function (_React$Component) {
384
388
  if (noStyles) {
385
389
  style = styles.default;
386
390
  activeStyle = styles.active;
387
- acceptStyle = style.active;
391
+ acceptStyle = styles.active;
388
392
  rejectStyle = styles.rejected;
389
393
  disabledStyle = styles.disabled;
390
394
  }
391
395
 
392
- var appliedStyle = _extends({}, style);
396
+ var appliedStyle = _extends({ position: 'relative' }, style);
393
397
  if (activeStyle && isDragActive) {
394
- appliedStyle = _extends({}, style, activeStyle);
398
+ appliedStyle = _extends({}, appliedStyle, activeStyle);
395
399
  }
396
400
  if (acceptStyle && isDragAccept) {
397
401
  appliedStyle = _extends({}, appliedStyle, acceptStyle);
@@ -400,14 +404,22 @@ var Dropzone = function (_React$Component) {
400
404
  appliedStyle = _extends({}, appliedStyle, rejectStyle);
401
405
  }
402
406
  if (disabledStyle && disabled) {
403
- appliedStyle = _extends({}, style, disabledStyle);
407
+ appliedStyle = _extends({}, appliedStyle, disabledStyle);
404
408
  }
405
409
 
406
410
  var inputAttributes = {
407
411
  accept: accept,
408
412
  disabled: disabled,
409
413
  type: 'file',
410
- style: { display: 'none' },
414
+ style: _extends({
415
+ position: 'absolute',
416
+ top: 0,
417
+ right: 0,
418
+ bottom: 0,
419
+ left: 0,
420
+ opacity: 0.00001,
421
+ pointerEvents: 'none'
422
+ }, inputProps.style),
411
423
  multiple: supportMultiple && multiple,
412
424
  ref: this.setRefs,
413
425
  onChange: this.onDrop,
@@ -429,7 +441,8 @@ var Dropzone = function (_React$Component) {
429
441
  onFileDialogCancel = props.onFileDialogCancel,
430
442
  maxSize = props.maxSize,
431
443
  minSize = props.minSize,
432
- 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']);
433
446
 
434
447
  return React.createElement(
435
448
  'div',
@@ -465,7 +478,7 @@ Dropzone.propTypes = {
465
478
  * Windows. In some cases there might not be a mime type set at all.
466
479
  * See: https://github.com/react-dropzone/react-dropzone/issues/276
467
480
  */
468
- accept: PropTypes.string,
481
+ accept: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
469
482
 
470
483
  /**
471
484
  * Contents of the dropzone
@@ -567,6 +580,13 @@ Dropzone.propTypes = {
567
580
  */
568
581
  disabledStyle: PropTypes.object,
569
582
 
583
+ /**
584
+ * getDataTransferItems handler
585
+ * @param {Event} event
586
+ * @returns {Array} array of File objects
587
+ */
588
+ getDataTransferItems: PropTypes.func,
589
+
570
590
  /**
571
591
  * onClick callback
572
592
  * @param {Event} event
@@ -619,7 +639,9 @@ Dropzone.defaultProps = {
619
639
  disabled: false,
620
640
  disablePreview: false,
621
641
  disableClick: false,
642
+ inputProps: {},
622
643
  multiple: true,
623
644
  maxSize: Infinity,
624
- minSize: 0
645
+ minSize: 0,
646
+ getDataTransferItems: defaultGetDataTransferItem
625
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
  }