import clsx from "clsx"; import { useEffect, useState } from "react"; import CheckEmptyCircle from "../icons/CheckEmptyCircleIcon"; import HintTooltip from "../tooltips/HintTooltip"; import { Colors } from "../../../application/enums/shared/Colors"; import { useApp } from "../../../hooks"; export default function InputCheckboxCards({ title, name, options, value, onChange, selectAndClearAll = false, disabled, required, help, hint, columns, display, }: { title?: string; name?: string; options: { name: string; value: string | number; color?: Colors; disabled?: boolean }[]; value?: (string | number)[]; onChange?: (value: (string | number)[]) => void; selectAndClearAll?: boolean; disabled?: boolean; required?: boolean; help?: string; hint?: React.ReactNode; columns?: number; display?: "name" | "value" | "nameAndValue"; }) { const { t } = useApp(); const [selected, setSelected] = useState(value || []); const [displayType, setDisplayType] = useState<"name" | "value" | "nameAndValue">("name"); useEffect(() => { if (display) { setDisplayType(display); } else { const hasNames = options.some((item) => item.name); if (!hasNames) { setDisplayType("value"); } else { const namesAreDifferent = options.some((item) => item.name !== item.value.toString()); if (namesAreDifferent) { setDisplayType("nameAndValue"); } else { setDisplayType("name"); } } } }, [display, options]); useEffect(() => { if (value && !isEqual(selected.sort(), value?.sort())) { setSelected(value); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [value]); useEffect(() => { if (onChange && !isEqual(selected.sort(), value?.sort())) { onChange(selected); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selected]); function isEqual(a: any, b: any) { return JSON.stringify(a) === JSON.stringify(b); } function isChecked(value: string | number) { return selected.includes(value); } return (
{/* selected: {JSON.stringify(selected)} */}
{title && ( )}
{selectAndClearAll && (
{selected.length !== options.length && ( )} {selected.length > 0 && ( )}
)}
{/* {selected.map((item) => { return ; })} */}
); }