import { Ref, useImperativeHandle, useRef, useState, forwardRef, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { Colors } from "~/application/enums/shared/Colors"; import ColorBadge from "~/components/ui/badges/ColorBadge"; import ButtonPrimary from "~/components/ui/buttons/ButtonPrimary"; import ButtonSecondary from "~/components/ui/buttons/ButtonSecondary"; import ButtonTertiary from "~/components/ui/buttons/ButtonTertiary"; import TrashIcon from "~/components/ui/icons/TrashIcon"; import InputCheckboxInline from "~/components/ui/input/InputCheckboxInline"; import InputSelector from "~/components/ui/input/InputSelector"; import InputText, { RefInputText } from "~/components/ui/input/InputText"; import ErrorModal, { RefErrorModal } from "~/components/ui/modals/ErrorModal"; import SlideOverWideEmpty from "~/components/ui/slideOvers/SlideOverWideEmpty"; import { getColors } from "~/utils/shared/ColorUtils"; import { updateItemByIdx } from "~/utils/shared/ObjectUtils"; export type OptionValue = { id?: string | null; parentId?: string | null; order: number; value: string; name: string | null; color?: Colors; options?: OptionValue[]; }; export interface RefPropertyOptionsForm { set: (options: OptionValue[]) => void; } interface Props { title: string; onSet: (items: OptionValue[]) => void; } const PropertyOptionsForm = ({ title, onSet }: Props, ref: Ref) => { useImperativeHandle(ref, () => ({ set })); const { t } = useTranslation(); const errorModal = useRef(null); const inputOption = useRef(null); const [withColors, setWithColors] = useState(false); const [withDescriptions, setWithDescriptions] = useState(false); const [open, setOpen] = useState(false); const [items, setItems] = useState([]); useEffect(() => { if (!withDescriptions) { setItems((items) => items.map((f) => ({ ...f, name: null }))); } }, [withDescriptions]); useEffect(() => { if (!withColors) { setItems((items) => items.map((f) => ({ ...f, color: undefined }))); } }, [withColors]); function set(options: OptionValue[]) { setItems(options); if (options.length === 0 && items.length === 0) { addOption(); } setWithColors(options.some((f) => f.color)); setWithDescriptions(options.some((f) => f.name)); setOpen(true); } function close() { setOpen(false); } function save() { const emptyItems = items.filter((f) => !f.value || f.value.trim() === ""); if (emptyItems.length > 0) { errorModal.current?.show(t("entities.errors.selectOptionCannotBeEmpty")); return; } onSet(items); close(); } function addOption() { const maxOrder = items.length === 0 ? 1 : Math.max(...items.map((o) => o.order)); setItems([ ...items, { id: null, parentId: null, order: maxOrder + 1, value: "", name: null, color: Colors.UNDEFINED, options: [], }, ]); setTimeout(() => { inputOption.current?.input.current?.focus(); }, 1); } return ( setOpen(false)}>
{t("models.property.optionEditor.add")}
{items.map((option, idx) => { return (
updateItemByIdx(items, setItems, idx, { value: e, }) } className="flex-auto" /> {withDescriptions && ( updateItemByIdx(items, setItems, idx, { name: e, }) } className="flex-auto" /> )} {withColors && (
updateItemByIdx(items, setItems, idx, { color: Number(e), }) } selectPlaceholder={""} className="w-32" options={ getColors().map((color) => { return { name: , value: color, }; }) ?? [] } >
)} setItems([...items.filter((_, index) => index !== idx)])}>
); })}
{t("shared.cancel")} {t("shared.save")}
); }; export default forwardRef(PropertyOptionsForm);