Files
Trilium/apps/client/src/widgets/react/FormToggle.tsx

56 lines
1.9 KiB
TypeScript
Raw Normal View History

import clsx from "clsx";
import "./FormToggle.css";
import HelpButton from "./HelpButton";
import { useEffect, useState } from "preact/hooks";
import { ComponentChildren } from "preact";
interface FormToggleProps {
currentValue: boolean | null;
onChange(newValue: boolean): void;
switchOnName: string;
2025-12-10 18:07:38 +02:00
switchOnTooltip?: string;
switchOffName: string;
2025-12-10 18:07:38 +02:00
switchOffTooltip?: string;
helpPage?: string;
2025-08-22 11:24:27 +03:00
disabled?: boolean;
afterName?: ComponentChildren;
}
export default function FormToggle({ currentValue, helpPage, switchOnName, switchOnTooltip, switchOffName, switchOffTooltip, onChange, disabled, afterName }: FormToggleProps) {
const [ disableTransition, setDisableTransition ] = useState(true);
useEffect(() => {
const timeout = setTimeout(() => {
setDisableTransition(false);
}, 100);
return () => clearTimeout(timeout);
}, []);
return (
<div className="switch-widget">
<span className="switch-name">{ currentValue ? switchOffName : switchOnName }</span>
{ afterName }
<label>
<div
className={clsx("switch-button", { "on": currentValue, disabled, "disable-transitions": disableTransition })}
title={currentValue ? switchOffTooltip : switchOnTooltip }
>
<input
className="switch-toggle"
type="checkbox"
checked={currentValue === true}
onInput={(e) => {
onChange(!currentValue);
e.preventDefault();
2025-08-22 11:24:27 +03:00
}}
disabled={disabled}
/>
</div>
</label>
{ helpPage && <HelpButton className="switch-help-button" helpPage={helpPage} />}
</div>
2025-12-10 18:07:38 +02:00
);
}