UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

198 lines (194 loc) 6.94 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/components/user-input/ToggleableInput.tsx var ToggleableInput_exports = {}; __export(ToggleableInput_exports, { ToggleableInput: () => ToggleableInput, ToggleableInputUncontrolled: () => ToggleableInputUncontrolled }); module.exports = __toCommonJS(ToggleableInput_exports); var import_react2 = require("react"); var import_lucide_react = require("lucide-react"); var import_clsx = __toESM(require("clsx")); // src/hooks/useSaveDelay.ts var import_react = require("react"); function useSaveDelay(setNotificationStatus, delay) { const [updateTimer, setUpdateTimer] = (0, import_react.useState)(void 0); const [notificationTimer, setNotificationTimer] = (0, import_react.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); } }; (0, import_react.useEffect)(() => { return () => { clearTimeout(updateTimer); clearTimeout(notificationTimer); }; }, []); return { restartTimer, clearUpdateTimer }; } // src/util/noop.ts var noop = () => void 0; // src/components/user-input/ToggleableInput.tsx var import_jsx_runtime = require("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] = (0, import_react2.useState)(initialState !== "display"); const { restartTimer, clearUpdateTimer } = useSaveDelay(() => void 0, 3e3); const ref = (0, import_react2.useRef)(null); const onEditCompletedWrapper = (text) => { onEditCompleted(text); clearUpdateTimer(); }; (0, import_react2.useEffect)(() => { if (isEditing) { ref.current?.focus(); } }, [isEditing]); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( "div", { className: "row items-center w-full gap-x-2 overflow-hidden", onClick: () => !isEditing ? setIsEditing(!isEditing) : void 0, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: (0, import_clsx.default)("row overflow-hidden", { "flex-1": isEditing }), children: isEditing ? /* @__PURE__ */ (0, import_jsx_runtime.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: (0, import_clsx.default)(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__ */ (0, import_jsx_runtime.jsx)( "span", { className: (0, import_clsx.default)(labelClassName, "max-w-xs break-words overflow-hidden"), children: value } ) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_lucide_react.Pencil, { className: (0, import_clsx.default)(`cursor-pointer`, { "text-transparent": isEditing }), size, style: { minWidth: `${size}px` } } ) ] } ), isEditing && disclaimer && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { className: "text-negative", children: disclaimer }) ] }); }; var ToggleableInputUncontrolled = ({ value: initialValue, onChangeText = noop, ...restProps }) => { const [value, setValue] = (0, import_react2.useState)(initialValue); (0, import_react2.useEffect)(() => { setValue(initialValue); }, [initialValue]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( ToggleableInput, { value, onChangeText: (text) => { setValue(text); onChangeText(text); }, ...restProps } ); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ToggleableInput, ToggleableInputUncontrolled }); //# sourceMappingURL=ToggleableInput.js.map