ListBox (리스트 박스)
ListBox는 옵션 목록을 보여주고 사용자가 그중 하나 이상을 선택할 수 있게 해주는 컴포넌트예요. 목록에서 항목을 고르는 일이 잦은 화면이라면 이 컴포넌트가 큰 도움이 돼요.
출처: HeroUI ListBox
본문
사용법 (Usage)
import { ListBox } from '@heroui/react';
{"name": "list-box-default"}
구조 (Anatomy)
import { ListBox, Label, Description, Header } from '@heroui/react';
export default () => (
<ListBox>
<ListBox.Item>
<Label />
<Description />
<ListBox.ItemIndicator />
</ListBox.Item>
<ListBox.Section>
<Header />
<ListBox.Item>
<Label />
</ListBox.Item>
</ListBox.Section>
</ListBox>
)
예시 (Examples)
비활성화된 항목 (With Disabled Items)
{"name": "list-box-with-disabled-items"}
섹션과 함께 (With Sections)
{"name": "list-box-with-sections"}
다중 선택 (Multi Select)
{"name": "list-box-multi-select"}
제어된 상태 (Controlled)
{"name": "list-box-controlled"}
가상화 (Virtualization)
ListBox는 Virtualizer를 통해 가상화를 지원해요. 뷰포트 안에 보이는 행만 렌더링해서 대용량 데이터도 효율적으로 그릴 수 있어요.
{"name": "list-box-virtualization"}
사용자 지정 체크 아이콘 (Custom Check Icon)
{"name": "list-box-custom-check-icon"}
렌더 함수 (Render Function)
{"name": "list-box-render-function"}
커스터마이징 (Customization)
Tailwind CSS
{"name": "list-box-custom-styles"}
Global CSS
ListBox 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.list-box {
@apply rounded-lg border border-border bg-surface p-2;
}
.list-box-item {
@apply rounded px-2 py-1 cursor-pointer;
}
.list-box-item--danger {
@apply text-danger;
}
.list-box-item__indicator {
@apply text-accent;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 쉽게 커스터마이징할 수 있게 해요.
CSS 클래스 (CSS Classes)
ListBox 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 클래스 (Base Classes) [!toc]
.list-box- 기본 리스트박스 컨테이너.list-box-item- 개별 리스트박스 항목.list-box-item__indicator- 선택 표시 아이콘.list-box-section- 항목을 그룹화하는 섹션 컨테이너
변형 클래스 (Variant Classes) [!toc]
.list-box--default- 기본 변형 스타일.list-box--danger- 위험 변형 스타일.list-box-item--default- 기본 항목 변형.list-box-item--danger- 위험 항목 변형
상태 클래스 (State Classes) [!toc]
.list-box-item[data-selected="true"]- 선택된 항목 상태.list-box-item[data-focus-visible="true"]- 포커스된 항목 상태.list-box-item[data-disabled="true"]- 비활성화된 항목 상태.list-box-item__indicator[data-visible="true"]- 표시된 인디케이터 상태
상호작용 상태 (Interactive States)
컴포넌트는 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 호버 (Hover): 항목에
:hover또는[data-hovered="true"] - 포커스 (Focus): 항목에
:focus-visible또는[data-focus-visible="true"] - 선택됨 (Selected): 항목에
[data-selected="true"] - 비활성화 (Disabled): 항목에
:disabled또는[data-disabled="true"]
API 참고 (API Reference)
ListBox
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label |
string |
- | Accessibility label for the listbox |
aria-labelledby |
string |
- | ID of element that labels the listbox |
selectionMode |
"none" | "single" | "multiple" |
"single" |
Selection behavior |
selectedKeys |
Selection |
- | Controlled selected keys |
defaultSelectedKeys |
Selection |
- | Initial selected keys |
onSelectionChange |
(keys: Selection) => void |
- | Handler called when selection changes |
disabledKeys |
Iterable<Key> |
- | Keys of disabled items |
onAction |
(key: Key) => void |
- | Handler called when an item is activated |
variant |
"default" | "danger" |
"default" |
Visual variant |
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | ListBox items and sections |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, ListBoxRenderProps> |
- | Overrides the default DOM element with a custom render function. |
ListBox.Item
| Prop | Type | Default | Description |
|---|---|---|---|
id |
Key |
- | Unique identifier for the item |
textValue |
string |
- | Text value for accessibility and typeahead |
isDisabled |
boolean |
false |
Whether this item is disabled |
variant |
"default" | "danger" |
"default" |
Visual variant |
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Item content or render function |
render |
(props: DetailedHTMLProps<LinkWithRequiredHref, HTMLAnchorElement> | React.JSX.IntrinsicElements[keyof React.JSX.IntrinsicElements], renderProps: ListBoxItemRenderProps) => ReactElement |
- | Overrides the default DOM element with a custom render function. |
ListBox.ItemIndicator
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Custom indicator content or render function |
ListBox.Section
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Section content including Header and Items |
Render Props
ListBox.Item 또는 ListBox.ItemIndicator에 렌더 함수를 사용하면 이 값들이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
Whether the item is selected |
isFocused |
boolean |
Whether the item is focused |
isDisabled |
boolean |
Whether the item is disabled |
isPressed |
boolean |
Whether the item is being pressed |
ListLayout
| Name | Type | Default | Description |
|---|---|---|---|
rowHeight |
number | undefined |
48 | The fixed height of a row in px. |
estimatedRowHeight |
number | undefined |
— | The estimated height of a row, when row heights are variable. |
headingHeight |
number | undefined |
48 | The fixed height of a section header in px. |
estimatedHeadingHeight |
number | undefined |
— | The estimated height of a section header, when the height is variable. |
loaderHeight |
number | undefined |
48 | The fixed height of a loader element in px. This loader is specifically for "load more" elements rendered when loading more rows at the root level or inside nested row/sections. |
dropIndicatorThickness |
number | undefined |
2 | The thickness of the drop indicator. |
gap |
number | undefined |
0 | The gap between items. |
padding |
number | undefined |
0 | The padding around the list. |
접근성 (Accessibility)
ListBox 컴포넌트는 ARIA 리스트박스 패턴을 구현하며 다음을 제공해요:
- 완전한 키보드 탐색 지원
- 선택 변경에 대한 스크린 리더 안내
- 올바른 포커스 관리
- 비활성화 상태 지원
- 타입어헤드 검색 기능
자세한 내용은 React Aria ListBox 문서를 참고하세요.