Skip to Content
文档

单选按钮卡片

用于从列表中选择一个选项

SourceStorybookRecipeArk

用法

¥Usage

import { RadioCard } from "@chakra-ui/react"
<RadioCard.Root>
  <RadioCard.Label />
  <RadioCard.Item>
    <RadioCard.ItemHiddenInput />
    <RadioCard.ItemControl>
      <RadioCard.ItemContent>
        <RadioCard.ItemText />
        <RadioCard.ItemDescription />
      </RadioCard.ItemContent>
      <RadioCard.ItemIndicator />
    </RadioCard.ItemControl>
  </RadioCard.Item>
</RadioCard.Root>
info

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

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

示例

¥Examples

描述

¥Description

以下是如何在单选按钮中添加更多描述的示例。

¥Here's an example of how to add some further description to the radio card.

尺寸

¥Sizes

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

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

颜色

¥Colors

colorPalette 属性传递给 RadioCard.Root 组件,即可更改单选按钮的颜色。

¥Pass the colorPalette prop to the RadioCard.Root component to change the color of the radio card.

变量

¥Variants

variant 属性传递给 RadioCard.Root 组件,以更改单选按钮卡片的视觉样式。

¥Pass the variant prop to the RadioCard.Root component to change the visual style of the radio card.

图标

¥Icon

将自定义图标放置在 RadioCard.ItemContent 中,在单选按钮卡片内渲染该图标。

¥Render a custom icon inside the radio card by placing it within RadioCard.ItemContent.

受控

¥Controlled

valueonValueChange 属性传递给 RadioCard.Root 组件以控制所选的单选卡片。

¥Pass the value and onValueChange props to the RadioCard.Root component to control the selected radio card.

无指示器

¥No Indicator

以下是如何在不使用指示器的情况下使用单选按钮卡片的示例。

¥Here's an example of how to use the radio card without an indicator.

无指示器(垂直)

¥No Indicator (Vertical)

这是一个没有指示器且内容垂直对齐的单选按钮卡示例。

¥Here's an example of a radio card with no indicator and content aligned vertically.

居中

¥Centered

这是一个文本居中的单选按钮卡示例。

¥Here's an example of a radio card with centered text.

合成

¥Composition

以下是将 RadioCard 与 Group 组件组合的示例。

¥Here's an example of composing the RadioCard with the Group component.

插件

¥Addon

使用 RadioCard.ItemAddon 组件为单选按钮卡片添加元数据。

¥Use the RadioCard.ItemAddon component to add metadata to the radio card.

已关闭组件

¥Closed Component

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

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

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

interface RadioCardItemProps extends RadioCard.ItemProps {
  icon?: React.ReactElement
  label?: React.ReactNode
  description?: React.ReactNode
  addon?: React.ReactNode
  indicator?: React.ReactNode | null
  indicatorPlacement?: "start" | "end" | "inside"
  inputProps?: React.InputHTMLAttributes<HTMLInputElement>
}

export const RadioCardItem = React.forwardRef<
  HTMLInputElement,
  RadioCardItemProps
>(function RadioCardItem(props, ref) {
  const {
    inputProps,
    label,
    description,
    addon,
    icon,
    indicator = <RadioCard.ItemIndicator />,
    indicatorPlacement = "end",
    ...rest
  } = props

  const hasContent = label || description || icon
  const ContentWrapper = indicator ? RadioCard.ItemContent : React.Fragment

  return (
    <RadioCard.Item {...rest}>
      <RadioCard.ItemHiddenInput ref={ref} {...inputProps} />
      <RadioCard.ItemControl>
        {indicatorPlacement === "start" && indicator}
        {hasContent && (
          <ContentWrapper>
            {icon}
            {label && <RadioCard.ItemText>{label}</RadioCard.ItemText>}
            {description && (
              <RadioCard.ItemDescription>
                {description}
              </RadioCard.ItemDescription>
            )}
            {indicatorPlacement === "inside" && indicator}
          </ContentWrapper>
        )}
        {indicatorPlacement === "end" && indicator}
      </RadioCard.ItemControl>
      {addon && <RadioCard.ItemAddon>{addon}</RadioCard.ItemAddon>}
    </RadioCard.Item>
  )
})

export const RadioCardRoot = RadioCard.Root
export const RadioCardLabel = RadioCard.Label
export const RadioCardItemIndicator = RadioCard.ItemIndicator

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

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

npx @chakra-ui/cli snippet add radio-card

以下是如何使用它

¥Here's how to use the it

<RadioCardRoot>
  <RadioCardLabel />
  <RadioCardItem />
</RadioCardRoot>

属性

¥Props

根元素

¥Root

PropDefaultType
orientation 'horizontal'
'vertical' | 'horizontal'

The orientation of the component

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

The color palette of the component

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

The size of the component

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

The variant of the component

align 'start'
'start' | 'end' | 'center'

The align 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

readOnly
boolean

Whether the checkbox is read-only

value
string

The controlled value of the radio group

justify
'start' | 'end' | 'center'

The justify of the component

Previous

PIN 码输入

Next

单选按钮