Advanced OTP Input Component with Auto-Focus and Paste Handling
In Nykaa's secure checkout and account management flows, One-Time Passwords (OTPs) are crucial for verifying user identity and authorizing transactions. A seamless and intuitive OTP input experience significantly enhances user trust and reduces abandonment rates. This problem challenges you to build a robust and highly interactive OTP input component that handles various user behaviors, providing a superior experience akin to what our customers expect.
Your primary task is to develop a reusable OtpInput React component that offers an advanced user experience for entering OTPs. The component should be highly configurable and handle common interaction patterns gracefully.
OtpInput component that accepts a length prop (number of digits, e.g., 4 or 6). It should render individual input fields for each digit.Backspace in an empty field, the focus should shift to the previous field. If Backspace is pressed in a non-empty field, it should clear that field's value first, then move focus if pressed again.isError prop to visually indicate an error state (e.g., invalid OTP).onChange callback that is triggered whenever the OTP value changes, and an onComplete callback that fires when all fields are filled with valid digits.isError prop), the input fields should display a distinct visual cue (e.g., red border).OtpInput component renders length number of individual input fields.Backspace in an empty field moves focus to the previous field.Backspace in a filled field clears its value and keeps focus on that field, then moves focus on subsequent Backspace press.onChange(otp: string) callback is invoked with the current OTP string whenever any digit changes.onComplete(otp: string) callback is invoked only when all length fields are filled with numeric digits.isError prop is true.type, inputmode, and aria-label attributes for accessibility.Constraints
OtpInput component is structured, its API (props, callbacks), and its reusability.useState, useRef, useEffect) to manage component state and interactions.Loading editor...