123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972 |
- var _excluded = ["children"],
- _excluded2 = ["open"],
- _excluded3 = ["refKey", "role", "onKeyDown", "onFocus", "onBlur", "onClick", "onDragEnter", "onDragOver", "onDragLeave", "onDrop"],
- _excluded4 = ["refKey", "onChange", "onClick"];
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
- /* 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>
- * ```
- */
- var Dropzone = /*#__PURE__*/forwardRef(function (_ref, ref) {
- var children = _ref.children,
- params = _objectWithoutProperties(_ref, _excluded);
- var _useDropzone = useDropzone(params),
- open = _useDropzone.open,
- props = _objectWithoutProperties(_useDropzone, _excluded2);
- useImperativeHandle(ref, function () {
- return {
- open: open
- };
- }, [open]); // TODO: Figure out why react-styleguidist cannot create docs if we don't return a jsx element
- return /*#__PURE__*/React.createElement(Fragment, null, children(_objectSpread(_objectSpread({}, props), {}, {
- open: open
- })));
- });
- Dropzone.displayName = "Dropzone"; // Add default props for react-docgen
- var 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,
- autoFocus: false
- };
- 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,
- /**
- * Set to true to focus the root element on render
- */
- autoFocus: 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. It must return null if there's no errors.
- * @param {File} file
- * @returns {FileError|FileError[]|null}
- */
- 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
- */
- var 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 {boolean} autoFocus Set to true to auto focus the root element.
- * @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() {
- var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var _defaultProps$props = _objectSpread(_objectSpread({}, defaultProps), props),
- accept = _defaultProps$props.accept,
- disabled = _defaultProps$props.disabled,
- getFilesFromEvent = _defaultProps$props.getFilesFromEvent,
- maxSize = _defaultProps$props.maxSize,
- minSize = _defaultProps$props.minSize,
- multiple = _defaultProps$props.multiple,
- maxFiles = _defaultProps$props.maxFiles,
- onDragEnter = _defaultProps$props.onDragEnter,
- onDragLeave = _defaultProps$props.onDragLeave,
- onDragOver = _defaultProps$props.onDragOver,
- onDrop = _defaultProps$props.onDrop,
- onDropAccepted = _defaultProps$props.onDropAccepted,
- onDropRejected = _defaultProps$props.onDropRejected,
- onFileDialogCancel = _defaultProps$props.onFileDialogCancel,
- onFileDialogOpen = _defaultProps$props.onFileDialogOpen,
- useFsAccessApi = _defaultProps$props.useFsAccessApi,
- autoFocus = _defaultProps$props.autoFocus,
- preventDropOnDocument = _defaultProps$props.preventDropOnDocument,
- noClick = _defaultProps$props.noClick,
- noKeyboard = _defaultProps$props.noKeyboard,
- noDrag = _defaultProps$props.noDrag,
- noDragEventsBubbling = _defaultProps$props.noDragEventsBubbling,
- onError = _defaultProps$props.onError,
- validator = _defaultProps$props.validator;
- var acceptAttr = useMemo(function () {
- return acceptPropAsAcceptAttr(accept);
- }, [accept]);
- var pickerTypes = useMemo(function () {
- return pickerOptionsFromAccept(accept);
- }, [accept]);
- var onFileDialogOpenCb = useMemo(function () {
- return typeof onFileDialogOpen === "function" ? onFileDialogOpen : noop;
- }, [onFileDialogOpen]);
- var onFileDialogCancelCb = useMemo(function () {
- return typeof onFileDialogCancel === "function" ? onFileDialogCancel : noop;
- }, [onFileDialogCancel]);
- /**
- * @constant
- * @type {React.MutableRefObject<HTMLElement>}
- */
- var rootRef = useRef(null);
- var inputRef = useRef(null);
- var _useReducer = useReducer(reducer, initialState),
- _useReducer2 = _slicedToArray(_useReducer, 2),
- state = _useReducer2[0],
- dispatch = _useReducer2[1];
- var isFocused = state.isFocused,
- isFileDialogActive = state.isFileDialogActive;
- var fsAccessApiWorksRef = useRef(typeof window !== "undefined" && window.isSecureContext && useFsAccessApi && canUseFileSystemAccessAPI()); // Update file dialog active state when the window is focused on
- var onWindowFocus = function onWindowFocus() {
- // Execute the timeout only if the file dialog is opened in the browser
- if (!fsAccessApiWorksRef.current && isFileDialogActive) {
- setTimeout(function () {
- if (inputRef.current) {
- var files = inputRef.current.files;
- if (!files.length) {
- dispatch({
- type: "closeDialog"
- });
- onFileDialogCancelCb();
- }
- }
- }, 300);
- }
- };
- useEffect(function () {
- window.addEventListener("focus", onWindowFocus, false);
- return function () {
- window.removeEventListener("focus", onWindowFocus, false);
- };
- }, [inputRef, isFileDialogActive, onFileDialogCancelCb, fsAccessApiWorksRef]);
- var dragTargetsRef = useRef([]);
- var onDocumentDrop = function 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(function () {
- if (preventDropOnDocument) {
- document.addEventListener("dragover", onDocumentDragOver, false);
- document.addEventListener("drop", onDocumentDrop, false);
- }
- return function () {
- if (preventDropOnDocument) {
- document.removeEventListener("dragover", onDocumentDragOver);
- document.removeEventListener("drop", onDocumentDrop);
- }
- };
- }, [rootRef, preventDropOnDocument]); // Auto focus the root when autoFocus is true
- useEffect(function () {
- if (!disabled && autoFocus && rootRef.current) {
- rootRef.current.focus();
- }
- return function () {};
- }, [rootRef, autoFocus, disabled]);
- var onErrCb = useCallback(function (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]);
- var onDragEnterCb = useCallback(function (event) {
- event.preventDefault(); // Persist here because we need the event later after getFilesFromEvent() is done
- event.persist();
- stopPropagation(event);
- dragTargetsRef.current = [].concat(_toConsumableArray(dragTargetsRef.current), [event.target]);
- if (isEvtWithFiles(event)) {
- Promise.resolve(getFilesFromEvent(event)).then(function (files) {
- if (isPropagationStopped(event) && !noDragEventsBubbling) {
- return;
- }
- var fileCount = files.length;
- var isDragAccept = fileCount > 0 && allFilesAccepted({
- files: files,
- accept: acceptAttr,
- minSize: minSize,
- maxSize: maxSize,
- multiple: multiple,
- maxFiles: maxFiles,
- validator: validator
- });
- var isDragReject = fileCount > 0 && !isDragAccept;
- dispatch({
- isDragAccept: isDragAccept,
- isDragReject: isDragReject,
- isDragActive: true,
- type: "setDraggedFiles"
- });
- if (onDragEnter) {
- onDragEnter(event);
- }
- }).catch(function (e) {
- return onErrCb(e);
- });
- }
- }, [getFilesFromEvent, onDragEnter, onErrCb, noDragEventsBubbling, acceptAttr, minSize, maxSize, multiple, maxFiles, validator]);
- var onDragOverCb = useCallback(function (event) {
- event.preventDefault();
- event.persist();
- stopPropagation(event);
- var hasFiles = isEvtWithFiles(event);
- if (hasFiles && event.dataTransfer) {
- try {
- event.dataTransfer.dropEffect = "copy";
- } catch (_unused) {}
- /* eslint-disable-line no-empty */
- }
- if (hasFiles && onDragOver) {
- onDragOver(event);
- }
- return false;
- }, [onDragOver, noDragEventsBubbling]);
- var onDragLeaveCb = useCallback(function (event) {
- event.preventDefault();
- event.persist();
- stopPropagation(event); // Only deactivate once the dropzone and all children have been left
- var targets = dragTargetsRef.current.filter(function (target) {
- return 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)
- var 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]);
- var setFiles = useCallback(function (files, event) {
- var acceptedFiles = [];
- var fileRejections = [];
- files.forEach(function (file) {
- var _fileAccepted = fileAccepted(file, acceptAttr),
- _fileAccepted2 = _slicedToArray(_fileAccepted, 2),
- accepted = _fileAccepted2[0],
- acceptError = _fileAccepted2[1];
- var _fileMatchSize = fileMatchSize(file, minSize, maxSize),
- _fileMatchSize2 = _slicedToArray(_fileMatchSize, 2),
- sizeMatch = _fileMatchSize2[0],
- sizeError = _fileMatchSize2[1];
- var customErrors = validator ? validator(file) : null;
- if (accepted && sizeMatch && !customErrors) {
- acceptedFiles.push(file);
- } else {
- var errors = [acceptError, sizeError];
- if (customErrors) {
- errors = errors.concat(customErrors);
- }
- fileRejections.push({
- file: file,
- errors: errors.filter(function (e) {
- return e;
- })
- });
- }
- });
- if (!multiple && acceptedFiles.length > 1 || multiple && maxFiles >= 1 && acceptedFiles.length > maxFiles) {
- // Reject everything and empty accepted files
- acceptedFiles.forEach(function (file) {
- fileRejections.push({
- file: file,
- errors: [TOO_MANY_FILES_REJECTION]
- });
- });
- acceptedFiles.splice(0);
- }
- dispatch({
- acceptedFiles: acceptedFiles,
- fileRejections: 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]);
- var onDropCb = useCallback(function (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(function (files) {
- if (isPropagationStopped(event) && !noDragEventsBubbling) {
- return;
- }
- setFiles(files, event);
- }).catch(function (e) {
- return onErrCb(e);
- });
- }
- dispatch({
- type: "reset"
- });
- }, [getFilesFromEvent, setFiles, onErrCb, noDragEventsBubbling]); // Fn for opening the file dialog programmatically
- var openFileDialog = useCallback(function () {
- // 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
- var opts = {
- multiple: multiple,
- types: pickerTypes
- };
- window.showOpenFilePicker(opts).then(function (handles) {
- return getFilesFromEvent(handles);
- }).then(function (files) {
- setFiles(files, null);
- dispatch({
- type: "closeDialog"
- });
- }).catch(function (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(new Error("Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no <input> was provided."));
- }
- } 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
- var onKeyDownCb = useCallback(function (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
- var onFocusCb = useCallback(function () {
- dispatch({
- type: "focus"
- });
- }, []);
- var onBlurCb = useCallback(function () {
- dispatch({
- type: "blur"
- });
- }, []); // Cb to open the file dialog when click occurs on the dropzone
- var onClickCb = useCallback(function () {
- 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]);
- var composeHandler = function composeHandler(fn) {
- return disabled ? null : fn;
- };
- var composeKeyboardHandler = function composeKeyboardHandler(fn) {
- return noKeyboard ? null : composeHandler(fn);
- };
- var composeDragHandler = function composeDragHandler(fn) {
- return noDrag ? null : composeHandler(fn);
- };
- var stopPropagation = function stopPropagation(event) {
- if (noDragEventsBubbling) {
- event.stopPropagation();
- }
- };
- var getRootProps = useMemo(function () {
- return function () {
- var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
- _ref2$refKey = _ref2.refKey,
- refKey = _ref2$refKey === void 0 ? "ref" : _ref2$refKey,
- role = _ref2.role,
- onKeyDown = _ref2.onKeyDown,
- onFocus = _ref2.onFocus,
- onBlur = _ref2.onBlur,
- onClick = _ref2.onClick,
- onDragEnter = _ref2.onDragEnter,
- onDragOver = _ref2.onDragOver,
- onDragLeave = _ref2.onDragLeave,
- onDrop = _ref2.onDrop,
- rest = _objectWithoutProperties(_ref2, _excluded3);
- return _objectSpread(_objectSpread(_defineProperty({
- 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]);
- var onInputElementClick = useCallback(function (event) {
- event.stopPropagation();
- }, []);
- var getInputProps = useMemo(function () {
- return function () {
- var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
- _ref3$refKey = _ref3.refKey,
- refKey = _ref3$refKey === void 0 ? "ref" : _ref3$refKey,
- onChange = _ref3.onChange,
- onClick = _ref3.onClick,
- rest = _objectWithoutProperties(_ref3, _excluded4);
- var inputProps = _defineProperty({
- accept: acceptAttr,
- multiple: multiple,
- type: "file",
- style: {
- display: "none"
- },
- onChange: composeHandler(composeEventHandlers(onChange, onDropCb)),
- onClick: composeHandler(composeEventHandlers(onClick, onInputElementClick)),
- tabIndex: -1
- }, refKey, inputRef);
- return _objectSpread(_objectSpread({}, inputProps), rest);
- };
- }, [inputRef, accept, multiple, onDropCb, disabled]);
- return _objectSpread(_objectSpread({}, state), {}, {
- isFocused: isFocused && !disabled,
- getRootProps: getRootProps,
- getInputProps: getInputProps,
- rootRef: rootRef,
- inputRef: 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 _objectSpread(_objectSpread({}, state), {}, {
- isFocused: true
- });
- case "blur":
- return _objectSpread(_objectSpread({}, state), {}, {
- isFocused: false
- });
- case "openDialog":
- return _objectSpread(_objectSpread({}, initialState), {}, {
- isFileDialogActive: true
- });
- case "closeDialog":
- return _objectSpread(_objectSpread({}, state), {}, {
- isFileDialogActive: false
- });
- case "setDraggedFiles":
- return _objectSpread(_objectSpread({}, state), {}, {
- isDragActive: action.isDragActive,
- isDragAccept: action.isDragAccept,
- isDragReject: action.isDragReject
- });
- case "setFiles":
- return _objectSpread(_objectSpread({}, state), {}, {
- acceptedFiles: action.acceptedFiles,
- fileRejections: action.fileRejections
- });
- case "reset":
- return _objectSpread({}, initialState);
- default:
- return state;
- }
- }
- function noop() {}
- export { ErrorCode } from "./utils";
|