UNPKG

@sikka/hawa

Version:

Modern UI Kit made with Tailwind

175 lines (172 loc) • 7.07 kB
"use client"; import { HelperText, Label, Skeleton, cn } from "./chunk-5MN7WB7B.mjs"; // elements/input/Input.tsx import React, { forwardRef } from "react"; var Input = forwardRef( ({ margin = "none", width = "full", preview = false, forceHideHelperText = false, labelProps, placeholder, showCount, inputProps, countPosition = "bottom", ...props }, ref) => { var _a; let marginStyles = { none: "hawa-mb-0", normal: "hawa-mb-3", large: "hawa-mb-5" }; let widthStyles = { small: "hawa-w-full hawa-max-w-2xs", normal: "hawa-w-1/2", full: "hawa-w-full", auto: "" }; let defaultStyle = "hawa-flex hawa-max-h-fit hawa-h-fit hawa-relative hawa-flex-col hawa-justify-center hawa-gap-0"; let defaultInputStyle = "hawa-block hawa-w-full hawa-rounded hawa-border hawa-transition-all hawa-bg-background hawa-p-3 hawa-text-sm placeholder:hawa-text-muted-foreground"; const handleChange = (e) => { let newValue = e.target.value; if (props.prefixText) { if (newValue.length < props.prefixText.length) { newValue = props.prefixText; } else { const isSubstring = props.prefixText.startsWith(newValue); if (!isSubstring && !newValue.startsWith(props.prefixText)) { newValue = `${props.prefixText}${newValue}`; } } } if (props.onChange) { const newEvent = { ...e, target: { ...e.target, value: newValue } }; props.onChange(newEvent); } }; return /* @__PURE__ */ React.createElement( "div", { className: cn( defaultStyle, marginStyles[margin], widthStyles[width], props.containerClassName, "hawa-w-full hawa-gap-2" ) }, props.label && /* @__PURE__ */ React.createElement(Label, { ...labelProps }, props.label), /* @__PURE__ */ React.createElement("div", { className: "hawa-flex hawa-flex-row hawa-w-full hawa-items-center" }, props.outsidePrefix && /* @__PURE__ */ React.createElement( "span", { className: cn( "hawa-me-2 hawa-opacity-90", !forceHideHelperText && "hawa-mb-2" ) }, props.outsidePrefix ), props.isLoading ? /* @__PURE__ */ React.createElement("div", { className: "hawa-pb-2 hawa-w-full" }, /* @__PURE__ */ React.createElement(Skeleton, { className: "hawa-h-[40px] hawa-w-full" })) : props.isLoadingError ? /* @__PURE__ */ React.createElement("div", { className: "hawa-pb-2 hawa-w-full" }, /* @__PURE__ */ React.createElement( Skeleton, { animation: "none", className: "hawa-h-[40px] hawa-w-full !hawa-bg-destructive/[0.3]", content: /* @__PURE__ */ React.createElement("div", { className: "hawa-flex hawa-flex-row hawa-gap-2" }, /* @__PURE__ */ React.createElement( "svg", { xmlns: "http://www.w3.org/2000/svg", width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", className: "hawa-text-destructive" }, /* @__PURE__ */ React.createElement("path", { d: "m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" }), /* @__PURE__ */ React.createElement("path", { d: "M12 9v4" }), /* @__PURE__ */ React.createElement("path", { d: "M12 17h.01" }) ), /* @__PURE__ */ React.createElement("span", null, /* @__PURE__ */ React.createElement("span", { className: "hawa-text-destructive" }, props.loadingErrorMesssage || "Error loading data"))) } )) : /* @__PURE__ */ React.createElement(React.Fragment, null, !props.hideSeparator && /* @__PURE__ */ React.createElement( "div", { className: cn( "hawa-absolute hawa-top-[22px] hawa-h-[0.8px] hawa-w-full hawa-bg-gray-200 hawa-transition-all dark:hawa-bg-gray-800", preview ? "hawa-opacity-100" : "hawa-opacity-0" ) } ), /* @__PURE__ */ React.createElement("div", { className: "hawa-flex hawa-flex-col hawa-w-full hawa-gap-2" }, /* @__PURE__ */ React.createElement("div", { className: "hawa-relative" }, props.startIcon && /* @__PURE__ */ React.createElement("div", { className: "hawa-absolute hawa-start-3 hawa-top-1/2 hawa--translate-y-1/2" }, props.startIcon), props.endIcon && /* @__PURE__ */ React.createElement( "div", { className: cn( "hawa-absolute hawa-end-3 hawa-top-1/2 hawa--translate-y-1/2", (_a = props.endIconProps) == null ? void 0 : _a.className ) }, props.endIcon ), /* @__PURE__ */ React.createElement( "input", { required: true, dir: props.dir, type: props.type, value: props.value, onChange: handleChange, autoComplete: props.autoComplete, defaultValue: props.defaultValue, placeholder, disabled: props.disabled || preview, style: { height: 40 }, ...inputProps, className: cn( defaultInputStyle, "focus-visible:hawa-outline-none focus-visible:hawa-ring-2 focus-visible:hawa-ring-ring focus-visible:hawa-ring-offset-0 dark:hawa-text-white", { "hawa-pe-9": props.endIcon, "hawa-ps-9": props.startIcon, "hawa-pe-[60px]": countPosition === "center" }, preview && "hawa-border-transparent hawa-bg-transparent hawa-px-0", inputProps == null ? void 0 : inputProps.className ) } )), !forceHideHelperText && /* @__PURE__ */ React.createElement(HelperText, { helperText: props.helperText }), !props.disabled && forceHideHelperText && /* @__PURE__ */ React.createElement( "div", { className: cn( "hawa-absolute hawa-end-0 hawa-top-[47px] hawa-z-20 hawa-translate-y-1/2 hawa-rounded hawa-bg-background hawa-text-start hawa-text-xs hawa-text-helper-color hawa-drop-shadow-md hawa-transition-all", props.helperText ? "hawa-border hawa-p-1" : "hawa-border-none hawa-p-0" ) }, props.helperText ), showCount && /* @__PURE__ */ React.createElement( "div", { className: cn( "hawa-absolute hawa-translate-y-1/2 hawa-text-start hawa-text-xs hawa-transition-all", { "hawa-end-0 hawa-top-[62px]": countPosition === "bottom", "hawa-bottom-[62px] hawa-end-0": countPosition === "top", "hawa-end-2": countPosition === "center" } ) }, props.value ? String(props.value).length : 0, "/", props.maxLength )))) ); } ); export { Input };