import { EventKey, callHandler, mergeDefaultProps } from "@kobalte/utils"; import { type JSX, type ValidComponent, splitProps } from "solid-js"; import { type ElementOf, Polymorphic, type PolymorphicProps, } from "../polymorphic"; import { type RadioGroupItemDataSet, useRadioGroupItemContext, } from "./radio-group-item-context"; export interface RadioGroupItemControlOptions {} export interface RadioGroupItemControlCommonProps< T extends HTMLElement = HTMLElement, > { id: string; onClick: JSX.EventHandlerUnion; onKeyDown: JSX.EventHandlerUnion; } export interface RadioGroupItemControlRenderProps extends RadioGroupItemControlCommonProps, RadioGroupItemDataSet {} export type RadioGroupItemControlProps< T extends ValidComponent | HTMLElement = HTMLElement, > = RadioGroupItemControlOptions & Partial>>; /** * The element that visually represents a radio button. */ export function RadioGroupItemControl( props: PolymorphicProps>, ) { const context = useRadioGroupItemContext(); const mergedProps = mergeDefaultProps( { id: context.generateId("control"), }, props as RadioGroupItemControlProps, ); const [local, others] = splitProps(mergedProps, ["onClick", "onKeyDown"]); const onClick: JSX.EventHandlerUnion = (e) => { callHandler(e, local.onClick); context.select(); context.inputRef()?.focus(); }; const onKeyDown: JSX.EventHandlerUnion = (e) => { callHandler(e, local.onKeyDown); if (e.key === EventKey.Space) { context.select(); context.inputRef()?.focus(); } }; return ( as="div" onClick={onClick} onKeyDown={onKeyDown} {...context.dataset()} {...others} /> ); }