组件

Input OTP

A one-time code as a row of separate hand-drawn boxes. The box that takes the next character wears the blue ring and a blinking pencil stroke; typed characters are written in script; a wrong code turns the boxes red. Underneath is one real input (the input-otp package), so paste, SMS autofill, password managers and the mobile keyboard just work.

安装

terminal
npx shadcn@latest add @wobko/input-otp

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Six digits

Click and type, or paste "482915" (three digits are already in). The box with the caret wears the blue ring and a blinking pencil stroke; each digit writes itself in; the dash is only decoration.

Letters and digits, a wrong code

pattern lets letters in (anything else is refused, also when pasted). The right code here is AB12; the box starts on AB13, so you can see what a wrong code looks like: red boxes and a message you write. Change the last character to 2 and it clears.

Verify your email

The whole flow: the last digit completes the code (onComplete), which checks it; a wrong one turns the boxes red and offers a fresh start; 246810 is the right code. Resend is a link-style Button.

Three and three

shadcn's `input-otp-demo`: six digits in two groups with the separator between them. Type, paste, or use Backspace; the boxes are drawn, the input behind them is one real field.

Digits and letters

shadcn's `input-otp-pattern`: `pattern` takes a regular expression, and the package's presets are re-exported from the component file so the example needs no second import. Anything the pattern refuses is dropped, pasted or typed.

Two-two-two

shadcn's `input-otp-separator`: three groups of two with a separator between each pair. The dash is decoration only; the screen reader hears one six-digit field.

Controlled

shadcn's `input-otp-controlled`: `value` and `onChange` hold the code in your state, and a line under the boxes echoes it back. The line is a live region, so it is read out as you type.

API 参考

InputOTP

The code input: `OTPInput` from the input-otp package. All its props work (`maxLength`, `value` / `onChange`, `onComplete`, `pattern`, `pasteTransformer`, `inputMode`, `autoComplete`, `disabled`, `name`, `required`...). It is a text input with `autocomplete="one-time-code"` and a numeric keyboard by default. The boxes are its children.

属性类型默认值说明
maxLengthnumber-How many characters; render that many `InputOTPSlot`s.
invalidboolean-Red pencil boxes (also set by `aria-invalid`, which is put on the input too).
pasteTransformer(pasted: string) => stringdrops spaces and hyphensCleans what is pasted before `pattern` looks at it, so "123 456" or "123-456" from an email fills the row (the package would refuse the whole paste). Pass `(t) => t` to switch it off.
patternstring-A regular expression the code must match: `REGEXP_ONLY_DIGITS`, `REGEXP_ONLY_CHARS` and `REGEXP_ONLY_DIGITS_AND_CHARS` are re-exported from this file.
containerClassNamestring-Classes for the row around the boxes (default: `flex flex-nowrap gap-3`, at most as wide as its parent). Add `w-full justify-center` to center it, `flex-wrap` if you would rather it broke into rows.

InputOTPSlot

One box of the code, `index` says which character it shows. A small ink host with its own hand: pencil when empty, fine-liner once it holds a character; the slot with the caret gets the blue ring (drawn for that one slot only) and a blinking pencil I-beam (a CSS mask, not ink); each typed character pops in, after the first keystroke, never at load.

属性类型默认值说明
indexnumber-Position in the code.
seedstring | number-Pins the hand: the same seed always draws the same box.

无障碍

  • There is one real `<input>` (not six), so name it with a Label (`htmlFor` on the id you pass) or `aria-label`. Screen readers read the code as typed; the boxes are decoration (their drawings are `aria-hidden`).
  • Typing, Backspace, arrow keys, selection, paste and SMS autofill (`autocomplete="one-time-code"`) are the browser's own; a `pattern` filters what can be typed or pasted. Password managers get room for their badge (`pushPasswordManagerStrategy`).
  • `aria-invalid` is set on the input when `invalid` is; the boxes turn red with a drawn pencil box, not only a color change, and a message (`role="alert"`) is yours to add.
  • The active box has a drawn blue ring and a caret; in Windows high contrast it gets a real Highlight outline. The blink stops under reduced motion (and `data-motion="reduced"`).
  • Form reset: the code lives in the package's own state, so `<button type="reset">` does not clear it; control the value (`value` + `onChange`) and clear it in your own reset handler.
  • Touch: boxes are 40 x 48 px (36 x 40 compact, 32 x 36 calm; on a phone card they narrow to about 38 px, never below 24), and the invisible input covers the whole row, so a tap anywhere focuses it.

源码

CLI 会复制进你项目的文件。

正在加载源码