106k
New

单选框组

一组可选按钮——称为单选按钮——一次最多只能选中一个按钮。

import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"

安装

pnpm dlx shadcn@latest add radio-group

用法

import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
<RadioGroup defaultValue="option-one">
  <div className="flex items-center gap-3">
    <RadioGroupItem value="option-one" id="option-one" />
    <Label htmlFor="option-one">选项一</Label>
  </div>
  <div className="flex items-center gap-3">
    <RadioGroupItem value="option-two" id="option-two" />
    <Label htmlFor="option-two">选项二</Label>
  </div>
</RadioGroup>

示例

描述

使用 Field 组件添加描述的单选组项。

Standard spacing for most use cases.

More space between elements.

Minimal spacing for dense layouts.

import {
  Field,
  FieldContent,

选择卡片

使用 FieldLabel 包裹整个 Field,实现可点击的卡片式选择。

import {
  Field,
  FieldContent,

字段集

使用 FieldSetFieldLegend 对单选项进行分组,并添加标签和描述。

Subscription Plan

Yearly and lifetime plans offer significant savings.

import {
  Field,
  FieldDescription,

禁用

RadioGroupItem 上使用 disabled 属性禁用单个选项。

import { Field, FieldLabel } from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"

无效

RadioGroupItem 上使用 aria-invalid,在 Field 上使用 data-invalid 来显示验证错误。

Notification Preferences

Choose how you want to receive notifications.

import {
  Field,
  FieldDescription,

API 参考

请参阅 Radix UI 单选框组 文档。