Skip to content

Commit fb0fa76

Browse files
samejrericallam
authored andcommitted
Adds new InputOTP chadcn component
1 parent 1cc622b commit fb0fa76

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)