Skip to Content
文档

分段控件

用于从一组线性选项中选择一个选项。

StorybookRecipeArk

用法

¥Usage

import { SegmentGroup } from "@chakra-ui/react"
<SegmentGroup.Root>
  <SegmentGroup.Indicator />
  <SegmentGroup.Item>
    <SegmentGroup.ItemText />
    <SegmentGroup.ItemHiddenInput />
  </SegmentGroup.Item>
</SegmentGroup.Root>

快捷键

¥Shortcuts

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

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

SegmentGroupItems

SegmentGroupItems 快捷方式根据 items 属性渲染项目列表。

¥The SegmentGroupItems shortcut renders a list of items based on the items prop.

有效:

¥This works:

<>
  {items.map((item) => (
    <SegmentGroup.Item key={item.value} value={item.value}>
      <SegmentGroup.ItemText>{item.label}</SegmentGroup.ItemText>
      <SegmentGroup.ItemHiddenInput />
    </SegmentGroup.Item>
  ))}
</>

如果你不需要自定义项目,这可能会更简洁:

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

<SegmentGroup.Items items={items} />

示例

¥Examples

尺寸

¥Sizes

使用 size 属性更改分段控件的大小。

¥Use the size prop to change the size of the segmented control.

size = xs

size = sm

size = md

size = lg

受控

¥Controlled

使用 valueonValueChange 属性控制所选项目。

¥Use the value and onValueChange props to control the selected item.

钩子表单

¥Hook Form

以下是如何将 SegmentedControlreact-hook-form 一起使用的示例。

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

垂直

¥Vertical

默认情况下,分段控件是水平的。将 orientation 属性设置为 vertical 可更改分段控件的方向。

¥By default, the segmented control is horizontal. Set the orientation prop to vertical to change the orientation of the segmented control.

已禁用

¥Disabled

使用 disabled 属性禁用分段控件。

¥Use the disabled prop to disable the segmented control.

已禁用项目

¥Disabled Item

使用项目上的 disabled 属性将其禁用。

¥Use the disabled prop on the item to disable it.

图标

¥Icon

label 渲染为 ReactNode 以渲染图标。

¥Render the label as a ReactNode to render an icon.

卡片

¥Card

以下是如何在 Card 中使用 SegmentedControl 的示例。

¥Here's an example of how to use the SegmentedControl within a Card.

Find your dream home

属性

¥Props

根元素

¥Root

PropDefaultType
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

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

选择(原生)