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.
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.
npx shadcn@latest add @wobko/input-otp第一次使用?先把 registry 加进 components.json。 如何安装
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.
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.
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.
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.
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.
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.
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.
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.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| maxLength | number | - | How many characters; render that many `InputOTPSlot`s. |
| invalid | boolean | - | Red pencil boxes (also set by `aria-invalid`, which is put on the input too). |
| pasteTransformer | (pasted: string) => string | drops spaces and hyphens | Cleans 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. |
| pattern | string | - | 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. |
| containerClassName | string | - | 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. |
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.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| index | number | - | Position in the code. |
| seed | string | number | - | Pins the hand: the same seed always draws the same box. |
CLI 会复制进你项目的文件。
正在加载源码