Skip to Content
文档

单选按钮

用于从多个选项中选择一个选项

StorybookRecipeArk

用法

¥Usage

import { RadioGroup } from "@chakra-ui/react"
<RadioGroup.Root>
  <RadioGroup.Item>
    <RadioGroup.ItemHiddenInput />
    <RadioGroup.ItemIndicator />
    <RadioGroup.ItemText />
  </RadioGroup.Item>
</RadioGroup.Root>
info

如果你更喜欢封闭的组件组合,请查看 以下代码片段

¥If you prefer a closed component composition, check out the snippet below.

示例

¥Examples

受控

¥Controlled

valueonValueChange 属性传递给 RadioGroup.Root 组件,以控制选中的单选按钮。

¥Pass the value and onValueChange props to the RadioGroup.Root component to control the selected radio button.

颜色

¥Colors

colorPalette 属性传递给 RadioGroup.Root 组件,即可更改组件的配色方案。

¥Pass the colorPalette prop to the RadioGroup.Root component to change the color scheme of the component.

gray

red

green

blue

teal

pink

purple

cyan

orange

yellow

尺寸

¥Sizes

size 属性传递给 RadioGroup.Root 组件,以更改单选按钮组件的大小。

¥Pass the size prop to the RadioGroup.Root component to change the size of the radio component.

变量

¥Variants

variant 属性传递给 RadioGroup.Root 组件以更改单选按钮组件的外观。

¥Pass the variant prop to the RadioGroup.Root component to change the appearance of the radio component.

已禁用

¥Disabled

disabled 属性传递给 RadioGroup.Item 组件,以禁用单选框。

¥Pass the disabled prop to the RadioGroup.Item component to make the radio disabled.

钩子表单

¥Hook Form

使用 react-hook-form 中的 Controller 组件控制表单中的单选按钮组。

¥Use the Controller component from react-hook-form to control the radio group within a form

Select value

已关闭组件

¥Closed Component

以下是如何设置封闭组件组合的单选按钮。

¥Here's how to setup the Radio for a closed component composition.

import { RadioGroup as ChakraRadioGroup } from "@chakra-ui/react"
import * as React from "react"

export interface RadioProps extends ChakraRadioGroup.ItemProps {
  rootRef?: React.RefObject<HTMLDivElement | null>
  inputProps?: React.InputHTMLAttributes<HTMLInputElement>
}

export const Radio = React.forwardRef<HTMLInputElement, RadioProps>(
  function Radio(props, ref) {
    const { children, inputProps, rootRef, ...rest } = props
    return (
      <ChakraRadioGroup.Item ref={rootRef} {...rest}>
        <ChakraRadioGroup.ItemHiddenInput ref={ref} {...inputProps} />
        <ChakraRadioGroup.ItemIndicator />
        {children && (
          <ChakraRadioGroup.ItemText>{children}</ChakraRadioGroup.ItemText>
        )}
      </ChakraRadioGroup.Item>
    )
  },
)

export const RadioGroup = ChakraRadioGroup.Root

如果你想自动将封闭式组件添加到项目中,请运行以下命令:

¥If you want to automatically add the closed component to your project, run the command:

npx @chakra-ui/cli snippet add radio

以下是如何使用它:

¥Here's how to use it:

<RadioGroup>
  <Radio />
</RadioGroup>

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

variant 'solid'
'outline' | 'subtle' | 'solid'

The variant of the component

size 'md'
'xs' | 'sm' | 'md' | 'lg'

The size of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

defaultValue
string

The initial value of the checked radio when rendered. Use when you don't need to control the value of the radio group.

disabled
boolean

If `true`, the radio group will be disabled

form
string

The associate form of the underlying input.

id
string

The unique identifier of the machine.

ids
Partial<{ root: string label: string indicator: string item: (value: string) => string itemLabel: (value: string) => string itemControl: (value: string) => string itemHiddenInput: (value: string) => string }>

The ids of the elements in the radio. Useful for composition.

name
string

The name of the input fields in the radio (Useful for form submission).

onValueChange
(details: ValueChangeDetails) => void

Function called once a radio is checked

orientation
'horizontal' | 'vertical'

Orientation of the radio group

readOnly
boolean

Whether the checkbox is read-only

value
string

The controlled value of the radio group

Previous

单选按钮卡片

Next

评分