Segment Group (세그먼트 그룹)
뷰 안의 섹션을 구성하고 그 사이를 탐색할 수 있게 해주는 컴포넌트예요. 라디오 그룹처럼 동작하면서 선택된 항목에 이동하는 인디케이터를 표시하는 시각적 컨트롤이에요.
출처: 문서
본문
구조 (Anatomy)
세그먼트 그룹의 기본 구조는 다음과 같아요. Root 안에 Indicator와 각 Item들을 배치해요.
<SegmentGroup.Root>
<SegmentGroup.Indicator />
<SegmentGroup.Item>
<SegmentGroup.ItemText />
<SegmentGroup.ItemControl />
<SegmentGroup.ItemHiddenInput />
</SegmentGroup.Item>
</SegmentGroup.Root>
예제 (Examples)
기본 (Basic)
defaultValue로 선택될 항목을 지정해요. 각 SegmentGroup.Item에는 고유한 value를 전달해요.
import { SegmentGroup } from '@ark-ui/react/segment-group'
import styles from 'styles/segment-group.module.css'
export const Basic = () => {
const frameworks = ['React', 'Solid', 'Svelte', 'Vue']
return (
<SegmentGroup.Root className={styles.Root} defaultValue="React">
<SegmentGroup.Indicator className={styles.Indicator} />
{frameworks.map((framework) => (
<SegmentGroup.Item className={styles.Item} key={framework} value={framework}>
<SegmentGroup.ItemText className={styles.ItemText}>{framework}</SegmentGroup.ItemText>
<SegmentGroup.ItemControl className={styles.ItemControl} />
<SegmentGroup.ItemHiddenInput />
</SegmentGroup.Item>
))}
</SegmentGroup.Root>
)
}
제어된 방식 (Controlled)
제어된 SegmentGroup 컴포넌트를 만들려면 value prop으로 현재 선택된 세그먼트를 관리하고, onValueChange 이벤트 핸들러가 호출될 때 갱신해요:
import { SegmentGroup } from '@ark-ui/react/segment-group'
import { useState } from 'react'
import styles from 'styles/segment-group.module.css'
export const Controlled = () => {
const frameworks = ['React', 'Solid', 'Svelte', 'Vue']
const [value, setValue] = useState<string | null>(null)
return (
<SegmentGroup.Root className={styles.Root} value={value} onValueChange={(e) => setValue(e.value)}>
<SegmentGroup.Indicator className={styles.Indicator} />
{frameworks.map((framework) => (
<SegmentGroup.Item className={styles.Item} key={framework} value={framework}>
<SegmentGroup.ItemText className={styles.ItemText}>{framework}</SegmentGroup.ItemText>
<SegmentGroup.ItemControl className={styles.ItemControl} />
<SegmentGroup.ItemHiddenInput />
</SegmentGroup.Item>
))}
</SegmentGroup.Root>
)
}
루트 프로바이더 (Root Provider)
세그먼트 그룹을 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useSegmentGroup 훅을 사용하는 거예요. 이렇게 하면 컴포넌트 밖에서 상태와 메서드에 접근할 수 있어요.
import { SegmentGroup, useSegmentGroup } from '@ark-ui/react/segment-group'
import styles from 'styles/segment-group.module.css'
export const RootProvider = () => {
const frameworks = ['React', 'Solid', 'Svelte', 'Vue']
const segmentGroup = useSegmentGroup({ defaultValue: 'React' })
return (
<div className="stack">
<SegmentGroup.RootProvider className={styles.Root} value={segmentGroup}>
<SegmentGroup.Indicator className={styles.Indicator} />
{frameworks.map((framework) => (
<SegmentGroup.Item className={styles.Item} key={framework} value={framework}>
<SegmentGroup.ItemText className={styles.ItemText}>{framework}</SegmentGroup.ItemText>
<SegmentGroup.ItemControl className={styles.ItemControl} />
<SegmentGroup.ItemHiddenInput />
</SegmentGroup.Item>
))}
</SegmentGroup.RootProvider>
<output>selected: {segmentGroup.value}</output>
</div>
)
}
비활성화 (Disabled)
세그먼트를 비활성화하려면 SegmentGroup.Item 컴포넌트에 disabled prop을 전달하기만 하면 돼요:
import { SegmentGroup } from '@ark-ui/react/segment-group'
import styles from 'styles/segment-group.module.css'
export const Disabled = () => {
const frameworks = ['React', 'Solid', 'Svelte', 'Vue']
return (
<SegmentGroup.Root className={styles.Root} defaultValue="React">
<SegmentGroup.Indicator className={styles.Indicator} />
{frameworks.map((framework) => (
<SegmentGroup.Item className={styles.Item} key={framework} value={framework} disabled={framework === 'Svelte'}>
<SegmentGroup.ItemText className={styles.ItemText}>{framework}</SegmentGroup.ItemText>
<SegmentGroup.ItemControl className={styles.ItemControl} />
<SegmentGroup.ItemHiddenInput />
</SegmentGroup.Item>
))}
</SegmentGroup.Root>
)
}
API 레퍼런스 (API Reference)
Root
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
defaultValue
Type: string
Required: false
Default Value: undefined
Description: The initial value of the checked radio when rendered.
Use when you don't need to control the value of the segment group.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: If true, the segment group will be disabled
form
Type: string
Required: false
Default Value: undefined
Description: The associate form of the underlying input.
id
Type: string
Required: false
Default Value: undefined
Description: The unique identifier of the machine.
ids
Type: Partial<{ root: string label: string indicator: string item: (value: string) => string itemLabel: (value: string) => string itemControl: (value: string) => string itemHiddenInput: (value: string) => string }>
Required: false
Default Value: undefined
Description: The ids of the elements in the radio. Useful for composition.
invalid
Type: boolean
Required: false
Default Value: undefined
Description: If true, the segment group is marked as invalid.
name
Type: string
Required: false
Default Value: undefined
Description: The name of the input fields in the radio
(Useful for form submission).
onValueChange
Type: (details: ValueChangeDetails) => void
Required: false
Default Value: undefined
Description: Function called once a radio is checked
orientation
Type: 'horizontal' | 'vertical'
Required: false
Default Value: undefined
Description: Orientation of the segment group
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: Whether the segment group is read-only
required
Type: boolean
Required: false
Default Value: undefined
Description: If true, the segment group is marked as required.
value
Type: string
Required: false
Default Value: undefined
Description: The controlled value of the segment group
데이터 속성 (Data Attributes)
data-scope: segment-group
data-part: root
data-orientation: The orientation of the segment-group
data-disabled: Present when disabled
data-invalid: Present when invalid
data-required: Present when required
Indicator
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: segment-group
data-part: indicator
data-disabled: Present when disabled
data-orientation: The orientation of the indicator
ItemControl
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: segment-group
data-part: item-control
data-active: Present when active or pressed
ItemHiddenInput
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Item
Props
value
Type: string
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: undefined
invalid
Type: boolean
Required: false
Default Value: undefined
Description: undefined
ItemText
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Label
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: segment-group
data-part: label
data-orientation: The orientation of the label
data-disabled: Present when disabled
data-invalid: Present when invalid
data-required: Present when required
RootProvider
Props
value
Type: UseRadioGroupReturn
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
접근성 (Accessibility)
Radio WAI-ARIA design pattern을 준수해요.
키보드 지원 (Keyboard Support)
Tab
Description: Moves focus to either the checked radio item or the first radio item in the group.
Space
Description: When focus is on an unchecked radio item, checks it.
ArrowDown
Description: Moves focus and checks the next radio item in the group.
ArrowRight
Description: Moves focus and checks the next radio item in the group.
ArrowUp
Description: Moves focus to the previous radio item in the group.
ArrowLeft
Description: Moves focus to the previous radio item in the group.