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