import { type Component, type JSX, createSignal, splitProps, createMemo, type ParentComponent, children, createEffect } from "solid-js"; import { switchDisabledStyle, switchHandleContainerStyle, switchHandleStyle, switchIconsStyle, switchIconStyle, switchInputStyle, switchSelectedStyle, switchStyle, switchTrackStyle, switchUnselectedStyle } from "./switch.css"; import { Ripple} from "../ripple"; import { resolveFirst } from "@solid-primitives/refs"; import clsx from "clsx/lite"; import { Focus } from "../focus"; import { MaterialSymbolController } from "../icon"; export interface SwitchProps extends JSX.HTMLAttributes { selected: boolean; onChanged: (value: boolean) => void; required?: boolean; disabled?: boolean; } export const Switch: ParentComponent = (props) => { const [localProps, otherProps] = splitProps( props, [ "selected", "onChanged", "required", "disabled", "children", "class", "classList", ] ); const resolved = resolveFirst(() => localProps.children); let inputRef!: HTMLInputElement; return (
{ const ignoreEvent = event.defaultPrevented || event.key !== "Enter"; if (ignoreEvent || localProps.disabled) return; inputRef.click(); }} classList={{ [switchSelectedStyle]: props.selected, [switchUnselectedStyle]: !props.selected, [switchDisabledStyle]: props.disabled, ...localProps.classList, }} {...otherProps as JSX.HTMLAttributes}> props.onChanged(event.currentTarget.checked)} onInput={event => props.onChanged(event.currentTarget.checked)} type="checkbox" aria-hidden="true" />
{localProps.children}
); }