키보드 내비게이션, 타자 검색(type-ahead search), 단일/다중 선택을 지원하는 스타일 없는 리스트박스 컴포넌트예요.
출처: 문서
본문
기능 (Features)
- 하위 컴포넌트 구성 API:
Root, List, Option, OptionIndicator, Header, Filter, Empty, Footer
- 키보드 포커스 순환과 타자 검색을 갖춘 단일/다중 선택
- 그룹 헤더 렌더링을 갖춘 옵션 그룹화
- 커스터마이즈 가능한 필터링 로직과 통합된 내장 필터 입력
사용법 (Usage)
import { Listbox } from 'primereact/listbox';
<Listbox.Root>
<Listbox.Header>
<Listbox.Filter />
</Listbox.Header>
<Listbox.List>
<Listbox.Option>
<Listbox.OptionIndicator />
</Listbox.Option>
</Listbox.List>
<Listbox.Empty />
<Listbox.Footer />
</Listbox.Root>
동작 (Behavior)
다형적 렌더링 (Polymorphic Rendering)
as를 사용해 하위 컴포넌트가 렌더링할 HTML 요소를 바꿀 수 있어요.
<Listbox.Root as="section">
<Listbox.List as="div">...</Listbox.List>
</Listbox.Root>
렌더 함수 자식 (Render Function Children)
Listbox.List는 커스텀 옵션 렌더링을 위한 컴포넌트 인스턴스에 접근할 수 있는 렌더 함수를 children으로 받아요.
<Listbox.List>
{(instance) =>
instance.options?.map((option, index) => (
<Listbox.Option key={index} index={index}>
{instance.listbox?.getOptionLabel(option)}
</Listbox.Option>
))
}
</Listbox.List>
Pass Through
<DocPTViewer name="listbox-pt" components={['ListboxRoot']} />
API
ListboxRoot
| Label |
Type |
Description |
| root |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| hiddenElement |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the hidden sentinel element's DOM element. |
| header |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the header's DOM element. |
| footer |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the footer's DOM element. |
| list |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the list's DOM element. |
| empty |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the empty message's DOM element. |
| optionGroup |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the option group's DOM element. |
| option |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the option's DOM element. |
| optionIndicator |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the option indicator's DOM element. |
| filter |
ListboxRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the filter's DOM element. |
ListboxList
| Label |
Type |
Description |
| root |
ListboxListPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ListboxOption
| Attribute |
Value |
data-selected |
Present when selected |
data-unselected |
Present when unselected |
data-focused |
Present when focused |
data-disabled |
Present when disabled |
| Label |
Type |
Description |
| root |
ListboxOptionPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ListboxOptionIndicator
| Attribute |
Value |
data-selected |
Present when selected |
data-unselected |
Present when unselected |
| Label |
Type |
Description |
| root |
ListboxOptionIndicatorPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
ListboxHeaderPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ListboxFilter
| Label |
Type |
Description |
| root |
ListboxFilterPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ListboxEmpty
| Label |
Type |
Description |
| root |
ListboxEmptyPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
ListboxFooterPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
컴포넌트를 설명하는 값은 aria-labelledby나 aria-label prop으로 제공할 수 있어요. 목록 요소는 listbox 역할을 가지며, 다중 선택이 활성화되면 aria-multiselectable이 true로 설정돼요. 각 목록 항목은 aria-selected와 aria-disabled 속성을 가진 option 역할을 가져요.
metaKeySelection이 활성화되면 포인터 기반 다중 선택에 플랫폼 메타 키가 필요해요(Windows/Linux에서는 Ctrl, macOS에서는 Cmd). 키보드 상호작용은 표준 listbox 단축키로 계속 사용할 수 있어요.
키보드 지원 (Keyboard Support)
| Key |
Function |
tab |
Moves focus to the first selected option, if there is none then first option receives the focus. |
up arrow |
Moves focus to the previous option. |
down arrow |
Moves focus to the next option. |
enter |
Toggles the selected state of the focused option. |
space |
Toggles the selected state of the focused option. |
home |
Moves focus to the first option. |
end |
Moves focus to the last option. |
shift + down arrow |
Moves focus to the next option and toggles the selection state. |
shift + up arrow |
Moves focus to the previous option and toggles the selection state. |
shift + space |
Selects the items between the most recently selected option and the focused option. |
control + shift + home |
Selects the focused option and all the options up to the first one. |
control + shift + end |
Selects the focused option and all the options down to the last one. |
control + a |
Selects all options. |
page up |
Jumps visual focus to the first option. |
page down |
Jumps visual focus to the last option. |
any printable character |
Moves focus to the option whose label starts with the characters being typed. |
| Key |
Function |
down arrow |
Moves focus to the next option, if there is none then visual focus does not change. |
up arrow |
Moves focus to the previous option, if there is none then visual focus does not change. |
left arrow |
Removes the visual focus from the current option and moves input cursor to one character left. |
right arrow |
Removes the visual focus from the current option and moves input cursor to one character right. |
home |
Moves input cursor at the beginning, if not then moves focus to the first option. |
end |
Moves input cursor at the end, if not then moves focus to the last option. |
tab |
Moves focus to the next focusable element in the component. If there is none, moves focus to next element in page. |
더 알아보기 (Learn more)