{ "name": "radio", "displayName": "Radio", "type": "component", "files": [ { "name": "Radio.tsx", "content": "import {\n createContext,\n forwardRef,\n useContext,\n useId,\n useRef,\n type KeyboardEvent,\n type ReactNode,\n} from 'react';\nimport { cn, vc } from '../../utils/cn';\nimport styles from './Radio.module.css';\n\n// -------------------- Context --------------------\n\ninterface RadioGroupContextValue {\n name: string;\n value?: string;\n onChange: (v: string) => void;\n orientation: 'horizontal' | 'vertical';\n registerRef: (value: string, node: HTMLInputElement | null) => void;\n focusNext: (currentValue: string, direction: 1 | -1) => void;\n}\nconst RadioGroupContext = createContext(null);\n\n// -------------------- RadioButton --------------------\n\nexport interface RadioButtonProps {\n value: string;\n label: string;\n disabled?: boolean;\n className?: string;\n 'data-testid'?: string;\n}\n\nexport const RadioButton = forwardRef(\n function RadioButton(\n {\n value,\n label,\n disabled,\n className,\n 'data-testid': dataTestId = 'tatva-radio',\n },\n ref,\n ) {\n const group = useContext(RadioGroupContext);\n if (!group) throw new Error('RadioButton must be used within a RadioGroup');\n\n const id = useId();\n const checked = group.value === value;\n const nodeRef = useRef(null);\n\n return (\n \n {\n nodeRef.current = node;\n group.registerRef(value, node);\n if (typeof ref === 'function') ref(node);\n else if (ref)\n (ref as React.MutableRefObject).current =\n node;\n }}\n id={id}\n type=\"radio\"\n role=\"radio\"\n name={group.name}\n value={value}\n checked={checked}\n disabled={disabled}\n className={styles.input}\n aria-checked={checked}\n tabIndex={\n disabled\n ? -1\n : checked || (group.value === undefined && !disabled)\n ? 0\n : -1\n }\n data-testid={dataTestId}\n onChange={() => group.onChange(value)}\n onKeyDown={(e: KeyboardEvent) => {\n if (e.key === 'ArrowDown' || e.key === 'ArrowRight') {\n e.preventDefault();\n group.focusNext(value, 1);\n } else if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') {\n e.preventDefault();\n group.focusNext(value, -1);\n }\n }}\n />\n \n {label}\n \n );\n },\n);\n\n// -------------------- RadioGroup --------------------\n\nexport interface RadioGroupProps {\n name: string;\n value?: string;\n defaultValue?: string;\n onChange?: (value: string) => void;\n orientation?: 'horizontal' | 'vertical';\n children: ReactNode;\n className?: string;\n label?: string;\n}\n\nexport function RadioGroup({\n name,\n value,\n defaultValue,\n onChange,\n orientation = 'vertical',\n children,\n className,\n label,\n}: RadioGroupProps) {\n const [internal, setInternal] = useControlled(value, defaultValue);\n const refs = useRef>(new Map());\n\n const registerRef = (v: string, node: HTMLInputElement | null) => {\n if (node) refs.current.set(v, node);\n else refs.current.delete(v);\n };\n\n const focusNext = (currentValue: string, direction: 1 | -1) => {\n const entries = Array.from(refs.current.entries()).filter(\n ([, el]) => !el.disabled,\n );\n if (entries.length === 0) return;\n const idx = entries.findIndex(([v]) => v === currentValue);\n if (idx === -1) return;\n const nextIdx = (idx + direction + entries.length) % entries.length;\n const [nextValue, nextEl] = entries[nextIdx];\n nextEl.focus();\n setInternal(nextValue);\n onChange?.(nextValue);\n };\n\n const handleChange = (v: string) => {\n setInternal(v);\n onChange?.(v);\n };\n\n return (\n \n \n {children}\n \n \n );\n}\n\n// Tiny controlled/uncontrolled helper.\nimport { useState, useCallback } from 'react';\nfunction useControlled(\n controlled: T | undefined,\n defaultValue: T | undefined,\n): [T | undefined, (v: T) => void] {\n const [uncontrolled, setUncontrolled] = useState(defaultValue);\n const isControlled = controlled !== undefined;\n const value = isControlled ? controlled : uncontrolled;\n const setValue = useCallback(\n (v: T) => {\n if (!isControlled) setUncontrolled(v);\n },\n [isControlled],\n );\n return [value, setValue];\n}\n" }, { "name": "Radio.module.css", "content": ".group {\n display: flex;\n gap: var(--tatva-space-3);\n}\n.orientationVertical {\n flex-direction: column;\n align-items: flex-start;\n}\n.orientationHorizontal {\n flex-direction: row;\n flex-wrap: wrap;\n align-items: center;\n}\n\n.wrapper {\n display: inline-flex;\n align-items: center;\n gap: var(--tatva-space-2);\n cursor: pointer;\n font-family: var(--tatva-font-family);\n font-size: var(--tatva-font-size-md);\n color: var(--tatva-color-fg);\n user-select: none;\n}\n\n.disabled {\n opacity: 0.55;\n cursor: not-allowed;\n}\n\n.input {\n position: absolute;\n opacity: 0;\n width: 1px;\n height: 1px;\n margin: 0;\n}\n\n.dot {\n display: inline-block;\n width: 18px;\n height: 18px;\n border-radius: 50%;\n border: 1px solid var(--tatva-color-border-strong);\n background-color: var(--tatva-color-bg);\n transition:\n border-color var(--tatva-transition-fast),\n background-color var(--tatva-transition-fast),\n box-shadow var(--tatva-transition-fast);\n position: relative;\n flex: 0 0 auto;\n}\n\n.input:focus-visible + .dot {\n border-color: var(--tatva-color-primary-500);\n box-shadow: 0 0 0 3px var(--tatva-color-primary-200);\n}\n\n.input:checked + .dot {\n border-color: var(--tatva-color-primary-600);\n}\n\n.input:checked + .dot::after {\n content: '';\n position: absolute;\n inset: 4px;\n border-radius: 50%;\n background-color: var(--tatva-color-primary-600);\n}\n\n.label {\n line-height: var(--tatva-line-height-normal);\n}\n" }, { "name": "index.ts", "content": "export { RadioGroup, RadioButton } from './Radio';\nexport type { RadioGroupProps, RadioButtonProps } from './Radio';\n" } ], "registryDependencies": [], "dependencies": [], "devDependencies": [] }