ivt
Version:
Ivt Components Library
98 lines (95 loc) • 3.8 kB
JavaScript
import * as React from 'react';
import { c as cn } from './utils-BDcRwQMd.mjs';
import { s as styleInputError, a as styleInput } from './styles-lQyUmHf7.mjs';
import { I as Input } from './input-B-W81Xmi.mjs';
import { X } from './x-B6hDJA_l.mjs';
import { B as Badge } from './badge-ZU62MOiS.mjs';
const InputFile = /*#__PURE__*/ React.forwardRef(({ className, value, isError, disabled, placeholder, ...props }, ref)=>{
const internalRef = React.useRef(null);
const mergedRef = React.useCallback((node)=>{
internalRef.current = node;
if (typeof ref === "function") {
ref(node);
} else if (ref) {
ref.current = node;
}
}, [
ref
]);
let fileName = "";
if (value instanceof File) {
fileName = value.name;
} else if (typeof value === "string" && value.length > 0) {
const withoutQuery = value.split("?")[0];
fileName = withoutQuery.split("/").pop() || value;
}
const renderFileName = ()=>{
if (!fileName) {
return /*#__PURE__*/ React.createElement("span", {
className: "text-muted-foreground truncate"
}, placeholder || "Nenhum arquivo escolhido");
}
const lastDotIndex = fileName.lastIndexOf(".");
if (lastDotIndex === -1) {
return /*#__PURE__*/ React.createElement("span", {
className: "truncate"
}, fileName);
}
const namePart = fileName.substring(0, lastDotIndex);
const extPart = fileName.substring(lastDotIndex);
return /*#__PURE__*/ React.createElement("div", {
className: "flex min-w-0 items-center"
}, /*#__PURE__*/ React.createElement("span", {
className: "truncate"
}, namePart), /*#__PURE__*/ React.createElement("span", {
className: "shrink-0"
}, extPart));
};
const handleClear = (e)=>{
e.preventDefault();
e.stopPropagation();
if (internalRef.current) {
internalRef.current.value = "";
}
if (props.onChange) {
const event = {
target: {
files: null,
value: ""
},
currentTarget: {
files: null,
value: ""
}
};
props.onChange(event);
}
};
return /*#__PURE__*/ React.createElement("div", {
className: "relative flex items-center gap-2"
}, !disabled && /*#__PURE__*/ React.createElement(Input, {
type: "file",
ref: mergedRef,
className: "absolute inset-0 z-10 h-full w-full cursor-pointer opacity-0",
disabled: disabled,
...props
}), /*#__PURE__*/ React.createElement("div", {
className: cn(styleInput, "flex w-full cursor-pointer items-center justify-between text-sm", isError && styleInputError, disabled && "cursor-not-allowed opacity-50", className)
}, /*#__PURE__*/ React.createElement("div", {
className: "flex-1 min-w-0 flex items-center"
}, renderFileName()), fileName && !disabled ? /*#__PURE__*/ React.createElement("div", {
className: "relative z-20 ml-2 shrink-0"
}, /*#__PURE__*/ React.createElement("button", {
type: "button",
onClick: handleClear,
className: "cursor-pointer p-1"
}, /*#__PURE__*/ React.createElement(X, {
className: "h-4 w-4 opacity-50 hover:opacity-100"
}))) : !disabled && /*#__PURE__*/ React.createElement(Badge, {
variant: "secondary",
className: "pointer-events-none shrink-0"
}, "Selecionar arquivo")));
});
InputFile.displayName = "InputFile";
export { InputFile as I };
//# sourceMappingURL=input-file-BH3SMNJ7.mjs.map