InputOTP
인증 코드와 보안 인증을 위한 일회용 비밀번호 입력 컴포넌트예요.
출처: HeroUI InputOTP
본문
사용법 (Usage)
import { InputOTP } from '@heroui/react';
{
"name": "input-otp-basic"
}
구조 (Anatomy)
import { InputOTP } from '@heroui/react';
export default () => (
<InputOTP maxLength={6}>
<InputOTP.Group>
<InputOTP.Slot index={0} />
<InputOTP.Slot index={1} />
{/* ...rest of the slots */}
</InputOTP.Group>
<InputOTP.Separator />
<InputOTP.Group>
<InputOTP.Slot index={3} />
{/* ...rest of the slots */}
</InputOTP.Group>
</InputOTP>
)
InputOTP는 @guilherme_rodz의 input-otp를 기반으로 만들어져, OTP 입력 컴포넌트를 위한 유연하고 접근 가능한 기반을 제공해요.
예시 (Examples)
변형 (Variants)
InputOTP 컴포넌트는 두 가지 시각적 변형을 지원해요:
primary(기본값) - 그림자가 있는 표준 스타일, 대부분의 사용 사례에 적합secondary- 그림자가 없는 낮은 강조 변형, Surface 컴포넌트에서 사용에 적합
{
"name": "input-otp-variants"
}
Surface 안에서 (In Surface)
Surface 컴포넌트 안에서 사용할 때는 variant="secondary"를 사용해 surface 배경에 적합한 낮은 강조 변형을 적용해요.
{
"name": "input-otp-on-surface"
}
비활성화 상태 (Disabled State)
{
"name": "input-otp-disabled"
}
네 자리 (Four Digits)
{
"name": "input-otp-four-digits"
}
제어 (Controlled)
값을 제어해 상태와 동기화하고, 입력을 지우거나 커스텀 검증을 구현할 수 있어요.
{
"name": "input-otp-controlled"
}
완료 시 (On Complete)
onComplete 콜백을 사용해 모든 슬롯이 채워졌을 때 동작을 트리거할 수 있어요.
{
"name": "input-otp-on-complete"
}
폼 예시 (Form Example)
검증과 제출이 포함된 완전한 2단계 인증 폼이에요.
{
"name": "input-otp-form-example"
}
패턴 사용 (With Pattern)
pattern 프로퍼티를 사용해 입력을 특정 문자로 제한할 수 있어요. HeroUI는 REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS 같은 공통 패턴을 내보내줘요.
{
"name": "input-otp-with-pattern"
}
검증과 함께 (With Validation)
isInvalid를 검증 메시지와 함께 사용해 오류를 표시할 수 있어요.
{
"name": "input-otp-with-validation"
}
커스터마이징 (Customization)
Tailwind CSS
{
"name": "input-otp-custom-styles"
}
Global CSS
InputOTP 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.input-otp {
@apply gap-3;
}
.input-otp__slot {
@apply size-12 rounded-xl border-2 font-bold;
}
.input-otp__slot[data-active="true"] {
@apply border-accent-500 ring-2 ring-accent-200;
}
.input-otp__separator {
@apply w-2 h-1 bg-border-strong rounded-full;
}
}
스타일 참조 (Styling Reference)
HeroUI는 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽게 만들기 위해 BEM 방법론을 따르고 있어요.
CSS 클래스 (CSS Classes)
InputOTP 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 클래스 (Base Classes)
.input-otp- 기본 컨테이너.input-otp__container- input-otp 라이브러리의 내부 컨테이너.input-otp__group- 슬롯 그룹.input-otp__slot- 개별 입력 슬롯.input-otp__slot-value- 슬롯 안의 문자.input-otp__caret- 깜빡이는 캐럿 표시기.input-otp__separator- 그룹 사이의 시각적 구분선
상태 클래스 (State Classes)
.input-otp__slot[data-active="true"]- 현재 활성 슬롯.input-otp__slot[data-filled="true"]- 문자가 있는 슬롯.input-otp__slot[data-disabled="true"]- 비활성화된 슬롯.input-otp__slot[data-invalid="true"]- 유효하지 않은 슬롯.input-otp__container[data-disabled="true"]- 비활성화된 컨테이너
상호작용 상태 (Interactive States)
컴포넌트는 유연성을 위해 CSS 가상 클래스와 데이터 속성을 모두 지원해요:
- 호버 (Hover): 슬롯의
:hover또는[data-hovered="true"] - 활성 (Active): 슬롯의
[data-active="true"](현재 포커스됨) - 채워짐 (Filled): 슬롯의
[data-filled="true"](문자 포함) - 비활성화 (Disabled): 컨테이너와 슬롯의
[data-disabled="true"] - 유효하지 않음 (Invalid): 슬롯의
[data-invalid="true"]
API 참조 (API Reference)
InputOTP
InputOTP는 추가 기능이 있는 input-otp 라이브러리를 기반으로 만들어졌어요.
기본 프로퍼티 (Base Props)
| Prop | Type | Default | Description |
|---|---|---|---|
maxLength |
number |
- | 필수. 입력 슬롯의 수. |
value |
string |
- | 제어된 값 (제공하지 않으면 비제어). |
onChange |
(value: string) => void |
- | 값이 변경될 때 호출되는 핸들러. |
onComplete |
(value: string) => void |
- | 모든 슬롯이 채워졌을 때 호출되는 핸들러. |
className |
string |
- | 컨테이너의 추가 CSS 클래스. |
containerClassName |
string |
- | 내부 컨테이너의 CSS 클래스. |
variant |
"primary" | "secondary" |
"primary" |
컴포넌트의 시각적 변형. primary는 그림자가 있는 기본 스타일. secondary는 그림자가 없는 낮은 강조 변형으로 surface에서 사용에 적합. |
children |
React.ReactNode |
- | InputOTP.Group, InputOTP.Slot, InputOTP.Separator 컴포넌트. |
검증 프로퍼티 (Validation Props)
| Prop | Type | Default | Description |
|---|---|---|---|
isDisabled |
boolean |
false |
입력이 비활성화되었는지 여부. |
isInvalid |
boolean |
false |
입력이 유효하지 않은 상태인지 여부. |
validationErrors |
string[] |
- | 서버 측 또는 커스텀 검증 오류. |
validationDetails |
ValidityState |
- | HTML5 검증 세부 정보. |
입력 프로퍼티 (Input Props)
| Prop | Type | Default | Description |
|---|---|---|---|
pattern |
string |
- | 허용되는 문자에 대한 정규식 패턴 (예: REGEXP_ONLY_DIGITS). |
textAlign |
'left' | 'center' | 'right' |
'left' |
슬롯 안의 텍스트 정렬. |
inputMode |
'numeric' | 'text' | 'decimal' | 'tel' | 'search' | 'email' | 'url' |
'numeric' |
모바일 장치의 가상 키보드 유형. |
placeholder |
string |
- | 빈 슬롯의 플레이스홀더 텍스트. |
pasteTransformer |
(text: string) => string |
- | 붙여넣은 텍스트 변환 (예: 하이픈 제거). |
폼 프로퍼티 (Form Props)
| Prop | Type | Default | Description |
|---|---|---|---|
name |
string |
- | 폼 제출을 위한 name 속성. |
autoFocus |
boolean |
- | 마운트 시 첫 번째 슬롯에 포커스할지 여부. |
InputOTP.Group
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | 그룹의 추가 CSS 클래스. |
children |
React.ReactNode |
- | InputOTP.Slot 컴포넌트. |
InputOTP.Slot
| Prop | Type | Default | Description |
|---|---|---|---|
index |
number |
- | 필수. 슬롯의 0 기반 인덱스. |
className |
string |
- | 슬롯의 추가 CSS 클래스. |
InputOTP.Separator
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | 구분선의 추가 CSS 클래스. |
내보낸 패턴 (Exported Patterns)
HeroUI는 편의를 위해 input-otp의 공통 정규식 패턴을 다시 내보내줘요:
import { REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS_AND_CHARS } from '@heroui/react';
// Use with pattern prop
<InputOTP pattern={REGEXP_ONLY_DIGITS} maxLength={6}>
{/* ... */}
</InputOTP>
- REGEXP_ONLY_DIGITS - 숫자 문자만 (0-9)
- REGEXP_ONLY_CHARS - 알파벳 문자만 (a-z, A-Z)
- REGEXP_ONLY_DIGITS_AND_CHARS - 영숫자 문자 (0-9, a-z, A-Z)