# Radio group API

> Explore the API of Cimpress UI's radio and radio group components.

Source: https://ui.cimpress.io/components/radio-group/api/

## Import

Cimpress UI exports two radio-related components:

- `RadioGroup`: container component that groups related radio buttons together, providing a shared label, optional description, error handling, and state management.
- `Radio`: component to display a radio button within a radio group.

```tsx
import { Radio, RadioGroup } from '@cimpress-ui/react';
```

## Controlled/uncontrolled usage

```tsx
import { Radio, RadioGroup, Stack } from '@cimpress-ui/react';
import { useState } from 'react';

export default function Demo() {
  const [value, setValue] = useState('apple');

  return (
    <Stack gap={32}>
      <RadioGroup label="Favorite fruit (controlled)" value={value} onChange={setValue}>
        <Radio value="apple">Apple</Radio>
        <Radio value="banana">Banana</Radio>
        <Radio value="cherry">Cherry</Radio>
      </RadioGroup>

      <RadioGroup label="Favorite fruit (uncontrolled)" defaultValue="apple">
        <Radio value="apple">Apple</Radio>
        <Radio value="banana">Banana</Radio>
        <Radio value="cherry">Cherry</Radio>
      </RadioGroup>
    </Stack>
  );
}
```

## Accessibility notes

`Radio` cannot be used on its own, and must always be a child of `RadioGroup`.

`RadioGroup` is a single tab stop within the page's tab order. Use the Tab key to navigate to the group, and the arrow keys to navigate between radio buttons within the group.

`Radio` and `RadioGroup` require textual labels to remain accessible to assistive technologies. See our [accessibility guide](/dev-guides/accessibility#labelling) for more details.

## Forms

Radio and radio group can integrate with native HTML forms. See our [forms guide](/dev-guides/forms) to learn how to work with forms.

## API reference

### Radio

Renders a single option within `RadioGroup`.

See [radio group usage guidelines](https://ui.cimpress.io/components/radio-group/).

- `ref`: `Ref<HTMLLabelElement>`

  The `ref` type for this component.

**Props: `RadioProps`**

- `value` (required): `string`

  The value of the radio button, used when submitting an HTML form.
  See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio#Value).

- `children`: `StringLikeChildren`

  The label rendered next to the radio.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `aria-label`: `string`

  Defines a string value that labels the current element.

- `aria-labelledby`: `string`

  Identifies the element (or elements) that labels the current element.

- `aria-describedby`: `string`

  Identifies the element (or elements) that describes the object.

- `aria-details`: `string`

  Identifies the element (or elements) that provide a detailed, extended description for the object.

- `autoFocus`: `boolean`

  Whether the element should receive focus on render.

- `isDisabled`: `boolean`

  Whether the radio button is disabled or not.
  Shows that a selection exists, but is not available in that circumstance.

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.

### RadioGroup

Allows users to select a single item from a visible list.

See [radio group usage guidelines](https://ui.cimpress.io/components/radio-group/).

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `RadioGroupProps`**

- `children` (required): `ReactNode`

  Radio buttons belonging to the group.

- `direction`: `Responsive<Direction>`

  Determines the direction in which radio buttons will be stacked.

  Defaults to `'vertical'`.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `label`: `string`

  The content to display as the label.

- `aria-label`: `string`

  Defines a string value that labels the current element.

- `aria-labelledby`: `string`

  Identifies the element (or elements) that labels the current element.

- `aria-describedby`: `string`

  Identifies the element (or elements) that describes the object.

- `aria-details`: `string`

  Identifies the element (or elements) that provide a detailed, extended description for the object.

- `form`: `string`

  The `<form>` element to associate the input with.
  The value of this attribute must be the id of a `<form>` in the same document.
  See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form).

- `name`: `string`

  The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#name).

- `description`: `string`

  A description for the field. Provides a hint such as specific requirements for what to choose.

- `error`: `FieldError`

  An error message for the field.

- `validate`: `(value: string | null) => string | true | string[] | undefined`

  A function that returns an error message (or `true`) if a given value is invalid.
  Validation errors are displayed to the user when the form is submitted.
  For real-time validation, use the `error` prop instead.

- `isRequired`: `boolean`

  Whether user input is required on the input before form submission.

- `isInvalid`: `boolean`

  Whether the input value is invalid.

- `isDisabled`: `boolean`

  Whether the input is disabled.

- `isReadOnly`: `boolean`

  Whether the input can be selected but not changed by the user.

- `value`: `string | null`

  The current value (controlled).

- `defaultValue`: `string | null`

  The default value (uncontrolled).

- `onChange`: `(value: string) => void`

  Handler that is called when the value changes.

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.
