-
-
Notifications
You must be signed in to change notification settings - Fork 780
/
index.js
executable file
·1000 lines (904 loc) · 29.4 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint prefer-template: 0 */
import React, {
forwardRef,
Fragment,
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useReducer,
useRef,
} from "react";
import PropTypes from "prop-types";
import { fromEvent } from "file-selector";
import {
acceptPropAsAcceptAttr,
allFilesAccepted,
composeEventHandlers,
fileAccepted,
fileMatchSize,
canUseFileSystemAccessAPI,
isAbort,
isEvtWithFiles,
isIeOrEdge,
isPropagationStopped,
isSecurityError,
onDocumentDragOver,
pickerOptionsFromAccept,
TOO_MANY_FILES_REJECTION,
} from "./utils/index";
/**
* Convenience wrapper component for the `useDropzone` hook
*
* ```jsx
* <Dropzone>
* {({getRootProps, getInputProps}) => (
* <div {...getRootProps()}>
* <input {...getInputProps()} />
* <p>Drag 'n' drop some files here, or click to select files</p>
* </div>
* )}
* </Dropzone>
* ```
*/
const Dropzone = forwardRef(({ children, ...params }, ref) => {
const { open, ...props } = useDropzone(params);
useImperativeHandle(ref, () => ({ open }), [open]);
// TODO: Figure out why react-styleguidist cannot create docs if we don't return a jsx element
return <Fragment>{children({ ...props, open })}</Fragment>;
});
Dropzone.displayName = "Dropzone";
// Add default props for react-docgen
const defaultProps = {
disabled: false,
getFilesFromEvent: fromEvent,
maxSize: Infinity,
minSize: 0,
multiple: true,
maxFiles: 0,
preventDropOnDocument: true,
noClick: false,
noKeyboard: false,
noDrag: false,
noDragEventsBubbling: false,
validator: null,
useFsAccessApi: true,
};
Dropzone.defaultProps = defaultProps;
Dropzone.propTypes = {
/**
* Render function that exposes the dropzone state and prop getter fns
*
* @param {object} params
* @param {Function} params.getRootProps Returns the props you should apply to the root drop container you render
* @param {Function} params.getInputProps Returns the props you should apply to hidden file input you render
* @param {Function} params.open Open the native file selection dialog
* @param {boolean} params.isFocused Dropzone area is in focus
* @param {boolean} params.isFileDialogActive File dialog is opened
* @param {boolean} params.isDragActive Active drag is in progress
* @param {boolean} params.isDragAccept Dragged files are accepted
* @param {boolean} params.isDragReject Some dragged files are rejected
* @param {File[]} params.acceptedFiles Accepted files
* @param {FileRejection[]} params.fileRejections Rejected files and why they were rejected
*/
children: PropTypes.func,
/**
* Set accepted file types.
* Checkout https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker types option for more information.
* Keep in mind that mime type determination is not reliable across platforms. CSV files,
* for example, are reported as text/plain under macOS but as application/vnd.ms-excel under
* Windows. In some cases there might not be a mime type set at all (https://github.com/react-dropzone/react-dropzone/issues/276).
*/
accept: PropTypes.objectOf(PropTypes.arrayOf(PropTypes.string)),
/**
* Allow drag 'n' drop (or selection from the file dialog) of multiple files
*/
multiple: PropTypes.bool,
/**
* If false, allow dropped items to take over the current browser window
*/
preventDropOnDocument: PropTypes.bool,
/**
* If true, disables click to open the native file selection dialog
*/
noClick: PropTypes.bool,
/**
* If true, disables SPACE/ENTER to open the native file selection dialog.
* Note that it also stops tracking the focus state.
*/
noKeyboard: PropTypes.bool,
/**
* If true, disables drag 'n' drop
*/
noDrag: PropTypes.bool,
/**
* If true, stops drag event propagation to parents
*/
noDragEventsBubbling: PropTypes.bool,
/**
* Minimum file size (in bytes)
*/
minSize: PropTypes.number,
/**
* Maximum file size (in bytes)
*/
maxSize: PropTypes.number,
/**
* Maximum accepted number of files
* The default value is 0 which means there is no limitation to how many files are accepted.
*/
maxFiles: PropTypes.number,
/**
* Enable/disable the dropzone
*/
disabled: PropTypes.bool,
/**
* Use this to provide a custom file aggregator
*
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
*/
getFilesFromEvent: PropTypes.func,
/**
* Cb for when closing the file dialog with no selection
*/
onFileDialogCancel: PropTypes.func,
/**
* Cb for when opening the file dialog
*/
onFileDialogOpen: PropTypes.func,
/**
* Set to true to use the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API
* to open the file picker instead of using an `<input type="file">` click event.
*/
useFsAccessApi: PropTypes.bool,
/**
* Cb for when the `dragenter` event occurs.
*
* @param {DragEvent} event
*/
onDragEnter: PropTypes.func,
/**
* Cb for when the `dragleave` event occurs
*
* @param {DragEvent} event
*/
onDragLeave: PropTypes.func,
/**
* Cb for when the `dragover` event occurs
*
* @param {DragEvent} event
*/
onDragOver: PropTypes.func,
/**
* Cb for when the `drop` event occurs.
* Note that this callback is invoked after the `getFilesFromEvent` callback is done.
*
* Files are accepted or rejected based on the `accept`, `multiple`, `minSize` and `maxSize` props.
* `accept` must be a valid [MIME type](http://www.iana.org/assignments/media-types/media-types.xhtml) according to [input element specification](https://www.w3.org/wiki/HTML/Elements/input/file) or a valid file extension.
* If `multiple` is set to false and additional files are dropped,
* all files besides the first will be rejected.
* Any file which does not have a size in the [`minSize`, `maxSize`] range, will be rejected as well.
*
* Note that the `onDrop` callback will always be invoked regardless if the dropped files were accepted or rejected.
* If you'd like to react to a specific scenario, use the `onDropAccepted`/`onDropRejected` props.
*
* `onDrop` will provide you with an array of [File](https://developer.mozilla.org/en-US/docs/Web/API/File) objects which you can then process and send to a server.
* For example, with [SuperAgent](https://github.com/visionmedia/superagent) as a http/ajax library:
*
* ```js
* function onDrop(acceptedFiles) {
* const req = request.post('/upload')
* acceptedFiles.forEach(file => {
* req.attach(file.name, file)
* })
* req.end(callback)
* }
* ```
*
* @param {File[]} acceptedFiles
* @param {FileRejection[]} fileRejections
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
*/
onDrop: PropTypes.func,
/**
* Cb for when the `drop` event occurs.
* Note that if no files are accepted, this callback is not invoked.
*
* @param {File[]} files
* @param {(DragEvent|Event)} event
*/
onDropAccepted: PropTypes.func,
/**
* Cb for when the `drop` event occurs.
* Note that if no files are rejected, this callback is not invoked.
*
* @param {FileRejection[]} fileRejections
* @param {(DragEvent|Event)} event
*/
onDropRejected: PropTypes.func,
/**
* Cb for when there's some error from any of the promises.
*
* @param {Error} error
*/
onError: PropTypes.func,
/**
* Custom validation function
* @param {File} file
* @returns {FileError|FileError[]}
*/
validator: PropTypes.func,
};
export default Dropzone;
/**
* A function that is invoked for the `dragenter`,
* `dragover` and `dragleave` events.
* It is not invoked if the items are not files (such as link, text, etc.).
*
* @callback dragCb
* @param {DragEvent} event
*/
/**
* A function that is invoked for the `drop` or input change event.
* It is not invoked if the items are not files (such as link, text, etc.).
*
* @callback dropCb
* @param {File[]} acceptedFiles List of accepted files
* @param {FileRejection[]} fileRejections List of rejected files and why they were rejected
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
*/
/**
* A function that is invoked for the `drop` or input change event.
* It is not invoked if the items are files (such as link, text, etc.).
*
* @callback dropAcceptedCb
* @param {File[]} files List of accepted files that meet the given criteria
* (`accept`, `multiple`, `minSize`, `maxSize`)
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
*/
/**
* A function that is invoked for the `drop` or input change event.
*
* @callback dropRejectedCb
* @param {File[]} files List of rejected files that do not meet the given criteria
* (`accept`, `multiple`, `minSize`, `maxSize`)
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
*/
/**
* A function that is used aggregate files,
* in a asynchronous fashion, from drag or input change events.
*
* @callback getFilesFromEvent
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
* @returns {(File[]|Promise<File[]>)}
*/
/**
* An object with the current dropzone state.
*
* @typedef {object} DropzoneState
* @property {boolean} isFocused Dropzone area is in focus
* @property {boolean} isFileDialogActive File dialog is opened
* @property {boolean} isDragActive Active drag is in progress
* @property {boolean} isDragAccept Dragged files are accepted
* @property {boolean} isDragReject Some dragged files are rejected
* @property {File[]} acceptedFiles Accepted files
* @property {FileRejection[]} fileRejections Rejected files and why they were rejected
*/
/**
* An object with the dropzone methods.
*
* @typedef {object} DropzoneMethods
* @property {Function} getRootProps Returns the props you should apply to the root drop container you render
* @property {Function} getInputProps Returns the props you should apply to hidden file input you render
* @property {Function} open Open the native file selection dialog
*/
const initialState = {
isFocused: false,
isFileDialogActive: false,
isDragActive: false,
isDragAccept: false,
isDragReject: false,
acceptedFiles: [],
fileRejections: [],
};
/**
* A React hook that creates a drag 'n' drop area.
*
* ```jsx
* function MyDropzone(props) {
* const {getRootProps, getInputProps} = useDropzone({
* onDrop: acceptedFiles => {
* // do something with the File objects, e.g. upload to some server
* }
* });
* return (
* <div {...getRootProps()}>
* <input {...getInputProps()} />
* <p>Drag and drop some files here, or click to select files</p>
* </div>
* )
* }
* ```
*
* @function useDropzone
*
* @param {object} props
* @param {import("./utils").AcceptProp} [props.accept] Set accepted file types.
* Checkout https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker types option for more information.
* Keep in mind that mime type determination is not reliable across platforms. CSV files,
* for example, are reported as text/plain under macOS but as application/vnd.ms-excel under
* Windows. In some cases there might not be a mime type set at all (https://github.com/react-dropzone/react-dropzone/issues/276).
* @param {boolean} [props.multiple=true] Allow drag 'n' drop (or selection from the file dialog) of multiple files
* @param {boolean} [props.preventDropOnDocument=true] If false, allow dropped items to take over the current browser window
* @param {boolean} [props.noClick=false] If true, disables click to open the native file selection dialog
* @param {boolean} [props.noKeyboard=false] If true, disables SPACE/ENTER to open the native file selection dialog.
* Note that it also stops tracking the focus state.
* @param {boolean} [props.noDrag=false] If true, disables drag 'n' drop
* @param {boolean} [props.noDragEventsBubbling=false] If true, stops drag event propagation to parents
* @param {number} [props.minSize=0] Minimum file size (in bytes)
* @param {number} [props.maxSize=Infinity] Maximum file size (in bytes)
* @param {boolean} [props.disabled=false] Enable/disable the dropzone
* @param {getFilesFromEvent} [props.getFilesFromEvent] Use this to provide a custom file aggregator
* @param {Function} [props.onFileDialogCancel] Cb for when closing the file dialog with no selection
* @param {boolean} [props.useFsAccessApi] Set to true to use the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API
* to open the file picker instead of using an `<input type="file">` click event.
* @param {Function} [props.onFileDialogOpen] Cb for when opening the file dialog
* @param {dragCb} [props.onDragEnter] Cb for when the `dragenter` event occurs.
* @param {dragCb} [props.onDragLeave] Cb for when the `dragleave` event occurs
* @param {dragCb} [props.onDragOver] Cb for when the `dragover` event occurs
* @param {dropCb} [props.onDrop] Cb for when the `drop` event occurs.
* Note that this callback is invoked after the `getFilesFromEvent` callback is done.
*
* Files are accepted or rejected based on the `accept`, `multiple`, `minSize` and `maxSize` props.
* `accept` must be an object with keys as a valid [MIME type](http://www.iana.org/assignments/media-types/media-types.xhtml) according to [input element specification](https://www.w3.org/wiki/HTML/Elements/input/file) and the value an array of file extensions (optional).
* If `multiple` is set to false and additional files are dropped,
* all files besides the first will be rejected.
* Any file which does not have a size in the [`minSize`, `maxSize`] range, will be rejected as well.
*
* Note that the `onDrop` callback will always be invoked regardless if the dropped files were accepted or rejected.
* If you'd like to react to a specific scenario, use the `onDropAccepted`/`onDropRejected` props.
*
* `onDrop` will provide you with an array of [File](https://developer.mozilla.org/en-US/docs/Web/API/File) objects which you can then process and send to a server.
* For example, with [SuperAgent](https://github.com/visionmedia/superagent) as a http/ajax library:
*
* ```js
* function onDrop(acceptedFiles) {
* const req = request.post('/upload')
* acceptedFiles.forEach(file => {
* req.attach(file.name, file)
* })
* req.end(callback)
* }
* ```
* @param {dropAcceptedCb} [props.onDropAccepted]
* @param {dropRejectedCb} [props.onDropRejected]
* @param {(error: Error) => void} [props.onError]
*
* @returns {DropzoneState & DropzoneMethods}
*/
export function useDropzone(props = {}) {
const {
accept,
disabled,
getFilesFromEvent,
maxSize,
minSize,
multiple,
maxFiles,
onDragEnter,
onDragLeave,
onDragOver,
onDrop,
onDropAccepted,
onDropRejected,
onFileDialogCancel,
onFileDialogOpen,
useFsAccessApi,
preventDropOnDocument,
noClick,
noKeyboard,
noDrag,
noDragEventsBubbling,
onError,
validator,
} = {
...defaultProps,
...props,
};
const acceptAttr = useMemo(() => acceptPropAsAcceptAttr(accept), [accept]);
const pickerTypes = useMemo(() => pickerOptionsFromAccept(accept), [accept]);
const onFileDialogOpenCb = useMemo(
() => (typeof onFileDialogOpen === "function" ? onFileDialogOpen : noop),
[onFileDialogOpen]
);
const onFileDialogCancelCb = useMemo(
() =>
typeof onFileDialogCancel === "function" ? onFileDialogCancel : noop,
[onFileDialogCancel]
);
const rootRef = useRef(null);
const inputRef = useRef(null);
const [state, dispatch] = useReducer(reducer, initialState);
const { isFocused, isFileDialogActive } = state;
const fsAccessApiWorksRef = useRef(
typeof window !== "undefined" &&
window.isSecureContext &&
useFsAccessApi &&
canUseFileSystemAccessAPI()
);
// Update file dialog active state when the window is focused on
const onWindowFocus = () => {
// Execute the timeout only if the file dialog is opened in the browser
if (!fsAccessApiWorksRef.current && isFileDialogActive) {
setTimeout(() => {
if (inputRef.current) {
const { files } = inputRef.current;
if (!files.length) {
dispatch({ type: "closeDialog" });
onFileDialogCancelCb();
}
}
}, 300);
}
};
useEffect(() => {
window.addEventListener("focus", onWindowFocus, false);
return () => {
window.removeEventListener("focus", onWindowFocus, false);
};
}, [inputRef, isFileDialogActive, onFileDialogCancelCb, fsAccessApiWorksRef]);
const dragTargetsRef = useRef([]);
const onDocumentDrop = (event) => {
if (rootRef.current && rootRef.current.contains(event.target)) {
// If we intercepted an event for our instance, let it propagate down to the instance's onDrop handler
return;
}
event.preventDefault();
dragTargetsRef.current = [];
};
useEffect(() => {
if (preventDropOnDocument) {
document.addEventListener("dragover", onDocumentDragOver, false);
document.addEventListener("drop", onDocumentDrop, false);
}
return () => {
if (preventDropOnDocument) {
document.removeEventListener("dragover", onDocumentDragOver);
document.removeEventListener("drop", onDocumentDrop);
}
};
}, [rootRef, preventDropOnDocument]);
const onErrCb = useCallback(
(e) => {
if (onError) {
onError(e);
} else {
// Let the user know something's gone wrong if they haven't provided the onError cb.
console.error(e);
}
},
[onError]
);
const onDragEnterCb = useCallback(
(event) => {
event.preventDefault();
// Persist here because we need the event later after getFilesFromEvent() is done
event.persist();
stopPropagation(event);
dragTargetsRef.current = [...dragTargetsRef.current, event.target];
if (isEvtWithFiles(event)) {
Promise.resolve(getFilesFromEvent(event))
.then((files) => {
if (isPropagationStopped(event) && !noDragEventsBubbling) {
return;
}
const fileCount = files.length;
const isDragAccept =
fileCount > 0 &&
allFilesAccepted({
files,
accept: acceptAttr,
minSize,
maxSize,
multiple,
maxFiles,
});
const isDragReject = fileCount > 0 && !isDragAccept;
dispatch({
isDragAccept,
isDragReject,
isDragActive: true,
type: "setDraggedFiles",
});
if (onDragEnter) {
onDragEnter(event);
}
})
.catch((e) => onErrCb(e));
}
},
[
getFilesFromEvent,
onDragEnter,
onErrCb,
noDragEventsBubbling,
acceptAttr,
minSize,
maxSize,
multiple,
maxFiles,
]
);
const onDragOverCb = useCallback(
(event) => {
event.preventDefault();
event.persist();
stopPropagation(event);
const hasFiles = isEvtWithFiles(event);
if (hasFiles && event.dataTransfer) {
try {
event.dataTransfer.dropEffect = "copy";
} catch {} /* eslint-disable-line no-empty */
}
if (hasFiles && onDragOver) {
onDragOver(event);
}
return false;
},
[onDragOver, noDragEventsBubbling]
);
const onDragLeaveCb = useCallback(
(event) => {
event.preventDefault();
event.persist();
stopPropagation(event);
// Only deactivate once the dropzone and all children have been left
const targets = dragTargetsRef.current.filter(
(target) => rootRef.current && rootRef.current.contains(target)
);
// Make sure to remove a target present multiple times only once
// (Firefox may fire dragenter/dragleave multiple times on the same element)
const targetIdx = targets.indexOf(event.target);
if (targetIdx !== -1) {
targets.splice(targetIdx, 1);
}
dragTargetsRef.current = targets;
if (targets.length > 0) {
return;
}
dispatch({
type: "setDraggedFiles",
isDragActive: false,
isDragAccept: false,
isDragReject: false,
});
if (isEvtWithFiles(event) && onDragLeave) {
onDragLeave(event);
}
},
[rootRef, onDragLeave, noDragEventsBubbling]
);
const setFiles = useCallback(
(files, event) => {
const acceptedFiles = [];
const fileRejections = [];
files.forEach((file) => {
const [accepted, acceptError] = fileAccepted(file, acceptAttr);
const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
const customErrors = validator ? validator(file) : null;
if (accepted && sizeMatch && !customErrors) {
acceptedFiles.push(file);
} else {
let errors = [acceptError, sizeError];
if (customErrors) {
errors = errors.concat(customErrors);
}
fileRejections.push({ file, errors: errors.filter((e) => e) });
}
});
if (
(!multiple && acceptedFiles.length > 1) ||
(multiple && maxFiles >= 1 && acceptedFiles.length > maxFiles)
) {
// Reject everything and empty accepted files
acceptedFiles.forEach((file) => {
fileRejections.push({ file, errors: [TOO_MANY_FILES_REJECTION] });
});
acceptedFiles.splice(0);
}
dispatch({
acceptedFiles,
fileRejections,
type: "setFiles",
});
if (onDrop) {
onDrop(acceptedFiles, fileRejections, event);
}
if (fileRejections.length > 0 && onDropRejected) {
onDropRejected(fileRejections, event);
}
if (acceptedFiles.length > 0 && onDropAccepted) {
onDropAccepted(acceptedFiles, event);
}
},
[
dispatch,
multiple,
acceptAttr,
minSize,
maxSize,
maxFiles,
onDrop,
onDropAccepted,
onDropRejected,
validator,
]
);
const onDropCb = useCallback(
(event) => {
event.preventDefault();
// Persist here because we need the event later after getFilesFromEvent() is done
event.persist();
stopPropagation(event);
dragTargetsRef.current = [];
if (isEvtWithFiles(event)) {
Promise.resolve(getFilesFromEvent(event))
.then((files) => {
if (isPropagationStopped(event) && !noDragEventsBubbling) {
return;
}
setFiles(files, event);
})
.catch((e) => onErrCb(e));
}
dispatch({ type: "reset" });
},
[getFilesFromEvent, setFiles, onErrCb, noDragEventsBubbling]
);
// Fn for opening the file dialog programmatically
const openFileDialog = useCallback(() => {
// No point to use FS access APIs if context is not secure
// https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts#feature_detection
if (fsAccessApiWorksRef.current) {
dispatch({ type: "openDialog" });
onFileDialogOpenCb();
// https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker
const opts = {
multiple,
types: pickerTypes,
};
window
.showOpenFilePicker(opts)
.then((handles) => getFilesFromEvent(handles))
.then((files) => {
setFiles(files, null);
dispatch({ type: "closeDialog" });
})
.catch((e) => {
// AbortError means the user canceled
if (isAbort(e)) {
onFileDialogCancelCb(e);
dispatch({ type: "closeDialog" });
} else if (isSecurityError(e)) {
fsAccessApiWorksRef.current = false;
// CORS, so cannot use this API
// Try using the input
if (inputRef.current) {
inputRef.current.value = null;
inputRef.current.click();
}
} else {
onErrCb(e);
}
});
return;
}
if (inputRef.current) {
dispatch({ type: "openDialog" });
onFileDialogOpenCb();
inputRef.current.value = null;
inputRef.current.click();
}
}, [
dispatch,
onFileDialogOpenCb,
onFileDialogCancelCb,
useFsAccessApi,
setFiles,
onErrCb,
pickerTypes,
multiple,
]);
// Cb to open the file dialog when SPACE/ENTER occurs on the dropzone
const onKeyDownCb = useCallback(
(event) => {
// Ignore keyboard events bubbling up the DOM tree
if (!rootRef.current || !rootRef.current.isEqualNode(event.target)) {
return;
}
if (
event.key === " " ||
event.key === "Enter" ||
event.keyCode === 32 ||
event.keyCode === 13
) {
event.preventDefault();
openFileDialog();
}
},
[rootRef, openFileDialog]
);
// Update focus state for the dropzone
const onFocusCb = useCallback(() => {
dispatch({ type: "focus" });
}, []);
const onBlurCb = useCallback(() => {
dispatch({ type: "blur" });
}, []);
// Cb to open the file dialog when click occurs on the dropzone
const onClickCb = useCallback(() => {
if (noClick) {
return;
}
// In IE11/Edge the file-browser dialog is blocking, therefore, use setTimeout()
// to ensure React can handle state changes
// See: https://github.com/react-dropzone/react-dropzone/issues/450
if (isIeOrEdge()) {
setTimeout(openFileDialog, 0);
} else {
openFileDialog();
}
}, [noClick, openFileDialog]);
const composeHandler = (fn) => {
return disabled ? null : fn;
};
const composeKeyboardHandler = (fn) => {
return noKeyboard ? null : composeHandler(fn);
};
const composeDragHandler = (fn) => {
return noDrag ? null : composeHandler(fn);
};
const stopPropagation = (event) => {
if (noDragEventsBubbling) {
event.stopPropagation();
}
};
const getRootProps = useMemo(
() =>
({
refKey = "ref",
role,
onKeyDown,
onFocus,
onBlur,
onClick,
onDragEnter,
onDragOver,
onDragLeave,
onDrop,
...rest
} = {}) => ({
onKeyDown: composeKeyboardHandler(
composeEventHandlers(onKeyDown, onKeyDownCb)
),
onFocus: composeKeyboardHandler(
composeEventHandlers(onFocus, onFocusCb)
),
onBlur: composeKeyboardHandler(composeEventHandlers(onBlur, onBlurCb)),
onClick: composeHandler(composeEventHandlers(onClick, onClickCb)),
onDragEnter: composeDragHandler(
composeEventHandlers(onDragEnter, onDragEnterCb)
),
onDragOver: composeDragHandler(
composeEventHandlers(onDragOver, onDragOverCb)
),
onDragLeave: composeDragHandler(
composeEventHandlers(onDragLeave, onDragLeaveCb)
),
onDrop: composeDragHandler(composeEventHandlers(onDrop, onDropCb)),
role: typeof role === "string" && role !== "" ? role : "presentation",
[refKey]: rootRef,
...(!disabled && !noKeyboard ? { tabIndex: 0 } : {}),
...rest,
}),
[
rootRef,
onKeyDownCb,
onFocusCb,
onBlurCb,
onClickCb,
onDragEnterCb,
onDragOverCb,
onDragLeaveCb,
onDropCb,
noKeyboard,
noDrag,
disabled,
]
);
const onInputElementClick = useCallback((event) => {
event.stopPropagation();
}, []);
const getInputProps = useMemo(
() =>
({ refKey = "ref", onChange, onClick, ...rest } = {}) => {
const inputProps = {
accept: acceptAttr,
multiple,
type: "file",
style: { display: "none" },
onChange: composeHandler(composeEventHandlers(onChange, onDropCb)),
onClick: composeHandler(
composeEventHandlers(onClick, onInputElementClick)
),
tabIndex: -1,
[refKey]: inputRef,
};
return {
...inputProps,
...rest,
};
},
[inputRef, accept, multiple, onDropCb, disabled]
);
return {
...state,
isFocused: isFocused && !disabled,
getRootProps,
getInputProps,
rootRef,
inputRef,
open: composeHandler(openFileDialog),
};
}
/**
* @param {DropzoneState} state
* @param {{type: string} & DropzoneState} action
* @returns {DropzoneState}
*/
function reducer(state, action) {
/* istanbul ignore next */
switch (action.type) {
case "focus":
return {
...state,
isFocused: true,
};
case "blur":
return {
...state,
isFocused: false,
};
case "openDialog":
return {
...initialState,
isFileDialogActive: true,
};
case "closeDialog":
return {
...state,
isFileDialogActive: false,
};
case "setDraggedFiles":
return {
...state,
isDragActive: action.isDragActive,
isDragAccept: action.isDragAccept,
isDragReject: action.isDragReject,
};
case "setFiles":
return {
...state,
acceptedFiles: action.acceptedFiles,
fileRejections: action.fileRejections,
};
case "reset":
return {
...initialState,
};
default:
return state;
}
}
function noop() {}
export { ErrorCode } from "./utils";