Skip to content

Radio Group

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

Installation

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

Usage

*
Vueexamples/usage.vueExpand codeHide code
<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>

Examples

Variants

Vueexamples/variants.vueExpand codeHide code
<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

Vueexamples/sizes.vueExpand codeHide code
<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

Vueexamples/orientation.vueExpand codeHide code
<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

Vueexamples/with-description.vueExpand codeHide code
<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

Vueexamples/indicator.vueExpand codeHide code
<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

Vueexamples/inverted.vueExpand codeHide code
<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

Vueexamples/disabled.vueExpand codeHide code
<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

Vueexamples/invalid.vueExpand codeHide code
<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

Vueexamples/unstyled.vueExpand codeHide code
<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>

Props

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

Events

EventType
update:modelValuevalue: AcceptableValue
选中值变更时触发(含 v-model);由 Reka RadioGroupRoot 转发
value: AcceptableValue
changevalue: AcceptableValue
update:modelValue 时同步触发
value: AcceptableValue

Slots

SlotDescription
indicator覆盖各项指示器内容;作用域参数 { state }('checked' | 'unchecked')
覆盖各项指示器内容;作用域参数 { state }('checked' | 'unchecked')
label覆盖各项标签;作用域参数 { state };未传时回退到 item.label
覆盖各项标签;作用域参数 { state };未传时回退到 item.label
description覆盖各项描述;作用域参数 { state };未传时回退到 item.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

KeyDescription
Tab进入 / 离开组(组内仅一项在 Tab 序中)
进入 / 离开组(组内仅一项在 Tab 序中)
ArrowUp / ArrowDown垂直方向时在组内移动焦点并选中该项
垂直方向时在组内移动焦点并选中该项
ArrowLeft / ArrowRight水平方向时在组内移动焦点并选中该项
水平方向时在组内移动焦点并选中该项
Space选中当前焦点项
选中当前焦点项

Files

  • src/components/radio-group/radio-group.vue
  • src/components/radio-group/radio-group.props.ts
  • src/components/radio-group/radio-group.style.ts