RadioGroup
RadioGroup는 목록에서 하나의 옵션을 선택하는 데 사용하는 라디오 그룹 컴포넌트예요. 해요체로 쉽게 설명드릴게요.
본문
사용법 (Usage)
import { RadioGroup, Radio } from '@heroui/react';
{"name": "radio-group-basic"}
구조 (Anatomy)
import {RadioGroup, Radio, Label, Description, FieldError} from '@heroui/react';
export default () => (
<RadioGroup>
<Label />
<Description />
<Radio value="option1">
<Radio.Content> {/* The clickable area: control + label */}
<Radio.Control>
<Radio.Indicator>
<span>✓</span> {/* Custom indicator (optional) */}
</Radio.Indicator>
</Radio.Control>
Label {/* plain text — the clickable label */}
</Radio.Content>
<Description /> {/* Sibling — stays outside the button (announced via aria-describedby) */}
<FieldError /> {/* Optional — per-radio validation message */}
</Radio>
<FieldError /> {/* Optional — group-level validation */}
</RadioGroup>
)
예시 (Examples)
가로 방향 (Horizontal Orientation)
{"name": "radio-group-horizontal"}
변형 (Variants)
RadioGroup 컴포넌트는 두 가지 시각적 변형을 지원해요:
primary(기본값) - 기본 배경의 표준 스타일로, 대부분의 사용 사례에 적합해요secondary- 강조가 낮은 변형으로, Surface 컴포넌트 안에서 사용하기 적합해요
{"name": "radio-group-variants"}
Surface 안에서 (In Surface)
Surface 컴포넌트 안에서 사용할 때는 variant="secondary"를 사용해 표면 배경에 적합한 낮은 강조 변형을 적용해요.
{"name": "radio-group-on-surface"}
비활성화 (Disabled)
{"name": "radio-group-disabled"}
제어된 상태 (Controlled)
{"name": "radio-group-controlled"}
비제어 상태 (Uncontrolled)
업데이트에만 반응하면 될 때는 defaultValue와 onChange를 함께 사용해요.
{"name": "radio-group-uncontrolled"}
유효성 검사 (Validation)
{"name": "radio-group-validation"}
배송 & 결제 (Delivery & Payment)
{"name": "radio-group-delivery-and-payment", "isBgSolid": true, "align": "start"}
커스텀 표시기 (Custom Indicator)
{"name": "radio-group-custom-indicator"}
렌더 함수 (Render Function)
{"name": "radio-group-render-function"}
커스터마이징 (Customization)
Tailwind CSS
{"name": "radio-group-custom-styles"}
Global CSS
RadioGroup 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.radio-group {
@apply gap-2;
}
.radio {
@apply gap-4 rounded-lg border border-border p-3 hover:bg-surface-hovered;
}
.radio__control {
@apply border-2 border-accent;
}
.radio__indicator {
@apply bg-primary;
}
.radio__content {
@apply gap-1;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 쉽게 커스터마이징할 수 있게 해요.
CSS 클래스 (CSS Classes)
RadioGroup 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
Base 클래스 [!toc]
.radio-group- 기본 라디오 그룹 컨테이너.radio- 개별 라디오 항목.radio__content- 라디오 버튼 (컨트롤 + 라벨 텍스트를 감싸는 RAC 라벨 래퍼).radio__control- 라디오 컨트롤 (원형 버튼).radio__indicator- 라디오 표시기 (안쪽 점)
수식어 클래스 [!toc]
비활성화 상태는 .radio에 [data-disabled="true"] / [aria-disabled="true"] (그리고 :disabled)로 적용돼요 — .radio--disabled 수식어 클래스는 존재하지 않아요.
상호작용 상태 (Interactive States)
라디오는 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 선택됨 (Selected):
[aria-checked="true"]또는[data-selected="true"](표시기 표시) - 호버 (Hover):
:hover또는[data-hovered="true"](테두리 색상 변경) - 포커스 (Focus):
:focus-visible또는[data-focus-visible="true"](포커스 링 표시) - 눌림 (Pressed):
:active또는[data-pressed="true"](크기 변환) - 비활성화 (Disabled):
:disabled또는[aria-disabled="true"](불투명도 감소, 포인터 이벤트 없음) - 유효하지 않음 (Invalid):
[data-invalid="true"]또는[aria-invalid="true"](오류 테두리 색상)
API 참고 (API Reference)
RadioGroup
| Prop | Type | Default | Description |
|---|---|---|---|
value |
string |
- | The current value (controlled) |
defaultValue |
string |
- | The default value (uncontrolled) |
onChange |
(value: string) => void |
- | Handler called when the value changes |
isDisabled |
boolean |
false |
Whether the radio group is disabled |
isRequired |
boolean |
false |
Whether the radio group is required |
isReadOnly |
boolean |
false |
Whether the radio group is read only |
isInvalid |
boolean |
false |
Whether the radio group is in an invalid state |
variant |
"primary" | "secondary" |
"primary" |
Visual variant of the component. primary is the default style with shadow. secondary is a lower emphasis variant without shadow, suitable for use in surfaces. |
name |
string |
- | The name of the radio group, used when submitting an HTML form |
orientation |
'horizontal' | 'vertical' |
'vertical' |
The orientation of the radio group |
children |
React.ReactNode | (values: RadioGroupRenderProps) => React.ReactNode |
- | Radio group content or render prop |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, RadioGroupRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Radio
| Prop | Type | Default | Description |
|---|---|---|---|
value |
string |
- | The value of the radio button |
isDisabled |
boolean |
false |
Whether the radio button is disabled |
name |
string |
- | The name of the radio button, used when submitting an HTML form |
children |
React.ReactNode | (values: RadioFieldRenderProps) => React.ReactNode |
- | Radio content or field render prop |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, RadioFieldRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Radio.Control
React.HTMLAttributes<HTMLSpanElement>를 확장해요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode |
- | The content to render inside the control wrapper (typically Radio.Indicator) |
Radio.Indicator
React.HTMLAttributes<HTMLSpanElement>를 확장해요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: RadioButtonRenderProps) => React.ReactNode |
- | Optional content or render prop that receives the current radio button state. |
Radio.Content
라디오의 클릭 가능한 영역이에요 (숨겨진 입력을 감싸는 <label>). 안에 Radio.Control과 Label을 배치해요. className은 RadioButtonRenderProps를 받는 렌더 함수를 허용해요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: RadioButtonRenderProps) => React.ReactNode |
- | The clickable content (typically Radio.Control and Label) |
RadioFieldRenderProps
루트 Radio에 렌더 프롭을 사용하면 이 필드 수준 값들이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
Whether the radio is currently selected |
isDisabled |
boolean |
Whether the radio is disabled |
isReadOnly |
boolean |
Whether the radio is read only |
isInvalid |
boolean |
Whether the radio is in an invalid state |
isRequired |
boolean |
Whether the radio is required |
RadioButtonRenderProps
Radio.Control과 Radio.Indicator는 버튼 수준 렌더 프롭(isHovered, isPressed, isFocusVisible 등)을 사용해요. 접근하려면 Radio.Control 자식으로 함수를 전달하거나 Radio.Indicator에 함수를 전달하면 돼요.