UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

174 lines (170 loc) 4.57 kB
// src/components/user-action/Textarea.tsx import { useEffect as useEffect2, useState as useState2 } from "react"; import clsx2 from "clsx"; // src/hooks/useDelay.ts import { useEffect, useState } from "react"; var defaultOptions = { delay: 3e3, disabled: false }; function useDelay(options) { const [timer, setTimer] = useState(void 0); const { delay, disabled } = { ...defaultOptions, ...options }; const clearTimer = () => { clearTimeout(timer); setTimer(void 0); }; const restartTimer = (onDelayFinish) => { if (disabled) { return; } clearTimeout(timer); setTimer(setTimeout(() => { onDelayFinish(); setTimer(void 0); }, delay)); }; useEffect(() => { return () => { clearTimeout(timer); }; }, [timer]); useEffect(() => { if (disabled) { clearTimeout(timer); setTimer(void 0); } }, [disabled, timer]); return { restartTimer, clearTimer, hasActiveTimer: !!timer }; } // src/util/noop.ts var noop = () => void 0; // src/components/user-action/Label.tsx import clsx from "clsx"; import { jsx } from "react/jsx-runtime"; var styleMapping = { labelSmall: "textstyle-label-sm", labelMedium: "textstyle-label-md", labelBig: "textstyle-label-lg" }; var Label = ({ children, name, labelType = "labelSmall", className, ...props }) => { return /* @__PURE__ */ jsx("label", { ...props, className: clsx(styleMapping[labelType], className), children: children ? children : name }); }; // src/components/user-action/Textarea.tsx import { jsx as jsx2, jsxs } from "react/jsx-runtime"; var Textarea = ({ label, headline, id, resizable = false, onChange = noop, onChangeText = noop, disclaimer, onBlur = noop, onEditCompleted = noop, saveDelayOptions, defaultStyle = true, disabled = false, className, ...props }) => { const [hasFocus, setHasFocus] = useState2(false); const { restartTimer, clearTimer } = useDelay(saveDelayOptions); const onEditCompletedWrapper = (text) => { onEditCompleted(text); clearTimer(); }; return /* @__PURE__ */ jsxs("div", { className: "w-full", children: [ label && /* @__PURE__ */ jsx2( Label, { ...label, htmlFor: id, className: clsx2("mb-1", label.className), labelType: label.labelType ?? "labelSmall" } ), /* @__PURE__ */ jsxs( "div", { className: clsx2( "relative", { "border-2 rounded-lg": defaultStyle, "bg-input-background text-input-text hover:border-primary focus-within:border-primary": defaultStyle && !disabled, "border-disabled-border bg-disabled-background cursor-not-allowed": defaultStyle && disabled } ), children: [ headline && /* @__PURE__ */ jsx2("span", { className: "mx-3 mt-3 block textstyle-label-md", children: headline }), /* @__PURE__ */ jsx2( "textarea", { id, className: clsx2("pt-0 px-3 border-transparent appearance-none w-full leading-tight focus:ring-0 focus:outline-none", { "resize-none": !resizable, "h-32": defaultStyle, "mt-3": !headline, "text-disabled-text": disabled }, className), onChange: (event) => { const value = event.target.value; restartTimer(() => { onEditCompletedWrapper(value); }); onChange(event); onChangeText(value); }, onFocus: () => { setHasFocus(true); }, onBlur: (event) => { onBlur(event); onEditCompletedWrapper(event.target.value); setHasFocus(false); }, disabled, ...props } ) ] } ), hasFocus && disclaimer && /* @__PURE__ */ jsx2("label", { className: "text-negative", children: disclaimer }) ] }); }; var TextareaUncontrolled = ({ value = "", onChangeText = noop, ...props }) => { const [text, setText] = useState2(value); useEffect2(() => { setText(value); }, [value]); return /* @__PURE__ */ jsx2( Textarea, { ...props, value: text, onChangeText: (text2) => { setText(text2); onChangeText(text2); } } ); }; export { Textarea, TextareaUncontrolled }; //# sourceMappingURL=Textarea.mjs.map