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

Checkbox

원문 보기 위키 갱신

Checkbox는 compound composition 방식과 그룹(group) 지원을 제공하는, styled가 없는 접근성 높은 체크박스 컴포넌트예요. 원하는 대로 완전히 커스텀한 체크박스 컨트롤을 만들 수 있어요.

출처: 문서

본문

기능 (Features)

  • 3개의 하위 컴포넌트(Root, Box, Indicator)로 구성된 compound 컴포넌트 API
  • 여러 체크박스에서 공유되는 값 배열을 관리하는 CheckboxGroup
  • 전체 선택 / 부분 선택 패턴을 위한 불확정(indeterminate) 상태

사용법 (Usage)

import { Checkbox } from 'primereact/checkbox';
import { CheckboxGroup } from 'primereact/checkboxgroup';
<Checkbox.Root>
    <Checkbox.Box>
        <Checkbox.Indicator />
    </Checkbox.Box>
</Checkbox.Root>

<CheckboxGroup>
    <Checkbox.Root value="a">
        <Checkbox.Box>
            <Checkbox.Indicator />
        </Checkbox.Box>
    </Checkbox.Root>
    <Checkbox.Root value="b">
        <Checkbox.Box>
            <Checkbox.Indicator />
        </Checkbox.Box>
    </Checkbox.Root>
</CheckboxGroup>

동작 (Behavior)

다형적 렌더링 (Polymorphic Rendering)

아무 하위 컴포넌트에 as를 사용해 렌더링되는 HTML 요소를 바꿀 수 있어요.

<Checkbox.Root as="label">
    <Checkbox.Box as="span">
        <Checkbox.Indicator as="div" />
    </Checkbox.Box>
</Checkbox.Root>

기본 요소: Root=div, Box=div, Indicator=span.

렌더 함수 자식 (Render Function Children)

Checkbox.Box와 Checkbox.Indicator는 렌더 함수를 자식으로 받아 컴포넌트 인스턴스에 접근할 수 있게 해 줘요.

<Checkbox.Box>{(instance) => <span>{instance.checkbox?.state.checked ? '✓' : ''}</span>}</Checkbox.Box>

패스 스루 (Pass Through)

<DocPTViewer name="checkbox-pt" components={['CheckboxRoot']} />

API

CheckboxRoot

Attribute Value
data-scope "checkbox"
data-part "root"
data-checked Present when checked
data-unchecked Present when unchecked
data-disabled Present when disabled
data-invalid Present when invalid
data-indeterminate Present when indeterminate
Label Type Description
root CheckboxRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
input CheckboxRootPassThroughType<InputHTMLAttributes> Used to pass attributes to the input's DOM element.
box CheckboxRootPassThroughType<HTMLAttributes> Used to pass attributes to the box's DOM element.
indicator CheckboxRootPassThroughType<HTMLAttributes> Used to pass attributes to the indicator's DOM element.

CheckboxBox

Attribute Value
data-scope "checkbox"
data-part "box"
data-checked Present when checked
data-unchecked Present when unchecked
data-disabled Present when disabled
data-invalid Present when invalid
data-indeterminate Present when indeterminate
Label Type Description
root CheckboxBoxPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

CheckboxIndicator

Attribute Value
data-scope "checkbox"
data-part "indicator"
data-checked Present when checked
data-unchecked Present when unchecked
data-disabled Present when disabled
data-invalid Present when invalid
data-indeterminate Present when indeterminate
Label Type Description
root CheckboxIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

Checkbox.Root는 보조 기술(assistive technologies)이 체크박스로 발표하는 숨겨진 네이티브 <input type="checkbox">를 렌더링해요. 이 입력은 aria-checked로 체크 상태를 반영하고, 불확정 상태에서는 aria-checked="mixed"를 사용해요. aria-label, aria-labelledby, 또는 inputId를 이용한 연결된 <label> 요소로 접근 가능한 이름을 제공해 주세요.

CheckboxGroup 안에서는 각 체크박스가 개별적으로 발표돼요. inputId + <label htmlFor>로 라벨을 연결하거나, 각 Checkbox.Root에 aria-label / aria-labelledby를 제공해 주세요.

키보드 지원 (Keyboard Support)

Key Function
tab Moves focus to the next checkbox.
space Toggles checked or unchecked state.

더 알아보기 (Learn more)