OTP Input Component with Advanced Interaction
Secure user authentication often relies on One-Time Passwords (OTPs) sent via SMS or email. A well-designed OTP input experience is crucial for both security and user satisfaction. It minimizes friction, reduces errors, and instills confidence in the user interface. This problem challenges you to build a robust and user-friendly OTP input component, focusing on intuitive interactions and accessibility.
Your primary task is to create a reusable OTPInput React component that handles a numeric OTP.
OTPInput that accepts length: number (e.g., 6 for a 6-digit OTP) and onChange: (otp: string) => void props.<input type="text" />) for each digit of the OTP.Backspace in an empty field should move focus to the previous field and clear its content.Backspace in a non-empty field should clear its content and remain focused on that field.Delete should clear the current field and remain focused.onChange callback should be triggered with the complete OTP string whenever its value changes. When all fields are filled, ensure the complete OTP is passed.isInvalid: boolean prop is passed to the component.aria-label, inputmode="numeric", pattern="[0-9]") for better accessibility.isInvalid is true, the input fields should visually reflect this (e.g., a red border around all fields, or a red error message below).OTPInput component renders length number of input fields.Backspace in an empty field moves focus to the previous field and clears it.Backspace in a filled field clears it and keeps focus.Delete clears the current field and keeps focus.onChange callback is invoked with the current partial or complete OTP string on every change.isInvalid prop is true.Constraints
length will be 8 digits.OTPInput component is structured, its props, and its ability to be used in different contexts.Loading editor...