UNPKG

@braineet/ui

Version:

Braineet design system

72 lines (71 loc) 2.64 kB
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /* eslint-disable react/no-array-index-key */ import React from 'react'; import { nanoid } from 'nanoid'; import { useDropzone } from 'react-dropzone'; import { UPLOAD_ITEM_STATUS } from './useUploadState/constants'; import { useUploadStore } from './useUploadStore'; import { jsx as _jsx } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var assignRef = function assignRef(ref, value) { if (typeof ref === 'function') { ref(value); } else if (typeof ref === 'object' && ref !== null && 'current' in ref) { // eslint-disable-next-line no-param-reassign ref.current = value; } }; var UploadRenderer = /*#__PURE__*/React.forwardRef(function (_ref, ref) { var state = _ref.state, children = _ref.children, options = _ref.options, openRef = _ref.openRef, inputProps = _ref.inputProps; /** hooks */ var store = useUploadStore(state); var uid = React.useMemo(function () { return nanoid(); }, []); var disabled = store.disabled, readonly = store.readonly; /** handles */ var onDrop = function onDrop(droppedFiles) { var newFilesAdded = store.addItems(droppedFiles, { status: UPLOAD_ITEM_STATUS.UPLOADING }); store.uploadItems(newFilesAdded); }; var _useDropzone = useDropzone(_extends({ disabled: disabled || readonly, onDrop: onDrop, useFsAccessApi: false }, options)), getRootProps = _useDropzone.getRootProps, getInputProps = _useDropzone.getInputProps, isDragActive = _useDropzone.isDragActive, isFocused = _useDropzone.isFocused, open = _useDropzone.open; React.useEffect(function () { assignRef(openRef, open); }, []); return /*#__PURE__*/_jsxs(_Fragment, { children: [children && !readonly ? /*#__PURE__*/React.cloneElement(children, _extends({ disabled: disabled, isDragActive: isDragActive, isFocused: isFocused }, getRootProps())) : null, /*#__PURE__*/_jsx("input", _extends({ ref: ref, type: "file", autoComplete: "off", style: { display: 'none' }, "aria-hidden": "true" }, inputProps, getInputProps()), uid)] }); }); UploadRenderer.defaultProps = { options: {} }; export default UploadRenderer;