Files
teatea-pension/components/ui/checkbox.tsx

44 lines
1.3 KiB
TypeScript

"use client";
import * as React from "react";
import { Checkbox as KumoCheckbox, type CheckboxProps as KumoCheckboxProps } from "@cloudflare/kumo/components/checkbox";
import { cn } from "@/lib/utils";
type CheckboxProps = Omit<KumoCheckboxProps, "checked" | "onCheckedChange"> & {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
};
export function Checkbox({
checked,
className,
defaultChecked = false,
name,
onCheckedChange,
...props
}: CheckboxProps): React.ReactElement {
const [uncontrolledChecked, setUncontrolledChecked] = React.useState(defaultChecked);
const isControlled = checked !== undefined;
const currentChecked = isControlled ? checked : uncontrolledChecked;
return (
<span className="teatea-checkbox contents">
<KumoCheckbox
checked={currentChecked}
className={cn("teatea-checkbox-control", className)}
onCheckedChange={(nextChecked) => {
const booleanValue = Boolean(nextChecked);
if (!isControlled) {
setUncontrolledChecked(booleanValue);
}
onCheckedChange?.(booleanValue);
}}
{...props}
/>
{name && currentChecked ? <input name={name} type="hidden" value="on" /> : null}
</span>
);
}