{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "typed-radio-group",
  "type": "registry:component",
  "title": "TypedRadioGroup",
  "description": "A headless, type-safe radio group component with full TypeScript inference and customizable rendering.",
  "registryDependencies": [
    "radio-group",
    "https://typedora-ui.netlify.app/r/typed-utils.json"
  ],
  "files": [
    {
      "path": "registry/default/typed-radio-group/typed-radio-group.tsx",
      "content": "\"use client\";\n\nimport * as RadioGroupPrimitive from \"@radix-ui/react-radio-group\";\nimport { CircleIcon } from \"lucide-react\";\nimport * as React from \"react\";\nimport { RadioGroup, RadioGroupItem } from \"@/components/ui/radio-group\";\nimport type { ExtractOption, ExtractOptionValue } from \"@/lib/typed-utils\";\n\n// =============================================================================\n// Internal Indicator Component (matches shadcn/ui style)\n// =============================================================================\n\nfunction RadioGroupIndicator(\n  props: React.ComponentProps<typeof RadioGroupPrimitive.Indicator>,\n) {\n  return (\n    <RadioGroupPrimitive.Indicator\n      data-slot=\"radio-group-indicator\"\n      className=\"relative flex items-center justify-center\"\n      {...props}\n    >\n      <CircleIcon className=\"fill-primary absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2\" />\n    </RadioGroupPrimitive.Indicator>\n  );\n}\n\n// =============================================================================\n// Types\n// =============================================================================\n\n/**\n * Supported value types for TypedRadioGroup.\n * Values are serialized to strings internally but typed correctly externally.\n */\nexport type RadioValueType = string | number | boolean;\n\n/**\n * Default option type with value and label.\n * Use this as a reference for your custom option types.\n */\nexport type RadioOption<T extends RadioValueType = string> = {\n  value: T;\n  label: string;\n  disabled?: boolean;\n};\n\n// =============================================================================\n// Render Props Types\n// =============================================================================\n\n/**\n * Props passed to renderItem function.\n * @template TOption - The full option type including custom properties\n */\nexport type RadioItemRenderProps<TOption> = {\n  /** The option data */\n  option: TOption;\n  /** Whether this option is currently selected */\n  isSelected: boolean;\n  /** Whether this option is disabled */\n  isDisabled: boolean;\n  /** Props to spread on RadioGroupItem */\n  itemProps: {\n    value: string;\n    id: string;\n    disabled?: boolean;\n  };\n  /** Default item component */\n  Item: typeof RadioGroupItem;\n  /** Default indicator component */\n  Indicator: typeof RadioGroupIndicator;\n};\n\n/**\n * Props passed to renderGroup function.\n * Used to customize the layout wrapper around radio items.\n */\nexport type RadioGroupRenderProps = {\n  /** Rendered items to be placed inside your custom wrapper */\n  children: React.ReactNode;\n};\n\n/**\n * Core props for TypedRadioGroup (for documentation).\n * The actual component also accepts all RadioGroup props (className, disabled, etc.)\n */\nexport interface TypedRadioGroupBaseProps<\n  TOptions extends readonly Record<string, unknown>[],\n  TValueKey extends string = \"value\",\n  TLabelKey extends string = \"label\",\n> {\n  /** Array of radio options */\n  options: TOptions;\n  /** The key to use for the option value (default: \"value\") */\n  valueKey?: TValueKey;\n  /** The key to use for the option label (default: \"label\") */\n  labelKey?: TLabelKey;\n  /** Controlled value */\n  value?: ExtractOptionValue<TOptions, TValueKey>;\n  /** Default value for uncontrolled usage */\n  defaultValue?: ExtractOptionValue<TOptions, TValueKey>;\n  /** Callback when value changes - receives the typed value */\n  onValueChange?: (value: ExtractOptionValue<TOptions, TValueKey>) => void;\n  /** Custom render for each item */\n  renderItem?: (\n    props: RadioItemRenderProps<ExtractOption<TOptions>>,\n  ) => React.ReactNode;\n  /**\n   * Custom render for wrapping the items inside RadioGroup.\n   * Use this for adding custom elements between items (e.g., dividers).\n   * For simple layout changes, prefer using the `className` prop directly.\n   */\n  renderGroup?: (props: RadioGroupRenderProps) => React.ReactNode;\n  /** CSS class name for the RadioGroup container */\n  className?: string;\n  /** Whether the entire radio group is disabled */\n  disabled?: boolean;\n  /** The name of the group (for form submission) */\n  name?: string;\n  /** Whether selection is required */\n  required?: boolean;\n  /** The orientation of the radio group */\n  orientation?: \"horizontal\" | \"vertical\";\n  /** When true, keyboard navigation will loop */\n  loop?: boolean;\n}\n\n/**\n * Props for the TypedRadioGroup component.\n * @template TOptions - The options array type\n * @template TValueKey - The key used for the value property (default: \"value\")\n * @template TLabelKey - The key used for the label property (default: \"label\")\n */\nexport interface TypedRadioGroupProps<\n  TOptions extends readonly Record<string, unknown>[],\n  TValueKey extends string = \"value\",\n  TLabelKey extends string = \"label\",\n> extends Omit<\n      React.ComponentProps<typeof RadioGroup>,\n      \"value\" | \"defaultValue\" | \"onValueChange\" | \"children\" | \"onValueChange\"\n    >,\n    TypedRadioGroupBaseProps<TOptions, TValueKey, TLabelKey> {}\n\n// =============================================================================\n// Helpers\n// =============================================================================\n\n/**\n * Serialize a value to string for internal use with Radix RadioGroup\n */\nfunction serializeValue<T extends RadioValueType>(value: T): string {\n  if (typeof value === \"boolean\") {\n    return value ? \"__boolean_true__\" : \"__boolean_false__\";\n  }\n  if (typeof value === \"number\") {\n    return `__number_${value}__`;\n  }\n  return String(value);\n}\n\n/**\n * Deserialize a string value back to its original type\n */\nfunction deserializeValue<TOption extends Record<string, unknown>>(\n  serialized: string,\n  options: readonly TOption[],\n  valueKey: keyof TOption,\n): TOption[keyof TOption] {\n  const found = options.find(\n    (opt) => serializeValue(opt[valueKey] as RadioValueType) === serialized,\n  );\n  return found?.[valueKey] as TOption[keyof TOption];\n}\n\n/**\n * Generate a unique ID for a radio item\n */\nfunction generateItemId(index: number, groupId: string): string {\n  return `${groupId}-item-${index}`;\n}\n\n// =============================================================================\n// Default Render Functions\n// =============================================================================\n\nfunction createDefaultRenderItem<\n  TOption extends Record<string, unknown>,\n  TLabelKey extends keyof TOption,\n>(labelKey: TLabelKey) {\n  return function defaultRenderItem({\n    option,\n    itemProps,\n    Item,\n    Indicator,\n  }: RadioItemRenderProps<TOption>): React.ReactNode {\n    const label = option[labelKey];\n    return (\n      <label\n        htmlFor={itemProps.id}\n        className=\"flex items-center gap-2 cursor-pointer\"\n      >\n        <Item {...itemProps}>\n          <Indicator />\n        </Item>\n        <span>{String(label ?? \"\")}</span>\n      </label>\n    );\n  };\n}\n\nfunction defaultRenderGroup({\n  children,\n}: RadioGroupRenderProps): React.ReactNode {\n  return children;\n}\n\n// =============================================================================\n// Component\n// =============================================================================\n\n/**\n * A headless, type-safe radio group component with full TypeScript inference\n * and customizable rendering via render props.\n *\n * @example\n * ```tsx\n * // Default usage with value/label keys\n * <TypedRadioGroup\n *   options={[\n *     { value: \"apple\", label: \"Apple\" },\n *     { value: \"banana\", label: \"Banana\", description: \"Yellow fruit\" },\n *   ] as const}\n *   renderItem={({ option, itemProps, Item, Indicator }) => (\n *     <label htmlFor={itemProps.id} className=\"flex items-center gap-2\">\n *       <Item {...itemProps}>\n *         <Indicator />\n *       </Item>\n *       <div>\n *         <span>{option.label}</span>\n *         {option.description && <p>{option.description}</p>}\n *       </div>\n *     </label>\n *   )}\n *   onValueChange={(value) => {\n *     // value: \"apple\" | \"banana\"\n *   }}\n * />\n *\n * // Custom keys usage with id/name\n * <TypedRadioGroup\n *   options={[\n *     { id: 1, name: \"John Doe\" },\n *     { id: 2, name: \"Jane Smith\" },\n *   ] as const}\n *   valueKey=\"id\"\n *   labelKey=\"name\"\n *   onValueChange={(value) => {\n *     // value: 1 | 2\n *   }}\n * />\n * ```\n */\nfunction TypedRadioGroup<\n  TOptions extends readonly Record<string, unknown>[],\n  TValueKey extends string = \"value\",\n  TLabelKey extends string = \"label\",\n>({\n  options,\n  valueKey = \"value\" as TValueKey,\n  labelKey = \"label\" as TLabelKey,\n  value,\n  defaultValue,\n  onValueChange,\n  renderItem,\n  renderGroup = defaultRenderGroup,\n  ...props\n}: TypedRadioGroupProps<TOptions, TValueKey, TLabelKey>) {\n  // Extract the actual option type for internal use\n  type TOption = ExtractOption<TOptions> & Record<string, unknown>;\n  type TValue = ExtractOptionValue<TOptions, TValueKey>;\n\n  // Generate a stable ID for this radio group instance\n  const groupId = React.useId();\n\n  // Track internal value for uncontrolled mode\n  const [internalValue, setInternalValue] = React.useState<TValue | undefined>(\n    defaultValue,\n  );\n\n  // Use controlled value if provided, otherwise use internal state\n  const currentValue = value !== undefined ? value : internalValue;\n\n  const handleValueChange = (serialized: string) => {\n    const deserialized = deserializeValue(\n      serialized,\n      options as unknown as readonly TOption[],\n      valueKey as keyof TOption,\n    );\n\n    // Update internal state for uncontrolled mode\n    if (value === undefined) {\n      setInternalValue(deserialized as TValue);\n    }\n\n    if (onValueChange) {\n      onValueChange(deserialized as TValue);\n    }\n  };\n\n  const serializedValue =\n    currentValue !== undefined\n      ? serializeValue(currentValue as RadioValueType)\n      : undefined;\n  const serializedDefaultValue =\n    defaultValue !== undefined\n      ? serializeValue(defaultValue as RadioValueType)\n      : undefined;\n\n  // Use provided renderItem or create default with labelKey\n  const actualRenderItem =\n    renderItem ??\n    createDefaultRenderItem<TOption, keyof TOption>(labelKey as keyof TOption);\n\n  // Render all items\n  const renderItems = () => {\n    return (options as unknown as readonly TOption[]).map((option, index) => {\n      const optionValue = option[valueKey as keyof TOption] as RadioValueType;\n      const serialized = serializeValue(optionValue);\n      const isSelected = serializedValue === serialized;\n      const itemId = generateItemId(index, groupId);\n      const isDisabled = (option as { disabled?: boolean }).disabled ?? false;\n\n      return (\n        <React.Fragment key={serialized}>\n          {actualRenderItem({\n            option,\n            isSelected,\n            isDisabled,\n            itemProps: {\n              value: serialized,\n              id: itemId,\n              disabled: isDisabled,\n            },\n            Item: RadioGroupItem,\n            Indicator: RadioGroupIndicator,\n          })}\n        </React.Fragment>\n      );\n    });\n  };\n\n  // Render the group content (items wrapped by renderGroup)\n  const groupContent = renderGroup({\n    children: renderItems(),\n  });\n\n  return (\n    <RadioGroup\n      value={serializedValue}\n      defaultValue={serializedDefaultValue}\n      onValueChange={handleValueChange}\n      {...props}\n    >\n      {groupContent}\n    </RadioGroup>\n  );\n}\n\nexport { TypedRadioGroup };\n",
      "type": "registry:component"
    }
  ]
}