Input OTP
fysk/input-otpFreeComponent
Accessible one-time password component with copy paste functionality.
Install it
npx shadcn@latest add https://fysk.dev/r/input-otp.jsonSource
1"use client"23import * as React from "react"4import { cn } from "@/lib/utils"5import { useFyskConfig } from "@/components/fysk-provider"67export interface OTPInputProps8 extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "onChange" | "type" | "size"> {9 length?: number10 onChange?: (value: string) => void11 variant?: "default" | "condensed" | "underline"12 size?: "default" | "xs" | "sm" | "md" | "lg" | "xl"13 state?: "idle" | "loading" | "success" | "error"14 inputType?: "numeric" | "alphanumeric" | "alpha"15 Icon?: React.ReactNode16}1718const OTPInput = React.forwardRef<HTMLDivElement, OTPInputProps>(19 ({20 length: propLength,21 onChange,22 variant = "default",23 size = "default",24 state = "idle",25 inputType = "numeric",26 Icon,27 className,28 disabled,29 ...props30 }, ref) => {31 const length = propLength || 632 const [otp, setOtp] = React.useState<string[]>(new Array(length).fill(""))33 const inputRefs = React.useRef<(HTMLInputElement | null)[]>([])3435 React.useEffect(() => {36 setOtp((prev) => {37 if (prev.length === length) return prev38 const newOtp = new Array(length).fill("")39 // Preserve existing values, truncating if necessary40 for (let i = 0; i < Math.min(prev.length, length); i++) {41 newOtp[i] = prev[i]42 }43 return newOtp44 })45 }, [length])4647 const patterns = {48 numeric: /^[0-9]$/,49 alpha: /^[a-zA-Z]$/,50 alphanumeric: /^[a-zA-Z0-9]$/51 }5253 const validateChar = (char: string) => {54 return patterns[inputType].test(char)55 }5657 const handleChange = (e: React.ChangeEvent<HTMLInputElement>, index: number) => {58 const value = e.target.value5960 // Handle clearing61 if (value === "") {62 const newOtp = [...otp]63 newOtp[index] = ""64 setOtp(newOtp)65 onChange?.(newOtp.join(""))66 return67 }6869 // Get the last character typed70 const char = value.substring(value.length - 1)7172 if (!validateChar(char)) return7374 const newOtp = [...otp]75 newOtp[index] = char76 setOtp(newOtp)7778// … truncated
What it pulls in
Other registry items
Files it writes
More from fysk
All 18 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Avataravatar | fysk | Components | Free | 2 deps | |
| Badgebadge | fysk | Components | Free | 1 dep | |
| Buttonbutton | fysk | Components | Free | 2 deps | |
| Empty Stateempty | fysk | Components | Free | 2 deps | |
| Formform | fysk | Components | Free | 4 deps | |
| fysk-providerfysk-provider | fysk | Components | Free | 2 deps | |
| Inputinput | fysk | Components | Free | 1 dep | |
| Kbdkbd | fysk | Components | Free | 1 dep |
