analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
108 lines (106 loc) • 3.48 kB
JavaScript
import {
Button_default
} from "./chunk-LAYB7IKW.mjs";
import {
Text_default
} from "./chunk-IMCIR6TJ.mjs";
import {
cn
} from "./chunk-53ICLDGS.mjs";
// src/components/FileAttachment/FileAttachment.tsx
import { useRef } from "react";
import { PaperclipIcon } from "@phosphor-icons/react/dist/csr/Paperclip";
import { FileTextIcon } from "@phosphor-icons/react/dist/csr/FileText";
import { XIcon } from "@phosphor-icons/react/dist/csr/X";
import { jsx, jsxs } from "react/jsx-runtime";
var formatFileSize = (bytes) => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
if (bytes < 1024 * 1024 * 1024)
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
};
var generateFileId = () => {
return crypto.randomUUID();
};
var FileAttachment = ({
files,
onFilesAdd,
onFileRemove,
readOnly = false,
buttonLabel = "Anexar",
multiple = true,
className,
hideButton = false
}) => {
const fileInputRef = useRef(null);
const handleFileChange = (event) => {
const selectedFiles = event.target.files;
if (selectedFiles && selectedFiles.length > 0) {
const filesArray = Array.from(selectedFiles);
onFilesAdd(filesArray);
}
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
};
const handleAttachClick = () => {
fileInputRef.current?.click();
};
return /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col gap-2", className), children: [
/* @__PURE__ */ jsx(
"input",
{
type: "file",
ref: fileInputRef,
className: "hidden",
onChange: handleFileChange,
multiple,
"aria-label": "Selecionar arquivos"
}
),
!readOnly && !hideButton && /* @__PURE__ */ jsxs(
Button_default,
{
type: "button",
variant: "outline",
size: "small",
onClick: handleAttachClick,
className: "self-start flex items-center gap-2",
children: [
/* @__PURE__ */ jsx(PaperclipIcon, { size: 16 }),
buttonLabel
]
}
),
files.length > 0 && /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: files.map((attachedFile) => /* @__PURE__ */ jsxs(
"div",
{
className: "flex items-center gap-2 bg-background-50 px-3 py-2 rounded-lg border border-border-100",
children: [
/* @__PURE__ */ jsx(FileTextIcon, { size: 16, className: "text-text-500 shrink-0" }),
/* @__PURE__ */ jsx(Text_default, { className: "text-sm text-text-700 truncate max-w-[200px]", children: attachedFile.file.name }),
/* @__PURE__ */ jsx(Text_default, { className: "text-xs text-text-400 shrink-0", children: formatFileSize(attachedFile.file.size) }),
!readOnly && /* @__PURE__ */ jsx(
"button",
{
type: "button",
onClick: () => onFileRemove(attachedFile.id),
className: "text-text-400 hover:text-error-500 transition-colors shrink-0",
"aria-label": `Remover ${attachedFile.file.name}`,
children: /* @__PURE__ */ jsx(XIcon, { size: 14 })
}
)
]
},
attachedFile.id
)) })
] });
};
var FileAttachment_default = FileAttachment;
export {
formatFileSize,
generateFileId,
FileAttachment_default
};
//# sourceMappingURL=chunk-WAAIPWQL.mjs.map