# Radio Group

> A Radio Group widget for selecting a single option from a list of predefined choices

Radio Group widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) that present a set of mutually exclusive options. Users can select exactly one option from the group.


```ts title="radiogroup.ts"

export default [
  gui.inputs.radiogroup('preference', {
    options: [
      {
        label: 'Option A',
        value: 'a',
      },
      {
        label: 'Option B',
        value: 'b',
      },
      {
        label: 'Option C',
        value: 'c',
      },
    ],
    label: 'Select your preference',
    uid: 'radiogroup_id',
  }),
];
```

_Live demo on the docs page. Form definition: [radiogroup.ts](https://golemui.com/template/widgets-dx/radiogroup/radiogroup.ts)._

## Props

Use these `props` to customize your Radio Group widget.

| prop         | type                       | description                                                                         |
| ------------ | -------------------------- | ----------------------------------------------------------------------------------- |
| `options`    | array                      | **Required**. Collection of options to display                                      |
| `appearance` | `'default' \| 'segmented'` | `'segmented'` shows the options as a row of joined buttons. Defaults to `'default'` |
| `direction`  | `row` \| `column`          | Layout direction for the radio options. Defaults to `column`                        |
| `hint`       | string                     | Description text displayed below the label                                          |
| `labelField` | string                     | Custom key to use for the display label of each option. Defaults to `label`         |
| `valueField` | string                     | Custom key to use for the underlying value of each option. Defaults to `value`      |

### Hint

<Aside>A `label` is required for the `hint` to be displayed.</Aside>

Use the property `hint` to provide instructions for the user.


```ts title="radiogroup.ts"

export default [
  gui.inputs.radiogroup('preference', {
    options: [
      {
        label: 'Email',
        value: 'email',
      },
      {
        label: 'SMS',
        value: 'sms',
      },
    ],
    hint: 'We will use this method to send you notifications.',
    label: 'Select your preference',
    uid: 'radiogroup_hint',
  }),
];
```

_Live demo on the docs page. Form definition: [radiogroup-hint.ts](https://golemui.com/template/widgets-dx/radiogroup/radiogroup-hint.ts)._

### Custom Fields

You can map different fields for the options using `labelField` and `valueField`.


```ts title="radiogroup.ts"

export default [
  gui.inputs.radiogroup('preference', {
    options: [
      {
        name: 'Choice 1',
        id: '1',
      },
      {
        name: 'Choice 2',
        id: '2',
      },
    ],
    labelField: 'name',
    valueField: 'id',
    label: 'Select your preference',
    uid: 'radiogroup_custom',
  }),
];
```

_Live demo on the docs page. Form definition: [radiogroup-custom.ts](https://golemui.com/template/widgets-dx/radiogroup/radiogroup-custom.ts)._

### Direction

Use the `direction` property to display radio options horizontally.


```ts title="radiogroup.ts"

export default [
  gui.inputs.radiogroup('preference', {
    options: [
      {
        label: 'Option A',
        value: 'a',
      },
      {
        label: 'Option B',
        value: 'b',
      },
      {
        label: 'Option C',
        value: 'c',
      },
    ],
    direction: 'row',
    label: 'Select your preference',
    uid: 'radiogroup_direction',
  }),
];
```

_Live demo on the docs page. Form definition: [radiogroup-direction.ts](https://golemui.com/template/widgets-dx/radiogroup/radiogroup-direction.ts)._

### Segmented

Use `appearance: 'segmented'` to show the options as a segmented control, a row of joined buttons, for a few short options such as Day, Week and Month. The value, the keyboard and the validation stay those of a radio group.


```ts title="radiogroup.ts"

export default [
  gui.inputs.radiogroup('view', {
    appearance: 'segmented',
    options: [
      {
        label: 'Day',
        value: 'day',
      },
      {
        label: 'Week',
        value: 'week',
      },
      {
        label: 'Month',
        value: 'month',
      },
    ],
    label: 'View',
    defaultValue: 'week',
  }),
];
```

_Live demo on the docs page. Form definition: [radiogroup-segmented.ts](https://golemui.com/template/widgets-dx/radiogroup/radiogroup-segmented.ts)._

## Styling

Radio Groups can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md).

### CSS Variables

Following you will find a list with the CSS Variables and a quick description of what you will style.

| CSS Variable                | Description                                                           |
| --------------------------- | --------------------------------------------------------------------- |
| `--gui-intent-primary`      | Color for the inner circle when selected, and of the selected segment |
| `--gui-intent-primary-text` | Text of the selected segment                                          |
| `--gui-border-strong`       | Border of the segments                                                |
| `--gui-bg-surface-hover`    | Background of a segment under the pointer                             |

### Anatomy

This is the anatomy of the Radio Group Widget in case you want to use your CSS styles.


```html title="radiogroup-anatomy.html"
<gui-radiogroup-input class="gui-radiogroup gui-field">
  <gui-radiogroup class="gui-field">
    <span class="gui-label" id="fieldUid_label">
      <span class="gui-label__text">Label</span>
      <div class="gui-widget-hint" id="fieldUid_hint" aria-hidden="true">Hint</div>
    </span>

    <div
      role="radiogroup"
      class="gui-widget"
      id="fieldUid"
      aria-labelledby="fieldUid_label"
      aria-describedby="fieldUid_hint"
    >
      <label for="fieldUid_0">
        <input
          type="radio"
          tabindex="0"
          id="fieldUid_0"
          name="fieldUid"
          form="fieldUid_none"
          value="a"
          aria-describedby="fieldUid_hint"
        />
        Option A
      </label>
      <label for="fieldUid_1">
        <input
          type="radio"
          tabindex="-1"
          id="fieldUid_1"
          name="fieldUid"
          form="fieldUid_none"
          value="b"
          aria-describedby="fieldUid_hint"
        />
        Option B
      </label>
      <label for="fieldUid_2">
        <input
          type="radio"
          tabindex="-1"
          id="fieldUid_2"
          name="fieldUid"
          form="fieldUid_none"
          value="c"
          aria-describedby="fieldUid_hint"
        />
        Option C
      </label>
    </div>

    <div class="gui-validator gui-validator--empty" id="fieldUid_errors" role="alert"></div>
  </gui-radiogroup>
</gui-radiogroup-input>
```
