UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

119 lines (115 loc) 3.92 kB
// src/components/user-input/Textarea.tsx import { useState as useState2 } from "react"; import clsx from "clsx"; // src/hooks/useSaveDelay.ts import { useEffect, useState } from "react"; function useSaveDelay(setNotificationStatus, delay) { const [updateTimer, setUpdateTimer] = useState(void 0); const [notificationTimer, setNotificationTimer] = useState(void 0); const restartTimer = (onSave) => { clearTimeout(updateTimer); setUpdateTimer(setTimeout(() => { onSave(); setNotificationStatus(true); clearTimeout(notificationTimer); setNotificationTimer(setTimeout(() => { setNotificationStatus(false); clearTimeout(notificationTimer); }, delay)); clearTimeout(updateTimer); }, delay)); }; const clearUpdateTimer = (hasSaved = true) => { clearTimeout(updateTimer); if (hasSaved) { setNotificationStatus(true); clearTimeout(notificationTimer); setNotificationTimer(setTimeout(() => { setNotificationStatus(false); clearTimeout(notificationTimer); }, delay)); } else { setNotificationStatus(false); } }; useEffect(() => { return () => { clearTimeout(updateTimer); clearTimeout(notificationTimer); }; }, []); return { restartTimer, clearUpdateTimer }; } // src/util/noop.ts var noop = () => void 0; // src/components/user-input/Label.tsx 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", ...props }) => { return /* @__PURE__ */ jsx("label", { ...props, children: children ? children : /* @__PURE__ */ jsx("span", { className: styleMapping[labelType], children: name }) }); }; // src/components/user-input/Textarea.tsx import { jsx as jsx2, jsxs } from "react/jsx-runtime"; var Textarea = ({ label, headline, id, resizable = false, onChange = noop, disclaimer, onBlur = noop, onEditCompleted = noop, defaultStyle = true, className, ...props }) => { const [hasFocus, setHasFocus] = useState2(false); const { restartTimer, clearUpdateTimer } = useSaveDelay(() => void 0, 3e3); const onEditCompletedWrapper = (text) => { onEditCompleted(text); clearUpdateTimer(); }; return /* @__PURE__ */ jsxs("div", { className: "w-full", children: [ label && /* @__PURE__ */ jsx2(Label, { ...label, htmlFor: id, className: clsx("mb-1", label.className), labelType: label.labelType ?? "labelSmall" }), /* @__PURE__ */ jsxs("div", { className: `${clsx(" bg-surface text-on-surface focus-within:border-primary relative", { "shadow border-2 border-gray-300 hover:border-primary rounded-lg": defaultStyle })}`, children: [ headline && /* @__PURE__ */ jsx2("span", { className: "mx-3 mt-3 block text-gray-700 font-bold", children: headline }), /* @__PURE__ */ jsx2( "textarea", { id, className: clsx("pt-0 px-3 border-transparent focus:border-transparent focus:ring-0 appearance-none border w-full leading-tight focus:outline-none", { "resize-none": !resizable, "h-32": defaultStyle, "mt-3": !headline }, className), onChange: (event) => { const value = event.target.value; restartTimer(() => { onEditCompletedWrapper(value); }); onChange(value); }, onFocus: () => { setHasFocus(true); }, onBlur: (event) => { onBlur(event); onEditCompletedWrapper(event.target.value); setHasFocus(false); }, ...props } ) ] }), hasFocus && disclaimer && /* @__PURE__ */ jsx2("label", { className: "text-negative", children: disclaimer }) ] }); }; export { Textarea }; //# sourceMappingURL=Textarea.mjs.map