# Select Box
URL: /react/components/select-box
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/select-box.mdx
A UI element that uses a container with clear borders to select one or more options from a defined list.
사용 가능 버전: @seed-design/react@0.0.1, @seed-design/css@0.0.1
## Preview
```tsx
import { HStack } from "@seed-design/react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
export default function SelectBoxPreview() {
return (
} />
}
/>
} />
} />
}
/>
} />
);
}
```
## Installation
- npm: npx @seed-design/cli@latest add ui:select-box
- pnpm: pnpm dlx @seed-design/cli@latest add ui:select-box
- yarn: yarn dlx @seed-design/cli@latest add ui:select-box
- bun: bun x @seed-design/cli@latest add ui:select-box
## Props
### Check Select Box
#### `CheckSelectBoxGroup`
#### `CheckSelectBox`
#### `CheckSelectBoxCheckmark`
### Radio Select Box
#### `RadioSelectBoxRoot`
#### `RadioSelectBoxItem`
#### `RadioSelectBoxRadiomark`
## Examples
### 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 {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
const POSSIBLE_FRUIT_VALUES = ["apple", "melon", "mango"] as const;
type CheckFormValues = Record<(typeof POSSIBLE_FRUIT_VALUES)[number], boolean>;
interface RadioFormValues {
fruit: (typeof POSSIBLE_FRUIT_VALUES)[number];
}
export default function SelectBoxReactHookForm() {
// CheckSelectBox Form
const checkForm = useForm({
defaultValues: { apple: false, melon: true, mango: false },
});
// RadioSelectBox Form
const radioForm = useForm({
defaultValues: { fruit: "melon" },
});
const { field: radioField } = useController({ name: "fruit", control: radioForm.control });
const onCheckValid = useCallback((data: CheckFormValues) => {
window.alert(`CheckSelectBox:\n${JSON.stringify(data, null, 2)}`);
}, []);
const onRadioValid = useCallback((data: RadioFormValues) => {
window.alert(`RadioSelectBox:\n${JSON.stringify(data, null, 2)}`);
}, []);
const onCheckReset = useCallback(
(event: FormEvent) => {
event.preventDefault();
checkForm.reset();
},
[checkForm],
);
const onRadioReset = useCallback(
(event: FormEvent) => {
event.preventDefault();
radioForm.reset();
},
[radioForm],
);
return (
{POSSIBLE_FRUIT_VALUES.map((name) => (
))}
초기화
제출
{POSSIBLE_FRUIT_VALUES.map((value) => (
}
/>
))}
초기화
제출
);
}
interface CheckSelectBoxItemProps {
name: keyof CheckFormValues;
control: Control;
}
function CheckSelectBoxItem({ name, control }: CheckSelectBoxItemProps) {
const {
field: { value, ...restProps },
fieldState: { invalid },
} = useController({ name, control });
return (
}
/>
);
}
```
### Customizing Label
```tsx
import { Badge, HStack } from "@seed-design/react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
export default function SelectBoxCustomizingLabel() {
return (
} />
Melon
New
>
}
description="Elit cupidatat dolore fugiat enim veniam culpa."
suffix={}
/>
}
/>
} />
Melon
New
>
}
description="Elit cupidatat dolore fugiat enim veniam culpa."
suffix={}
/>
}
/>
);
}
```
### Listening to Value Changes
`CheckSelectBox` can detect checkbox selection state changes using `onCheckedChange`.
`RadioSelectBoxRoot` can detect radio button selection value changes using `onValueChange`.
```tsx
import { HStack, Text, VStack } from "@seed-design/react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
import { useState } from "react";
export default function SelectBoxValueChanges() {
const [checkCount, setCheckCount] = useState(0);
const [checkLastValue, setCheckLastValue] = useState(null);
const [radioCount, setRadioCount] = useState(0);
const [radioLastValue, setRadioLastValue] = useState(null);
return (
}
onCheckedChange={(checked) => {
setCheckCount((prev) => prev + 1);
setCheckLastValue(checked);
}}
/>
onCheckedChange called: {checkCount} times, last value: {`${checkLastValue ?? "-"}`}
{
setRadioCount((prev) => prev + 1);
setRadioLastValue(value);
}}
>
} />
} />
onValueChange called: {radioCount} times, last value: {radioLastValue ?? "-"}
);
}
```
### Grid Layout (Columns)
You can arrange items in multiple columns using the `columns` prop. If `columns` is greater than 1, the `layout` of child elements is automatically set to `"vertical"`.
If needed, you can override the layout of individual items by directly setting the `layout` prop.
```tsx
import { IconDiamond, IconIcecreamcone } from "@karrotmarket/react-multicolor-icon";
import { VStack } from "@seed-design/react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
export default function SelectBoxColumns() {
return (
}
label="옵션 1"
description="layout=vertical"
suffix={}
/>
}
label="옵션 2"
description="layout=vertical"
suffix={}
/>
}
defaultChecked
layout="horizontal"
label="layout=horizontal"
description="layout을 horizontal로 오버라이드"
suffix={}
/>
}
label="옵션 4"
description="layout=vertical"
suffix={}
/>
}
label="옵션 1"
suffix={}
/>
}
label="옵션 2"
suffix={}
/>
}
label="layout=horizontal"
description="layout을 horizontal로 오버라이드"
layout="horizontal"
suffix={}
/>
}
label="옵션 4"
suffix={}
/>
}
label="옵션 5"
suffix={}
/>
}
label="옵션 6"
suffix={}
/>
);
}
```
### With Suffix
You can display checkmarks, radio marks, or custom elements using the `suffix` prop. You can use `CheckSelectBoxCheckmark` and `RadioSelectBoxRadiomark`, or pass free-form elements like icons or text.
```tsx
import { IconPersonCircleLine } from "@karrotmarket/react-monochrome-icon";
import { Text, HStack, Box } from "@seed-design/react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
export default function SelectBoxWithSuffix() {
return (
} />
+1,000원
}
/>
}
/>
}
/>
+1,000원
}
/>
}
/>
);
}
```
### Collapsible Footer
You can display additional content using the `footer` prop. You can control the footer display conditions using the `footerVisibility` prop.
- `"when-selected"` (default): Displayed only when an item is selected
- `"when-not-selected"`: Displayed only when an item is not selected
- `"always"`: Always displayed
```tsx
import { Box, HStack, Text } from "@seed-design/react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
export default function SelectBoxCollapsibleFooter() {
return (
}
footer={
선택되었을 때만 보입니다.
}
/>
}
footerVisibility="always"
footer={
항상 보입니다.
}
/>
}
footerVisibility="when-not-selected"
footer={
선택되지 않았을 때만 보입니다.
}
/>
}
footer={
선택되었을 때만 보입니다.
}
/>
}
footerVisibility="always"
footer={
항상 보입니다.
}
/>
}
footerVisibility="when-not-selected"
footer={
선택되지 않았을 때만 보입니다.
}
/>
);
}
```
### Fieldset/RadioGroupField Integration
Fieldset/RadioGroupField-related props are available.
- `label` and `labelWeight`
- `indicator` and `showRequiredIndicator`
- `description` and `errorMessage`
- `disabled`, `invalid`, `name`, `form`: Only supported on `RadioSelectBoxRoot`.
```tsx
import { ActionButton, HStack, VStack, Box, Text } from "@seed-design/react";
import { useState } from "react";
import {
CheckSelectBox,
CheckSelectBoxCheckmark,
CheckSelectBoxGroup,
RadioSelectBoxItem,
RadioSelectBoxRadiomark,
RadioSelectBoxRoot,
} from "seed-design/ui/select-box";
export default function SelectBoxFieldset() {
const [checkErrors, setCheckErrors] = useState>({});
const [radioErrorMessage, setRadioErrorMessage] = useState();
const handleCheckSubmit = (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const fruits = formData.getAll("fruit");
if (fruits.includes("apple")) {
setCheckErrors({ apple: "Apple은 선택할 수 없습니다." });
return;
}
setCheckErrors({});
alert(JSON.stringify(fruits, null, 2));
};
const handleRadioSubmit = (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const color = formData.get("color");
if (color === "red") {
setRadioErrorMessage("Red는 선택할 수 없습니다.");
return;
}
setRadioErrorMessage(undefined);
alert(JSON.stringify({ color }, null, 2));
};
return (
);
}
```