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

핀 입력 (Pin Input)

원문 보기 위키 갱신

PIN 번호나 인증 코드를 입력할 때 사용하는 컴포넌트로, 자동 포커스 이동(auto-focus transfer)과 마스킹 옵션을 지원합니다. 각 자리마다 개별 입력 필드를 제공해 한 자씩 입력하면 다음 필드로 자동으로 이동해요.

출처: 문서

본문

해부 구조 (Anatomy)

<PinInput.Root>
  <PinInput.Label />
  <PinInput.Control>
    <PinInput.Input />
  </PinInput.Control>
  <PinInput.HiddenInput />
</PinInput.Root>

예시 (Examples)

import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const Basic = () => (
  <PinInput.Root className={styles.Root}>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)

플레이스홀더 (Placeholder)

기본 핀 입력 플레이스홀더 ○를 각 입력에서 바꾸려면 placeholder prop을 넘겨 원하는 값으로 설정하세요.

import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const CustomPlaceholder = () => (
  <PinInput.Root className={styles.Root} placeholder="*">
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)

완료 시 포커스 해제 (Blur on Complete)

기본적으로 마지막 입력이 채워지면 포커스를 유지하고 onValueComplete 콜백을 호출합니다. 사용자가 입력을 완료할 때 마지막 입력의 포커스를 해제하려면 blurOnComplete prop을 true로 설정하세요.

import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const BlurOnComplete = () => (
  <PinInput.Root className={styles.Root} blurOnComplete>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)

OTP 모드 (OTP Mode)

스마트폰의 OTP 자동 제안을 트리거하려면 autocomplete 속성을 "one-time-code"로 설정하는 것이 권장됩니다. 핀 입력 컴포넌트는 otp prop을 true로 설정하면 자동으로 이를 지원합니다.

import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const OTPMode = () => (
  <PinInput.Root className={styles.Root} otp>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)

마스킹 (Masking)

핀 입력으로 개인 정보나 민감한 정보를 수집할 때, <input type="password"/>와 비슷하게 입력된 값을 마스킹해야 할 수 있어요. mask prop을 true로 넘겨주세요.

import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const Mask = () => (
  <PinInput.Root className={styles.Root} mask>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)

변경 이벤트 (Change Events)

핀 입력 컴포넌트는 사용자가 입력할 때 여러 콜백 함수를 호출합니다.

  • onValueChange — 값이 변경될 때 호출되는 콜백입니다.
  • onValueComplete — 모든 필드가 완료되었을 때(입력하거나 붙여넣어서) 호출되는 콜백입니다.
  • onValueInvalid — 입력에 유효하지 않은 값이 입력될 때 호출되는 콜백입니다. 유효하지 않은 값은 지정된 "type"과 일치하지 않는 모든 값을 말합니다.

필드 (Field)

Field 컴포넌트는 핀 입력의 폼 관련 상태와 접근성 속성을 관리하는 데 도움을 줍니다. ARIA 라벨, 도움말 텍스트, 오류 텍스트 처리를 포함해 적절한 접근성을 보장합니다.

import { Field } from '@ark-ui/react/field'
import { PinInput } from '@ark-ui/react/pin-input'
import fieldStyles from 'styles/field.module.css'
import styles from 'styles/pin-input.module.css'

export const WithField = () => (
  <Field.Root className={fieldStyles.Root}>
    <PinInput.Root className={styles.Root}>
      <PinInput.Label className={styles.Label}>Label</PinInput.Label>
      <PinInput.Control className={styles.Control}>
        {[0, 1, 2].map((id, index) => (
          <PinInput.Input key={id} index={index} className={styles.Input} />
        ))}
      </PinInput.Control>
      <PinInput.HiddenInput />
    </PinInput.Root>
    <Field.HelperText className={fieldStyles.HelperText}>Additional Info</Field.HelperText>
    <Field.ErrorText className={fieldStyles.ErrorText}>Error Info</Field.ErrorText>
  </Field.Root>
)

루트 프로바이더 (Root Provider)

핀 입력을 제어하는 또 다른 방법은 RootProvider 컴포넌트와 usePinInput 훅을 사용하는 것입니다. 이렇게 하면 컴포넌트 외부에서 상태와 메서드에 접근할 수 있어요.

import { PinInput, usePinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const RootProvider = () => {
  const pinInput = usePinInput({ onValueComplete: (e) => alert(e.valueAsString) })

  return (
    <div className="stack">
      <button onClick={() => pinInput.focus()}>Focus</button>

      <PinInput.RootProvider value={pinInput} className={styles.Root}>
        <PinInput.Label className={styles.Label}>Label</PinInput.Label>
        <PinInput.Control className={styles.Control}>
          {[0, 1, 2].map((id, index) => (
            <PinInput.Input key={id} index={index} className={styles.Input} />
          ))}
        </PinInput.Control>
        <PinInput.HiddenInput />
      </PinInput.RootProvider>
    </div>
  )
}

API 참조 (API Reference)

Props

Root

Props

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.

autoFocus Type: boolean Required: false Default Value: undefined Description: 첫 번째 입력에 자동으로 포커스를 맞출지 여부입니다.

autoSubmit Type: boolean Required: false Default Value: undefined Description: 모든 입력이 채워졌을 때 소유하고 있는 폼을 자동 제출할지 여부입니다.

blurOnComplete Type: boolean Required: false Default Value: undefined Description: 값이 완료되었을 때 입력의 포커스를 해제할지 여부입니다.

count Type: number Required: false Default Value: undefined Description: SSR aria 속성을 개선하기 위해 렌더링할 입력의 수입니다. 다음 주요 버전에서는 필수가 될 예정입니다.

defaultValue Type: string[] Required: false Default Value: undefined Description: 렌더링 시 핀 입력의 초기 값입니다. 핀 입력의 값을 직접 제어할 필요가 없을 때 사용합니다.

disabled Type: boolean Required: false Default Value: undefined Description: 입력들이 비활성화되었는지 여부입니다.

form Type: string Required: false Default Value: undefined Description: 기본 입력 요소와 연관된 폼입니다.

id Type: string Required: false Default Value: undefined Description: 머신의 고유 식별자입니다.

ids Type: Partial<{ root: string hiddenInput: string label: string control: string input: (id: string) => string }> Required: false Default Value: undefined Description: 핀 입력에 있는 요소들의 id입니다. 컴포지션에 유용합니다.

invalid Type: boolean Required: false Default Value: undefined Description: 핀 입력이 유효하지 않은 상태인지 여부입니다.

mask Type: boolean Required: false Default Value: undefined Description: true인 경우, 입력 값이 type=password처럼 마스킹됩니다.

name Type: string Required: false Default Value: undefined Description: 입력 요소의 이름입니다. 폼 제출에 유용합니다.

onValueChange Type: (details: ValueChangeDetails) => void Required: false Default Value: undefined Description: 입력이 변경될 때 호출되는 함수입니다.

onValueComplete Type: (details: ValueChangeDetails) => void Required: false Default Value: undefined Description: 모든 입력에 유효한 값이 있을 때 호출되는 함수입니다.

onValueInvalid Type: (details: ValueInvalidDetails) => void Required: false Default Value: undefined Description: 유효하지 않은 값이 입력될 때 호출되는 함수입니다.

otp Type: boolean Required: false Default Value: undefined Description: true인 경우, 핀 입력 컴포넌트가 필드들에게 autocomplete="one-time-code"를 사용하도록 신호를 보냅니다.

pattern Type: string Required: false Default Value: undefined Description: 사용자가 입력한 값이 검사되는 정규 표현식입니다.

placeholder Type: string Required: false Default Value: "○" Description: 입력의 플레이스홀더 텍스트입니다.

readOnly Type: boolean Required: false Default Value: undefined Description: 핀 입력이 유효한 상태인지 여부입니다.

required Type: boolean Required: false Default Value: undefined Description: 핀 입력이 필수인지 여부입니다.

sanitizeValue Type: (value: string) => string Required: false Default Value: undefined Description: 검증 전에 붙여넣은 값을 정리하는 함수입니다. 대시, 공백 또는 기타 형식을 제거하는 데 유용합니다.

selectOnFocus Type: boolean Required: false Default Value: undefined Description: 입력에 포커스가 맞춰졌을 때 입력 값을 선택할지 여부입니다.

translations Type: IntlTranslations Required: false Default Value: undefined Description: 접근성 요소와 그 상태를 식별하는 지역화된 문자열을 지정합니다.

type Type: 'numeric' | 'alphanumeric' | 'alphabetic' Required: false Default Value: "numeric" Description: 핀 입력이 허용할 값의 유형입니다.

value Type: string[] Required: false Default Value: undefined Description: 핀 입력의 제어(controlled) 값입니다.

데이터 속성 (Data Attributes)

data-scope: pin-input data-part: root data-invalid: 유효하지 않을 때 표시됩니다. data-disabled: 비활성화되었을 때 표시됩니다. data-complete: 핀 입력 값이 완료되었을 때 표시됩니다. data-readonly: 읽기 전용일 때 표시됩니다.

Control

Props

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.

HiddenInput

Props

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.

Input

Props

index Type: number Required: true Default Value: undefined Description: undefined

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.

데이터 속성 (Data Attributes)

data-scope: pin-input data-part: input data-disabled: 비활성화되었을 때 표시됩니다. data-complete: 입력 값이 완료되었을 때 표시됩니다. data-filled: data-index: 항목의 인덱스입니다. data-invalid: 유효하지 않을 때 표시됩니다.

Label

Props

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.

데이터 속성 (Data Attributes)

data-scope: pin-input data-part: label data-invalid: 유효하지 않을 때 표시됩니다. data-disabled: 비활성화되었을 때 표시됩니다. data-complete: 라벨 값이 완료되었을 때 표시됩니다. data-required: 필수일 때 표시됩니다. data-readonly: 읽기 전용일 때 표시됩니다.

RootProvider

Props

value Type: UsePinInputReturn Required: true Default Value: undefined Description: undefined

asChild Type: boolean Required: false Default Value: undefined Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.

Context

API:

Property Type Description
value string[] 문자열 배열로 표현된 입력의 값입니다.
valueAsString string 문자열로 표현된 입력의 값입니다.
complete boolean 모든 입력이 채워졌는지 여부입니다.
count number 렌더링할 입력의 수입니다.
items number[] 입력 값들의 배열입니다.
setValue (value: string[]) => void 입력들의 값을 설정하는 함수입니다.
clearValue VoidFunction 입력들의 값을 지우는 함수입니다.
setValueAtIndex (index: number, value: string) => void 특정 인덱스의 입력 값을 설정하는 함수입니다.
focus VoidFunction 핀 입력에 포커스를 맞추는 함수입니다. 첫 번째 입력에 포커스를 맞춥니다.

접근성 (Accessibility)

키보드 지원 (Keyboard Support)

ArrowLeft Description: 이전 입력으로 포커스를 이동합니다.

ArrowRight Description: 다음 입력으로 포커스를 이동합니다.

Backspace Description: 현재 입력의 값을 삭제하고 이전 입력으로 포커스를 이동합니다.

Delete Description: 현재 입력의 값을 삭제합니다.

Control + V Description: 값을 입력 필드에 붙여넣습니다.

더 알아보기 (Learn more)