@helpwave/hightide
Version:
helpwave's component and theming library
270 lines (258 loc) • 10.1 kB
JavaScript
;
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/SearchableList.tsx
var SearchableList_exports = {};
__export(SearchableList_exports, {
SearchableList: () => SearchableList
});
module.exports = __toCommonJS(SearchableList_exports);
var import_react5 = require("react");
var import_lucide_react = require("lucide-react");
var import_clsx2 = __toESM(require("clsx"));
// src/hooks/useLanguage.tsx
var import_react2 = require("react");
// src/hooks/useLocalStorage.tsx
var import_react = require("react");
// src/hooks/useLanguage.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var DEFAULT_LANGUAGE = "en";
var LanguageContext = (0, import_react2.createContext)({ language: DEFAULT_LANGUAGE, setLanguage: (v) => v });
var useLanguage = () => (0, import_react2.useContext)(LanguageContext);
// src/hooks/useTranslation.ts
var useTranslation = (defaults, translationOverwrite = {}) => {
const { language: languageProp, translation: overwrite } = translationOverwrite;
const { language: inferredLanguage } = useLanguage();
const usedLanguage = languageProp ?? inferredLanguage;
let defaultValues = defaults[usedLanguage];
if (overwrite && overwrite[usedLanguage]) {
defaultValues = { ...defaultValues, ...overwrite[usedLanguage] };
}
return defaultValues;
};
// src/util/simpleSearch.ts
var MultiSearchWithMapping = (search, objects, mapping) => {
return objects.filter((object) => {
const mappedSearchKeywords = mapping(object).map((value) => value.toLowerCase().trim());
return !!mappedSearchKeywords.find((value) => value.includes(search.toLowerCase().trim()));
});
};
// src/components/user-input/Input.tsx
var import_react4 = require("react");
var import_clsx = __toESM(require("clsx"));
// src/hooks/useSaveDelay.ts
var import_react3 = require("react");
function useSaveDelay(setNotificationStatus, delay) {
const [updateTimer, setUpdateTimer] = (0, import_react3.useState)(void 0);
const [notificationTimer, setNotificationTimer] = (0, import_react3.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_react3.useEffect)(() => {
return () => {
clearTimeout(updateTimer);
clearTimeout(notificationTimer);
};
}, []);
return { restartTimer, clearUpdateTimer };
}
// src/util/noop.ts
var noop = () => void 0;
// src/components/user-input/Label.tsx
var import_jsx_runtime2 = require("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__ */ (0, import_jsx_runtime2.jsx)("label", { ...props, children: children ? children : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: styleMapping[labelType], children: name }) });
};
// src/components/user-input/Input.tsx
var import_jsx_runtime3 = require("react/jsx-runtime");
var ControlledInput = ({
id,
type = "text",
value,
label,
onChange = noop,
onChangeEvent = noop,
className = "",
onEditCompleted,
expanded = true,
onBlur,
containerClassName,
...restProps
}) => {
const {
restartTimer,
clearUpdateTimer
} = useSaveDelay(() => void 0, 3e3);
const ref = (0, import_react4.useRef)(null);
(0, import_react4.useEffect)(() => {
if (restProps.autoFocus) {
ref.current?.focus();
}
}, [restProps.autoFocus]);
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: (0, import_clsx.default)({ "w-full": expanded }, containerClassName), children: [
label && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Label, { ...label, htmlFor: id, className: (0, import_clsx.default)("mb-1", label.className) }),
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
"input",
{
ref,
value,
id,
type,
className: (0, import_clsx.default)("block bg-surface text-on-surface px-3 py-2 rounded-md w-full border-2 border-gray-200 hover:border-primary focus:outline-none focus:border-primary focus:ring-primary", className),
onBlur: (event) => {
if (onBlur) {
onBlur(event);
}
if (onEditCompleted) {
onEditCompleted(event.target.value, event);
clearUpdateTimer();
}
},
onChange: (e) => {
const value2 = e.target.value;
if (onEditCompleted) {
restartTimer(() => {
onEditCompleted(value2, e);
clearUpdateTimer();
});
}
onChange(value2, e);
onChangeEvent(e);
},
...restProps
}
)
] });
};
var FormInput = (0, import_react4.forwardRef)(function FormInput2({
id,
labelText,
errorText,
className,
labelClassName,
errorClassName,
containerClassName,
required,
...restProps
}, ref) {
const input = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
"input",
{
ref,
id,
...restProps,
className: (0, import_clsx.default)(
"block bg-surface text-on-surface px-3 py-2 rounded-md w-full border-2 border-gray-200 hover:border-primary focus:outline-none focus:border-primary focus:ring-primary",
{
"focus:border-primary focus:ring-primary": !errorText,
"focus:border-negative focus:ring-negative text-negative": !!errorText
},
className
)
}
);
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: (0, import_clsx.default)("flex flex-col gap-y-1", containerClassName), children: [
labelText && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { htmlFor: id, className: (0, import_clsx.default)("textstyle-label-md", labelClassName), children: [
labelText,
required && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "text-primary font-bold", children: "*" })
] }),
input,
errorText && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("label", { htmlFor: id, className: (0, import_clsx.default)("text-negative", errorClassName), children: errorText })
] });
});
// src/components/SearchableList.tsx
var import_jsx_runtime4 = require("react/jsx-runtime");
var defaultSearchableListTranslation = {
en: {
search: "Search",
nothingFound: "Nothing found"
},
de: {
search: "Suche",
nothingFound: "Nichts gefunden"
}
};
var SearchableList = ({
overwriteTranslation,
list,
initialSearch = "",
searchMapping,
itemMapper,
className
}) => {
const translation = useTranslation(defaultSearchableListTranslation, overwriteTranslation);
const [search, setSearch] = (0, import_react5.useState)(initialSearch);
(0, import_react5.useEffect)(() => setSearch(initialSearch), [initialSearch]);
const filteredEntries = (0, import_react5.useMemo)(() => MultiSearchWithMapping(search, list, searchMapping), [search, list, searchMapping]);
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: (0, import_clsx2.default)("col gap-y-2", className), children: [
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "row justify-between gap-x-2 items-center", children: [
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "flex-1", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ControlledInput, { value: search, onChange: setSearch, placeholder: translation.search }) }),
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react.Search, { size: 20 })
] }),
filteredEntries.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "col gap-y-1", children: filteredEntries.map(itemMapper) }),
!filteredEntries.length && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "row justify-center", children: translation.nothingFound })
] });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SearchableList
});
//# sourceMappingURL=SearchableList.js.map