2025-08-05 22:29:17 +03:00
|
|
|
import { Tooltip } from "bootstrap";
|
2025-08-10 17:19:39 +03:00
|
|
|
import { useEffect, useRef, useMemo, useCallback } from "preact/hooks";
|
2025-08-05 22:29:17 +03:00
|
|
|
import { escapeQuotes } from "../../services/utils";
|
2025-08-07 19:20:35 +03:00
|
|
|
import { ComponentChildren } from "preact";
|
2025-08-10 17:19:39 +03:00
|
|
|
import { memo } from "preact/compat";
|
2025-08-05 22:29:17 +03:00
|
|
|
|
2025-08-03 21:18:18 +03:00
|
|
|
interface FormCheckboxProps {
|
|
|
|
|
name: string;
|
2025-08-07 19:20:35 +03:00
|
|
|
label: string | ComponentChildren;
|
2025-08-05 15:13:09 +03:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2025-08-05 18:05:41 +03:00
|
|
|
disabled?: boolean;
|
2025-08-03 21:18:18 +03:00
|
|
|
onChange(newValue: boolean): void;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 17:19:39 +03:00
|
|
|
const FormCheckbox = memo(({ name, disabled, label, currentValue, onChange, hint }: FormCheckboxProps) => {
|
2025-08-05 22:29:17 +03:00
|
|
|
const labelRef = useRef<HTMLLabelElement>(null);
|
|
|
|
|
|
2025-08-10 17:19:39 +03:00
|
|
|
// Fix: Move useEffect outside conditional
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!hint || !labelRef.current) return;
|
|
|
|
|
|
|
|
|
|
const tooltipInstance = Tooltip.getOrCreateInstance(labelRef.current, {
|
|
|
|
|
html: true,
|
|
|
|
|
template: '<div class="tooltip tooltip-top" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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]);
|
2025-08-05 22:29:17 +03:00
|
|
|
|
2025-08-03 21:18:18 +03:00
|
|
|
return (
|
2025-08-05 20:00:39 +03:00
|
|
|
<div className="form-checkbox">
|
2025-08-05 15:13:09 +03:00
|
|
|
<label
|
|
|
|
|
className="form-check-label tn-checkbox"
|
2025-08-10 17:19:39 +03:00
|
|
|
style={labelStyle}
|
|
|
|
|
title={titleText}
|
2025-08-05 22:29:17 +03:00
|
|
|
ref={labelRef}
|
|
|
|
|
>
|
2025-08-03 21:18:18 +03:00
|
|
|
<input
|
|
|
|
|
className="form-check-input"
|
|
|
|
|
type="checkbox"
|
|
|
|
|
name={name}
|
|
|
|
|
checked={currentValue || false}
|
|
|
|
|
value="1"
|
2025-08-05 18:05:41 +03:00
|
|
|
disabled={disabled}
|
2025-08-10 17:19:39 +03:00
|
|
|
onChange={handleChange} />
|
2025-08-03 21:18:18 +03:00
|
|
|
{label}
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-10 17:19:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default FormCheckbox;
|