analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
167 lines (165 loc) • 5.18 kB
JavaScript
import {
Button_default
} from "./chunk-LAYB7IKW.mjs";
import {
Text_default
} from "./chunk-IMCIR6TJ.mjs";
import {
cn
} from "./chunk-53ICLDGS.mjs";
// src/components/ImageUpload/ImageUpload.tsx
import { useRef, useState } from "react";
import { ImageIcon } from "@phosphor-icons/react/dist/csr/Image";
import { PaperclipIcon } from "@phosphor-icons/react/dist/csr/Paperclip";
import { XIcon } from "@phosphor-icons/react/dist/csr/X";
import { jsx, jsxs } from "react/jsx-runtime";
function ImageUpload({
selectedFile,
onFileSelect,
onRemoveFile,
buttonText = "Inserir imagem",
buttonIcon,
accept = "image/*",
disabled = false,
className,
maxSize,
onSizeError,
onTypeError,
variant = "default"
}) {
const fileInputRef = useRef(null);
const [internalFile, setInternalFile] = useState(null);
const currentFile = selectedFile ?? internalFile;
const hasFile = currentFile !== null;
const handleButtonClick = () => {
if (!disabled) {
fileInputRef.current?.click();
}
};
const handleFileChange = (event) => {
const file = event.target.files?.[0];
if (!file) return;
const acceptedTypes = accept.split(",").map((type) => type.trim());
const isValidType = acceptedTypes.some((type) => {
if (type.endsWith("/*")) {
const mainType = type.split("/")[0];
return file.type.startsWith(mainType + "/");
}
if (type.startsWith(".")) {
return file.name.toLowerCase().endsWith(type.toLowerCase());
}
return file.type === type;
});
if (!isValidType) {
onTypeError?.(file);
return;
}
if (maxSize && file.size > maxSize) {
onSizeError?.(file, maxSize);
return;
}
if (selectedFile === void 0) {
setInternalFile(file);
}
onFileSelect?.(file);
event.target.value = "";
};
const handleRemoveFile = () => {
if (!disabled) {
if (selectedFile === void 0) {
setInternalFile(null);
}
onRemoveFile?.();
}
};
if (variant === "compact") {
return /* @__PURE__ */ jsxs("div", { className: cn("inline-flex items-center gap-2", className), children: [
hasFile ? /* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2 bg-muted rounded-md px-3 py-1.5", children: [
/* @__PURE__ */ jsx(PaperclipIcon, { className: "h-4 w-4 text-text-800" }),
/* @__PURE__ */ jsx(Text_default, { size: "sm", weight: "medium", className: "text-text-800", children: currentFile.name }),
/* @__PURE__ */ jsx(
"button",
{
type: "button",
onClick: handleRemoveFile,
disabled,
className: "hover:opacity-70 disabled:cursor-not-allowed disabled:opacity-50",
"aria-label": "Remover imagem",
title: "Remover imagem",
children: /* @__PURE__ */ jsx(XIcon, { className: "h-3 w-3 text-primary-950" })
}
)
] }) : /* @__PURE__ */ jsxs(
Button_default,
{
type: "button",
variant: "link",
size: "extra-small",
onClick: handleButtonClick,
disabled,
children: [
buttonIcon || /* @__PURE__ */ jsx(ImageIcon, { className: "h-4 w-4" }),
/* @__PURE__ */ jsx("span", { className: "ml-2", children: buttonText })
]
}
),
/* @__PURE__ */ jsx(
"input",
{
ref: fileInputRef,
type: "file",
accept,
className: "hidden",
onChange: handleFileChange,
disabled
}
)
] });
}
return /* @__PURE__ */ jsxs("div", { className: cn("space-y-3", className), children: [
hasFile ? /* @__PURE__ */ jsx("div", { className: "space-y-2", children: /* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2 bg-muted rounded-md px-3 py-2", children: [
/* @__PURE__ */ jsx(PaperclipIcon, { className: "h-4 w-4 text-text-800" }),
/* @__PURE__ */ jsx(Text_default, { size: "sm", weight: "medium", className: "text-text-800", children: currentFile.name }),
/* @__PURE__ */ jsx(
"button",
{
type: "button",
onClick: handleRemoveFile,
disabled,
className: "hover:opacity-70 disabled:cursor-not-allowed disabled:opacity-50",
"aria-label": "Remover imagem",
title: "Remover imagem",
children: /* @__PURE__ */ jsx(XIcon, { className: "h-4 w-4 text-primary-950 cursor-pointer" })
}
)
] }) }) : /* @__PURE__ */ jsxs(
Button_default,
{
type: "button",
variant: "link",
className: "gap-1.5",
onClick: handleButtonClick,
disabled,
children: [
buttonIcon || /* @__PURE__ */ jsx(ImageIcon, { className: "h-4 w-4" }),
buttonText
]
}
),
/* @__PURE__ */ jsx(
"input",
{
ref: fileInputRef,
type: "file",
accept,
className: "hidden",
onChange: handleFileChange,
disabled
}
)
] });
}
export {
ImageUpload
};
//# sourceMappingURL=chunk-IPVIZIHN.mjs.map