UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

150 lines (149 loc) 6.69 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _UploadInfo, _UploadFileList, _br; const _excluded = ["buttonTextSingular", "textSingular"], _excluded2 = ["id", "variant", "skeleton", "className", "acceptedFileTypes", "filesAmountLimit", "fileMaxSize", "onChange", "onFileDelete", "onFileClick", "download", "allowDuplicates", "title", "text", "fileTypeTableCaption", "fileTypeDescription", "fileSizeDescription", "fileAmountDescription", "fileSizeContent", "buttonText", "loadingText", "errorLargeFile", "errorUnsupportedFile", "errorAmountLimit", "deleteButton", "fileListAriaLabel", "disableDragAndDrop"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { useCallback } from 'react'; import classnames from 'classnames'; import { createSpacingClasses } from '../space/SpacingHelper'; import Provider from '../../shared/Provider'; import Context from '../../shared/Context'; import { extendPropsWithContext, makeUniqueId } from '../../shared/component-helper'; import useId from '../../shared/helpers/useId'; import HeightAnimation from '../height-animation/HeightAnimation'; import UploadFileInput from './UploadFileInput'; import useUpload from './useUpload'; import UploadDropzone from './UploadDropzone'; import { UploadContext, defaultProps } from './UploadContext'; import { verifyFiles } from './UploadVerify'; import UploadFileList from './UploadFileList'; import UploadInfo from './UploadInfo'; import FormLabel from '../FormLabel'; export { defaultProps }; const Upload = localProps => { const context = React.useContext(Context); const _context$getTranslati = context.getTranslation(localProps).Upload, { buttonTextSingular, textSingular } = _context$getTranslati, translations = _objectWithoutProperties(_context$getTranslati, _excluded); if ((localProps === null || localProps === void 0 ? void 0 : localProps.filesAmountLimit) === 1) { translations.buttonText = buttonTextSingular; translations.text = textSingular; } const extendedProps = extendPropsWithContext(localProps, defaultProps, { skeleton: context === null || context === void 0 ? void 0 : context.skeleton }, translations, context.Upload); const { id: idProp, variant, skeleton, className, acceptedFileTypes, filesAmountLimit, fileMaxSize, onChange, onFileDelete, onFileClick, download, allowDuplicates, title, text, fileTypeTableCaption, fileTypeDescription, fileSizeDescription, fileAmountDescription, fileSizeContent, buttonText, loadingText, errorLargeFile, errorUnsupportedFile, errorAmountLimit, deleteButton, fileListAriaLabel, disableDragAndDrop } = extendedProps, props = _objectWithoutProperties(extendedProps, _excluded2); const spacingClasses = createSpacingClasses(props); const id = useId(idProp); const { files, setFiles, setInternalFiles, getExistingFile } = useUpload(id); const onInputUpload = useCallback(newFiles => { const mergedFiles = [...files, ...newFiles.map(fileItem => { const { file } = fileItem; const existingFile = getExistingFile(file, files); fileItem.exists = Boolean(existingFile); fileItem.id = fileItem.exists && !allowDuplicates ? existingFile.id : makeUniqueId(); return fileItem; })]; const filesToVerify = allowDuplicates ? mergedFiles : mergedFiles.filter(({ exists }) => !exists); const verifiedFiles = verifyFiles(filesToVerify, { fileMaxSize, acceptedFileTypes, errorUnsupportedFile, errorLargeFile }); const validFiles = [...verifiedFiles].slice(0, filesAmountLimit); setFiles(validFiles); setInternalFiles(mergedFiles); if (typeof onChange === 'function') { onChange({ files: validFiles }); } return validFiles; }, [acceptedFileTypes, errorLargeFile, errorUnsupportedFile, fileMaxSize, files, filesAmountLimit, getExistingFile, onChange, setFiles, setInternalFiles]); const wrapperIsHeightAnimation = disableDragAndDrop || props.disabled; const UploadWrapper = wrapperIsHeightAnimation ? HeightAnimation : UploadDropzone; return React.createElement(UploadContext.Provider, { value: _objectSpread(_objectSpread({}, extendedProps), {}, { id, onInputUpload }) }, React.createElement(Provider, { skeleton: skeleton }, React.createElement(UploadWrapper, _extends({ className: classnames(`dnb-upload dnb-upload--${variant || 'normal'}`, spacingClasses, className) }, !wrapperIsHeightAnimation ? { hideOutline: variant === 'compact' } : {}), variant !== 'compact' && (_UploadInfo || (_UploadInfo = React.createElement(UploadInfo, null))), variant === 'compact' && (title || text) && React.createElement(React.Fragment, null, React.createElement(CompactLabel, { id: id, title: title, text: text }), props === null || props === void 0 ? void 0 : props.children), React.createElement(UploadFileInput, _extends({ disabled: props.disabled, files: files }, props)), _UploadFileList || (_UploadFileList = React.createElement(UploadFileList, null))))); }; function CompactLabel(props) { const { id, title, text } = props; return React.createElement(FormLabel, { forId: `${id}-input`, vertical: true, className: "dnb-upload__label" }, React.createElement("span", null, title && React.createElement("span", null, title), title && text && (_br || (_br = React.createElement("br", null))), text && React.createElement("span", { className: "dnb-upload__text" }, text))); } Upload.useUpload = useUpload; Upload._formElement = true; Upload._supportsSpacingProps = true; export default Upload; //# sourceMappingURL=Upload.js.map