UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

162 lines (159 loc) 4.81 kB
// src/components/user-input/ToggleableInput.tsx import { useEffect as useEffect2, useRef, useState as useState2 } from "react"; import { Pencil } from "lucide-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/ToggleableInput.tsx import { jsx, jsxs } from "react/jsx-runtime"; var ToggleableInput = ({ type = "text", value, onChange = noop, onChangeText = noop, onEditCompleted = noop, labelClassName = "", initialState = "display", size = 20, disclaimer, onBlur, ...restProps }) => { const [isEditing, setIsEditing] = useState2(initialState !== "display"); const { restartTimer, clearUpdateTimer } = useSaveDelay(() => void 0, 3e3); const ref = useRef(null); const onEditCompletedWrapper = (text) => { onEditCompleted(text); clearUpdateTimer(); }; useEffect2(() => { if (isEditing) { ref.current?.focus(); } }, [isEditing]); return /* @__PURE__ */ jsxs("div", { children: [ /* @__PURE__ */ jsxs( "div", { className: "row items-center w-full gap-x-2 overflow-hidden", onClick: () => !isEditing ? setIsEditing(!isEditing) : void 0, children: [ /* @__PURE__ */ jsx("div", { className: clsx("row overflow-hidden", { "flex-1": isEditing }), children: isEditing ? /* @__PURE__ */ jsx( "input", { ref, ...restProps, value, type, onChange: (event) => { const value2 = event.target.value; restartTimer(() => { onEditCompletedWrapper(value2); }); onChangeText(value2); onChange(event); }, onBlur: (event) => { if (onBlur) { onBlur(event); } onEditCompletedWrapper(value); setIsEditing(false); }, onKeyDown: (event) => { if (event.key === "Enter") { setIsEditing(false); onEditCompletedWrapper(value); } }, className: clsx(labelClassName, `w-full border-none rounded-none ring-0 outline-0 shadow-transparent decoration-primary p-0 underline-offset-4`, { underline: isEditing }), onFocus: (event) => event.target.select() } ) : /* @__PURE__ */ jsx( "span", { className: clsx(labelClassName, "max-w-xs break-words overflow-hidden"), children: value } ) }), /* @__PURE__ */ jsx( Pencil, { className: clsx(`cursor-pointer`, { "text-transparent": isEditing }), size, style: { minWidth: `${size}px` } } ) ] } ), isEditing && disclaimer && /* @__PURE__ */ jsx("label", { className: "text-negative", children: disclaimer }) ] }); }; var ToggleableInputUncontrolled = ({ value: initialValue, onChangeText = noop, ...restProps }) => { const [value, setValue] = useState2(initialValue); useEffect2(() => { setValue(initialValue); }, [initialValue]); return /* @__PURE__ */ jsx( ToggleableInput, { value, onChangeText: (text) => { setValue(text); onChangeText(text); }, ...restProps } ); }; export { ToggleableInput, ToggleableInputUncontrolled }; //# sourceMappingURL=ToggleableInput.mjs.map