# Checkbox
URL: /react/components/checkbox
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/checkbox.mdx
사용자가 하나 이상의 옵션을 선택할 수 있게 해주는 컴포넌트입니다. 목록에서 여러 항목을 선택하거나 약관 동의와 같은 선택적 작업에 사용됩니다.
사용 가능 버전: @seed-design/react@0.0.1, @seed-design/css@0.0.1
## Preview
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxPreview() {
return (
);
}
```
## Installation
- npm: npx @seed-design/cli@latest add ui:checkbox
- pnpm: pnpm dlx @seed-design/cli@latest add ui:checkbox
- yarn: yarn dlx @seed-design/cli@latest add ui:checkbox
- bun: bun x @seed-design/cli@latest add ui:checkbox
## Props
### `CheckboxGroup`
### `Checkbox`
### `Checkmark`
## Examples
### Sizes
```tsx
import { HStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxSize() {
return (
);
}
```
### Tones and Variants
#### Brand
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxBrand() {
return (
);
}
```
#### Neutral
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxNeutral() {
return (
);
}
```
### Indeterminate
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxIndeterminate() {
return (
);
}
```
### Weights
`weight="default"`와 `weight="stronger"`는 더 이상 사용되지 않습니다. 대신
`weight="regular"`와 `weight="bold"`를 사용하세요.
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxWeights() {
return (
);
}
```
### Long Label
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxLongLabel() {
return (
);
}
```
### Disabled
```tsx
import { VStack } from "@seed-design/react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxDisabled() {
return (
);
}
```
### Listening to Value Changes
`onCheckedChange`를 사용하여 체크박스의 선택 상태 변경을 감지할 수 있습니다.
이벤트를 활용해야 하는 경우 `inputProps`를 통해 내부 `` 요소에 직접 이벤트 핸들러를 추가할 수 있습니다.
```tsx
import { VStack, Text } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
import { useState } from "react";
export default function CheckboxValueChanges() {
const [count, setCount] = useState(0);
const [lastValue, setLastValue] = useState(null);
return (
{
setCount((prev) => prev + 1);
setLastValue(checked);
}}
/>
onCheckedChange called: {count} times, last value: {`${lastValue ?? "-"}`}
);
}
```
### Use Cases
#### React Hook Form
```tsx
import { HStack, VStack } from "@seed-design/react";
import { useCallback, type FormEvent } from "react";
import { useController, useForm, type Control } from "react-hook-form";
import { ActionButton } from "seed-design/ui/action-button";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
const POSSIBLE_FRUIT_VALUES = ["apple", "melon", "mango"] as const;
type FormValues = Record<(typeof POSSIBLE_FRUIT_VALUES)[number], boolean>;
export default function CheckboxReactHookForm() {
const { handleSubmit, reset, setValue, control } = useForm({
defaultValues: {
apple: false,
melon: true,
mango: false,
},
});
const onValid = useCallback((data: FormValues) => {
window.alert(JSON.stringify(data, null, 2));
}, []);
const onReset = useCallback(
(event: FormEvent) => {
event.preventDefault();
reset();
},
[reset],
);
return (
{POSSIBLE_FRUIT_VALUES.map((name) => (
))}
초기화
setValue("mango", true)}
>
mango 선택
제출
);
}
interface CheckboxItemProps {
name: keyof FormValues;
control: Control;
}
function CheckboxItem({ name, control }: CheckboxItemProps) {
const {
field: { value, ...restProps },
fieldState: { invalid },
} = useController({ name, control });
return (
);
}
```
#### Using `Checkmark`
`Checkmark`는 독립적인 체크 마크 컴포넌트로, Checkbox Primitive 컴포넌트와 조합하여 커스텀 레이아웃을 위해 사용할 수 있습니다.
```tsx
import { HStack, Text, VStack } from "@seed-design/react";
import { Checkbox } from "@seed-design/react/primitive";
import { Checkmark } from "seed-design/ui/checkbox";
function CustomCheckbox({ children, ...props }: Checkbox.RootProps) {
return (
{children}
);
}
export default function CheckboxCheckmark() {
return (
regular
medium
bold
);
}
```
### Fieldset Integration
`CheckboxGroup`을 사용하여 여러 체크박스를 그룹화하고 `label`, `description`, `errorMessage` 등의 Fieldset 관련 prop을 사용할 수 있습니다.
```tsx
import { ActionButton, HStack, VStack } from "@seed-design/react";
import { useState } from "react";
import { Checkbox, CheckboxGroup } from "seed-design/ui/checkbox";
export default function CheckboxFieldset() {
const [firstErrors, setFirstErrors] = useState>({});
const [secondErrors, setSecondErrors] = useState>({});
const handleFirstSubmit = (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const fruits = formData.getAll("fruit");
if (fruits.includes("apple")) {
setFirstErrors({ apple: "Apple은 선택할 수 없습니다." });
return;
}
setFirstErrors({});
alert(JSON.stringify(fruits, null, 2));
};
const handleSecondSubmit = (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const agreements = formData.getAll("agreement");
const hasTerms = agreements.includes("terms");
const hasPrivacy = agreements.includes("privacy");
if (!hasTerms || !hasPrivacy) {
setSecondErrors({
...(!hasTerms && { terms: "필수 항목에 동의해 주세요." }),
...(!hasPrivacy && { privacy: "필수 항목에 동의해 주세요." }),
});
return;
}
setSecondErrors({});
alert(JSON.stringify(agreements, null, 2));
};
return (
);
}
```
## Scale Feedback
`Checkbox`는 눌렸을 때 `Checkmark`가 살짝 줄어드는 피드백을 제공합니다.
`Checkmark`를 감싸는 요소에 [Scale Feedback](/react/components/concepts/scale-feedback)을 직접 적용하는 경우, 바깥 요소와 `Checkmark`가 함께 줄어들어 축소가 이중으로 나타납니다. `Checkmark`의 상위 요소에서 `--seed-checkmark-feedback-scale`을 `1`로 설정하면 `Checkmark`의 축소만 끌 수 있습니다. 이 변수는 상속되므로 `Checkbox` 전체를 감싸는 요소에 설정해도 됩니다.
```css title="MyRow.css"
.my-row {
--seed-checkmark-feedback-scale: 1;
}
```