---
title: "Radio Group"
description: "A group of radio buttons that binds to FormField context."
url: https://weme-ui.github.io/slim/components/radio-group.md
---
# Radio Group

单选组。可以单独用，也可以放进 `FormField`。有 `FormField` 时会合并 `id` / `name` / `aria-*` / `data-*`。基于 Reka UI `RadioGroupRoot`，按 `items` 渲染选项，支持外观变体、方向与指示器位置。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add radio-group
```

## Usage

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import RadioGroup from '../radio-group.vue'

const schema = v.object({
  plan: v.pipe(v.string(), v.minLength(1, '请选择一项')),
})

const defaultValues = {
  plan: '',
}

const items: RadioGroupItem[] = [
  { label: 'Hobby', value: 'hobby' },
  { label: 'Pro', value: 'pro' },
  { label: 'Enterprise', value: 'enterprise' },
]

function onFormSubmit(values: { plan: string }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="plan" label="Plan" :native-label="false" required>
      <RadioGroup
        :model-value="field.state.value"
        :items="items"
        @update:model-value="field.api.handleChange"
      />
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>
```

## Props

| 属性           | 说明                                                                                                         | 类型                            | 默认值         |
| -------------- | ------------------------------------------------------------------------------------------------------------ | ------------------------------- | -------------- |
| `as`           | 根节点渲染为的元素或组件（Reka UI `Primitive`）                                                              | `AsTag \| Component`            | `'div'`        |
| `items`        | 选项列表；单项含 `value`（`AcceptableValue`）、`label`、`description`、`disabled`、`class`                   | `RadioGroupItem[]`              | —              |
| `modelValue`   | 受控选中值；可用 `v-model`；`AcceptableValue` 为 `string \| number \| bigint \| Record<string, any> \| null` | `AcceptableValue`               | —              |
| `defaultValue` | 非受控初始选中值                                                                                             | `AcceptableValue`               | —              |
| `loop`         | 键盘漫游到尽头时是否循环                                                                                     | `boolean`                       | `true`         |
| `dir`          | 阅读方向（影响水平漫游）；未传时继承 `ConfigProvider`                                                        | `'ltr' \| 'rtl'`                | —              |
| `variant`      | 外观变体                                                                                                     | `'soft' \| 'outline' \| 'card'` | `'soft'`       |
| `size`         | 尺寸                                                                                                         | `'sm' \| 'md' \| 'lg'`          | `'md'`         |
| `orientation`  | 布局与漫游方向                                                                                               | `'horizontal' \| 'vertical'`    | `'horizontal'` |
| `indicator`    | 指示器位置；`none` 时仅保留无障碍指示器（可视隐藏）                                                          | `'start' \| 'end' \| 'none'`    | `'start'`      |
| `inverted`     | 反色样式变体                                                                                                 | `boolean`                       | —              |
| `unstyled`     | 移除样式变体类，仅保留结构                                                                                   | `boolean`                       | —              |
| `id`           | 控件 `id`；未传时回退到 FormField Context                                                                    | `string`                        | —              |
| `name`         | 控件 `name`；未传时回退到 FormField Context                                                                  | `string`                        | —              |
| `required`     | 是否必填；会与 FormField Context 的 `required` 合并                                                          | `boolean`                       | —              |
| `disabled`     | 是否禁用整组；会与 FormField Context 的 `disabled` 合并                                                      | `boolean`                       | —              |
| `invalid`      | 是否无效；会与 FormField Context 的 `invalid` 合并                                                           | `boolean`                       | —              |
| `class`        | 根节点额外 class                                                                                             | `any`                           | —              |
| `ui`           | 覆盖 slots 样式（`root` / `item` / `indicator` / `indicatorIcon` / `content` / `label` / `description`）     | `Partial<RadioGroupStyleSlots>` | —              |

## Events

| 事件                | 说明                                                            | 参数                     |
| ------------------- | --------------------------------------------------------------- | ------------------------ |
| `update:modelValue` | 选中值变更时触发（含 `v-model`）；由 Reka `RadioGroupRoot` 转发 | `value: AcceptableValue` |
| `change`            | `update:modelValue` 时同步触发                                  | `value: AcceptableValue` |

## Slots

| 插槽          | 说明                                                                     |
| ------------- | ------------------------------------------------------------------------ |
| `indicator`   | 覆盖各项指示器内容；作用域参数 `{ state }`（`'checked' \| 'unchecked'`） |
| `label`       | 覆盖各项标签；作用域参数 `{ state }`；未传时回退到 `item.label`          |
| `description` | 覆盖各项描述；作用域参数 `{ state }`；未传时回退到 `item.description`    |

## Accessibility

遵循 WAI-ARIA Radio Group 设计模式。基于 Reka UI `RadioGroupRoot`（`role="radiogroup"`、`aria-orientation`、`aria-required`）与 `RadioGroupItem`（`role="radio"`、`aria-checked`、`data-state`）。组内始终启用 roving focus。组根通过 `useFormFieldControl` 合并 `id` / `name` / `disabled` / `aria-*` 与字段 `data-*`，并向 FormField 注册为控件。根节点设置 `data-slot="radio-group"`、`data-indicator`。`Enter` 在各项上被显式 `preventDefault`，不触发选中。

### Keyboard Interactions

| Key                        | Description                            |
| -------------------------- | -------------------------------------- |
| `Tab`                      | 进入 / 离开组（组内仅一项在 Tab 序中） |
| `ArrowUp` / `ArrowDown`    | 垂直方向时在组内移动焦点并选中该项     |
| `ArrowLeft` / `ArrowRight` | 水平方向时在组内移动焦点并选中该项     |
| `Space`                    | 选中当前焦点项                         |

## Examples

### Variants

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-sm w-full">
    <RadioGroup variant="soft" :items="items" />

    <RadioGroup variant="outline" :items="items" />

    <RadioGroup variant="card" :items="items" />
  </div>
</template>
```

### Sizes

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-sm w-full">
    <RadioGroup size="sm" :items="items" />

    <RadioGroup size="md" :items="items" />

    <RadioGroup size="lg" :items="items" />
  </div>
</template>
```

### Orientation

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering' },
  { label: 'Product', value: 'product' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-md w-full">
    <RadioGroup orientation="vertical" :items="items" />

    <RadioGroup orientation="horizontal" :items="items" />
  </div>
</template>
```

### With Description

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  {
    label: 'Hobby',
    value: 'hobby',
    description: 'For personal projects and experiments.',
  },
  {
    label: 'Pro',
    value: 'pro',
    description: 'For growing teams that need more seats.',
  },
  {
    label: 'Enterprise',
    value: 'enterprise',
    description: 'For organizations with advanced controls.',
  },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <RadioGroup :items="items" />
  </div>
</template>
```

### Indicator

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-sm w-full">
    <RadioGroup indicator="start" :items="items" />

    <RadioGroup indicator="end" :items="items" />

    <RadioGroup indicator="none" :items="items" />
  </div>
</template>
```

### Inverted

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-sm w-full">
    <RadioGroup :items="items" />

    <RadioGroup inverted :items="items" />
  </div>
</template>
```

### Disabled

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering', disabled: true },
  { label: 'Product', value: 'product' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <RadioGroup :items="items" />
  </div>
</template>
```

### Invalid

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering' },
]
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <RadioGroup :items="items" />
    <RadioGroup invalid :items="items" />
  </div>
</template>
```

### Unstyled

```vue
<script lang="ts" setup>
import type { RadioGroupItem } from '../radio-group.props'
import RadioGroup from '../radio-group.vue'

const items: RadioGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <RadioGroup unstyled class="flex flex-col gap-2" :items="items" />
  </div>
</template>
```