"use client";
import * as React from "react";
import { ControlFieldLabel } from "../../control-layout";
import { Checkbox, Field, Switch } from "../../primitives";
type BooleanControlBaseProps = {
checked: boolean;
disabled?: boolean;
name: string;
onCheckedChange?: (checked: boolean) => void;
showLabel?: boolean;
};
export type SwitchControlProps = BooleanControlBaseProps;
export type CheckboxControlProps = BooleanControlBaseProps;
function useBooleanControlValue({
checked,
onCheckedChange,
}: {
checked: boolean;
onCheckedChange?: (checked: boolean) => void;
}): [boolean, (checked: boolean) => void] {
const [currentChecked, setCurrentChecked] = React.useState(checked);
React.useEffect(() => {
setCurrentChecked(checked);
}, [checked]);
return [
currentChecked,
(nextChecked: boolean) => {
setCurrentChecked(nextChecked);
onCheckedChange?.(nextChecked);
},
];
}
export function SwitchControl({
checked,
disabled = false,
name,
onCheckedChange,
showLabel = true,
}: SwitchControlProps): React.JSX.Element {
const [currentChecked, updateChecked] = useBooleanControlValue({ checked, onCheckedChange });
return (
{showLabel ? {name} : null}
);
}
export function CheckboxControl({
checked,
disabled = false,
name,
onCheckedChange,
showLabel = true,
}: CheckboxControlProps): React.JSX.Element {
const [currentChecked, updateChecked] = useBooleanControlValue({ checked, onCheckedChange });
return (
updateChecked(nextChecked === true)}
/>
{showLabel ? {name} : null}
);
}