# Radio Group

> A set of visible options that holds one persistent field value.

```tsx title="hero.tsx"
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { Radio, RadioGroup } from '@langgenius/dify-ui/radio-group'

const retrievalMethods = [
  { label: 'Vector search', value: 'semantic_search' },
  { label: 'Full-text search', value: 'full_text_search' },
  { label: 'Hybrid search', value: 'hybrid_search' },
]

export function RadioGroupHeroDemo() {
  return (
    <Field name="retrievalMethod" className="w-full max-w-80">
      <Fieldset
        render={<RadioGroup defaultValue="hybrid_search" className="flex-col items-start gap-1" />}
      >
        <FieldsetLegend>Retrieval method</FieldsetLegend>
        {retrievalMethods.map((method) => (
          <FieldItem key={method.value}>
            <FieldLabel className="flex items-start gap-2">
              <Radio value={method.value} className="mt-0.5" />
              {method.label}
            </FieldLabel>
          </FieldItem>
        ))}
      </Fieldset>
    </Field>
  )
}
```

## Usage guidelines

- Use `RadioGroup` for one persistent field value from a small set of options that all stay visible. Use `Select` to choose from a closed, scannable list without text entry, `SegmentedControl` for one mode, view or filter for content that stays in place, and `CheckboxGroup` for several values from a visible set.
- Every radio belongs to a `RadioGroup`. Do not render a standalone `Radio` or `RadioItem`.
- Name the group with `Fieldset` and `FieldsetLegend`, and name each option with its own `FieldLabel` inside a `FieldItem`. Use `aria-label` on the group only when no visible legend exists.
- Use `Radio` for the standard dot in a form row. Use `RadioItem` when custom content is itself the option, such as a card, and put `RadioControl` inside it for the indicator. `RadioControl` is visual only, never a standalone radio.
- Keep the caller's value type. `RadioGroup<Value>`, `Radio<Value>` and `RadioItem<Value>` accept unions, booleans and numbers; JSX children do not inherit the parent's generic, so type each option when its value is not a string.
- Pass `disabled`, `readOnly`, `value`, `defaultValue` and `onValueChange` to `RadioGroup`, not to `Fieldset`. `Fieldset` owns the group semantics and the legend relationship only.

## Anatomy

```tsx
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { Radio, RadioControl, RadioGroup, RadioItem } from '@langgenius/dify-ui/radio-group'
```

```tsx
<Field>
  <Fieldset render={<RadioGroup />}>
    <FieldsetLegend />
    <FieldItem>
      <FieldLabel>
        <Radio />
        Label
      </FieldLabel>
    </FieldItem>
    <FieldItem>
      <RadioItem>
        <RadioControl />
      </RadioItem>
    </FieldItem>
  </Fieldset>
</Field>
```

`Field` gives the group its form `name`. `Fieldset` renders the `RadioGroup` element through `render`, so the legend names the `radiogroup` directly.

## Examples

### Option cards

Make the whole card the radio with `RadioItem` and render it as a native button. `RadioControl` draws the dot; point `aria-labelledby` at the title and `aria-describedby` at the supporting text so the name stays short.

```tsx title="option-cards.tsx"
import { Field, FieldItem } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { RadioControl, RadioGroup, RadioItem } from '@langgenius/dify-ui/radio-group'
import * as React from 'react'

type IndexingTechnique = 'high_quality' | 'economy'

const techniques = [
  {
    value: 'high_quality',
    title: 'High quality',
    description: 'Embeds every chunk with the workspace embedding model.',
  },
  {
    value: 'economy',
    title: 'Economy',
    description: 'Builds a keyword index without spending tokens.',
  },
] satisfies Array<{ value: IndexingTechnique; title: string; description: string }>

export function RadioGroupOptionCardsDemo() {
  const id = React.useId()

  return (
    <Field name="indexingTechnique" className="w-full max-w-100">
      <Fieldset
        render={
          <RadioGroup<IndexingTechnique>
            defaultValue="high_quality"
            className="flex-col items-stretch gap-2"
          />
        }
      >
        <FieldsetLegend className="py-1">Index method</FieldsetLegend>
        {techniques.map((technique) => (
          <FieldItem key={technique.value}>
            <RadioItem<IndexingTechnique>
              value={technique.value}
              nativeButton
              render={<button type="button" />}
              aria-labelledby={`${id}-${technique.value}-title`}
              aria-describedby={`${id}-${technique.value}-description`}
              className="flex w-full items-start gap-3 rounded-xl border border-components-option-card-option-border bg-components-option-card-option-bg p-3 text-start outline-offset-2 outline-state-accent-solid transition-colors not-data-checked:hover:border-components-option-card-option-border-hover not-data-checked:hover:bg-components-option-card-option-bg-hover focus-visible:outline-2 focus-visible:outline-solid not-data-checked:active:bg-state-base-active data-checked:border-components-option-card-option-selected-border data-checked:bg-components-option-card-option-selected-bg motion-reduce:transition-none"
            >
              <span className="flex min-w-0 grow flex-col gap-0.5">
                <span
                  id={`${id}-${technique.value}-title`}
                  className="system-sm-semibold text-text-primary"
                >
                  {technique.title}
                </span>
                <span
                  id={`${id}-${technique.value}-description`}
                  className="system-xs-regular text-text-tertiary"
                >
                  {technique.description}
                </span>
              </span>
              <RadioControl className="mt-0.5" />
            </RadioItem>
          </FieldItem>
        ))}
      </Fieldset>
    </Field>
  )
}
```

### Typed controlled value

Control the value with `value` and `onValueChange` when another part of the page reads it. Here the value is a `boolean`, typed on the group and on each `Radio`.

```tsx title="typed-value.tsx"
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { Radio, RadioGroup } from '@langgenius/dify-ui/radio-group'
import * as React from 'react'

export function RadioGroupTypedValueDemo() {
  const [stream, setStream] = React.useState(true)

  return (
    <div className="flex w-full max-w-80 flex-col items-start gap-2">
      <Field name="stream">
        <Fieldset
          render={
            <RadioGroup<boolean>
              value={stream}
              onValueChange={setStream}
              className="flex-wrap gap-x-4 gap-y-0"
            />
          }
        >
          <FieldsetLegend className="w-full">Response mode</FieldsetLegend>
          <FieldItem>
            <FieldLabel className="flex items-start gap-2">
              <Radio<boolean> value={true} className="mt-0.5" />
              Streaming
            </FieldLabel>
          </FieldItem>
          <FieldItem>
            <FieldLabel className="flex items-start gap-2">
              <Radio<boolean> value={false} className="mt-0.5" />
              Blocking
            </FieldLabel>
          </FieldItem>
        </Fieldset>
      </Field>
      <p className="system-xs-regular text-text-tertiary">
        The API returns {stream ? 'server-sent events as tokens arrive' : 'one complete response'}.
      </p>
    </div>
  )
}
```

### Disabled option

Set `disabled` on a `FieldItem` to disable one option and its label. A `FieldDescription` in the same item explains why. Arrow keys skip the disabled option.

```tsx title="disabled.tsx"
import { Field, FieldDescription, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { Radio, RadioGroup } from '@langgenius/dify-ui/radio-group'

export function RadioGroupDisabledDemo() {
  return (
    <Field name="parentMode" className="w-full max-w-80">
      <Fieldset
        render={<RadioGroup defaultValue="paragraph" className="flex-col items-start gap-1" />}
      >
        <FieldsetLegend>Parent chunk</FieldsetLegend>
        <FieldItem>
          <FieldLabel className="flex items-start gap-2">
            <Radio value="paragraph" className="mt-0.5" />
            Paragraph
          </FieldLabel>
        </FieldItem>
        <FieldItem disabled>
          <FieldLabel className="flex items-start gap-2">
            <Radio value="full-doc" className="mt-0.5" />
            Full document
          </FieldLabel>
          <FieldDescription className="ps-6">
            Available for documents under 10,000 tokens.
          </FieldDescription>
        </FieldItem>
        <FieldItem>
          <FieldLabel className="flex items-start gap-2">
            <Radio value="section" className="mt-0.5" />
            Section heading
          </FieldLabel>
        </FieldItem>
      </Fieldset>
    </Field>
  )
}
```

## Keyboard

| Key                       | Action                                                                           |
| ------------------------- | -------------------------------------------------------------------------------- |
| `Tab`                     | Moves focus into the group on the selected radio, then out of the group.         |
| `ArrowDown`, `ArrowRight` | Moves focus to the next enabled radio and selects it, wrapping at the end.       |
| `ArrowUp`, `ArrowLeft`    | Moves focus to the previous enabled radio and selects it, wrapping at the start. |

## API reference

### Radio

A styled radio button control for standard form rows; pair with a visible `FieldLabel` or `<label>`. Renders a `<span>` element and a hidden `<input>` beside.

Inherits `className`, `render`, `style`, `nativeButton`, `value`, `disabled`, `required`, `readOnly`, `inputRef` from [Base UI Radio.Root](https://base-ui.com/react/components/radio).

**Data attributes**

| Attribute        | Description                                                                 |
| ---------------- | --------------------------------------------------------------------------- |
| `data-checked`   | Present when the radio is checked.                                          |
| `data-unchecked` | Present when the radio is not checked.                                      |
| `data-disabled`  | Present when the radio is disabled.                                         |
| `data-readonly`  | Present when the radio is readonly.                                         |
| `data-required`  | Present when the radio is required.                                         |
| `data-valid`     | Present when the radio is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the radio is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the radio has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the radio's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the radio is checked (when wrapped in Field.Root).             |
| `data-focused`   | Present when the radio is focused (when wrapped in Field.Root).             |

### RadioControl

Renders the standard visual radio indicator inside a custom `RadioItem`. Renders a `<span>` element.

Inherits `className`, `render`, `style` from [Base UI null](https://base-ui.com/react/handbook/composition).

**Data attributes**

| Attribute               | Description                                                                                            |
| ----------------------- | ------------------------------------------------------------------------------------------------------ |
| `data-required`         | Present when the user must choose a value before submitting a form.                                    |
| `data-transitionstatus` | The transition status of the component.                                                                |
| `data-checked`          | Present when the radio button is currently selected.                                                   |
| `data-disabled`         | Present when the component should ignore user interaction.                                             |
| `data-readonly`         | Present when the user should be unable to select the radio button.                                     |
| `data-touched`          | Present when the radio button has been touched (when wrapped in Field.Root).                           |
| `data-dirty`            | Present when the radio button's value has changed from its initial value (when wrapped in Field.Root). |
| `data-filled`           | Present when the radio button has a value (when wrapped in Field.Root).                                |
| `data-focused`          | Present when the radio button is focused (when wrapped in Field.Root).                                 |

### RadioGroup

Provides shared single-selection state and keyboard navigation for a series of radio buttons. Renders a `<div>` element.

Inherits `disabled`, `readOnly`, `required`, `name`, `form`, `value`, `defaultValue`, `onValueChange`, `inputRef`, `className`, `render`, `style` from [Base UI RadioGroup](https://base-ui.com/react/components/radio-group).

**Data attributes**

| Attribute       | Description                               |
| --------------- | ----------------------------------------- |
| `data-disabled` | Present when the radio group is disabled. |

### RadioItem

An unstyled radio item for custom radio layouts; pair with `RadioControl` to render the standard indicator. Renders a `<span>` element and a hidden `<input>` beside.

Inherits `value`, `disabled`, `required`, `readOnly`, `inputRef`, `className`, `render`, `style`, `nativeButton` from [Base UI Radio.Root](https://base-ui.com/react/components/radio).

**Data attributes**

| Attribute        | Description                                                                 |
| ---------------- | --------------------------------------------------------------------------- |
| `data-checked`   | Present when the radio is checked.                                          |
| `data-unchecked` | Present when the radio is not checked.                                      |
| `data-disabled`  | Present when the radio is disabled.                                         |
| `data-readonly`  | Present when the radio is readonly.                                         |
| `data-required`  | Present when the radio is required.                                         |
| `data-valid`     | Present when the radio is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the radio is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the radio has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the radio's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the radio is checked (when wrapped in Field.Root).             |
| `data-focused`   | Present when the radio is focused (when wrapped in Field.Root).             |

### RadioSkeleton

Accepts the props of the element it renders.
