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

Checkbox (Checkbox)

원문 보기 위키 갱신

Checkbox는 사용자가 체크(checked)와 체크 해제(unchecked) 상태를 전환할 수 있게 해주는 선택 가능한 컨트롤이에요. children이 없으면 애니메이션 체크 표시가 있는 기본 인디케이터를 렌더링하고, 표면(surface) 컨텍스트를 자동으로 감지해 적절하게 스타일을 적용하지요. 렌더 함수로 상태에 접근할 수도 있어요.

출처: 문서

본문

Import

import { Checkbox } from 'heroui-native';

Anatomy

<Checkbox>
  <Checkbox.Indicator>...</Checkbox.Indicator>
</Checkbox>
  • Checkbox: 선택 상태와 사용자 상호작용을 처리하는 메인 컨테이너예요. children이 없으면 애니메이션 체크 표시가 있는 기본 인디케이터를 렌더링해요. 적절한 스타일링을 위해 표면 컨텍스트를 자동으로 감지해요. 커스터마이즈하거나 비활성화할 수 있는 누르기 스케일 애니메이션을 제공해요. 상태(isSelected, isInvalid, isDisabled)에 접근할 수 있는 렌더 함수 children을 지원해요.
  • Checkbox.Background: 체크박스 콘텐츠 뒤에 렌더링되는 선택적 테마 인식 배경 컨테이너예요. 활성 테마가 기본 배경 콘텐츠(예: glass)를 등록했을 때 invalid가 아닌 동안 secondary variant에 대해 자동으로 마운트돼요. background prop으로 교체하거나 제거할 수 있어요.
  • Checkbox.Indicator: 선택 시 기본 슬라이드, 스케일, 투명도, 테두리 반경 애니메이션을 가진 선택적 체크 표시 컨테이너예요. children이 없으면 SVG 경로 그리기 애니메이션으로 애니메이션 체크 아이콘을 렌더링해요. 모든 애니메이션은 개별적으로 커스터마이즈하거나 비활성화할 수 있어요. 상태에 접근할 수 있는 렌더 함수 children을 지원해요.

Usage

기본 사용법

Checkbox 컴포넌트는 children이 없으면 기본 애니메이션 인디케이터로 렌더링돼요. 표면 배경 위에 있는지 자동으로 감지해 적절한 스타일을 적용해요.

<Checkbox isSelected={isSelected} onSelectedChange={setIsSelected} />

커스텀 인디케이터 사용

Indicator에서 렌더 함수를 사용해 상태에 따라 커스텀 아이콘을 보여주거나 숨길 수 있어요.

<Checkbox isSelected={isSelected} onSelectedChange={setIsSelected}>
  <Checkbox.Indicator>
    {({ isSelected }) => (isSelected ? <CheckIcon /> : null)}
  </Checkbox.Indicator>
</Checkbox>

Invalid 상태

isInvalid prop으로 검증 오류를 표시할 수 있어요. danger 색상 스타일이 적용돼요.

<Checkbox
  isSelected={isSelected}
  onSelectedChange={setIsSelected}
  isInvalid={hasError}
/>

커스텀 애니메이션

루트 체크박스와 인디케이터 모두의 애니메이션을 커스터마이즈하거나 비활성화할 수 있어요.

{
  /* Disable all animations (root and indicator) */
}
<Checkbox
  animation="disable-all"
  isSelected={isSelected}
  onSelectedChange={setIsSelected}
>
  <Checkbox.Indicator />
</Checkbox>;

{
  /* Disable only root animation */
}
<Checkbox
  animation="disabled"
  isSelected={isSelected}
  onSelectedChange={setIsSelected}
>
  <Checkbox.Indicator />
</Checkbox>;

{
  /* Disable only indicator animation */
}
<Checkbox isSelected={isSelected} onSelectedChange={setIsSelected}>
  <Checkbox.Indicator animation="disabled" />
</Checkbox>;

{
  /* Custom animation configuration */
}
<Checkbox
  animation={{ scale: { value: [1, 0.9], timingConfig: { duration: 200 } } }}
  isSelected={isSelected}
  onSelectedChange={setIsSelected}
>
  <Checkbox.Indicator
    animation={{
      scale: { value: [0.5, 1] },
      opacity: { value: [0, 1] },
      translateX: { value: [-8, 0] },
      borderRadius: { value: [12, 0] },
    }}
  />
</Checkbox>;

Example

import {
  Checkbox,
  Description,
  ControlField,
  Label,
  Separator,
  Surface,
} from "heroui-native";
import React from 'react';
import { View, Text } from 'react-native';

interface CheckboxFieldProps {
  isSelected: boolean;
  onSelectedChange: (value: boolean) => void;
  title: string;
  description: string;
}

const CheckboxField: React.FC<CheckboxFieldProps> = ({
  isSelected,
  onSelectedChange,
  title,
  description,
}) => {
  return (
    <ControlField isSelected={isSelected} onSelectedChange={onSelectedChange}>
      <ControlField.Indicator>
        <Checkbox className="mt-0.5" />
      </ControlField.Indicator>
      <View className="flex-1">
        <Label className="text-lg">{title}</Label>
        <Description className="text-base">
          {description}
        </Description>
      </View>
    </ControlField>
  );
};

export default function BasicUsage() {
  const [fields, setFields] = React.useState({
    newsletter: true,
    marketing: false,
    terms: false,
  });

  const fieldConfigs: Record<
    keyof typeof fields,
    { title: string; description: string }
  > = {
    newsletter: {
      title: 'Subscribe to newsletter',
      description: 'Get weekly updates about new features and tips',
    },
    marketing: {
      title: 'Marketing communications',
      description: 'Receive promotional emails and special offers',
    },
    terms: {
      title: 'Accept terms and conditions',
      description: 'Agree to our Terms of Service and Privacy Policy',
    },
  };

  const handleFieldChange = (key: keyof typeof fields) => (value: boolean) => {
    setFields((prev) => ({ ...prev, [key]: value }));
  };

  const fieldKeys = Object.keys(fields) as Array<keyof typeof fields>;

  return (
    <View className="flex-1 items-center justify-center px-5">
      <Surface className="py-5 w-full">
        {fieldKeys.map((key, index) => (
          <React.Fragment key={key}>
            {index > 0 && <Separator className="my-4" />}
            <CheckboxField
              isSelected={fields[key]}
              onSelectedChange={handleFieldChange(key)}
              title={fieldConfigs[key].title}
              description={fieldConfigs[key].description}
            />
          </React.Fragment>
        ))}
      </Surface>
    </View>
  );
}

더 많은 예시는 GitHub 저장소에서 찾을 수 있어요.

API Reference

Checkbox

prop type default description
children React.ReactNode | ((props: CheckboxRenderProps) => React.ReactNode) undefined 체크박스를 커스터마이즈할 자식 요소 또는 렌더 함수
isSelected boolean undefined 체크박스가 현재 선택되었는지 여부
onSelectedChange (isSelected: boolean) => void undefined 체크박스 선택 상태가 바뀔 때 호출되는 콜백
isDisabled boolean false 체크박스가 비활성화되어 상호작용할 수 없는지 여부
isInvalid boolean false 체크박스가 invalid인지(danger 색상 표시) 여부
variant 'primary' | 'secondary' 'primary' 체크박스용 variant 스타일
hitSlop number 6 누를 수 있는 영역의 hit slop
animation CheckboxRootAnimation - 애니메이션 구성
isAnimatedStyleActive boolean true 애니메이션 스타일(react-native-reanimated) 활성 여부
className string undefined 적용할 추가 CSS 클래스
background React.ReactNode - 체크박스 콘텐츠 뒤의 배경 레이어. undefined는 invalid가 아닌 동안 secondary variant의 테마 인식 기본값을 렌더링, 커스텀 노드는 교체, null은 제거
...PressableProps PressableProps - 모든 표준 React Native Pressable props 지원(disabled 제외)

CheckboxRenderProps

prop type description
isSelected boolean 체크박스가 선택되었는지 여부
isInvalid boolean 체크박스가 invalid인지 여부
isDisabled boolean 체크박스가 비활성화되었는지 여부

CheckboxRootAnimation

체크박스 루트 컴포넌트용 애니메이션 구성이에요. 다음 값이 될 수 있어요:

  • false 또는 "disabled": 루트 애니메이션만 비활성화
  • "disable-all": 자식을 포함한 모든 애니메이션 비활성화
  • true 또는 undefined: 기본 애니메이션 사용
  • object: 커스텀 애니메이션 구성
prop type default description
state 'disabled' | 'disable-all' | boolean - 프로퍼티를 커스터마이즈하면서 애니메이션 비활성화
scale.value [number, number] [1, 0.96] 스케일 값 [선택 안 함, 선택함]
scale.timingConfig WithTimingConfig { duration: 150 } 애니메이션 타이밍 구성

Checkbox.Background

체크박스 콘텐츠 뒤에 렌더링되는 absolute-fill 컨테이너예요. children이 없으면 활성 라이브러리 테마가 기본 콘텐츠(예: glass 블러 레이어)를 결정하고, children을 전달하면 동일한 위치 지정과 클리핑으로 커스텀 콘텐츠를 호스팅할 수 있어요.

prop type default description
children React.ReactNode - 배경 컨테이너 내부의 커스텀 콘텐츠
className string - 추가 CSS 클래스
...ViewProps ViewProps - 모든 표준 View props 지원

Checkbox.Indicator

prop type default description
children React.ReactNode | ((props: CheckboxRenderProps) => React.ReactNode) undefined 체크박스 인디케이터용 콘텐츠 또는 렌더 함수
className string undefined 인디케이터용 추가 CSS 클래스
iconProps CheckboxIndicatorIconProps undefined 기본 애니메이션 체크 아이콘용 커스텀 props
animation CheckboxIndicatorAnimation - 애니메이션 구성
isAnimatedStyleActive boolean true 애니메이션 스타일(react-native-reanimated) 활성 여부
...AnimatedViewProps AnimatedProps<ViewProps> - 모든 표준 React Native Animated View props 지원

CheckboxIndicatorIconProps

기본 애니메이션 체크 아이콘을 커스터마이즈하기 위한 props예요.

prop type description
size number 아이콘 크기
strokeWidth number 아이콘 스트로크 너비
color string 아이콘 색상(기본: 테마 accent-foreground)
enterDuration number 등장 애니메이션(체크 나타남) 지속 시간
exitDuration number 퇴장 애니메이션(체크 사라짐) 지속 시간

CheckboxIndicatorAnimation

체크박스 인디케이터 컴포넌트용 애니메이션 구성이에요. 다음 값이 될 수 있어요:

  • false 또는 "disabled": 모든 애니메이션 비활성화
  • true 또는 undefined: 기본 애니메이션 사용
  • object: 커스텀 애니메이션 구성
prop type default description
state 'disabled' | boolean - 프로퍼티를 커스터마이즈하면서 애니메이션 비활성화
opacity.value [number, number] [0, 1] 투명도 값 [선택 안 함, 선택함]
opacity.timingConfig WithTimingConfig { duration: 100 } 애니메이션 타이밍 구성
borderRadius.value [number, number] [8, 0] 테두리 반경 값 [선택 안 함, 선택함]
borderRadius.timingConfig WithTimingConfig { duration: 50 } 애니메이션 타이밍 구성
translateX.value [number, number] [-4, 0] TranslateX 값 [선택 안 함, 선택함]
translateX.timingConfig WithTimingConfig { duration: 100 } 애니메이션 타이밍 구성
scale.value [number, number] [0.8, 1] 스케일 값 [선택 안 함, 선택함]
scale.timingConfig WithTimingConfig { duration: 100 } 애니메이션 타이밍 구성

Hooks

useCheckbox

커스텀 컴포넌트나 컴파운드 컴포넌트 내에서 체크박스 컨텍스트 값에 접근하기 위한 훅이에요.

import { useCheckbox } from 'heroui-native';

const CustomIndicator = () => {
  const { isSelected, isInvalid, isDisabled } = useCheckbox();
  // ... your implementation
};

반환 값 (Returns): UseCheckboxReturn

property type description
isSelected boolean | undefined 체크박스가 현재 선택되었는지 여부
onSelectedChange ((isSelected: boolean) => void) | undefined 체크박스 선택 상태를 바꾸는 콜백 함수
isDisabled boolean 체크박스가 비활성화되어 상호작용할 수 없는지 여부
isInvalid boolean 체크박스가 invalid인지(danger 색상 표시) 여부
nativeID string | undefined 체크박스 요소의 Native ID

Note: 이 훅은 Checkbox 컴포넌트 내부에서 사용해야 해요. 체크박스 컨텍스트 밖에서 호출하면 에러가 발생합니다.

더 알아보기 (Learn more)