import { Tooltip } from "bootstrap"; import { useEffect, useRef, useMemo, useCallback } from "preact/hooks"; import { escapeQuotes } from "../../services/utils"; import { ComponentChildren } from "preact"; import { CSSProperties, memo } from "preact/compat"; import { useUniqueName } from "./hooks"; interface FormCheckboxProps { id?: string; name?: string; label: string | ComponentChildren; /** * If set, the checkbox label will be underlined and dotted, indicating a hint. When hovered, it will show the hint text. */ hint?: string; currentValue: boolean; disabled?: boolean; onChange(newValue: boolean): void; containerStyle?: CSSProperties; } const FormCheckbox = memo(({ name, id: _id, disabled, label, currentValue, onChange, hint, containerStyle }: FormCheckboxProps) => { const id = _id ?? useUniqueName(name); const labelRef = useRef(null); // Fix: Move useEffect outside conditional useEffect(() => { if (!hint || !labelRef.current) return; const tooltipInstance = Tooltip.getOrCreateInstance(labelRef.current, { html: true, template: '' }); return () => tooltipInstance?.dispose(); }, [hint]); // Proper dependency // Memoize style object const labelStyle = useMemo(() => hint ? { textDecoration: "underline dotted var(--main-text-color)" } : undefined, [hint] ); // Memoize onChange handler const handleChange = useCallback((e: Event) => { onChange((e.target as HTMLInputElement).checked); }, [onChange]); // Memoize title attribute const titleText = useMemo(() => hint ? escapeQuotes(hint) : undefined, [hint]); return (
); }); export default FormCheckbox;