ProComponents, templates & AI tooling
2.3k

RadioGroup 单选框组

单选按钮组,同一时间只能选中一个选项。

导入

import { RadioGroup } from 'heroui-native';

结构

<RadioGroup>
  <RadioGroup.Item>
    <Label>...</Label>
    <Description>...</Description>
    <Radio>
      <Radio.Indicator>
        <Radio.IndicatorThumb />
      </Radio.Indicator>
    </Radio>
  </RadioGroup.Item>
  <FieldError>...</FieldError>
</RadioGroup>
  • RadioGroup:管理单选项选中状态的容器,支持横向与纵向布局。
  • RadioGroup.Item:组内单个选项,必须放在 RadioGroup 内。处理选中状态;在仅提供文本子节点时会渲染默认 <Radio /> 指示器。支持渲染函数子节点以访问状态(isSelectedisInvalidisDisabled)。
  • Label:可选的可点击文字标签,与单选项关联以提升无障碍。请直接使用 Label 组件。
  • Description:标签下方的可选说明文字。请直接使用 Description 组件。
  • Radio:置于 RadioGroup.Item 内的 Radio 组件,用于渲染单选指示器。会自动识别 RadioGroupItem 上下文并从中获取 isSelectedisDisabledisInvalidvariant
  • Radio.Indicator:单选圆环的可选容器;无子节点时渲染默认拇指样式,管理选中视觉。完整 API 见 Radio
  • Radio.IndicatorThumb:选中时显示的可选内圆,随选中状态缩放动画;可替换为自定义内容。见 Radio
  • FieldError:在组无效时显示的错误信息,带动画显示在组内容下方。请直接使用 FieldError 组件。

用法

基础用法

使用简单字符串子节点时,会自动渲染标题与指示器。

<RadioGroup value={value} onValueChange={setValue}>
  <RadioGroup.Item value="option1">选项 1</RadioGroup.Item>
  <RadioGroup.Item value="option2">选项 2</RadioGroup.Item>
  <RadioGroup.Item value="option3">选项 3</RadioGroup.Item>
</RadioGroup>

带说明文字

在每个选项下方添加描述以补充上下文。

import { RadioGroup, Radio, Label, Description } from 'heroui-native';
import { View } from 'react-native';

<RadioGroup value={value} onValueChange={setValue}>
  <RadioGroup.Item value="standard">
    <View>
      <Label>标准配送</Label>
      <Description>5–7 个工作日送达</Description>
    </View>
    <Radio />
  </RadioGroup.Item>
  <RadioGroup.Item value="express">
    <View>
      <Label>加急配送</Label>
      <Description>2–3 个工作日送达</Description>
    </View>
    <Radio />
  </RadioGroup.Item>
</RadioGroup>;

自定义指示器

使用 Radio 子组件将默认拇指替换为自定义内容。

import { RadioGroup, Radio, Label } from 'heroui-native';

<RadioGroup value={value} onValueChange={setValue}>
  <RadioGroup.Item value="custom">
    {({ isSelected }) => (
      <>
        <Label>自定义选项</Label>
        <Radio>
          <Radio.Indicator>
            {isSelected && (
              <Animated.View entering={FadeIn}>
                <Icon name="check" size={12} />
              </Animated.View>
            )}
          </Radio.Indicator>
        </Radio>
      </>
    )}
  </RadioGroup.Item>
</RadioGroup>;

使用渲染函数

RadioGroup.Item 上使用渲染函数以访问状态并自定义整块内容。

import { RadioGroup, Radio, Label } from 'heroui-native';

<RadioGroup value={value} onValueChange={setValue}>
  <RadioGroup.Item value="option1">
    {({ isSelected, isInvalid, isDisabled }) => (
      <>
        <Label>选项 1</Label>
        <Radio>
          <Radio.Indicator>{isSelected && <CustomIcon />}</Radio.Indicator>
        </Radio>
      </>
    )}
  </RadioGroup.Item>
</RadioGroup>;

显示错误信息

在单选组下方展示校验错误。

import { RadioGroup, FieldError } from 'heroui-native';

function RadioGroupWithError() {
  const [value, setValue] = React.useState<string | undefined>(undefined);

  return (
    <RadioGroup value={value} onValueChange={setValue} isInvalid={!value}>
      <RadioGroup.Item value="agree">我同意条款</RadioGroup.Item>
      <RadioGroup.Item value="disagree">我不同意</RadioGroup.Item>
      <FieldError isInvalid={!value}>
        请选择一项以继续
      </FieldError>
    </RadioGroup>
  );
}

示例

import {
  Description,
  Label,
  Radio,
  RadioGroup,
  Separator,
  Surface,
} from 'heroui-native';
import React from 'react';
import { View } from 'react-native';

export default function RadioGroupExample() {
  const [selection, setSelection] = React.useState('desc1');

  return (
    <Surface className="w-full">
      <RadioGroup value={selection} onValueChange={setSelection}>
        <RadioGroup.Item value="desc1">
          <View>
            <Label>标准配送</Label>
            <Description>5–7 个工作日送达</Description>
          </View>
          <Radio />
        </RadioGroup.Item>
        <Separator className="my-1" />
        <RadioGroup.Item value="desc2">
          <View>
            <Label>加急配送</Label>
            <Description>2–3 个工作日送达</Description>
          </View>
          <Radio />
        </RadioGroup.Item>
        <Separator className="my-1" />
        <RadioGroup.Item value="desc3">
          <View>
            <Label>次日达</Label>
            <Description>下一个工作日送达</Description>
          </View>
          <Radio />
        </RadioGroup.Item>
      </RadioGroup>
    </Surface>
  );
}

更多示例见 GitHub 仓库

API 参考

RadioGroup

proptypedefaultdescription
childrenReact.ReactNodeundefined单选组内容
valuestring | undefinedundefined当前选中值
onValueChange(val: string) => voidundefined选中值变化时的回调
isDisabledbooleanfalse是否禁用整个单选组
isInvalidbooleanfalse组是否处于无效状态
variant'primary' | 'secondary'undefined单选组样式变体(子项未单独设置时继承)
animation"disable-all" | undefinedundefined动画配置。使用 "disable-all" 可关闭包含子节点在内的全部动画
classNamestringundefined自定义 className
...ViewPropsViewProps-支持全部标准 React Native View 属性

RadioGroup.Item

proptypedefaultdescription
childrenReact.ReactNode | ((props: RadioGroupItemRenderProps) => React.ReactNode)undefined选项内容,或用于自定义项的渲染函数
valuestringundefined该选项关联的值
isDisabledbooleanfalse是否禁用该选项
isInvalidbooleanfalse该选项是否无效
variant'primary' | 'secondary''primary'该选项的样式变体
hitSlopnumber6可点击区域的热区扩展
classNamestringundefined自定义 className
...PressablePropsPressableProps-支持全部标准 Pressable 属性(不含 disabled)

RadioGroupItemRenderProps

proptypedescription
isSelectedboolean该选项是否选中
isInvalidboolean该选项是否无效
isDisabledboolean该选项是否禁用

Radio(位于 RadioGroup.Item 内)

Radio 放在 RadioGroup.Item 内用于渲染单选指示器。此时会自动识别 RadioGroupItem 上下文并从中获取 isSelectedisDisabledisInvalidvariant,无需手动传参。

使用 <Radio /> 获得默认指示器,或组合 Radio.IndicatorRadio.IndicatorThumb 自定义样式。

proptypedefaultdescription
childrenReact.ReactNode | ((props: RadioRenderProps) => React.ReactNode)undefined子元素或渲染函数以自定义单选
variant'primary' | 'secondary''primary'单选视觉变体
isSelectedbooleanundefined是否选中
isDisabledbooleanundefined是否禁用且不可交互
isInvalidbooleanfalse是否无效(危险色)
classNamestringundefined额外 CSS 类
animationRadioRootAnimation-单选根动画配置
onSelectedChange(isSelected: boolean) => voidundefined选中状态变化时的回调
...PressablePropsPressableProps-支持全部标准 Pressable 属性(不含 disabled)

RadioRenderProps

proptypedescription
isSelectedboolean是否选中
isDisabledboolean是否禁用
isInvalidboolean是否无效

RadioRootAnimation

单选根组件的动画配置,可为:

  • "disable-all":关闭包含子节点(Indicator、IndicatorThumb)在内的全部动画
  • undefined:使用默认动画

Radio.Indicator

proptypedefaultdescription
childrenReact.ReactNodeundefined指示器内容
classNamestringundefined指示器额外 CSS 类
...AnimatedViewPropsAnimatedProps<ViewProps>-支持全部 Reanimated Animated.View 属性

Radio.IndicatorThumb

proptypedefaultdescription
classNamestringundefined拇指区域额外 CSS 类
animationRadioIndicatorThumbAnimation-拇指动画配置
isAnimatedStyleActivebooleantrue是否启用 Reanimated 动画样式
...AnimatedViewPropsAnimatedProps<ViewProps>-支持全部 Reanimated Animated.View 属性

RadioIndicatorThumbAnimation

单选指示器拇指的动画配置,可为:

  • false"disabled":关闭全部动画
  • trueundefined:使用默认动画
  • object:自定义动画配置
proptypedefaultdescription
state'disabled' | boolean-在自定义属性时用于禁用动画
scale.value[number, number][1.5, 1]缩放值 [未选中, 已选中]
scale.timingConfigWithTimingConfig{ duration: 300, easing: Easing.out(Easing.ease) }动画时间配置

说明: 标签、说明与错误信息请直接使用基础组件:

Hooks

useRadioGroup

返回值

属性类型描述
valuestring | undefined当前选中值
isDisabledboolean单选组是否禁用
isInvalidboolean单选组是否无效
variant'primary' | 'secondary'单选组样式变体
onValueChange(value: string) => void修改选中值的函数

useRadioGroupItem

返回值

属性类型描述
isSelectedboolean该选项是否选中
isDisabledboolean | undefined该选项是否禁用
isInvalidboolean | undefined该选项是否无效
variant'primary' | 'secondary' | undefined该选项的样式变体
onSelectedChange((isSelected: boolean) => void) | undefined修改选中状态的回调(在组内选中该项)

本页目录