Files
heroui-vue/apps/docs/components/radio-group.md
T
2026-10-22 09:15:27 +08:00

273 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: RadioGroup 单选框组
description: 用于从列表中选择单个选项的单选组。
icon: updated
component: radio-group
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Radio,
RadioContent,
RadioControl,
RadioIndicator,
Label,
Description,
FieldError,
RadioGroup,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview
name="radio-group-basic"
/>
## 组件结构
使用 RadioGroup、Radio、RadioContent、RadioControl 和 RadioIndicator 具名导出组合。
```vue
<script setup lang="ts">
import {
Radio,
RadioContent,
RadioControl,
RadioIndicator,
Label,
Description,
FieldError,
RadioGroup,
} from "@heroui-vue/components";
</script>
<template>
<RadioGroup>
<Label>选择选项</Label>
<Radio value="basic">
<RadioContent>
<RadioControl><RadioIndicator /></RadioControl>
<Label>选项</Label>
</RadioContent>
<Description />
</Radio>
<FieldError />
</RadioGroup>
</template>
```
## 示例
### 水平排列
<ComponentPreview
name="radio-group-horizontal"
/>
### 变体
RadioGroup 组件支持两种视觉变体:
- **`primary`**(默认)— 带默认背景的标准样式,适用于大多数场景
- **`secondary`** — 低强调变体,适合在 Surface 等表面背景上使用
<ComponentPreview name="radio-group-variants" />
### 表面样式
在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"`,以应用适合表面背景的低强调变体。
<ComponentPreview
name="radio-group-on-surface"
/>
### 禁用
<ComponentPreview
name="radio-group-disabled"
/>
### 受控组件
<ComponentPreview
name="radio-group-controlled"
/>
### 非受控
仅需响应更新时,将 `defaultValue` 与 `update:modelValue` 配合使用。
<ComponentPreview
name="radio-group-uncontrolled"
/>
### 表单校验
<ComponentPreview
name="radio-group-validation"
/>
### 配送与支付
<ComponentPreview
isBgSolid
align="start"
name="radio-group-delivery-and-payment"
/>
### 自定义指示器
<ComponentPreview
name="radio-group-custom-indicator"
/>
### 渲染函数
<ComponentPreview
name="radio-group-render-function"
/>
## 自定义样式
### Tailwind CSS
<ComponentPreview name="radio-group-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-radio-group {
@apply gap-2;
}
.hv-radio {
@apply gap-4 rounded-lg border border-border p-3 hover:bg-surface-hovered;
}
.hv-radio__control {
@apply border-2 border-accent;
}
.hv-radio__indicator {
@apply bg-primary;
}
.hv-radio__content {
@apply gap-1;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
RadioGroup 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/radio-group.css)):
#### 基础类
- `.hv-radio-group` - 单选组基础容器
- `.hv-radio` - 单个单选项
- `.hv-radio__control` - 单选控件(圆形按钮)
- `.hv-radio__indicator` - 单选指示器(内部圆点)
- `.hv-radio__content` - 单选内容包裹层
#### 修饰类
禁用态通过 `.hv-radio` 上的 `[data-disabled="true"]` / `[aria-disabled="true"]`(以及 `:disabled`)应用——没有 `.hv-radio--disabled` 修饰类。
### 交互状态
单选项同时支持 CSS 伪类与 data 属性,便于灵活定制:
- **Selected**:`[aria-checked="true"]` 或 `[data-selected="true"]`(显示指示器)
- **Hover**:`:hover` 或 `[data-hovered="true"]`(边框颜色变化)
- **Focus**:`:focus-visible` 或 `[data-focus-visible="true"]`(显示焦点环)
- **Pressed**:`:active` 或 `[data-pressed="true"]`(缩放变换)
- **Disabled**:`:disabled` 或 `[aria-disabled="true"]`(降低透明度并禁用指针事件)
- **Invalid**:`[data-invalid="true"]` 或 `[aria-invalid="true"]`(错误边框颜色)
## API 参考
本库采用原生 label/input 承载选择与提交,复用共享字段、Form、Fieldset 禁用和错误协议。原生输入是唯一的提交值来源;组内没有 Reka 的重复隐藏值或带索引字段名。组校验另有无 name、不可 Tab 聚焦的约束代理,不产生 FormData 条目。
### RadioGroup
| 属性 | 类型 | 默认值 | 描述 |
| -------------------------- | --------------------------------------------------------------------------------- | ---------------------- | -------------------------------------- |
| `modelValue` | `string \| undefined` | 非受控 | 当前值,使用 v-model |
| `defaultValue` | `string \| undefined` | `undefined` | 非受控初始值副本 |
| `name` / `form` / `id` | `string` | - / - / 自动 | 统一原生提交键、表单归属及组 ID |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 组视觉样式;Checkbox 成员可显式覆盖 |
| `isDisabled` / `disabled` | `boolean` | `false` | 合并 Fieldset 禁用,后代不能解除 |
| `isReadOnly` | `boolean` | `false` | 阻止成员点击、空格和方向键修改 |
| `isRequired` / `isInvalid` | `boolean` | `false` | 要求至少一项可用选择 / 错误展示 |
| `validate` | `(value: string \| undefined) => string \| string[] \| true \| null \| undefined` | - | 同步组校验 |
| `validationBehavior` | `'native' \| 'aria'` | 继承 Form,否则 native | 原生提交拦截或仅错误语义 |
| `class` / `style` | Vue class/style 绑定 | - | 合并根样式,接受字符串/数组/对象 class |
| 默认插槽 | 作用域插槽 | - | 标签、说明、成员、错误 |
| `orientation` | `'vertical' \| 'horizontal'` | `'vertical'` | 布局及 aria-orientation |
| `dir` / `loop` | `'ltr' \| 'rtl'` / `boolean` | 继承方向 / `true` | 方向键映射及循环 |
### Radio
| 属性 | 类型 | 默认值 | 描述 |
| ------------------------------------------------------ | -------------------- | ------- | --------------------------- |
| `value` | `string` | 必填 | 组内唯一值 |
| `id` | `string` | 自动 | 实际 radio ID |
| `isDisabled` / `disabled` / `isReadOnly` / `isInvalid` | `boolean` | `false` | 成员状态,与组状态合并 |
| `class` / `style` | Vue class/style 绑定 | - | 字段根;ARIA/事件落在 input |
| `name` / `form` | `string` | - | 组内统一由 RadioGroup 管理 |
Radio 必须在 RadioGroup 中使用。不为未选中的 Radio 输出 data-selected;默认 Indicator 保持 CSS :empty 内圆点。Tab 进入选中可用成员或首个可用成员;方向键跳过禁用项,支持 RTL、Home/End,且调用者 keydown.prevent 可以取消。
### 事件与暴露
| 接口 | 行为 |
| ------------------------------ | ---------------------------------------------------------------------- |
| `update:modelValue` / `change` | 发出新的数组或标量;不修改传入数组;父级拒绝后恢复所有原生 checked |
| `focus()` / `element` | 聚焦选中可用成员或首个可用成员;空组聚焦组根。element 是组 HTMLElement |
CheckboxGroup 按原生约定提交同名重复条目,通过 FormData.getAll(name) 获取;RadioGroup 只提交一个选中可用值。未指定 name 不产生提交值。每个成员 value 必须唯一;重复或动态改成重复值会抛出明确错误。不为移除的选择自动选择别的成员;模型保留调用者的值,FormData 只包含实际存在的可用成员。
required 检查整个组,不能让每个 Checkbox 都变成必选;native 无效提交聚焦第一个可用成员,空组聚焦根。只读保留提交值并跳过必填/自定义约束。reset 尊重 preventDefault,受控值不覆盖,非受控恢复初始副本。动态 name/form 会更新提交和 reset 归属。
Label 在组中渲染 span 并注册 aria-labelledby;不要手写 for 指向组 div。每个成员独立隔离 Description/FieldError,同时合并组描述和当前组错误。后端错误使用 Form.validationErrors 按 name 映射,编辑后忽略当前响应,新对象可恢复错误。暂不提供异步 validate 或完整 validationDetails。
### RadioContent / Control / Indicator
| 部件 | 根与插槽 | 属性落点 |
| --------- | ------------------------------------------ | ------------------------------------------ |
| Content | 原生 label,包含实际 input;默认作用域插槽 | class/style/原生 label 属性及事件 |
| Control | span,默认插槽 | class/style/DOM 属性与事件 |
| Indicator | span,默认作用域插槽 | 装饰 aria-hidden;自定义内容替换基线指示器 |
具名导出使用 RadioContent、RadioControl、RadioIndicator;不使用 React 对象式子部件。class 均支持字符串、数组和对象。默认图形精确来自基线,文档自定义图标使用 Tabler。Content 输出 selected/indeterminate/hovered/pressed/focus-visible 状态标记供定制样式使用。
### 作用域插槽状态
| 参数 | 类型 | 说明 |
| ---------------------------------------------------------- | --------- | ------------------------------ |
| `isSelected` / `isIndeterminate` | `boolean` | 选中 / 半选(半选仅 Checkbox) |
| `isDisabled` / `isReadOnly` / `isRequired` / `isInvalid` | `boolean` | 当前状态 |
| `isHovered` / `isPressed` / `isFocused` / `isFocusVisible` | `boolean` | 单成员交互状态 |
组作用域提供 value、validationErrors、isDisabled/isReadOnly/isRequired/isInvalid,以及 state.value/state.setValue;单成员另提供 state.isSelected/state.setSelected/state.toggle。不把组值和单成员状态混用。
### 框架适配与边界
render 自定义根类型未支持;基线 div+data-custom 示例以 Vue 根属性透传保留效果。SSR 预规划直接/原生/Fragment 包裹的标签、说明、错误与直接成员;本库 Content 内的说明也支持。任意用户 Vue 包装组件无法静态展开,需要显式 aria-labelledby/aria-describedby 和 ID 保证首屏关联。没有宣称完整读屏、触屏、Nuxt 或逐像素验收。
## 相关组件
[Checkbox](./checkbox) · [CheckboxGroup](./checkbox-group) · [RadioGroup](./radio-group) · [Fieldset](./fieldset)