--- title: Input OTP description: Accessible one-time password component with copy paste functionality. component: true links: doc: https://input-otp.rodz.dev --- ## About Input OTP is built on top of [input-otp](https://github.com/guilhermerodz/input-otp) by [@guilherme_rodz](https://twitter.com/guilherme_rodz). ## Installation CLI Manual Run the following command: ```bash npx shadcn@latest add input-otp ``` Install the following dependencies: ```bash npm install input-otp ``` Copy and paste the following code into your project. Update the import paths to match your project setup. ## Usage ```tsx showLineNumbers import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, } from "@/components/ui/input-otp" ``` ```tsx showLineNumbers ``` ## Examples ### Pattern Use the `pattern` prop to define a custom pattern for the OTP input. ```tsx showLineNumbers {1,7} import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp" ... {/* ... */} ``` ### Separator You can use the `` component to add a separator between the input groups. ```tsx showLineNumbers {4,15} import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, } from "@/components/ui/input-otp" ... ``` ### Controlled You can use the `value` and `onChange` props to control the input value. ### Form ## Changelog ### 2024-03-19 Composition We've made some updates and replaced the render props pattern with composition. Here's how to update your code if you prefer the composition pattern. **Note:** You are not required to update your code if you are using the `render` prop. It is still supported. Update to the latest version of `input-otp`. ```bash npm install input-otp@latest ``` Update `input-otp.tsx` ```diff showLineNumbers title="input-otp.tsx" {2,8-11} - import { OTPInput, SlotProps } from "input-otp" + import { OTPInput, OTPInputContext } from "input-otp" const InputOTPSlot = React.forwardRef< React.ElementRef<"div">, - SlotProps & React.ComponentPropsWithoutRef<"div"> - >(({ char, hasFakeCaret, isActive, className, ...props }, ref) => { + React.ComponentPropsWithoutRef<"div"> & { index: number } + >(({ index, className, ...props }, ref) => { + const inputOTPContext = React.useContext(OTPInputContext) + const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index] ``` Then replace the `render` prop in your code. ```diff showLineNumbers {2-12} ``` ### 2024-03-19 Disabled To add a disabled state to the input, update `` as follows: ```tsx showLineNumbers title="input-otp.tsx" {4,7-11} const InputOTP = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, containerClassName, ...props }, ref) => ( )) InputOTP.displayName = "InputOTP" ```