Select
접을 수 있는 옵션 목록을 표시하고 사용자가 그중 하나를 선택할 수 있게 해주는 셀렉트예요.
출처: HeroUI Select
본문
사용법 (Usage)
import { Select } from "@heroui/react";
{"component": "select-default"}
구조 (Anatomy)
import {Select, Label, Description, Header, ListBox, Separator} from "@heroui/react";
export default () => (
<Select>
<Label />
<Select.Trigger>
<Select.Value />
<Select.ClearButton />
<Select.Indicator />
</Select.Trigger>
<Description />
<Select.Popover>
<ListBox>
<ListBox.Item>
<Label />
<Description />
<ListBox.ItemIndicator />
</ListBox.Item>
<ListBox.Section>
<Header />
<ListBox.Item>
<Label />
</ListBox.Item>
</ListBox.Section>
</ListBox>
</Select.Popover>
</Select>
);
예시 (Examples)
변형 (Variants)
Select 컴포넌트는 두 가지 시각적 변형을 지원해요:
primary(기본값) - 그림자가 있는 표준 스타일, 대부분의 사용 사례에 적합secondary- 그림자가 없는 낮은 강조 변형, Surface 컴포넌트에서 사용하기 적합
{"component": "select-variants"}
전체 너비 (Full Width)
{"component": "select-full-width"}
설명 포함 (With Description)
{"component": "select-with-description"}
필수 (Required)
{"component": "select-required"}
비활성화 (Disabled)
{"component": "select-disabled"}
비활성 옵션 포함 (With Disabled Options)
{"component": "select-with-disabled-options"}
다중 선택 (Multiple Select)
{"component": "select-multiple-select"}
섹션 포함 (With Sections)
{"component": "select-with-sections"}
제어 (Controlled)
{"component": "select-controlled"}
제어 다중 선택 (Controlled Multiple)
{"component": "select-controlled-multiple"}
제어 열림 상태 (Controlled Open State)
{"component": "select-controlled-open-state"}
비동기 로딩 (Asynchronous Loading)
{"component": "select-asynchronous-loading"}
커스텀 인디케이터 (Custom Indicator)
{"component": "select-custom-indicator"}
지우기 버튼 포함 (With Clear Button)
Select.Trigger는 버튼이므로 지우기 위해 중첩된 <button>은 사용할 수 없어요. 트리거 안에 Select.ClearButton을 합성하세요 — 버튼이 아닌 컨트롤로 렌더링되어 메뉴를 열지 않아요.
{"component": "select-with-clear-button"}
ARIA는 버튼의 자식을 표현적(presentational)으로 취급하므로, 지우기 컨트롤은 포커스를 받을 수 없어요. 따라서 aria-hidden 이며 포인터 상호작용으로만 동작해요. Select.ClearButton이 합성되면 트리거는 Backspace나 Delete로도 선택을 지워요. 이것이 키보드 및 스크린 리더 사용자가 선택을 지우는 방식이에요. 두 경로 모두 onClear를 호출해요.
커스텀 값 (Custom Value)
{"component": "select-custom-value"}
렌더 함수 (Render Function)
{"component": "select-render-function"}
서피스 내에서 (In Surface)
Surface 컴포넌트 안에서 사용할 때는 variant="secondary" 를 사용해서 서피스 배경에 어울리는 낮은 강조 변형을 적용하세요.
{"component": "select-on-surface"}
커스터마이징 (Customization)
Tailwind CSS
{"component": "select-custom-styles"}
전역 CSS (Global CSS)
Select 컴포넌트 클래스를 커스터마이징하려면 @layer components 디렉티브를 사용하세요.
@layer components {
.select {
@apply flex flex-col gap-1;
}
.select__trigger {
@apply rounded-lg border border-border bg-surface p-2;
}
.select__value {
@apply text-current;
}
.select__indicator {
@apply text-muted;
}
.select__popover {
@apply rounded-lg border border-border bg-surface p-2;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽게 만들어요.
CSS 클래스 (CSS Classes)
Select 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 클래스 [!toc]
.select- 기본 select 컨테이너.select__trigger- select를 여는 버튼.select__value- 표시되는 값 또는 플레이스홀더.select__clear-button- 트리거 안의 선택적 지우기 컨트롤.select__indicator- 드롭다운 인디케이터 아이콘.select__popover- 팝오버 컨테이너
변형 클래스 [!toc]
.select--primary- 그림자가 있는 기본 변형 (기본값).select--secondary- 그림자가 없는 보조 변형, 서피스에서 사용하기 적합
상태 클래스 [!toc]
.select[data-invalid="true"]- 유효하지 않은 상태.select__trigger[data-focus-visible="true"]- 포커스된 트리거 상태.select__trigger[data-disabled="true"]- 비활성화된 트리거 상태.select__value[data-placeholder="true"]- 플레이스홀더 상태.select__clear-button[data-empty="true"]- 선택이 없을 때 지우기 버튼 숨김.select__indicator[data-open="true"]- 열린 인디케이터 상태
인터랙티브 상태 (Interactive States)
컴포넌트는 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 호버: 트리거의
:hover또는[data-hovered="true"] - 포커스: 트리거의
:focus-visible또는[data-focus-visible="true"] - 비활성화: select의
:disabled또는[data-disabled="true"] - 열림: 인디케이터의
[data-open="true"]
API 참고 (API Reference)
Select
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder |
string |
'Select an item' |
Temporary text that occupies the select when it is empty |
selectionMode |
"single" | "multiple" |
"single" |
Whether single or multiple selection is enabled |
isOpen |
boolean |
- | Sets the open state of the menu (controlled) |
defaultOpen |
boolean |
- | Sets the default open state of the menu (uncontrolled) |
onOpenChange |
(isOpen: boolean) => void |
- | Handler called when the open state changes |
disabledKeys |
Iterable<Key> |
- | Keys of disabled items |
isDisabled |
boolean |
- | Whether the select is disabled |
value |
Key | Key[] | null |
- | Current value (controlled) |
defaultValue |
Key | Key[] | null |
- | Default value (uncontrolled) |
onChange |
(value: Key | Key[] | null) => void |
- | Handler called when the value changes |
onClear |
() => void |
- | Handler called when the selection is cleared |
isRequired |
boolean |
- | Whether user input is required |
isInvalid |
boolean |
- | Whether the select value is invalid |
name |
string |
- | The name of the input, used when submitting an HTML form |
autoComplete |
string |
- | Describes the type of autocomplete functionality |
fullWidth |
boolean |
false |
Whether the select should take full width of its container |
variant |
"primary" | "secondary" |
"primary" |
Visual variant of the component. primary is the default style with shadow. secondary is a lower emphasis variant without shadow, suitable for use in surfaces. |
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Select content or render function |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SelectRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Select.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Trigger content or render function |
Select.Value
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Value content or render function |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SelectValueRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Select.Indicator
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Custom indicator content |
Select.ClearButton
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Custom content, replacing the default icon |
onClick |
(e: MouseEvent) => void |
- | Handler called when the control is clicked |
Select.ClearButton은 span으로 렌더링되므로 버튼 중첩 없이 Select.Trigger(버튼) 안에 있을 수 있어요. 선택이 비어 있으면 시각적으로 숨겨져요.
Select.Popover
| Prop | Type | Default | Description |
|---|---|---|---|
placement |
"bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right" | "right top" | "right bottom" | "end" | "end top" | "end bottom" |
"bottom" |
Placement of the popover relative to the trigger |
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Content children |
렌더 프롭스 (Render Props)
Select.Value와 함께 렌더 함수를 사용하면 다음 값이 제공돼요:
| Prop | Type | Description |
|---|---|---|
defaultChildren |
ReactNode |
The default rendered value |
isPlaceholder |
boolean |
Whether the value is a placeholder |
state |
SelectState |
The state of the select |
selectedItems |
Node[] |
The currently selected items |
접근성 (Accessibility)
Select 컴포넌트는 ARIA 리스트박스 패턴을 구현하며 다음을 제공해요:
- 완전한 키보드 탐색 지원
- 선택 변경에 대한 스크린 리더 알림
- 올바른 포커스 관리
- 비활성화된 상태 지원
- Typeahead 검색 기능
- HTML 폼 통합
자세한 내용은 React Aria Select 문서를 참고하세요.