表单与选择@neumorphism-ui/radio-group

Radio Group

通过 React context 共享 name 和选中状态,同时每个项目都渲染为真实的 input[type=radio]。支持受控与非受控值、横向和纵向排列、disabled 及 required。

@neumorphism-ui/radio-group
Registry JSON

预览

安装

安装: radio-groupnpx shadcn@latest add @neumorphism-ui/radio-group

CLI 会依次处理 npm 依赖、base token、utils 和组件源码。

首次使用此 Registry?请先完成 namespace 配置 。

用法

用法: 安装npx shadcn@latest add @neumorphism-ui/radio-group @neumorphism-ui/label

Import

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

Example

Radio Group 用法代码
<RadioGroup
  defaultValue="comfortable"
  orientation="horizontal"
  aria-label="显示密度"
>
  <Label className="flex gap-2" htmlFor="density-compact">
    <RadioGroupItem id="density-compact" value="compact" />
    紧凑
  </Label>
  <Label className="flex gap-2" htmlFor="density-comfortable">
    <RadioGroupItem id="density-comfortable" value="comfortable" />
    舒适
  </Label>
</RadioGroup>

API 参考

组件属性类型默认值说明
RadioGroupvalue / defaultValuestring—受控的选中值或非受控初始值。
RadioGrouponValueChange(value: string) => void—选中值变化时调用。
RadioGrouporientation"horizontal" | "vertical""vertical"设置排列方向和 aria-orientation。
RadioGroupnamestringgenerated id各项目共享的原生 radio name。
RadioGroupdisabled / requiredbooleanfalse设置所有项目的输入限制。
RadioGroupItemvalue必填string—项目被选中时传给组合的值。

无障碍

  • 01

    在 RadioGroup 根元素上通过 aria-label 或 aria-labelledby 提供整个组合的名称。

  • 02

    将每个 RadioGroupItem 的 id 与 Label 的 htmlFor 关联。

  • 03

    由于使用真实的 radio input,会保留浏览器表单提交和键盘选择行为。