| 1 | import * as React from "react"; |
| 2 | import { OTPInput, OTPInputContext } from "input-otp"; |
| 3 | import { Dot } from "lucide-react"; |
| 4 | |
| 5 | import { cn } from "@/lib/utils"; |
| 6 | |
| 7 | const InputOTP = React.forwardRef<React.ElementRef<typeof OTPInput>, React.ComponentPropsWithoutRef<typeof OTPInput>>( |
| 8 | ({ className, containerClassName, ...props }, ref) => ( |
| 9 | <OTPInput |
| 10 | ref={ref} |
| 11 | containerClassName={cn("flex items-center gap-2 has-[:disabled]:opacity-50", containerClassName)} |
| 12 | className={cn("disabled:cursor-not-allowed", className)} |
| 13 | {...props} |
| 14 | /> |
| 15 | ), |
| 16 | ); |
| 17 | InputOTP.displayName = "InputOTP"; |
| 18 | |
| 19 | const InputOTPGroup = React.forwardRef<React.ElementRef<"div">, React.ComponentPropsWithoutRef<"div">>( |
| 20 | ({ className, ...props }, ref) => <div ref={ref} className={cn("flex items-center", className)} {...props} />, |
| 21 | ); |
| 22 | InputOTPGroup.displayName = "InputOTPGroup"; |
| 23 | |
| 24 | const InputOTPSlot = React.forwardRef< |
| 25 | React.ElementRef<"div">, |
| 26 | React.ComponentPropsWithoutRef<"div"> & { index: number } |
| 27 | >(({ index, className, ...props }, ref) => { |
| 28 | const inputOTPContext = React.useContext(OTPInputContext); |
| 29 | const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index]; |
| 30 | |
| 31 | return ( |
| 32 | <div |
| 33 | ref={ref} |
| 34 | className={cn( |
| 35 | "relative flex h-10 w-10 items-center justify-center border-y border-r border-input text-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md", |
| 36 | isActive && "z-10 ring-2 ring-ring ring-offset-background", |
| 37 | className, |
| 38 | )} |
| 39 | {...props} |
| 40 | > |
| 41 | {char} |
| 42 | {hasFakeCaret && ( |
| 43 | <div className="pointer-events-none absolute inset-0 flex items-center justify-center"> |
| 44 | <div className="animate-caret-blink h-4 w-px bg-foreground duration-1000" /> |
| 45 | </div> |
| 46 | )} |
| 47 | </div> |
| 48 | ); |
| 49 | }); |
| 50 | InputOTPSlot.displayName = "InputOTPSlot"; |
| 51 | |
| 52 | const InputOTPSeparator = React.forwardRef<React.ElementRef<"div">, React.ComponentPropsWithoutRef<"div">>( |
| 53 | ({ ...props }, ref) => ( |
| 54 | <div ref={ref} role="separator" {...props}> |
| 55 | <Dot /> |
| 56 | </div> |
| 57 | ), |
| 58 | ); |
| 59 | InputOTPSeparator.displayName = "InputOTPSeparator"; |
| 60 | |
| 61 | export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }; |