A component that selects a value from a list that opens when a trigger is pressed.
@seed-design/react@2.1.0, @seed-design/css@2.3.0
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectPreview () {
return (
< Box width = "240px" >
< SelectRoot defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "Fruit" placeholder = "Select a fruit" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "Apple" />
< SelectItem value = "banana" label = "Banana" />
< SelectItem value = "cherry" label = "Cherry" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
npx @seed-design/cli@latest add ui:select pnpm dlx @seed-design/cli@latest add ui:select yarn dlx @seed-design/cli@latest add ui:select bun x @seed-design/cli@latest add ui:select
Install dependencies
npm install @karrotmarket/react-monochrome-icon @seed-design/css @seed-design/react yarn add @karrotmarket/react-monochrome-icon @seed-design/css @seed-design/react pnpm add @karrotmarket/react-monochrome-icon @seed-design/css @seed-design/react bun add @karrotmarket/react-monochrome-icon @seed-design/css @seed-design/react Copy the code below and paste it in /**
* @file ui:select
* @requires @seed-design/react@^2.1.0
* @requires @seed-design/css@^2.3.0
**/
"use client" ;
import {
Field as SeedField,
PrefixIcon,
Select as SeedSelect,
VisuallyHidden,
} from "@seed-design/react" ;
import type { FieldLabelVariantProps } from "@seed-design/css/recipes/field-label" ;
import {
IconCheckmarkFatFill,
IconChevronDownSmallLine,
IconExclamationmarkCircleFill,
} from "@karrotmarket/react-monochrome-icon" ;
import * as React from "react" ;
export interface SelectRootProps extends SeedSelect . RootProps {
label ?: React . ReactNode ;
/**
* @default " medium "
*/
labelWeight ?: FieldLabelVariantProps [ "weight" ];
indicator ?: React . ReactNode ;
showRequiredIndicator ?: boolean ;
description ?: React . ReactNode ;
errorMessage ?: React . ReactNode ;
hiddenSelectProps ?: SeedSelect . HiddenSelectProps ;
fieldRef ?: React . Ref < HTMLDivElement >;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/select
*/
export const SelectRoot = ({
label,
labelWeight,
indicator,
showRequiredIndicator,
description,
errorMessage,
hiddenSelectProps,
fieldRef,
children,
... props
} : SelectRootProps ) => {
const renderHeader = label || indicator;
const renderDescription = !! description;
const renderErrorMessage = errorMessage && props.invalid;
const renderFooter = renderDescription || renderErrorMessage;
if (process.env. NODE_ENV !== "production" && ! label) {
console. warn (
"SelectRoot: Provide a `label` prop for better accessibility. Please ignore this warning if you've provided `aria-label` or `aria-labelledby` to the `SelectTrigger` inside. This warning will not be shown in production builds." ,
);
}
return (
< SeedField.Root
required = {props.required}
disabled = {props.disabled}
invalid = {props.invalid}
readOnly = {props.readOnly}
name = {props.name}
ref = {fieldRef}
>
{renderHeader && (
< SeedField.Header >
< SeedField.Label weight = {labelWeight}>
{label}
{showRequiredIndicator && < SeedField.RequiredIndicator />}
{indicator && < SeedField.IndicatorText >{indicator}</ SeedField.IndicatorText >}
</ SeedField.Label >
{ /* You might want to put your custom element here */ }
</ SeedField.Header >
)}
< SeedSelect.Root { ... props}>
{children}
< SeedSelect.HiddenSelect { ... hiddenSelectProps} />
</ SeedSelect.Root >
{renderFooter && (
< SeedField.Footer >
{renderDescription &&
(renderErrorMessage ? (
< VisuallyHidden asChild >
< SeedField.Description >{description}</ SeedField.Description >
</ VisuallyHidden >
) : (
< SeedField.Description >{description}</ SeedField.Description >
))}
{renderErrorMessage && (
< SeedField.ErrorMessage >
< PrefixIcon svg = {< IconExclamationmarkCircleFill />} />
{errorMessage}
</ SeedField.ErrorMessage >
)}
</ SeedField.Footer >
)}
</ SeedField.Root >
);
};
export interface SelectTriggerProps extends Omit < SeedSelect . TriggerProps , "children" > {
placeholder ?: React . ReactNode ;
prefixIcon ?: React . ReactNode ;
/**
* @default <IconChevronDownSmallLine />
*/
suffixIcon ?: React . ReactNode ;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/select
*/
export const SelectTrigger = React. forwardRef < HTMLButtonElement , SelectTriggerProps >(
({ placeholder , prefixIcon , suffixIcon = < IconChevronDownSmallLine />, ... props }, ref ) => {
return (
< SeedSelect.Trigger ref = {ref} { ... props}>
< SeedSelect.PrefixIcon fallback = {prefixIcon} />
< SeedSelect.Value />
{placeholder && < SeedSelect.Placeholder >{placeholder}</ SeedSelect.Placeholder >}
< SeedSelect.SuffixIcon svg = {suffixIcon} />
</ SeedSelect.Trigger >
);
},
);
SelectTrigger.displayName = "SelectTrigger" ;
export interface SelectContentProps extends SeedSelect . ContentProps {
positionerContainer ?: SeedSelect . PositionerProps [ "container" ];
}
/**
* @see https://crispy.carrot-soft.tech/react/components/select
*/
export const SelectContent = React. forwardRef < HTMLDivElement , SelectContentProps >(
({ children , positionerContainer , ... props }, ref ) => {
return (
< SeedSelect.Positioner container = {positionerContainer}>
< SeedSelect.Content ref = {ref} { ... props}>
< SeedSelect.ScrollArea >{children}</ SeedSelect.ScrollArea >
</ SeedSelect.Content >
</ SeedSelect.Positioner >
);
},
);
SelectContent.displayName = "SelectContent" ;
export interface SelectGroupProps extends SeedSelect . GroupProps {
label ?: React . ReactNode ;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/select
*/
export const SelectGroup = React. forwardRef < HTMLDivElement , SelectGroupProps >(
({ label , children , ... props }, ref ) => {
return (
< SeedSelect.Group ref = {ref} { ... props}>
{label && < SeedSelect.GroupLabel >{label}</ SeedSelect.GroupLabel >}
{children}
</ SeedSelect.Group >
);
},
);
SelectGroup.displayName = "SelectGroup" ;
export interface SelectItemProps extends Omit < SeedSelect . ItemProps , "children" > {
description ?: React . ReactNode ;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/select
*/
export const SelectItem = React. forwardRef < HTMLDivElement , SelectItemProps >(
({ description , ... props }, ref ) => {
return (
< SeedSelect.Item ref = {ref} { ... props}>
< SeedSelect.ItemPrefixIcon />
< SeedSelect.ItemBody >
< SeedSelect.ItemLabel />
{description && < SeedSelect.ItemDescription >{description}</ SeedSelect.ItemDescription >}
</ SeedSelect.ItemBody >
< SeedSelect.ItemIndicator selected = {< IconCheckmarkFatFill />} />
</ SeedSelect.Item >
);
},
);
SelectItem.displayName = "SelectItem" ;
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
import {
SelectRoot,
SelectTrigger,
SelectContent,
SelectGroup,
SelectItem,
} from "seed-design/ui/select" ;
< SelectRoot defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "Fruit" placeholder = "Select a fruit" />
< SelectContent >
< SelectGroup label = "Domestic" >
< SelectItem value = "apple" label = "Apple" />
< SelectItem value = "pear" label = "Pear" />
</ SelectGroup >
< SelectGroup label = "Imported" >
< SelectItem value = "banana" label = "Banana" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
SelectRoot: Manages the selected value (value) and open state.
SelectTrigger: Displays the selected value or placeholder, and opens the list when clicked.
SelectContent: Floating container that wraps the option list.
SelectGroup: Groups related options together. All SelectItem must be inside a SelectGroup. The group title can be displayed with label.
SelectItem: Individual option. Requires value, and shows a checkmark when selected.
label?React.ReactNode
indicator?React.ReactNode
showRequiredIndicator?boolean | undefined
description?React.ReactNode
errorMessage?React.ReactNode
hiddenSelectProps?SeedSelect.HiddenSelectProps | undefined
fieldRef?React.Ref < HTMLDivElement > | undefined
children?React.ReactNode
open?boolean | undefined
defaultOpen?boolean | undefined
onOpenChange?(( open : boolean , details ?: SelectOpenChangeDetails ) => void ) | undefined
defaultValue?string[] | undefined
onValueChange?(( value : string []) => void ) | undefined
disabled?boolean | undefined
invalid?boolean | undefined
readOnly?boolean | undefined
placeholder?React.ReactNode
prefixIcon?React.ReactNode
positionerContainer?React.RefObject < HTMLElement | null > | undefined
asChild?boolean | undefined
description?React.ReactNode
valuestring
disabled?boolean | undefined
textValue?string | undefined
The size of the trigger and list is set by size. (default: large)
When using responsive, the size automatically switches based on screen width.
import { VStack } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectSize () {
return (
< VStack gap = "x4" width = "240px" >
< SelectRoot size = "large" defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "과일 (large)" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< SelectRoot size = "medium" defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "과일 (medium)" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ VStack >
);
}
Group options with SelectGroup and display the group title with label. Regardless of the number of SelectGroup, all SelectItem must be inside a SelectGroup.
If there are two or more groups, a divider is automatically drawn between the groups.
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectGroups () {
return (
< Box width = "240px" >
< SelectRoot defaultValue = {[ "seoul" ]}>
< SelectTrigger aria-label = "지역" placeholder = "지역 선택" />
< SelectContent >
< SelectGroup label = "아시아" >
< SelectItem value = "seoul" label = "서울" />
< SelectItem value = "tokyo" label = "도쿄" />
< SelectItem value = "singapore" label = "싱가포르" />
< SelectItem value = "dubai" label = "두바이" />
</ SelectGroup >
< SelectGroup label = "유럽" >
< SelectItem value = "london" label = "런던" />
< SelectItem value = "paris" label = "파리" />
< SelectItem value = "berlin" label = "베를린" />
</ SelectGroup >
< SelectGroup label = "아메리카" >
< SelectItem value = "new-york" label = "뉴욕" />
< SelectItem value = "sao-paulo" label = "상파울루" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
Specifying multiple on SelectRoot allows selecting multiple options. The list does not close when an option is selected, and pressing an already selected option again deselects it.
The trigger displays the textValue of the selected options joined by ", ". This text can be changed with Custom Value Format .
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectMultiple () {
return (
< Box width = "240px" >
< SelectRoot multiple defaultValue = {[ "apple" , "cherry" ]}>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
< SelectItem value = "grape" label = "포도" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
Add supplementary descriptions to options with SelectItem's description prop.
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectWithDescription () {
return (
< Box width = "280px" >
< SelectRoot defaultValue = {[ "standard" ]}>
< SelectTrigger aria-label = "배송 방법" placeholder = "배송 방법 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "standard" label = "일반 배송" description = "3-5일 소요" />
< SelectItem value = "express" label = "빠른 배송" description = "1-2일 소요" />
< SelectItem value = "same-day" label = "당일 배송" description = "오늘 도착" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
Display icons on options with SelectItem's prefixIcon prop.
When one option is selected, that option's icon is displayed as the trigger's prefix icon. When no option is selected, the selected option has no icon, or multiple options are selected, the trigger displays the prefixIcon specified on SelectTrigger.
import {
IconGlobeLine,
IconLockLine,
IconPerson2Line,
IconPersonLine,
} from "@karrotmarket/react-monochrome-icon" ;
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectWithPrefixIcon () {
return (
< Box width = "280px" >
< SelectRoot >
< SelectTrigger
aria-label = "공유 대상"
placeholder = "공유 대상"
prefixIcon = {< IconPerson2Line />}
/>
< SelectContent >
< SelectGroup label = "그룹" >
< SelectItem value = "public" label = "전체 공개" prefixIcon = {< IconGlobeLine />} />
< SelectItem value = "followers" label = "팔로워만" prefixIcon = {< IconLockLine />} />
< SelectItem value = "private" label = "나만" prefixIcon = {< IconPersonLine />} />
</ SelectGroup >
< SelectGroup label = "사람" >
< SelectItem value = "kim" label = "김하늘" />
< SelectItem value = "lee" label = "이하늘" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
You can pass a ReactNode to SelectItem's label. This node is rendered only on the options in the list.
When specifying a ReactNode instead of a string as SelectItem's label, the trigger displays SelectItem's textValue. textValue is label if label is a string, otherwise value.
Therefore, when label is not a string, it is recommended to also specify textValue. textValue is used as the text displayed on the trigger, the string matched when finding options by typing on the keyboard (typeahead), and the text of the native <option> for form submission. If you pass a ReactNode instead of a string as label without specifying textValue, these values are all set to value by default, and a warning is printed in development mode.
If you want to change only the typeahead string, use typeaheadLabel.
import {
IconCarLine,
IconFigureBikeLine,
IconMetroFrontsideLine,
} from "@karrotmarket/react-monochrome-icon" ;
import { Badge, Box, HStack } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectCustomLabel () {
return (
< Box width = "280px" >
< SelectRoot defaultValue = {[ "metro" ]}>
< SelectTrigger aria-label = "이동 수단" placeholder = "이동 수단 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "bike" label = "자전거" prefixIcon = {< IconFigureBikeLine />} />
< SelectItem
value = "metro"
textValue = "지하철"
prefixIcon = {< IconMetroFrontsideLine />}
label = {
< HStack as = "span" align = "center" gap = "x1_5" >
지하철
< Badge variant = "weak" tone = "informative" >
가장 빠름
</ Badge >
</ HStack >
}
/>
< SelectItem
value = "car"
textValue = "자동차"
disabled
prefixIcon = {< IconCarLine />}
label = {
< HStack as = "span" align = "center" gap = "x1_5" >
자동차
< Badge variant = "weak" tone = "warning" >
고객지원에 문의
</ Badge >
</ HStack >
}
/>
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
Disable specific options with SelectItem's disabled, and disable the entire Select with SelectRoot's disabled.
import { VStack } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectDisabled () {
return (
< VStack gap = "x4" width = "240px" >
< SelectRoot defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" disabled />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< SelectRoot disabled defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "비활성 과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ VStack >
);
}
If there are many options, the list scrolls within itself. The maximum height of the list is set to the smaller of the specified maximum height and the remaining screen space around the trigger, and it is placed to avoid the notch and home indicator areas.
The list opens scrolled to the position where the currently selected option is visible.
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
const TIME_SLOTS = Array. from ({ length: 48 }, ( _ , index ) => {
const hour = String (Math. floor (index / 2 )). padStart ( 2 , "0" );
const minute = index % 2 === 0 ? "00" : "30" ;
return `${ hour }:${ minute }` ;
});
export default function SelectLongList () {
return (
< Box width = "240px" >
< SelectRoot defaultValue = {[ "14:00" ]}>
< SelectTrigger aria-label = "예약 시간" placeholder = "시간 선택" />
< SelectContent >
< SelectGroup >
{ TIME_SLOTS . map (( slot ) => (
< SelectItem key = {slot} value = {slot} label = {slot} />
))}
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
Set the list's position with SelectRoot's placement prop. The default value is "bottom".
import { Box } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectPlacement () {
return (
< Box width = "240px" >
< SelectRoot placement = "top" defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
);
}
You can control the selected value with value and onValueChange.
import { VStack } from "@seed-design/react" ;
import * as React from "react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectControlled () {
const [ value , setValue ] = React. useState < string []>([ "apple" ]);
return (
< VStack gap = "x2" width = "240px" >
< SelectRoot value = {value} onValueChange = {setValue}>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< span >선택된 값: {value. length > 0 ? value. join ( ", " ) : "없음" }</ span >
</ VStack >
);
}
You can control the list's open state with open and onOpenChange. To specify only the initial state, use defaultOpen.
When the list opens, focus moves to the list, and when it closes, it returns to the trigger.
import { VStack } from "@seed-design/react" ;
import * as React from "react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectControlledOpen () {
const [ open , setOpen ] = React. useState ( false );
return (
< VStack gap = "x2" width = "240px" >
< SelectRoot open = {open} onOpenChange = {setOpen} defaultValue = {[ "apple" ]}>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< ActionButton variant = "neutralWeak" disabled = {open} onClick = {() => setOpen ( true )}>
목록 열기
</ ActionButton >
< span >목록 상태: {open ? "열림" : "닫힘" }</ span >
</ VStack >
);
}
details is provided as the second argument to onOpenChange.
When opening (open: true)
"trigger": clicking SelectTrigger or using the ↓ , ↑ , Enter , Space keys while SelectTrigger is focused
When closing (open: false)
"trigger": clicking SelectTrigger while the list is open
"itemSelect": selecting an option
In multiple mode, this does not occur because the list does not close when an option is selected.
"keyboardClose": using the Enter or Space key when no option is highlighted
"escapeKeyDown": ESC key used
"interactOutside": click outside area
"focusOut": focus moves outside the list via Tab or similar
"cascadeDismiss": cascading close due to parent layer closing
import { Box, HStack, Text, VStack } from "@seed-design/react" ;
import { useState } from "react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectOpenChangeReason () {
const [ open , setOpen ] = useState ( false );
const [ openReason , setOpenReason ] = useState < string | null >( null );
const [ closeReason , setCloseReason ] = useState < string | null >( null );
return (
< VStack gap = "x4" align = "center" >
< Box width = "240px" >
< SelectRoot
open = {open}
onOpenChange = {( open , details ) => {
setOpen (open);
(open ? setOpenReason : setCloseReason)(details?.reason ?? null );
}}
defaultValue = {[ "apple" ]}
>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ Box >
< HStack gap = "x4" >
< Text fontSize = "t3" color = "fg.neutralMuted" >
마지막 열림 이유: {openReason ?? "-" }
</ Text >
< Text fontSize = "t3" color = "fg.neutralMuted" >
마지막 닫힘 이유: {closeReason ?? "-" }
</ Text >
</ HStack >
</ VStack >
);
}
Customize the selected value displayed on the trigger with SelectRoot's formatValue prop. By default, it displays the textValue of the selected options joined by ", ". The default value of textValue is label if label is a string, otherwise value.
Each item received by formatValue also contains label, so if you want to render a ReactNode label on the trigger, you can extract and use it here.
// Display only the label of the first selected option on the trigger
< SelectRoot formatValue = {( items ) => items[ 0 ]?.label} />
Using Intl.ListFormat allows you to combine the list with conjunctions appropriate for the locale.
import { HStack } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
const listFormat = new Intl. ListFormat ( "ko" , { type: "conjunction" });
export default function SelectFormatValue () {
return (
< HStack gap = "x4" width = "full" >
< SelectRoot
multiple
defaultValue = {[ "apple" , "banana" ]}
formatValue = {( items ) => listFormat. format (items. map (( item ) => item.textValue))}
>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< SelectRoot
multiple
defaultValue = {[ "apple" , "banana" , "cherry" ]}
formatValue = {([ first , ... rest ]) =>
rest. length > 0 ? `${ first . textValue } 외 ${ rest . length }개` : first.textValue
}
>
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ HStack >
);
}
You can use Field-related props such as label, description, and errorMessage.
import { VStack } from "@seed-design/react" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectField () {
return (
< VStack gap = "x6" width = "240px" >
< SelectRoot
label = "과일"
description = "가장 좋아하는 과일을 선택하세요."
defaultValue = {[ "apple" ]}
>
< SelectTrigger placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< SelectRoot label = "과일" labelWeight = "bold" invalid errorMessage = "과일을 선택해주세요." >
< SelectTrigger placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
</ VStack >
);
}
It can participate in form submission through the hidden native <select> that SelectRoot renders internally.
import { VStack } from "@seed-design/react" ;
import * as React from "react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
export default function SelectForm () {
const [ submitted , setSubmitted ] = React. useState < string | null >( null );
return (
< VStack asChild gap = "x2" width = "240px" >
< form
onSubmit = {( event ) => {
event. preventDefault ();
const data = new FormData (event.currentTarget);
setSubmitted ( String (data. get ( "fruit" )));
}}
>
< SelectRoot name = "fruit" required >
< SelectTrigger aria-label = "과일" placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< ActionButton type = "submit" variant = "neutralSolid" >
제출
</ ActionButton >
{submitted && < span >제출된 값: {submitted}</ span >}
</ form >
</ VStack >
);
}
By connecting value, onValueChange, invalid, and errorMessage, you can control the validation state with React Hook Form 's useController.
import { HStack, VStack } from "@seed-design/react" ;
import { useCallback, type FormEvent } from "react" ;
import { useController, useForm } from "react-hook-form" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import {
SelectContent,
SelectGroup,
SelectItem,
SelectRoot,
SelectTrigger,
} from "seed-design/ui/select" ;
interface FormValues {
fruit : string [];
}
export default function SelectReactHookForm () {
const { handleSubmit , reset , control } = useForm < FormValues >({
reValidateMode: "onSubmit" ,
defaultValues: {
fruit: [],
},
});
// SelectRoot is not a forwardRef component, so pass only the field props it
// accepts instead of spreading `...field` (which carries a `ref`).
const {
field : { value , onChange , name },
fieldState ,
} = useController ({
name: "fruit" ,
control,
rules: {
validate : ( value ) => value. length > 0 || "과일을 선택해주세요" ,
},
});
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" width = "full" as = "form" onSubmit = { handleSubmit (onValid)} onReset = {onReset}>
< SelectRoot
name = {name}
label = "과일"
description = "가장 좋아하는 과일을 선택하세요"
invalid = {fieldState.invalid}
errorMessage = {fieldState.error?.message}
value = {value}
onValueChange = {onChange}
showRequiredIndicator
>
< SelectTrigger placeholder = "과일 선택" />
< SelectContent >
< SelectGroup >
< SelectItem value = "apple" label = "사과" />
< SelectItem value = "banana" label = "바나나" />
< SelectItem value = "cherry" label = "체리" />
</ SelectGroup >
</ SelectContent >
</ SelectRoot >
< HStack gap = "x2" >
< ActionButton type = "reset" variant = "neutralWeak" >
초기화
</ ActionButton >
< ActionButton type = "submit" variant = "neutralSolid" flexGrow = { 1 }>
제출
</ ActionButton >
</ HStack >
</ VStack >
);
}