Checkbox
사용자가 목록에서 여러 항목을 선택하거나, 하나의 항목을 선택 상태로 표시할 수 있게 해주는 체크박스 컴포넌트입니다.
출처: HeroUI page-name
본문
사용법 (Usage)
import { Checkbox } from '@heroui/react';
"checkbox-basic"
구조 (Anatomy)
import { Checkbox, Description, FieldError } from '@heroui/react';
export default () => (
<Checkbox>
<Checkbox.Content>
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
Label {/* plain text — the clickable label + accessible name */}
</Checkbox.Content>
<Description /> {/* Optional — field-level help text */}
<FieldError /> {/* Optional — validation message */}
</Checkbox>
);
예제 (Examples)
변형 (Variants)
Checkbox 컴포넌트는 두 가지 시각적 변형을 지원합니다:
primary(기본값) - 기본 배경을 가진 표준 스타일, 대부분의 사용 사례에 적합secondary- 낮은 강조 변형, Surface 컴포넌트에서 사용하기 적합
"checkbox-variants"
완전 둥글게 (Full Rounded)
"checkbox-full-rounded"
비활성 (Disabled)
"checkbox-disabled"
외부 라벨 (External Label)
"checkbox-external-label"
설명과 함께 (With Description)
"checkbox-with-description"
기본 선택 (Default Selected)
"checkbox-default-selected"
유효하지 않음 (Invalid)
"checkbox-invalid"
제어 (Controlled)
"checkbox-controlled"
불확정 상태 (Indeterminate)
"checkbox-indeterminate"
폼 통합 (Form Integration)
"checkbox-form"
렌더 Props (Render Props)
"checkbox-render-props"
렌더 함수 (Render Function)
"checkbox-render-function"
커스텀 표시기 (Custom Indicator)
"checkbox-custom-indicator"
커스터마이징 (Customization)
Tailwind CSS
"checkbox-custom-styles"
Global CSS
Checkbox 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용하면 됩니다.
자세히 알아보기.
@layer components {
.checkbox {
@apply inline-flex gap-3 items-center;
}
.checkbox__control {
@apply size-5 border-2 border-gray-400 rounded data-[selected=true]:bg-blue-500 data-[selected=true]:border-blue-500;
/* Animated background indicator */
&::before {
@apply bg-accent pointer-events-none absolute inset-0 z-0 origin-center scale-50 rounded-md opacity-0 content-[''];
transition:
scale 200ms linear,
opacity 200ms linear,
background-color 200ms ease-out;
}
/* Show indicator when selected */
&[data-selected="true"]::before {
@apply scale-100 opacity-100;
}
}
.checkbox__indicator {
@apply text-white;
}
.checkbox__content {
@apply items-center gap-3;
}
}
스타일링 참조 (Styling Reference)
HeroUI는 BEM 방법론을 따르므로 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽습니다.
CSS 클래스
Checkbox 컴포넌트는 다음 CSS 클래스를 사용합니다 (소스 스타일 보기):
기본 클래스 [!toc]
.checkbox- 체크박스의 기본 컨테이너 (필드).checkbox__content- 컨트롤과 라벨 텍스트를 감싸는 클릭 가능한 라벨.checkbox__control- 체크박스 컨트롤 상자.checkbox__indicator- 체크박스 체크 표시 표시기
상호작용 상태 (Interactive States)
체크박스는 유연성을 위해 CSS 의사 클래스(pseudo-classes)와 데이터 속성(data attributes)을 모두 지원합니다:
- 선택됨 (Selected):
[data-selected="true"]또는[aria-checked="true"](체크 표시와 배경색 변경을 보여줌) - 불확정 (Indeterminate):
[data-indeterminate="true"](대시가 있는 불확정 상태 표시) - 유효하지 않음 (Invalid):
[data-invalid="true"]또는[aria-invalid="true"](위험 색상의 오류 상태 표시) - 호버 (Hover):
Checkbox.Control(버튼)의:hover또는[data-hovered="true"] - 포커스 (Focus): 버튼의
:focus-visible또는[data-focus-visible="true"](컨트롤에 포커스 링 표시) - 비활성 (Disabled): 필드의
[data-disabled="true"](도움말 텍스트를 포함한 불투명도 감소) - 눌림 (Pressed):
:active또는[data-pressed="true"]
API 참조 (API Reference)
Checkbox
React Aria CheckboxField에서 상속받습니다.
| Prop | Type | Default | Description |
|---|---|---|---|
isSelected |
boolean |
false |
체크박스가 선택되었는지 여부 |
defaultSelected |
boolean |
false |
기본적으로 체크박스가 선택되어 있는지 여부 (비제어) |
isIndeterminate |
boolean |
false |
체크박스가 불확정 상태인지 여부 |
isDisabled |
boolean |
false |
체크박스가 비활성화되었는지 여부 |
isInvalid |
boolean |
false |
체크박스가 유효하지 않은지 여부 |
isReadOnly |
boolean |
false |
체크박스가 읽기 전용인지 여부 |
isRequired |
boolean |
false |
체크박스가 선택되어야 하는지 여부 |
validate |
(value: boolean) => ValidationError | true | null | undefined |
- | 커스텀 검증 함수 |
validationBehavior |
'native' | 'aria' |
'native' |
네이티브 HTML 폼 검증을 사용할지 ARIA를 사용할지 여부 |
variant |
"primary" | "secondary" |
"primary" |
컴포넌트의 시각적 변형. primary는 그림자가 있는 기본 스타일입니다. secondary는 그림자가 없는 낮은 강조 변형으로 표면(surface)에서 사용하기 적합합니다. |
name |
string |
- | HTML 폼 제출 시 사용되는 입력 요소의 이름 |
value |
string |
- | HTML 폼 제출 시 사용되는 입력 요소의 값 |
onChange |
(isSelected: boolean) => void |
- | 체크박스 값이 변경될 때 호출되는 핸들러 |
children |
React.ReactNode | (values: CheckboxFieldRenderProps) => React.ReactNode |
- | 체크박스 콘텐츠 또는 필드 렌더 prop |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, CheckboxFieldRenderProps> |
- | 기본 DOM 요소를 커스텀 렌더 함수로 재정의합니다. |
Checkbox.Content
컨트롤과 라벨 텍스트를 감싸는 클릭 가능한 <label>입니다. Checkbox.Control과 Label을 안에 넣고, Description/FieldError는 Checkbox.Content의 형제 요소로 두세요. 라벨이 없는 체크박스의 경우 Label을 생략하고 Checkbox에 aria-label을 전달하세요.
| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | (values: CheckboxButtonRenderProps) => React.ReactNode |
- | 버튼 콘텐츠 (컨트롤 + 라벨) 또는 버튼 렌더 prop |
className |
string | (values: CheckboxButtonRenderProps) => string |
- | 클릭 가능한 라벨에 적용되는 클래스 |
CheckboxFieldRenderProps
루트 Checkbox에서 렌더 prop을 사용할 때 다음 필드 수준 값을 제공합니다:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
체크박스가 현재 선택되었는지 여부 |
isIndeterminate |
boolean |
체크박스가 불확정 상태인지 여부 |
isDisabled |
boolean |
체크박스가 비활성화되었는지 여부 |
isReadOnly |
boolean |
체크박스가 읽기 전용인지 여부 |
isInvalid |
boolean |
체크박스가 유효하지 않은지 여부 |
isRequired |
boolean |
체크박스가 필수인지 여부 |
CheckboxButtonRenderProps
Checkbox.Control과 Checkbox.Indicator는 버튼 수준 렌더 props (isHovered, isPressed, isFocusVisible 등)를 사용합니다. Checkbox.Control의 자식이나 Checkbox.Indicator에 함수를 전달하면 접근할 수 있습니다.