# RadioGroup

Radio is a control component that allows users to select one option from a set.

## Import

```tsx
import { RadioGroup } from '@coinbase/cds-mobile/controls/RadioGroup'
```

## Examples

### Deprecation Notice

:::danger Deprecated Component

**RadioGroup is deprecated and will be removed in a future version.**

Use **[ControlGroup](/components/inputs/ControlGroup)** with `accessibilityRole="radiogroup"` instead for better accessibility, consistency, and maintainability.

:::

:::caution Types

RadioGroup onChange fails without a string type declaration for setGroup fns.

```
const [group, setGroup] = useState<string>();
```

:::

### Migration Guide

#### ✅ Recommended: Using ControlGroup

The new recommended approach provides better accessibility, cleaner API, and consistent behavior across the design system.

```jsx
function RecommendedRadioGroup() {
  const options = [
    { value: 'btc', children: 'Bitcoin' },
    { value: 'eth', children: 'Ethereum' },
    { value: 'ltc', children: 'Litecoin' },
  ];

  const [selectedCurrency, setSelectedCurrency] = useState('btc');

  return (
    <VStack gap={2}>
      <ControlGroup
        accessibilityRole="radiogroup"
        ControlComponent={Radio}
        label="Choose a currency"
        options={options}
        value={selectedCurrency}
        onChange={setSelectedCurrency}
        name="recommended-radio-group"
      />
      <Text font="caption" color="fgMuted">
        Selected: {selectedCurrency}
      </Text>
    </VStack>
  );
}
```

#### Migration Steps

1. **Replace RadioGroup with [ControlGroup](/components/inputs/ControlGroup)**:

   ```jsx
   // Old (deprecated)
   <RadioGroup
     value={selectedValue}
     onChange={onChange}
     options={{ btc: 'Bitcoin', eth: 'Ethereum' }}
   />

   // New (recommended)
   <ControlGroup
     accessibilityRole="radiogroup"
     ControlComponent={Radio}
     options={[
       { value: 'btc', children: 'Bitcoin' },
       { value: 'eth', children: 'Ethereum' }
     ]}
     value={selectedValue}
     onChange={onChange}
   />
   ```

2. **Update options format**:

   ```jsx
   // Old: Object format
   const options = { btc: 'Bitcoin', eth: 'Ethereum' };

   // New: Array format
   const options = [
     { value: 'btc', children: 'Bitcoin' },
     { value: 'eth', children: 'Ethereum' },
   ];
   ```

3. **Update onChange handler**:

   ```jsx
   // Old: Direct value callback
   const oldOnChange = (value) => setSelected(value);

   // New: Value-only callback (mobile pattern)
   const newOnChange = (value) => setSelected(value);
   ```

4. **Update accessibility**:

   ```jsx
   // Old: No specific role requirement
   <RadioGroup label="Choose option" />

   // New: Explicit accessibility role
   <ControlGroup
     accessibilityRole="radiogroup"
     label="Choose option"
   />
   ```

### Legacy Usage (Deprecated)

:::warning Legacy Example

The following examples show the **deprecated** RadioGroup usage. **Do not use this in new code.** Use [ControlGroup](/components/inputs/ControlGroup) instead.

:::

#### Basic RadioGroup (Legacy)

```jsx
function LegacyRadioGroupDemo() {
  const options = {
    btc: 'Bitcoin',
    eth: 'Ethereum',
    dai: 'Dai',
  };

  const [group, setGroup] = useState('btc');

  return (
    <VStack gap={2}>
      <Text font="headline">Currency Selection (Legacy RadioGroup)</Text>
      <RadioGroup
        id="currency-radio-group"
        label="Choose a currency"
        name="radio-group1"
        value={group}
        onChange={setGroup}
        options={options}
      />
      <Text font="caption" color="fgMuted">
        Selected: {group}
      </Text>
    </VStack>
  );
}
```

#### With Accessibility Features (Legacy)

```jsx
function LegacyAccessibleRadioGroup() {
  const options = {
    'plan-basic': 'Basic Plan',
    'plan-pro': 'Professional Plan',
    'plan-enterprise': 'Enterprise Plan',
  };

  const [selectedPlan, setSelectedPlan] = useState('plan-pro');

  return (
    <RadioGroup
      accessibilityLabel="Choose your subscription plan"
      label="Subscription Plans"
      value={selectedPlan}
      onChange={setSelectedPlan}
      options={options}
    />
  );
}
```

#### Color Customization (Legacy)

Like other control components (i.e. Checkbox, Switch), you can customize the color of the Radio by setting the `controlColor` prop.

```jsx
function CustomColors() {
  // static values should live outside the component in product code.
  const options = {
    btc: 'Bitcoin',
    eth: 'Ethereum',
    dai: 'Dai',
  };

  const [group, setGroup] = useState();

  return (
    <RadioGroup
      id="mascot-radio-group"
      aria-labelledby="choose-a-mascot"
      name="radio-group2"
      controlColor="accentBoldGreen"
      value={group}
      onChange={setGroup}
      options={options}
    />
  );
}
```

For more advanced color customization, you can use additional style props on individual Radio components like `background`, `borderColor`, and `color`:

```jsx
function AdvancedCustomColors() {
  const [checked, setChecked] = useState(false);

  return (
    <VStack gap={2}>
      <Radio checked={checked} controlColor="bgPositive" onChange={() => setChecked((s) => !s)}>
        Control color prop
      </Radio>
      <Radio
        background={checked ? 'accentBoldPurple' : 'bg'}
        borderColor={checked ? 'bgNegative' : 'bgWarning'}
        checked={checked}
        color="bgPrimary"
        controlColor="bgPositive"
        onChange={() => setChecked((s) => !s)}
      >
        Advanced styling
      </Radio>
    </VStack>
  );
}
```

## Props

| Prop | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Record<T, ReactNode>` | Yes | `-` | Multiple choice options for the radio group. The object key represents the radio input value and the object value represents the radio option label. |
| `controlColor` | `currentColor \| fg \| fgMuted \| fgInverse \| fgPrimary \| fgWarning \| fgPositive \| fgNegative \| bg \| bgAlternate \| bgInverse \| bgOverlay \| bgElevation1 \| bgElevation2 \| bgPrimary \| bgPrimaryWash \| bgSecondary \| bgTertiary \| bgSecondaryWash \| bgNegative \| bgNegativeWash \| bgPositive \| bgPositiveWash \| bgWarning \| bgWarningWash \| bgLine \| bgLineHeavy \| bgLineInverse \| bgLinePrimary \| bgLinePrimarySubtle \| accentSubtleRed \| accentBoldRed \| accentSubtleGreen \| accentBoldGreen \| accentSubtleBlue \| accentBoldBlue \| accentSubtlePurple \| accentBoldPurple \| accentSubtleYellow \| accentBoldYellow \| accentSubtleGray \| accentBoldGray \| transparent` | No | `bgPrimary` | Sets the checked/active color of each control in the group. |
| `direction` | `horizontal \| vertical` | No | `vertical` | Direction a group of components should flow. |
| `gap` | `0 \| 1 \| 2 \| 0.25 \| 0.5 \| 0.75 \| 1.5 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10` | No | `-` | Gap to insert between siblings. |
| `label` | `null \| string \| number \| false \| true \| ReactElement<any, string \| JSXElementConstructor<any>> \| Iterable<ReactNode> \| ReactPortal` | No | `-` | Set a label summary for the group of radios. |
| `onChange` | `((value: T, checked?: boolean \| undefined) => void) \| undefined` | No | `-` | Handle change event when pressing on a radio option. |
| `radioAccessibilityLabel` | `string` | No | `-` | A11Y label to indicate order of radio buttons when focused on one button |
| `ref` | `null \| (instance: View \| null) => void \| RefObject<View>` | No | `-` | - |
| `testID` | `string` | No | `-` | Used to locate this element in unit and end-to-end tests. Under the hood, testID translates to data-testid on Web. On Mobile, testID stays the same - testID |
| `value` | `string` | No | `-` | Currently selected value. |


