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

InputOtp

원문 보기 위키 갱신

자동 포커스 관리와 키보드 탐색을 지원하는 스타일 없는 OTP 입력 컴포넌트예요.

출처: 문서

본문

특징 (Features)

  • 서브 컴포넌트(Root, Text)를 가진 복합 컴포넌트 API
  • 입력, 삭제, 화살표 키 입력 시 입력 슬롯 사이를 자동으로 이동하는 포커스 탐색
  • 여러 입력에 걸쳐 문자를 분배하는 클립보드 붙여넣기 지원
  • 입력된 값을 가리는 마스크 모드
  • 숫자 문자만 허용하는 정수 전용 모드

사용법 (Usage)

import { InputOtp } from 'primereact/inputotp';
<InputOtp.Root>
    <InputOtp.Text index={0} />
    <InputOtp.Text index={1} />
    <InputOtp.Text index={2} />
    <InputOtp.Text index={3} />
</InputOtp.Root>

동작 (Behavior)

다형 렌더링 (Polymorphic Rendering)

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

<InputOtp.Root as="section">
    <InputOtp.Text index={0} />
</InputOtp.Root>

렌더 함수 자식 (Render Function Children)

InputOtp.Root는 자식으로 렌더 함수를 받아 컴포넌트 인스턴스에 접근할 수 있게 해줘요.

<InputOtp.Root>
    {(instance) => (
        <>
            <InputOtp.Text index={0} />
            <InputOtp.Text index={1} />
            <span>{instance.state.value}</span>
        </>
    )}
</InputOtp.Root>

Pass Through

<DocPTViewer name="inputotp-pt" components={['InputOtpRoot']} />

API

InputOtpRoot

Attribute Value
data-scope "inputotp"
data-part "root"
Label Type Description
root InputOtpRootPassThroughType<InputHTMLAttributes> Used to pass attributes to the root's DOM element.
pcInputText InputOtpRootPassThroughType<InputHTMLAttributes> Used to pass attributes to the input's DOM element.

InputOtpText

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

접근성 (Accessibility)

스크린 리더 (Screen Reader)

InputOtp은 네이티브 input 요소 집합을 렌더링해요. 각 입력은 한 문자를 받아요. 루트 요소에 aria-label 또는 aria-labelledby로 접근 가능한 그룹 레이블을 제공하세요.

키보드 지원 (Keyboard Support)

Key Function
tab Moves focus to the input otp.
right arrow Moves focus to the next input element.
left arrow Moves focus to the previous input element.
backspace Deletes the input and moves focus to the previous input element.

더 알아보기 (Learn more)