Segmented Control
Used to pick one choice from a linear set of options
Usage
import { SegmentGroup } from "@chakra-ui/react"<SegmentGroup.Root>
<SegmentGroup.Indicator />
<SegmentGroup.Item>
<SegmentGroup.ItemText />
<SegmentGroup.ItemHiddenInput />
</SegmentGroup.Item>
</SegmentGroup.Root>Shortcuts
The SegmentGroup component also provides a set of shortcuts for common use
cases.
SegmentGroupItems
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
Use the size prop to change the size of the segmented control.
size = xs
size = sm
size = md
size = lg
Controlled
Use the value and onValueChange props to control the selected item.
Hook Form
Here's an example of how to use the SegmentedControl with react-hook-form.
Vertical
By default, the segmented control is horizontal. Set the orientation prop to
vertical to change the orientation of the segmented control.
Disabled
Use the disabled prop to disable the segmented control.
Disabled Item
Use the disabled prop on the item to disable it.
Custom Indicator
Customize the indicator appearance using CSS variables like
--segment-indicator-bg and --segment-indicator-shadow.
Color Palette
By default, the segment control doesn't support changing the design via the
colorPalette prop. This example shows how to customize the segmented control
to make the colorPalette prop work.
Icon
Render the label as a ReactNode to render an icon.
Card
Here's an example of how to use the SegmentedControl within a Card.
Find your dream home
Props
Root
| Prop | Default | Type |
|---|---|---|
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.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | booleanWhether to remove the component's style. | |
defaultValue | stringThe initial value of the checked radio when rendered. Use when you don't need to control the value of the radio group. | |
disabled | booleanIf `true`, the radio group will be disabled | |
form | stringThe associate form of the underlying input. | |
id | stringThe 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 | stringThe name of the input fields in the radio (Useful for form submission). | |
onValueChange | (details: ValueChangeDetails) => voidFunction called once a radio is checked | |
orientation | 'horizontal' | 'vertical'Orientation of the radio group | |
readOnly | booleanWhether the checkbox is read-only | |
value | stringThe controlled value of the radio group |