Checkbox
사용자가 하나 이상의 옵션을 선택할 수 있게 해주는 컴포넌트입니다. 목록에서 여러 항목을 선택하거나 약관 동의와 같은 선택적 작업에 사용됩니다.
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxPreview() {
return <Checkbox label="Hello World" defaultChecked />;
}Installation
npx @seed-design/cli@latest add ui:checkboxProps
Checkbox
Prop
Type
Checkmark
Prop
Type
Examples
Sizes
import { HStack, VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxSize() {
return (
<HStack gap="x8">
<VStack gap="x2">
<Checkbox label="Medium (default)" size="medium" defaultChecked />
<Checkbox label="Large" size="large" defaultChecked />
</VStack>
<VStack gap="x2">
<Checkbox label="Medium (default)" size="medium" variant="ghost" defaultChecked />
<Checkbox label="Large" size="large" variant="ghost" defaultChecked />
</VStack>
</HStack>
);
}Tones and Variants
Brand
import { VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxBrand() {
return (
<VStack gap="x2">
<Checkbox label="Square (default)" variant="square" tone="brand" defaultChecked />
<Checkbox label="Ghost" variant="ghost" tone="brand" defaultChecked />
</VStack>
);
}Neutral
import { VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxNeutral() {
return (
<VStack gap="x2">
<Checkbox label="Square (default)" variant="square" tone="neutral" defaultChecked />
<Checkbox label="Ghost" variant="ghost" tone="neutral" defaultChecked />
</VStack>
);
}Indeterminate
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxIndeterminate() {
return <Checkbox defaultChecked label="indeterminate" indeterminate />;
}Weights
Deprecated Props
weight="default"와 weight="stronger"는 더 이상 사용되지 않습니다. 대신 weight="regular"와 weight="bold"를 사용하세요.
import { VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxWeights() {
return (
<VStack gap="x4">
<Checkbox label="Regular Label Text" weight="regular" />
<Checkbox label="Bold Label Text" weight="bold" />
</VStack>
);
}Long Label
import { VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxLongLabel() {
return (
<VStack gap="x2">
<Checkbox
size="medium"
label="Consequat ut veniam aliqua deserunt occaecat enim occaecat veniam et et cillum nulla officia incididunt incididunt. Sint laboris labore occaecat fugiat culpa voluptate ullamco in elit dolore exercitation nulla."
/>
<Checkbox
size="large"
label="Consequat ut veniam aliqua deserunt occaecat enim occaecat veniam et et cillum nulla officia incididunt incididunt. Sint laboris labore occaecat fugiat culpa voluptate ullamco in elit dolore exercitation nulla."
/>
</VStack>
);
}Disabled
import { VStack } from "@seed-design/react";
import { Checkbox } from "seed-design/ui/checkbox";
export default function CheckboxDisabled() {
return (
<VStack gap="x2">
<Checkbox defaultChecked label="Disabled Checked, Square" disabled />
<Checkbox checked={false} label="Disabled without Checked, Square" disabled />
<Checkbox variant="ghost" defaultChecked label="Disabled Checked, Ghost" disabled />
<Checkbox variant="ghost" checked={false} label="Disabled without Checked, Ghost" disabled />
</VStack>
);
}Listening to Value Changes
onCheckedChange를 사용하여 체크박스의 선택 상태 변경을 감지할 수 있습니다.
이벤트를 활용해야 하는 경우 inputProps를 통해 내부 <input> 요소에 직접 이벤트 핸들러를 추가할 수 있습니다.
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<boolean | null>(null);
return (
<VStack gap="x4" align="center">
<Checkbox
label="Click me"
onCheckedChange={(checked) => {
setCount((prev) => prev + 1);
setLastValue(checked);
}}
/>
<Text>
onCheckedChange called: {count} times, last value: {`${lastValue ?? "-"}`}
</Text>
</VStack>
);
}Use Cases
React Hook Form
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 } 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<FormValues>({
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 (
<VStack gap="x3" as="form" onSubmit={handleSubmit(onValid)} onReset={onReset}>
<VStack>
{POSSIBLE_FRUIT_VALUES.map((name) => (
<CheckboxItem key={name} name={name} control={control} />
))}
</VStack>
<HStack gap="x2">
<ActionButton type="reset" variant="neutralWeak">
초기화
</ActionButton>
<ActionButton
type="button"
variant="neutralWeak"
flexGrow={1}
onClick={() => setValue("mango", true)}
>
mango 선택
</ActionButton>
<ActionButton type="submit" variant="neutralSolid" flexGrow={1}>
제출
</ActionButton>
</HStack>
</VStack>
);
}
interface CheckboxItemProps {
name: keyof FormValues;
control: Control<FormValues>;
}
function CheckboxItem({ name, control }: CheckboxItemProps) {
const {
field: { value, ...restProps },
fieldState: { invalid },
} = useController({ name, control });
return (
<Checkbox key={name} label={name} checked={value} inputProps={restProps} invalid={invalid} />
);
}Using Checkmark
Checkmark는 독립적인 체크 마크 컴포넌트로, 커스텀 레이아웃을 위해 사용할 수 있습니다.
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 (
<VStack asChild gap="x2" align="center">
<Checkbox.Root {...props}>
<Checkmark />
<Checkbox.HiddenInput />
{children}
</Checkbox.Root>
</VStack>
);
}
export default function CheckboxCheckmark() {
return (
<HStack gap="x6">
<CustomCheckbox>
<Text textStyle="t7Regular">regular</Text>
</CustomCheckbox>
<CustomCheckbox defaultChecked>
<Text textStyle="t7Medium">medium</Text>
</CustomCheckbox>
<CustomCheckbox>
<Text textStyle="t7Bold">bold</Text>
</CustomCheckbox>
</HStack>
);
}Last updated on