import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
安装
🌐 Installation
pnpm dlx shadcn@latest add radio-group
用法
🌐 Usage
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">Option One</Label>
</div>
<div className="flex items-center gap-3">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</div>
</RadioGroup>示例
🌐 Examples
描述
🌐 Description
使用 Field 组件的带描述的单选组项目。
🌐 Radio group items with a description using the Field component.
Standard spacing for most use cases.
More space between elements.
Minimal spacing for dense layouts.
import {
Field,
FieldContent,选择卡
🌐 Choice Card
使用 FieldLabel 封装整个 Field 以实现可点击的卡片式选择。
🌐 Use FieldLabel to wrap the entire Field for a clickable card-style selection.
import {
Field,
FieldContent,字段集
🌐 Fieldset
使用 FieldSet 和 FieldLegend 来对带有标签和描述的单选项进行分组。
🌐 Use FieldSet and FieldLegend to group radio items with a label and description.
import {
Field,
FieldDescription,已禁用
🌐 Disabled
在 RadioGroupItem 上使用 disabled 属性来禁用单个项目。
🌐 Use the disabled prop on RadioGroupItem to disable individual items.
import { Field, FieldLabel } from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
无效
🌐 Invalid
在 RadioGroupItem 上使用 aria-invalid,在 Field 上使用 data-invalid 来显示验证错误。
🌐 Use aria-invalid on RadioGroupItem and data-invalid on Field to show validation errors.
import {
Field,
FieldDescription,从右到左
🌐 RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
🌐 To enable RTL support in shadcn/ui, see the RTL configuration guide.
تباعد قياسي لمعظم حالات الاستخدام.
مساحة أكبر بين العناصر.
تباعد أدنى للتخطيطات الكثيفة.
"use client"
import * as React from "react"API参考
🌐 API Reference
请参阅 Radix UI 单选组 文档。
🌐 See the Radix UI Radio Group documentation.