@hitachivantara/uikit-react-core
Version:
Core React components for the NEXT Design System.
83 lines (82 loc) • 3.37 kB
JavaScript
import { jsxs, jsx, Fragment } from "react/jsx-runtime";
import { useDefaultProps } from "@hitachivantara/uikit-react-utils";
import { HvIcon } from "../../icons.js";
import { convertUnits } from "../utils.js";
import { useClasses } from "./File.styles.js";
import { staticClasses } from "./File.styles.js";
import { HvProgressBar } from "../../ProgressBar/ProgressBar.js";
import { HvTypography } from "../../Typography/Typography.js";
import { HvIconButton } from "../../IconButton/IconButton.js";
const getStatusIcon = (classes, status) => {
switch (status) {
case "success":
return /* @__PURE__ */ jsx(HvIcon, { name: "Success", className: classes?.icon, color: "positive" });
case "fail":
return /* @__PURE__ */ jsx(HvIcon, { name: "Fail", className: classes?.icon, color: "negative" });
default:
return /* @__PURE__ */ jsx("div", { className: classes?.icon });
}
};
const getProgressText = (data, classes) => {
const hasFailed = data.status === "fail";
const inProgress = data.status === "progress";
return /* @__PURE__ */ jsxs(Fragment, { children: [
data.progress || data.size || data.errorMessage ? ` | ` : null,
inProgress && data.progress != null && /* @__PURE__ */ jsx(HvTypography, { variant: "label", children: `${convertUnits(data.progress)} / ` }),
!hasFailed && data.size && /* @__PURE__ */ jsx(HvTypography, { children: `${convertUnits(data.size)}` }),
hasFailed && data.errorMessage && /* @__PURE__ */ jsx(HvTypography, { className: classes?.fail, children: data.errorMessage })
] });
};
const getProgressBarWith = ({ size, progress }) => {
const width = progress != null && size != null ? Math.round(progress * 100 / size) : 0;
return width;
};
const HvFile = (props) => {
const {
classes: classesProp,
data,
onFileRemoved,
removeFileButtonLabel
} = useDefaultProps("HvFile", props);
const { classes, cx } = useClasses(classesProp);
const hasError = data.status === "fail";
const inProgress = data.status === "progress";
const progressText = getProgressText(data, classes);
const statusIcon = getStatusIcon(classes, data.status);
const currentProgress = getProgressBarWith(data);
return /* @__PURE__ */ jsxs("li", { className: classes.root, children: [
!hasError && inProgress && /* @__PURE__ */ jsx(
HvProgressBar,
{
classes: {
root: classes.progressbar,
progressBarContainer: cx(
classes.progressbarContainer,
classes.progressbarBack
)
},
value: currentProgress,
hideLabel: true
}
),
statusIcon,
/* @__PURE__ */ jsxs("div", { className: classes.nameText, children: [
/* @__PURE__ */ jsx(HvTypography, { noWrap: true, variant: "label", children: data.name }),
/* @__PURE__ */ jsx("span", { className: classes.progressTextContainer, children: progressText })
] }),
data.preview && /* @__PURE__ */ jsx("div", { className: classes.previewContainer, children: data.preview }),
/* @__PURE__ */ jsx(
HvIconButton,
{
title: removeFileButtonLabel,
className: classes.removeButton,
onClick: () => onFileRemoved?.(data),
children: /* @__PURE__ */ jsx(HvIcon, { name: "Close", size: "xs" })
}
)
] });
};
export {
HvFile,
staticClasses as fileClasses
};