import { FC, createContext, useContext } from 'react' const SwitcherContext = createContext< | { name: string onChange?: (value: string) => any defaultValue?: string disabled: boolean } | undefined >(undefined) export const Switcher: FC<{ name: string onChange?: (value: string) => any defaultValue?: string disabled?: boolean }> = ({ name, children, onChange, defaultValue, disabled = false }) => { return (
{children}
) } export const SwitcherItem: FC<{ value: string label: string checked?: boolean }> = ({ value, label, checked = false }) => { const ctx = useContext(SwitcherContext) if (!ctx) { throw new Error('SwitcherItem must be a child of Switcher') } const { name, onChange, defaultValue, disabled } = ctx const id = `${name}-option-${value.replace(/\W/g, '')}` return ( <> { if (onChange) { onChange(evt.target.value) } }} /> ) }