75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { OTPInput, OTPInputContext } from "input-otp";
|
|
import { Minus } from "lucide-react";
|
|
|
|
import { cn } from "../../lib/utils";
|
|
import {
|
|
ComponentPropsWithoutRef,
|
|
ElementRef,
|
|
forwardRef,
|
|
useContext,
|
|
} from "react";
|
|
|
|
const InputOTP = forwardRef<
|
|
ElementRef<typeof OTPInput>,
|
|
ComponentPropsWithoutRef<typeof OTPInput>
|
|
>(({ className, containerClassName, ...props }, ref) => (
|
|
<OTPInput
|
|
ref={ref}
|
|
containerClassName={cn(
|
|
"flex items-center gap-2 has-[:disabled]:opacity-50",
|
|
containerClassName
|
|
)}
|
|
className={cn("disabled:cursor-not-allowed", className)}
|
|
{...props}
|
|
/>
|
|
));
|
|
InputOTP.displayName = "InputOTP";
|
|
|
|
const InputOTPGroup = forwardRef<
|
|
ElementRef<"div">,
|
|
ComponentPropsWithoutRef<"div">
|
|
>(({ className, ...props }, ref) => (
|
|
<div ref={ref} className={cn("flex items-center", className)} {...props} />
|
|
));
|
|
InputOTPGroup.displayName = "InputOTPGroup";
|
|
|
|
const InputOTPSlot = forwardRef<
|
|
ElementRef<"div">,
|
|
ComponentPropsWithoutRef<"div"> & { index: number }
|
|
>(({ index, className, ...props }, ref) => {
|
|
const inputOTPContext = useContext(OTPInputContext);
|
|
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index];
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className={cn(
|
|
"relative flex h-9 w-14 items-center justify-center text-sm border-b border-MEDIUM_GRAY transition-all ",
|
|
isActive && "z-10 rounded-sm ring-1 ring-ring",
|
|
className
|
|
)}
|
|
{...props}
|
|
>
|
|
{char}
|
|
{hasFakeCaret && (
|
|
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
|
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
});
|
|
InputOTPSlot.displayName = "InputOTPSlot";
|
|
|
|
const InputOTPSeparator = forwardRef<
|
|
ElementRef<"div">,
|
|
ComponentPropsWithoutRef<"div">
|
|
>(({ ...props }, ref) => (
|
|
<div ref={ref} role="separator" {...props}>
|
|
<Minus />
|
|
</div>
|
|
));
|
|
InputOTPSeparator.displayName = "InputOTPSeparator";
|
|
|
|
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator };
|