import { EventKey, callHandler, mergeDefaultProps } from "@kobalte/utils"; import { type JSX, type ValidComponent, splitProps } from "solid-js"; import { type FormControlDataSet, useFormControlContext, } from "../form-control"; import { type ElementOf, Polymorphic, type PolymorphicProps, } from "../polymorphic"; import { type CheckboxDataSet, useCheckboxContext } from "./checkbox-context"; export interface CheckboxControlOptions {} export interface CheckboxControlCommonProps< T extends HTMLElement = HTMLElement, > { id: string; onClick: JSX.EventHandlerUnion; onKeyDown: JSX.EventHandlerUnion; } export interface CheckboxControlRenderProps extends CheckboxControlCommonProps, FormControlDataSet, CheckboxDataSet {} export type CheckboxControlProps< T extends ValidComponent | HTMLElement = HTMLElement, > = CheckboxControlOptions & Partial>>; /** * The element that visually represents a checkbox. */ export function CheckboxControl( props: PolymorphicProps>, ) { const formControlContext = useFormControlContext(); const context = useCheckboxContext(); const mergedProps = mergeDefaultProps( { id: context.generateId("control"), }, props as CheckboxControlProps, ); const [local, others] = splitProps(mergedProps, ["onClick", "onKeyDown"]); const onClick: JSX.EventHandlerUnion = (e) => { callHandler(e, local.onClick); context.toggle(); context.inputRef()?.focus(); }; const onKeyDown: JSX.EventHandlerUnion = (e) => { callHandler(e, local.onKeyDown); if (e.key === EventKey.Space) { context.toggle(); context.inputRef()?.focus(); } }; return ( as="div" onClick={onClick} onKeyDown={onKeyDown} {...formControlContext.dataset()} {...context.dataset()} {...others} /> ); }