@helpwave/hightide
Version:
helpwave's component and theming library
519 lines (501 loc) • 16.6 kB
JavaScript
// src/components/properties/SelectProperty.tsx
import { List } from "lucide-react";
import clsx5 from "clsx";
// src/hooks/useLanguage.tsx
import { createContext, useContext, useEffect as useEffect2, useState as useState2 } from "react";
// src/hooks/useLocalStorage.tsx
import { useCallback, useEffect, useState } from "react";
// src/hooks/useLanguage.tsx
import { jsx } from "react/jsx-runtime";
var DEFAULT_LANGUAGE = "en";
var LanguageContext = createContext({ language: DEFAULT_LANGUAGE, setLanguage: (v) => v });
var useLanguage = () => 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/components/user-input/SearchableSelect.tsx
import { useState as useState5 } from "react";
import { Search } from "lucide-react";
// 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/Select.tsx
import { Menu } from "@headlessui/react";
import { ChevronDown, ChevronUp } from "lucide-react";
import clsx from "clsx";
// src/components/user-input/Label.tsx
import { jsx as jsx2 } 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__ */ jsx2("label", { ...props, children: children ? children : /* @__PURE__ */ jsx2("span", { className: styleMapping[labelType], children: name }) });
};
// src/components/user-input/Select.tsx
import { Fragment, jsx as jsx3, jsxs } from "react/jsx-runtime";
var Select = ({
value,
label,
options,
onChange,
isHidingCurrentValue = true,
hintText = "",
showDisabledOptions = true,
isDisabled,
className,
textColor = "text-menu-text",
hoverColor = "hover:brightness-90",
additionalItems,
selectedDisplayOverwrite
}) => {
let filteredOptions = isHidingCurrentValue ? options.filter((option) => option.value !== value) : options;
if (!showDisabledOptions) {
filteredOptions = filteredOptions.filter((value2) => !value2.disabled);
}
const selectedOption = options.find((option) => option.value === value);
if (value !== void 0 && selectedOption === void 0 && selectedDisplayOverwrite === void 0) {
console.warn("The selected value is not found in the options list. This might be an error on your part or default behavior if it is complex data type on which === does not work. In case of the latter use selectedDisplayOverwrite to set your selected text or component");
}
const borderColor = "border-menu-border";
return /* @__PURE__ */ jsxs("div", { className: clsx(className), children: [
label && /* @__PURE__ */ jsx3(Label, { ...label, labelType: label.labelType ?? "labelBig", className: clsx("mb-1", label.className) }),
/* @__PURE__ */ jsx3(Menu, { as: "div", className: "relative text-menu-text", children: ({ open }) => /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsxs(
Menu.Button,
{
className: clsx(
"inline-flex w-full justify-between items-center rounded-t-lg border-2 px-4 py-2 font-medium bg-menu-background text-menu-text",
textColor,
borderColor,
{
"rounded-b-lg": !open,
[hoverColor]: !isDisabled,
"bg-disabled-background cursor-not-allowed text-disabled": isDisabled
}
),
disabled: isDisabled,
children: [
/* @__PURE__ */ jsx3("span", { children: selectedDisplayOverwrite ?? selectedOption?.label ?? hintText }),
open ? /* @__PURE__ */ jsx3(ChevronUp, {}) : /* @__PURE__ */ jsx3(ChevronDown, {})
]
}
),
/* @__PURE__ */ jsxs(
Menu.Items,
{
className: "absolute w-full z-10 rounded-b-lg bg-menu-background text-menu-text shadow-lg max-h-[500px] overflow-y-auto",
children: [
(additionalItems ?? []).map((item, index) => /* @__PURE__ */ jsx3(
"div",
{
className: clsx(borderColor, "px-4 py-2 overflow-hidden whitespace-nowrap text-ellipsis border-2 border-t-0", {
"border-b-0 rounded-b-lg": filteredOptions.length === 0 && index === (additionalItems?.length ?? 1) - 1
}),
children: item
},
`additionalItems${index}`
)),
filteredOptions.map((option, index) => /* @__PURE__ */ jsx3(Menu.Item, { children: /* @__PURE__ */ jsx3(
"div",
{
className: clsx(
"px-4 py-2 overflow-hidden whitespace-nowrap text-ellipsis border-2 border-t-0 cursor-pointer",
option.className,
borderColor,
{
"brightness-90": option.value === value,
"brightness-95": index % 2 === 1,
"text-disabled bg-disabled-background cursor-not-allowed": !!option.disabled,
"bg-menu-background text-menu-text hover:brightness-90 cursor-pointer": !option.disabled,
"rounded-b-lg": index === filteredOptions.length - 1
}
),
onClick: () => {
if (!option.disabled) {
onChange(option.value);
}
},
children: option.label
}
) }, `item${index}`))
]
}
)
] }) })
] });
};
// src/components/user-input/Input.tsx
import {
useEffect as useEffect4,
useRef,
useState as useState4,
forwardRef
} from "react";
import clsx2 from "clsx";
// src/hooks/useSaveDelay.ts
import { useEffect as useEffect3, useState as useState3 } from "react";
function useSaveDelay(setNotificationStatus, delay) {
const [updateTimer, setUpdateTimer] = useState3(void 0);
const [notificationTimer, setNotificationTimer] = useState3(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);
}
};
useEffect3(() => {
return () => {
clearTimeout(updateTimer);
clearTimeout(notificationTimer);
};
}, []);
return { restartTimer, clearUpdateTimer };
}
// src/util/noop.ts
var noop = () => void 0;
// src/components/user-input/Input.tsx
import { jsx as jsx4, jsxs as jsxs2 } from "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 = useRef(null);
useEffect4(() => {
if (restProps.autoFocus) {
ref.current?.focus();
}
}, [restProps.autoFocus]);
return /* @__PURE__ */ jsxs2("div", { className: clsx2({ "w-full": expanded }, containerClassName), children: [
label && /* @__PURE__ */ jsx4(Label, { ...label, htmlFor: id, className: clsx2("mb-1", label.className) }),
/* @__PURE__ */ jsx4(
"input",
{
ref,
value,
id,
type,
className: clsx2("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 = forwardRef(function FormInput2({
id,
labelText,
errorText,
className,
labelClassName,
errorClassName,
containerClassName,
required,
...restProps
}, ref) {
const input = /* @__PURE__ */ jsx4(
"input",
{
ref,
id,
...restProps,
className: clsx2(
"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__ */ jsxs2("div", { className: clsx2("flex flex-col gap-y-1", containerClassName), children: [
labelText && /* @__PURE__ */ jsxs2("label", { htmlFor: id, className: clsx2("textstyle-label-md", labelClassName), children: [
labelText,
required && /* @__PURE__ */ jsx4("span", { className: "text-primary font-bold", children: "*" })
] }),
input,
errorText && /* @__PURE__ */ jsx4("label", { htmlFor: id, className: clsx2("text-negative", errorClassName), children: errorText })
] });
});
// src/components/user-input/SearchableSelect.tsx
import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
var SearchableSelect = ({
value,
options,
searchMapping,
...selectProps
}) => {
const [search, setSearch] = useState5("");
const filteredOptions = MultiSearchWithMapping(search, options, searchMapping);
return /* @__PURE__ */ jsx5(
Select,
{
value,
options: filteredOptions,
additionalItems: [/* @__PURE__ */ jsxs3("div", { className: "row gap-x-2 items-center", children: [
/* @__PURE__ */ jsx5(ControlledInput, { autoFocus: true, value: search, onChange: setSearch }),
/* @__PURE__ */ jsx5(Search, {})
] }, "selectSearch")],
...selectProps
}
);
};
// src/components/properties/PropertyBase.tsx
import { AlertTriangle } from "lucide-react";
import clsx4 from "clsx";
// src/components/Button.tsx
import clsx3 from "clsx";
import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
var ButtonSizePaddings = {
small: "btn-sm",
medium: "btn-md",
large: "btn-lg"
};
var TextButton = ({
children,
disabled = false,
color = "neutral",
size = "medium",
startIcon,
endIcon,
onClick,
className,
...restProps
}) => {
const colorClasses = {
negative: "bg-transparent text-button-text-negative-text",
neutral: "bg-transparent text-button-text-neutral-text"
}[color];
const iconColorClasses = {
negative: "text-button-text-negative-icon",
neutral: "text-button-text-neutral-icon"
}[color];
return /* @__PURE__ */ jsxs4(
"button",
{
onClick: disabled ? void 0 : onClick,
disabled: disabled || onClick === void 0,
className: clsx3(
className,
{
"text-disabled-text": disabled,
[clsx3(colorClasses, "hover:bg-button-text-hover-background rounded-full")]: !disabled
},
ButtonSizePaddings[size]
),
...restProps,
children: [
startIcon && /* @__PURE__ */ jsx6(
"span",
{
className: clsx3({
[iconColorClasses]: !disabled,
[`text-disabled-icon`]: disabled
}),
children: startIcon
}
),
children,
endIcon && /* @__PURE__ */ jsx6(
"span",
{
className: clsx3({
[iconColorClasses]: !disabled,
[`text-disabled-icon`]: disabled
}),
children: endIcon
}
)
]
}
);
};
// src/components/properties/PropertyBase.tsx
import { jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
var defaultPropertyBaseTranslation = {
en: {
remove: "Remove"
},
de: {
remove: "Entfernen"
}
};
var PropertyBase = ({
overwriteTranslation,
name,
input,
softRequired = false,
hasValue,
icon,
readOnly,
onRemove,
className = ""
}) => {
const translation = useTranslation(defaultPropertyBaseTranslation, overwriteTranslation);
const requiredAndNoValue = softRequired && !hasValue;
return /* @__PURE__ */ jsxs5("div", { className: clsx4("row gap-x-0 group", className), children: [
/* @__PURE__ */ jsxs5(
"div",
{
className: clsx4("row gap-x-2 !w-[200px] px-4 py-2 items-center rounded-l-xl border-2 border-r-0", {
"bg-gray-100 text-black group-hover:border-primary border-gray-400": !requiredAndNoValue,
"bg-warning text-surface-warning group-hover:border-warning border-warning/90": requiredAndNoValue
}, className),
children: [
icon,
name
]
}
),
/* @__PURE__ */ jsxs5(
"div",
{
className: clsx4("row grow justify-between items-center rounded-r-xl border-2 border-l-0", {
"bg-white group-hover:border-primary border-gray-400": !requiredAndNoValue,
"bg-surface-warning group-hover:border-warning border-warning/90": requiredAndNoValue
}, className),
children: [
input({ softRequired, hasValue }),
requiredAndNoValue && /* @__PURE__ */ jsx7("div", { className: "text-warning pr-4", children: /* @__PURE__ */ jsx7(AlertTriangle, { size: 24 }) }),
onRemove && /* @__PURE__ */ jsx7(
TextButton,
{
onClick: onRemove,
color: "negative",
className: clsx4("pr-4 items-center", { "!text-transparent": !hasValue || readOnly }),
disabled: !hasValue || readOnly,
children: translation.remove
}
)
]
}
)
] });
};
// src/components/properties/SelectProperty.tsx
import { jsx as jsx8 } from "react/jsx-runtime";
var defaultSingleSelectPropertyTranslation = {
en: {
select: "Select"
},
de: {
select: "Ausw\xE4hlen"
}
};
var SingleSelectProperty = ({
overwriteTranslation,
value,
options,
name,
readOnly = false,
softRequired,
onRemove,
...multiSelectProps
}) => {
const translation = useTranslation(defaultSingleSelectPropertyTranslation, overwriteTranslation);
const hasValue = value !== void 0;
return /* @__PURE__ */ jsx8(
PropertyBase,
{
name,
onRemove,
readOnly,
softRequired,
hasValue,
icon: /* @__PURE__ */ jsx8(List, { size: 16 }),
input: ({ softRequired: softRequired2 }) => /* @__PURE__ */ jsx8(
"div",
{
className: clsx5("row grow py-2 px-4 cursor-pointer", { "text-warning": softRequired2 && !hasValue }),
children: /* @__PURE__ */ jsx8(
SearchableSelect,
{
...multiSelectProps,
value,
options,
isDisabled: readOnly,
className: clsx5("w-full", { "bg-surface-warning": softRequired2 && !hasValue }),
hintText: `${translation.select}...`
}
)
}
)
}
);
};
export {
SingleSelectProperty
};
//# sourceMappingURL=SelectProperty.mjs.map