@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
120 lines (119 loc) • 3.56 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
import _URL from "core-js-pure/stable/url/index.js";
import React, { useMemo, useState } from 'react';
import classnames from 'classnames';
import { useValueProps } from "../../hooks/index.js";
import ValueBlock from "../../ValueBlock/index.js";
import ListFormat from "../../../../components/list-format/index.js";
import { getFileIcon } from "../../../../components/upload/UploadFileListCell.js";
import { BYTES_IN_A_MEGA_BYTE } from "../../../../components/upload/UploadVerify.js";
import { transformFiles } from "../../Field/Upload/Upload.js";
import { format } from "../../../../components/number-format/NumberUtils.js";
import { UploadFileLink } from "../../../../components/upload/UploadFileListLink.js";
import { isAsync } from "../../../../shared/helpers/isAsync.js";
function Upload(props) {
const preparedProps = {
fromExternal: transformFiles,
...props
};
const {
value,
format,
className,
variant = 'text',
listType,
download = false,
displaySize = false,
onFileClick,
...rest
} = useValueProps(preparedProps);
const list = useMemo(() => {
const valueToUse = (value === null || value === void 0 ? void 0 : value.map((uploadFile, index) => {
if (!uploadFile) {
return;
}
return React.createElement(UploadFileItem, {
key: index,
uploadFile: uploadFile,
download: download,
displaySize: displaySize,
onFileClick: onFileClick
});
})) || undefined;
if (valueToUse) {
return React.createElement(ListFormat, {
value: valueToUse,
format: format,
variant: variant,
listType: listType
});
}
}, [value, download, displaySize, onFileClick, format, variant, listType]);
return React.createElement(ValueBlock, _extends({
className: classnames('dnb-forms-value-upload', className)
}, rest), list);
}
function getSize(size) {
if (!size) {
return;
}
const sizeInMb = size / BYTES_IN_A_MEGA_BYTE;
return ` (${format(sizeInMb, {
decimals: 0
})} MB)`;
}
Upload._supportsSpacingProps = true;
export default Upload;
function UploadFileItem(props) {
const {
uploadFile,
download = false,
displaySize = false,
onFileClick
} = props;
const [loading, setLoading] = useState(false);
const {
file,
isLoading: fileIsLoading
} = uploadFile || {};
if (!file) {
return null;
}
const handleFileClickAsync = async uploadFile => {
setLoading(true);
try {
await onFileClick({
fileItem: uploadFile
});
} catch (error) {}
setLoading(false);
};
const onFileClickHandler = async () => {
if (typeof onFileClick === 'function') {
if (isAsync(onFileClick)) {
handleFileClickAsync(uploadFile);
} else {
onFileClick({
fileItem: uploadFile
});
}
}
};
const imageUrl = (file === null || file === void 0 ? void 0 : file.size) > 0 ? _URL.createObjectURL(file) : null;
const text = file.name + (displaySize ? ' ' + getSize(file.size) : '');
const isLoading = fileIsLoading || loading;
return React.createElement("span", {
className: "dnb-forms-value-upload__item"
}, getFileIcon(file, {
isLoading,
size: 'medium'
}, false), React.createElement(UploadFileLink, {
left: "x-small",
text: text,
href: imageUrl,
download: download,
onClick: onFileClick && onFileClickHandler
}));
}
//# sourceMappingURL=Upload.js.map