본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

AutoComplete

원문 보기 위키 갱신

실시간 검색, 키보드 탐색, 팝업 포지셔닝을 지원하는 스타일 없는(unstyled) 자동완성 입력 컴포넌트예요.

출처: 문서

본문

특징 (Features)

  • 복합 컴포넌트 API: Root, Input, Trigger, Indicator, Clear, Portal, Positioner, Popup, List, Option, OptionIndicator, Arrow, Header, Footer, Empty, Value 서브 컴포넌트
  • 구성 가능한 지연(delay)과 최소 길이를 가진 onComplete 콜백을 통한 실시간 검색
  • blur 시 일치하지 않는 입력을 지우는 강제 선택 모드
  • optionGroupLabel과 optionGroupChildren을 통한 옵션 그룹화
  • 내장된 지우기 버튼과 트리거 토글 지원

사용법 (Usage)

import { AutoComplete } from 'primereact/autocomplete';
<AutoComplete.Root>
    <AutoComplete.Input />
    <AutoComplete.Trigger>
        <AutoComplete.Indicator />
    </AutoComplete.Trigger>
    <AutoComplete.Clear />
    <AutoComplete.Portal>
        <AutoComplete.Positioner>
            <AutoComplete.Popup>
                <AutoComplete.Header />
                <AutoComplete.List>
                    <AutoComplete.Option>
                        <AutoComplete.OptionIndicator />
                    </AutoComplete.Option>
                </AutoComplete.List>
                <AutoComplete.Empty />
                <AutoComplete.Footer />
            </AutoComplete.Popup>
        </AutoComplete.Positioner>
    </AutoComplete.Portal>
</AutoComplete.Root>

동작 (Behavior)

모션 애니메이션 (Motion Animation)

<AutoComplete.Popup motionProps={{ name: 'p-autocomplete' }}>...</AutoComplete.Popup>

애니메이션 단계, CSS 변수, 숨김 전략에 대해서는 Motion을 참고하세요.

다형 렌더링 (Polymorphic Rendering)

어떤 서브 컴포넌트든 as를 사용해 렌더링되는 HTML 요소를 바꿀 수 있어요.

<AutoComplete.Input as={CustomInput} />
<AutoComplete.Trigger as="div">...</AutoComplete.Trigger>

렌더 함수 자식 (Render Function Children)

AutoComplete.List는 자식으로 렌더 함수를 받아 커스텀 옵션 렌더링을 위해 컴포넌트 인스턴스에 접근할 수 있게 해줘요.

<AutoComplete.List>
    {(instance) =>
        instance.options?.map((option, index) => (
            <AutoComplete.Option key={index} index={index}>
                {instance.listbox?.getOptionLabel(option)}
            </AutoComplete.Option>
        ))
    }
</AutoComplete.List>

Pass Through

<DocPTViewer name="autocomplete-pt" components={['AutoCompleteRoot']} />

API

AutoCompleteRoot

Attribute Value
data-scope "autocomplete"
data-part "root"
Label Type Description
root AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
trigger AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the trigger's DOM element.
pcInputText AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the pcInputText's DOM element.
value AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the value's DOM element (non-editable mode).
positioner AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the positioner's DOM element.
popup AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the popup's DOM element.
list AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the list's DOM element.
option AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the option's DOM element.
clearIcon AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the clearIcon's DOM element.
selection AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the selection's DOM element.
header AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the header's DOM element.
footer AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the footer's DOM element.
empty AutoCompleteRootPassThroughType<HTMLAttributes> Used to pass attributes to the empty's DOM element.

AutoCompleteInput

Attribute Value
data-scope "autocomplete"
data-part "input"
Label Type Description
root AutoCompleteInputPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteTrigger

Attribute Value
data-scope "autocomplete"
data-part "trigger"
data-positioner-open Present when popup is open
Label Type Description
root AutoCompleteTriggerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteIndicator

Attribute Value
data-scope "autocomplete"
data-part "indicator"
data-open Present when popup is open
data-closed Present when popup is closed
Label Type Description
root AutoCompleteIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteClear

Attribute Value
data-scope "autocomplete"
data-part "clear"
Label Type Description
root AutoCompleteClearPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompletePortal

Label Type Description
root AutoCompletePortalPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompletePositioner

Attribute Value
data-side "top" | "bottom" | "left" | "right", placement
data-align "start" | "center" | "end", alignment

CSS Custom Properties

포지셔너(positioner) 요소는 레이아웃과 transform 제어를 위한 CSS 커스텀 프로퍼티를 노출해요.

CSS Variable Description
--available-height Available vertical space in pixels
--available-width Available horizontal space in pixels
--transform-origin Computed transform origin for animations
--positioner-anchor-width Width of the anchor/trigger element
Label Type Description
root AutoCompletePositionerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompletePopup

Attribute Value
data-scope "autocomplete"
data-part "popup"
data-open Present when popup is open
Label Type Description
root AutoCompletePopupPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteList

Attribute Value
data-scope "autocomplete"
data-part "list"
Label Type Description
root AutoCompleteListPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteOption

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 AutoCompleteOptionPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteOptionIndicator

Attribute Value
data-selected Present when selected
data-unselected Present when unselected
Label Type Description
root AutoCompleteOptionIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteHeader

Label Type Description
root AutoCompleteHeaderPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteFooter

Label Type Description
root AutoCompleteFooterPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteEmpty

Label Type Description
root AutoCompleteEmptyPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteArrow

Attribute Value
data-side "top" | "bottom" | "left" | "right", placement
data-align "start" | "center" | "end", alignment

화살표 요소는 동적 포지셔닝을 위한 CSS 커스텀 프로퍼티를 노출해요.

CSS Variable Description
--placer-arrow-x Horizontal offset of arrow
--placer-arrow-y Vertical offset of arrow
--placer-arrow-left Left position of arrow element
--placer-arrow-top Top position of arrow element
Label Type Description
root AutoCompleteArrowPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AutoCompleteValue

Label Type Description
root AutoCompleteValuePassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

AutoComplete.Root는 콤보박스(combobox) 패턴을 사용해요. 입력에는 role="combobox"와 함께 aria-autocomplete, aria-haspopup, aria-expanded 속성이 있어요. 입력과 팝업의 관계는 aria-controls로 만들고, aria-activedescendant 속성은 팝업 리스트에서 키보드 탐색 중 스크린 리더가 어떤 옵션을 읽을지 알려줘요. 옵션에는 현재 선택을 나타내는 role="option"과 aria-selected가 있어요. 지우기 버튼에는 스크린 리더 접근성을 위한 aria-label이 있어요. aria-label, aria-labelledby 또는 연결된 label 요소로 접근 가능한 이름을 제공하세요.

키보드 지원 (Keyboard Support)

Key Function
tab Moves focus to the autocomplete element.
any printable character Opens the popup and displays matching suggestions.
enter Selects the focused option and closes the popup.
escape Closes the popup.
arrow down Opens the popup or moves focus to the next option.
arrow up Opens the popup or moves focus to the previous option.
arrow left Removes the visual focus from the current option and moves input cursor.
arrow right Removes the visual focus from the current option and moves input cursor.
home Moves focus to the first option.
end Moves focus to the last option.
pageUp Jumps visual focus to the first option.
pageDown Jumps visual focus to the last option.

더 알아보기 (Learn more)