UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

126 lines (125 loc) 4.23 kB
"use client"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; 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, { useEffect, useRef } from 'react'; import { UploadContext } from './UploadContext'; import UploadFileListCell from './UploadFileListCell'; import useUpload from './useUpload'; import { isAsync } from '../../shared/helpers/isAsync'; function UploadFileList() { const context = React.useContext(UploadContext); const { id, fileListAriaLabel, deleteButton, download, allowDuplicates, loadingText, onFileDelete, onFileClick, onChange } = context; const { files, setFiles, setInternalFiles } = useUpload(id); const filesRef = useRef(null); useEffect(() => { filesRef.current = files; }, [files]); if (files === null || files.length < 1) { return null; } const removeFile = fileToBeRemoved => { return filesRef.current.filter(fileListElement => fileListElement.file != fileToBeRemoved.file); }; const updateFile = (fileToBeUpdated, props) => { return filesRef.current.map(fileListElement => { const idIsSame = fileListElement.id && fileListElement.id === fileToBeUpdated.id; const fileIsSame = fileListElement.file && fileListElement.file === fileToBeUpdated.file; return idIsSame || fileIsSame ? _objectSpread(_objectSpread({}, fileListElement), props) : fileListElement; }); }; const updateFiles = updatedFiles => { setFiles(updatedFiles); setInternalFiles(updatedFiles); if (typeof onChange === 'function') { onChange({ files: updatedFiles }); } }; const handleDeleteAsync = async uploadFile => { updateFiles(updateFile(uploadFile, { isLoading: true, errorMessage: null })); try { await onFileDelete({ fileItem: uploadFile }); updateFiles(removeFile(uploadFile)); } catch (error) { updateFiles(updateFile(uploadFile, { isLoading: false, errorMessage: error.message })); } }; const handleFileClickAsync = async uploadFile => { updateFiles(updateFile(uploadFile, { isLoading: true })); await onFileClick({ fileItem: uploadFile }); updateFiles(updateFile(uploadFile, { isLoading: false })); }; return React.createElement("ul", { className: "dnb-upload__file-list", "aria-label": fileListAriaLabel }, files.map((uploadFile, index) => { const onDeleteHandler = async () => { if (typeof onFileDelete === 'function') { if (isAsync(onFileDelete)) { handleDeleteAsync(uploadFile); } else { onFileDelete({ fileItem: uploadFile }); updateFiles(removeFile(uploadFile)); } } else { updateFiles(removeFile(uploadFile)); } }; const onFileClickHandler = async () => { if (typeof onFileClick === 'function') { if (isAsync(onFileClick)) { handleFileClickAsync(uploadFile); } else { onFileClick({ fileItem: uploadFile }); } } }; return React.createElement(UploadFileListCell, { key: index, id: id, uploadFile: uploadFile, onDelete: onDeleteHandler, onClick: onFileClick && onFileClickHandler, deleteButtonText: deleteButton, loadingText: loadingText, download: download, allowDuplicates: allowDuplicates }); })); } export default UploadFileList; //# sourceMappingURL=UploadFileList.js.map