Radio Group (라디오 그룹)
여러 옵션 중에서 하나만 선택할 수 있게 해주는 컴포넌트예요. 라디오 그룹은 선택지를 한 번에 하나만 고르는 폼 컨트롤에 적합해요.
출처: 문서
본문
구조 (Anatomy)
라디오 그룹의 기본 구조는 다음과 같아요. Root 안에 Label, Indicator, 그리고 각 Item들을 배치해요.
<RadioGroup.Root>
<RadioGroup.Label />
<RadioGroup.Indicator />
<RadioGroup.Item>
<RadioGroup.ItemControl />
<RadioGroup.ItemText />
<RadioGroup.ItemHiddenInput />
</RadioGroup.Item>
</RadioGroup.Root>
예제 (Examples)
기본 (Basic)
defaultValue로 선택될 항목을 지정해요. 각 RadioGroup.Item에는 고유한 value를 전달해요.
import { RadioGroup } from '@ark-ui/react/radio-group'
import styles from 'styles/radio-group.module.css'
export const Basic = () => {
const frameworks = ['React', 'Solid', 'Vue']
return (
<RadioGroup.Root className={styles.Root} defaultValue="React">
<RadioGroup.Label className={styles.Label}>Framework</RadioGroup.Label>
{frameworks.map((framework) => (
<RadioGroup.Item className={styles.Item} key={framework} value={framework}>
<RadioGroup.ItemControl className={styles.ItemControl} />
<RadioGroup.ItemText className={styles.ItemText}>{framework}</RadioGroup.ItemText>
<RadioGroup.ItemHiddenInput />
</RadioGroup.Item>
))}
</RadioGroup.Root>
)
}
초기 값 (Initial Value)
라디오 그룹의 초기 값을 설정하려면 defaultValue prop에 기본적으로 선택할 라디오 항목의 값을 지정하면 돼요.
import { RadioGroup } from '@ark-ui/react/radio-group'
import styles from 'styles/radio-group.module.css'
export const InitialValue = () => {
const frameworks = ['React', 'Solid', 'Vue']
return (
<RadioGroup.Root className={styles.Root} defaultValue="Solid">
<RadioGroup.Label className={styles.Label}>Framework</RadioGroup.Label>
{frameworks.map((framework) => (
<RadioGroup.Item className={styles.Item} key={framework} value={framework}>
<RadioGroup.ItemControl className={styles.ItemControl} />
<RadioGroup.ItemText className={styles.ItemText}>{framework}</RadioGroup.ItemText>
<RadioGroup.ItemHiddenInput />
</RadioGroup.Item>
))}
</RadioGroup.Root>
)
}
제어된 방식 (Controlled)
제어된 Radio Group으로 만들려면 value prop으로 상태를 관리하고, onValueChange 이벤트 핸들러가 호출될 때 상태를 갱신해요:
import { RadioGroup } from '@ark-ui/react/radio-group'
import { useState } from 'react'
import styles from 'styles/radio-group.module.css'
export const Controlled = () => {
const frameworks = ['React', 'Solid', 'Vue']
const [value, setValue] = useState<string | null>(null)
return (
<RadioGroup.Root className={styles.Root} value={value} onValueChange={(e) => setValue(e.value)}>
<RadioGroup.Label className={styles.Label}>Framework</RadioGroup.Label>
{frameworks.map((framework) => (
<RadioGroup.Item className={styles.Item} key={framework} value={framework}>
<RadioGroup.ItemControl className={styles.ItemControl} />
<RadioGroup.ItemText className={styles.ItemText}>{framework}</RadioGroup.ItemText>
<RadioGroup.ItemHiddenInput />
</RadioGroup.Item>
))}
</RadioGroup.Root>
)
}
루트 프로바이더 (Root Provider)
라디오 그룹을 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useRadioGroup 훅을 사용하는 거예요. 이렇게 하면 컴포넌트 밖에서 상태와 메서드에 접근할 수 있어요.
import { RadioGroup, useRadioGroup } from '@ark-ui/react/radio-group'
import button from 'styles/button.module.css'
import styles from 'styles/radio-group.module.css'
export const RootProvider = () => {
const frameworks = ['React', 'Solid', 'Vue']
const radioGroup = useRadioGroup({ defaultValue: 'React' })
return (
<div className="stack">
<RadioGroup.RootProvider className={styles.Root} value={radioGroup}>
<RadioGroup.Label className={styles.Label}>Framework</RadioGroup.Label>
{frameworks.map((framework) => (
<RadioGroup.Item className={styles.Item} key={framework} value={framework}>
<RadioGroup.ItemControl className={styles.ItemControl} />
<RadioGroup.ItemText className={styles.ItemText}>{framework}</RadioGroup.ItemText>
<RadioGroup.ItemHiddenInput />
</RadioGroup.Item>
))}
</RadioGroup.RootProvider>
<button className={button.Root} onClick={() => radioGroup.setValue('Solid')}>
Set to Solid
</button>
</div>
)
}
비활성화 (Disabled)
라디오 그룹을 비활성화하려면 disabled prop을 true로 설정해요.
import { RadioGroup } from '@ark-ui/react/radio-group'
import styles from 'styles/radio-group.module.css'
export const Disabled = () => {
const frameworks = ['React', 'Solid', 'Vue']
return (
<RadioGroup.Root className={styles.Root} defaultValue="React" disabled>
<RadioGroup.Label className={styles.Label}>Framework</RadioGroup.Label>
{frameworks.map((framework) => (
<RadioGroup.Item className={styles.Item} key={framework} value={framework}>
<RadioGroup.ItemControl className={styles.ItemControl} />
<RadioGroup.ItemText className={styles.ItemText}>{framework}</RadioGroup.ItemText>
<RadioGroup.ItemHiddenInput />
</RadioGroup.Item>
))}
</RadioGroup.Root>
)
}
가이드 (Guides)
asChild
RadioGroup.Item 컴포넌트는 기본적으로 label 요소로 렌더링돼요. 라디오 그룹은 사용자에게 의미 있는 맥락을 제공하는 라벨이 필요한 폼 컨트롤이므로, 이렇게 해야 올바른 폼 시맨틱과 접근성을 보장할 수 있어요.
asChild prop을 사용할 때는 유효한 HTML 구조와 접근성 준수를 위해 RadioGroup.Item의 직접 자식으로 label 요소를 렌더링해야 해요.
// INCORRECT usage ❌
<RadioGroup.Item asChild>
<div>
<RadioGroup.ItemHiddenInput />
<RadioGroup.ItemText>
<RadioGroup.ItemControl />
</RadioGroup.ItemText>
</div>
</RadioGroup.Item>
// CORRECT usage ✅
<RadioGroup.Item asChild>
<label>
<RadioGroup.ItemHiddenInput />
<RadioGroup.ItemText>
<RadioGroup.ItemControl />
</RadioGroup.ItemText>
</label>
</RadioGroup.Item>
숨겨진 입력 (Hidden Input)
RadioGroup.ItemHiddenInput 컴포넌트는 숨겨진 HTML input 요소를 렌더링해서 폼 제출과 네이티브 폼 동작과의 통합을 가능하게 해요. 이 컴포넌트는 라디오 그룹이 올바르게 동작하는 데 필수적이에요:
- 브라우저가 폼 제출에 사용하는 기본 input 요소를 제공해요
- 폼 라이브러리·검증 시스템과의 통합을 가능하게 해요
- 네이티브 폼 리셋 기능과 함께 동작하도록 보장해요
// INCORRECT usage ❌
<RadioGroup.Item>
<RadioGroup.ItemText>
<RadioGroup.ItemControl />
</RadioGroup.ItemText>
</RadioGroup.Item>
// CORRECT usage ✅
<RadioGroup.Item>
<RadioGroup.ItemHiddenInput />
<RadioGroup.ItemText>
<RadioGroup.ItemControl />
</RadioGroup.ItemText>
</RadioGroup.Item>
API 레퍼런스 (API Reference)
속성 (Props)
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 radio group.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: If true, the radio 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 radio 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 radio group
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: Whether the radio group is read-only
required
Type: boolean
Required: false
Default Value: undefined
Description: If true, the radio group is marked as required.
value
Type: string
Required: false
Default Value: undefined
Description: The controlled value of the radio group
데이터 속성 (Data Attributes)
data-scope: radio-group
data-part: root
data-orientation: The orientation of the radio-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: radio-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: radio-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: radio-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.
컨텍스트 (Context)
API:
| Property | Type | Description |
|---|---|---|
value |
`string | null` |
setValue |
(value: string) => void |
Function to set the value of the radio group |
clearValue |
VoidFunction |
Function to clear the value of the radio group |
focus |
VoidFunction |
Function to focus the radio group |
getItemState |
(props: ItemProps) => ItemState |
Returns the state details of a radio input |
접근성 (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.