章节
组件
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
单选组用来呈现一组互斥的选项——选中其中一项,其余各项会自动取消选中。它基于 Base UI Radio Group 组件构建,并按新粗野主义的配方设计:粗边框、硬阴影、加粗字体。
适合以下场景:
- 套餐选择——把免费版、专业版、企业版做成选项卡片,强制只能选一个。
- 结账选项——配送时效或支付方式,每个选项都得一直摆在眼前。
- 2–5 个选项的设置项——主题、密度、通知频率,比下拉选择框少一步点击。
安装#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/radio-group.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/radio-group.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/radio-group.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/radio-group.json
安装以下依赖:
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/react
将以下代码复制并粘贴到您的项目中。
"use client"
import * as React from "react"
import { RadioGroup as RadioGroupPrimitive } from "radix-ui"
import { cn } from "@/lib/utils"
function RadioGroup({
className,
...props
}: React.ComponentProps<typeof RadioGroupPrimitive.Root>) {
return (
<RadioGroupPrimitive.Root
data-slot="radio-group"
className={cn("grid w-full gap-2", className)}
{...props}
/>
)
}
function RadioGroupItem({
className,
...props
}: React.ComponentProps<typeof RadioGroupPrimitive.Item>) {
return (
<RadioGroupPrimitive.Item
data-slot="radio-group-item"
className={cn(
"group/radio-group-item peer relative flex aspect-square size-5 shrink-0 rounded-full border-2 bg-input shadow-sm outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive",
className
)}
{...props}
>
<RadioGroupPrimitive.Indicator
data-slot="radio-group-indicator"
className="flex size-4 items-center justify-center"
>
<span className="absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2 rounded-full bg-primary" />
</RadioGroupPrimitive.Indicator>
</RadioGroupPrimitive.Item>
)
}
export { RadioGroup, RadioGroupItem }
更新导入路径以匹配您的项目结构。
用法#
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>组合#
使用以下组合来构建 RadioGroup:
RadioGroup
├── RadioGroupItem
└── RadioGroupItem示例#
描述#
使用 Field 组件为单选组的项目添加描述。
import {
Field,
FieldContent,选择卡片#
使用 FieldLabel 包裹整个 Field,即可实现可点击的卡片式选择。
import {
Field,
FieldContent,Fieldset#
使用 FieldSet 和 FieldLegend 将单选项目连同标签和描述组合在一起。
import {
Field,
FieldDescription,禁用#
使用 RadioGroup 上的 disabled 属性禁用所有项目。
import { Field, FieldLabel } from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
无效#
在 RadioGroupItem 上使用 aria-invalid、在 Field 上使用 data-invalid 以显示校验错误。
import {
Field,
FieldDescription,RTL#
若要在 Neobrutalism 中启用 RTL 支持,请参阅 RTL 配置指南。
"use client"
import * as React from "react"无障碍#
单选组遵循 WAI-ARIA Radio Group 模式:容器渲染为 role="radiogroup",每一项都是带 aria-checked 的 role="radio" 按钮,再由 roving tabindex 让整个组只占一个 Tab 停靠位。
键盘交互:
| 按键 | 操作 |
|---|---|
Tab / Shift + Tab | 把焦点移入组内(已选中项,否则第一项)或移出组外 |
Space | 若焦点所在项尚未选中,则将其选中 |
ArrowDown / ArrowRight | 把焦点移到下一项并选中它 |
ArrowUp / ArrowLeft | 把焦点移到上一项并选中它 |
在 RTL 下,左右方向键跟随阅读方向。
API 参考#
更多信息请参阅 Base UI 文档。