UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

195 lines (193 loc) 5.66 kB
import { Text_default } from "./chunk-IMCIR6TJ.mjs"; import { cn } from "./chunk-53ICLDGS.mjs"; // src/components/TextArea/TextArea.tsx import { forwardRef, useState, useId, useEffect, useRef, useImperativeHandle } from "react"; import { WarningCircleIcon } from "@phosphor-icons/react/dist/csr/WarningCircle"; import { jsx, jsxs } from "react/jsx-runtime"; var SIZE_CLASSES = { small: { textarea: "h-24 text-sm", // 96px height, 14px font textSize: "sm" }, medium: { textarea: "h-24 text-base", // 96px height, 16px font textSize: "md" }, large: { textarea: "h-24 text-lg", // 96px height, 18px font textSize: "lg" }, extraLarge: { textarea: "h-24 text-xl", // 96px height, 20px font textSize: "xl" } }; var BASE_TEXTAREA_CLASSES = "w-full box-border p-3 bg-background border border-solid rounded-[4px] resize-none focus:outline-none font-roboto font-normal leading-[150%] placeholder:text-text-600 transition-all duration-200"; var STATE_CLASSES = { default: { base: "border-border-300 bg-background text-text-600", hover: "hover:border-border-400", focus: "focus:border-border-500" }, hovered: { base: "border-border-400 bg-background text-text-600", hover: "", focus: "focus:border-border-500" }, focused: { base: "border-2 border-primary-950 bg-background text-text-900", hover: "", focus: "" }, invalid: { base: "border-2 border-red-700 bg-white text-gray-800", hover: "hover:border-red-700", focus: "focus:border-red-700" }, disabled: { base: "border-border-300 bg-background text-text-600 cursor-not-allowed opacity-40", hover: "", focus: "" } }; var TextArea = forwardRef( ({ label, size = "medium", state = "default", errorMessage, helperMessage, className = "", labelClassName = "", disabled, id, onChange, placeholder, required, showCharacterCount = false, maxLength, value, autoResize = false, minHeight = 96, ...props }, ref) => { const generatedId = useId(); const inputId = id ?? `textarea-${generatedId}`; const internalRef = useRef(null); useImperativeHandle(ref, () => internalRef.current); const [isFocused, setIsFocused] = useState(false); const currentLength = typeof value === "string" ? value.length : 0; const isNearLimit = maxLength && currentLength >= maxLength * 0.8; useEffect(() => { if (autoResize && internalRef.current) { const textarea = internalRef.current; textarea.style.height = "auto"; textarea.style.height = `${Math.max(textarea.scrollHeight, minHeight)}px`; } }, [autoResize, minHeight, value]); const handleChange = (event) => { onChange?.(event); }; const handleFocus = (event) => { setIsFocused(true); props.onFocus?.(event); }; const handleBlur = (event) => { setIsFocused(false); props.onBlur?.(event); }; let currentState = disabled ? "disabled" : state; if (isFocused && currentState !== "invalid" && currentState !== "disabled") { currentState = "focused"; } const sizeClasses = SIZE_CLASSES[size]; const stateClasses = STATE_CLASSES[currentState]; const heightClasses = autoResize ? "h-auto overflow-hidden" : sizeClasses.textarea; const fontSizeClass = sizeClasses.textarea.split(" ").find((c) => c.startsWith("text-")) ?? "text-base"; const textareaClasses = cn( BASE_TEXTAREA_CLASSES, autoResize ? fontSizeClass : sizeClasses.textarea, heightClasses, stateClasses.base, stateClasses.hover, stateClasses.focus, className ); return /* @__PURE__ */ jsxs("div", { className: `flex flex-col`, children: [ label && /* @__PURE__ */ jsxs( Text_default, { as: "label", htmlFor: inputId, size: sizeClasses.textSize, weight: "medium", color: "text-text-950", className: cn("mb-1.5", labelClassName), children: [ label, " ", required && /* @__PURE__ */ jsx("span", { className: "text-indicator-error", children: "*" }) ] } ), /* @__PURE__ */ jsx( "textarea", { ref: internalRef, id: inputId, disabled, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur, className: textareaClasses, placeholder, required, maxLength, value, ...props } ), errorMessage && /* @__PURE__ */ jsxs("p", { className: "flex gap-1 items-center text-sm text-indicator-error mt-1.5", children: [ /* @__PURE__ */ jsx(WarningCircleIcon, { size: 16 }), " ", errorMessage ] }), !errorMessage && showCharacterCount && maxLength && /* @__PURE__ */ jsxs( Text_default, { size: "sm", weight: "normal", className: `mt-1.5 ${isNearLimit ? "text-indicator-warning" : "text-text-500"}`, children: [ currentLength, "/", maxLength, " caracteres" ] } ), !errorMessage && helperMessage && !(showCharacterCount && maxLength) && /* @__PURE__ */ jsx(Text_default, { size: "sm", weight: "normal", className: "mt-1.5 text-text-500", children: helperMessage }) ] }); } ); TextArea.displayName = "TextArea"; var TextArea_default = TextArea; export { TextArea_default }; //# sourceMappingURL=chunk-LI2FS7M2.mjs.map