Skip to Content
文档

复选框

用于在表单中,当用户需要从多个选项中选择多个值时使用。

SourceStorybookRecipeArk

用法

¥Usage

import { Checkbox } from "@chakra-ui/react"
<Checkbox.Root>
  <Checkbox.HiddenInput />
  <Checkbox.Control>
    <Checkbox.Indicator />
  </Checkbox.Control>
  <Checkbox.Label />
</Checkbox.Root>
info

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

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

快捷键

¥Shortcuts

Checkbox 组件还提供了一组用于常见用例的快捷方式。

¥The Checkbox component also provides a set of shortcuts for common use cases.

CheckboxControl

此组件默认渲染其中的 Checkbox.Indicator

¥This component renders the Checkbox.Indicator within it by default.

有效:

¥This works:

<Checkbox.Control>
  <Checkbox.Indicator />
</Checkbox.Control>

如果你不需要自定义指示器,这可能会更简洁:

¥This might be more concise, if you don't need to customize the indicator:

<Checkbox.Control />

示例

¥Examples

变量

¥Variants

variant 属性传递给 Checkbox.Root 组件,以更改复选框的视觉样式。

¥Pass the variant prop to the Checkbox.Root component to change the visual style of the checkbox.

outline

subtle

solid

颜色

¥Colors

colorPalette 属性传递给 Checkbox.Root 组件,即可更改复选框的颜色。

¥Pass the colorPalette prop to the Checkbox.Root component to change the color of the checkbox.

gray

red

green

blue

teal

pink

purple

cyan

orange

yellow

尺寸

¥Sizes

size 属性传递给 Checkbox.Root 组件,即可更改复选框的大小。

¥Pass the size prop to the Checkbox.Root component to change the size of the checkbox.

状态

¥States

disabledinvalid 属性传递给 Checkbox.Root 组件,以更改复选框的视觉状态。

¥Pass the disabled or invalid prop to the Checkbox.Root component to change the visual state of the checkbox.

受控

¥Controlled

使用 checkedonCheckedChange 属性控制复选框的状态。

¥Use the checked and onCheckedChange props to control the state of the checkbox.

标签位置

¥Label Position

以下是如何将标签位置向右移动的示例。

¥Here's an example of how to change the label position to the right.

商店

¥Store

控制复选框的另一种方法是使用 RootProvider 组件和 useCheckbox 存储钩子。

¥An alternative way to control the checkbox is to use the RootProvider component and the useCheckbox store hook.

这样,你就可以从复选框外部访问复选框的状态和方法。

¥This way you can access the checkbox state and methods from outside the checkbox.

合成

¥Composition

以下是如何将复选框与字段组件组合的示例。

¥Here's an example of how to compose a checkbox with a field component.

钩子表单

¥Hook Form

以下是如何将 Checkbox 组件与 react-hook-form 库结合使用的示例。

¥Here's an example of how to use the Checkbox component with the react-hook-form library.

Checked: false

¥Group

使用 CheckboxGroup 组件将多个复选框分组。

¥Use the CheckboxGroup component to group multiple checkboxes together.

Select framework

组钩子表单

¥Group Hook Form

以下是如何将 CheckboxGroup 组件与 react-hook-form 库结合使用的示例。

¥Here's an example of how to use the CheckboxGroup component with the react-hook-form library.

Select your framework
Values: []

自定义图标

¥Custom Icon

Checkbox.Control 中渲染自定义图标,以更改复选框的图标。

¥Render a custom icon within Checkbox.Control to change the icon of the checkbox.

不确定

¥Indeterminate

checked 属性设置为 indeterminate 可显示不确定状态下的复选框。

¥Set the checked prop to indeterminate to show the checkbox in an indeterminate state.

描述

¥Description

以下是如何在复选框中添加更多描述的示例。

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

链接

¥Link

Checkbox.Label 中渲染锚标签,以向标签添加链接。

¥Render an anchor tag within the Checkbox.Label to add a link to the label.

已关闭组件

¥Closed Component

以下是如何为封闭组件组合设置复选框。

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

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

export interface CheckboxProps extends ChakraCheckbox.RootProps {
  icon?: React.ReactNode
  inputProps?: React.InputHTMLAttributes<HTMLInputElement>
  rootRef?: React.RefObject<HTMLLabelElement | null>
}

export const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
  function Checkbox(props, ref) {
    const { icon, children, inputProps, rootRef, ...rest } = props
    return (
      <ChakraCheckbox.Root ref={rootRef} {...rest}>
        <ChakraCheckbox.HiddenInput ref={ref} {...inputProps} />
        <ChakraCheckbox.Control>
          {icon || <ChakraCheckbox.Indicator />}
        </ChakraCheckbox.Control>
        {children != null && (
          <ChakraCheckbox.Label>{children}</ChakraCheckbox.Label>
        )}
      </ChakraCheckbox.Root>
    )
  },
)

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

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

npx @chakra-ui/cli snippet add checkbox

以下是如何使用它

¥Here's how to use the it

<Checkbox>Accept terms and conditions</Checkbox>

属性

¥Props

根元素

¥Root

PropDefaultType
value '\'on\''
string

The value of checkbox input. Useful for form submission.

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

The color palette of the component

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

The size of the component

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

The variant 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.

checked
CheckedState

The controlled checked state of the checkbox

defaultChecked
CheckedState

The initial checked state of the checkbox when rendered. Use when you don't need to control the checked state of the checkbox.

disabled
boolean

Whether the checkbox is disabled

form
string

The id of the form that the checkbox belongs to.

id
string

The unique identifier of the machine.

ids
Partial<{ root: string; hiddenInput: string; control: string; label: string }>

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

invalid
boolean

Whether the checkbox is invalid

name
string

The name of the input field in a checkbox. Useful for form submission.

onCheckedChange
(details: CheckedChangeDetails) => void

The callback invoked when the checked state changes.

readOnly
boolean

Whether the checkbox is read-only

required
boolean

Whether the checkbox is required

Previous

下载触发器

Next

复选框卡片