본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Checkbox (체크박스)

원문 보기 위키 갱신

체크박스는 사용자가 체크(checked) 상태와 체크되지 않은(not checked) 상태를 토글할 수 있게 해주는 컨트롤이에요. 옵션 선택, 동의 확인, 구독 설정 같은 상황에서 폭넓게 사용돼요.

출처: 문서

본문

Import (불러오기)

import { Checkbox } from "@kobalte/core/checkbox";
// or
import { Root, Input, ... } from "@kobalte/core/checkbox";
// or (deprecated)
import { Checkbox } from "@kobalte/core";

Features (기능)

  • 네이티브 HTML <input> 요소로 만들어져 있으며, 커스텀 스타일링을 위해 시각적으로 숨겨져 있어요.
  • 폼 리셋 이벤트와 동기화돼요.
  • 보조 기술을 위한 라벨링을 지원해요.
  • ARIA를 통해 입력과 연결된 설명(description) 및 오류 메시지 도움말 텍스트를 지원해요.
  • 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.

Anatomy (구조)

체크박스는 다음의 부분들로 이루어져요:

  • Checkbox: 체크박스의 루트 컨테이너예요.
  • Checkbox.Input: 체크박스 안에서 시각적으로 숨겨진 네이티브 html input이에요.
  • Checkbox.Control: 체크박스를 시각적으로 나타내는 요소예요.
  • Checkbox.Indicator: 체크박스가 체크 또는 불확정(indeterminate) 상태일 때 렌더링되는 시각적 표시자예요.
  • Checkbox.Label: 사용자에게 체크박스에 대한 정보를 주는 라벨이에요.
  • Checkbox.Description: 사용자에게 체크박스에 대한 추가 정보를 주는 설명이에요.
  • Checkbox.ErrorMessage: 사용자에게 체크박스의 검증 오류를 해결하는 방법을 알려주는 오류 메시지예요.
<Checkbox>
	<Checkbox.Input />
	<Checkbox.Control>
		<Checkbox.Indicator />
	</Checkbox.Control>
	<Checkbox.Label />
	<Checkbox.Description />
	<Checkbox.ErrorMessage />
</Checkbox>

Example (예제)

기본 예제를 통해 체크박스의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.

Usage (사용법)

Default checked (기본 체크)

초기 비제어 방식 값은 defaultChecked prop으로 제공할 수 있어요.

<Checkbox defaultChecked>...</Checkbox>

Controlled checked (제어 방식 체크)

checked prop을 사용하면 체크 상태를 제어할 수 있어요. 사용자가 체크박스를 누르면 onChange 이벤트가 발생하고 새 값이 전달돼요.

import { createSignal } from "solid-js";

function ControlledExample() {
	const [checked, setChecked] = createSignal(false);

	return (
		<>
			<Checkbox checked={checked()} onChange={setChecked}>
				...
			</Checkbox>
			<p>You are {checked() ? "subscribed" : "unsubscribed"}.</p>
		</>
	);
}

Description (설명)

Checkbox.Description 컴포넌트를 사용해 체크박스에 추가 도움말 텍스트를 연결할 수 있어요.

<Checkbox>
	<Checkbox.Input />
	<Checkbox.Control>
		<Checkbox.Indicator>
			<CheckIcon />
		</Checkbox.Indicator>
	</Checkbox.Control>
	<Checkbox.Label>Subscribe</Checkbox.Label>
	<Checkbox.Description>You will receive our weekly newsletter.</Checkbox.Description>
</Checkbox>

Error message (오류 메시지)

Checkbox.ErrorMessage 컴포넌트를 사용해 사용자가 검증 오류를 해결하도록 도울 수 있어요. 보조 기술에 체크박스가 유효하지 않음을 의미적으로 표시하기 위해 validationState prop과 함께 사용해야 해요.

기본적으로 validationState prop이 invalid로 설정된 경우에만 렌더링되며, 항상 오류 메시지를 렌더링하려면 forceMount prop을 사용해요 (예: 애니메이션 라이브러리 사용 시).

import { createSignal } from "solid-js";

function ErrorMessageExample() {
	const [checked, setChecked] = createSignal(false);

	return (
		<Checkbox
			checked={checked()}
			onChange={setChecked}
			validationState={!checked() ? "invalid" : "valid"}
		>
			<Checkbox.Input />
			<Checkbox.Control>
				<Checkbox.Indicator>
					<CheckIcon />
				</Checkbox.Indicator>
			</Checkbox.Control>
			<Checkbox.Label>Agree</Checkbox.Label>
			<Checkbox.ErrorMessage>You must agree to our Terms and Conditions.</Checkbox.ErrorMessage>
		</Checkbox>
	);
}

HTML forms (HTML 폼)

name과 value prop을 사용해 HTML 폼과 통합할 수 있어요.

function HTMLFormExample() {
	const onSubmit = (e: SubmitEvent) => {
		// handle form submission.
	};

	return (
		<form onSubmit={onSubmit}>
			<Checkbox name="newsletter" value="subscribe">
				...
			</Checkbox>
			<div>
				<button type="reset">Reset</button>
				<button type="submit">Submit</button>
			</div>
		</form>
	);
}

API Reference (API 참조)

Checkbox

Checkbox는 @kobalte/core/checkbox의 Root 임포트(그리고 더 이상 사용되지 않는 Checkbox.Root)와 동일해요.

Prop Description
checked boolean
The controlled checked state of the checkbox.
defaultChecked boolean
The default checked state when initially rendered. Useful when you do not need to control the checked state.
onChange (checked: boolean) => void
Event handler called when the checked state of the checkbox changes.
indeterminate boolean
Whether the checkbox is in an indeterminate state.
name string
The name of the checkbox, used when submitting an HTML form. See MDN.
value string
The value of the checkbox, used when submitting an HTML form. See MDN.
validationState 'valid' | 'invalid'
Whether the checkbox should display its "valid" or "invalid" visual styling.
required boolean
Whether the user must check the checkbox before the owning form can be submitted.
disabled boolean
Whether the checkbox is disabled.
readOnly boolean
Whether the checkbox can be checked but not changed by the user.
children JSX.Element | (state: CheckboxState) => JSX.Element
The children of the checkbox. Can be a JSX.Element or a render prop for having access to the internal state.
Render Prop Description
checked Accessor<boolean>
Whether the checkbox is checked or not.
indeterminate Accessor<boolean>
Whether the checkbox is in an indeterminate state.
Data attribute Description
data-valid Present when the checkbox is valid according to the validation rules.
data-invalid Present when the checkbox is invalid according to the validation rules.
data-required Present when the checkbox is required.
data-disabled Present when the checkbox is disabled.
data-readonly Present when the checkbox is read only.
data-checked Present when the checkbox is checked.
data-indeterminate Present when the checkbox is in an indeterminate state.

Checkbox.Input, Checkbox.Control, Checkbox.Indicator, Checkbox.Label, Checkbox.Description와 Checkbox.ErrorMessage는 동일한 data-attribute를 공유해요.

Checkbox.Indicator

Prop Description
forceMount boolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Checkbox.ErrorMessage

Prop Description
forceMount boolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Rendered elements (렌더링되는 요소)

Component Default rendered element
Checkbox div
Checkbox.Input input
Checkbox.Control div
Checkbox.Indicator div
Checkbox.Label label
Checkbox.Description div
Checkbox.ErrorMessage div

Accessibility (접근성)

Keyboard Interactions (키보드 상호작용)

Key Description
Space Toggles the checkbox on and off.

더 알아보기 (Learn more)