@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
123 lines (122 loc) • 5.58 kB
JavaScript
"use client";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _UploadInfo, _UploadFileList;
const _excluded = ["buttonTextSingular", "textSingular"],
_excluded2 = ["id", "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';
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,
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 UploadWrapper = disableDragAndDrop ? HeightAnimation : UploadDropzone;
return React.createElement(UploadContext.Provider, {
value: _objectSpread(_objectSpread({}, extendedProps), {}, {
id,
onInputUpload
})
}, React.createElement(Provider, {
skeleton: skeleton
}, React.createElement(UploadWrapper, {
className: classnames('dnb-upload', spacingClasses, className)
}, _UploadInfo || (_UploadInfo = React.createElement(UploadInfo, null)), React.createElement(UploadFileInput, props), _UploadFileList || (_UploadFileList = React.createElement(UploadFileList, null)))));
};
Upload.useUpload = useUpload;
Upload._formElement = true;
Upload._supportsSpacingProps = true;
export default Upload;
//# sourceMappingURL=Upload.js.map