Build an Accessible OTP Input Component
One-Time Passwords (OTPs) are a critical security measure for user authentication and transaction verification. A well-designed OTP input experience is essential for a smooth and secure user journey, minimizing friction and potential errors. In this problem, you will build a robust and user-friendly OTP input component that handles various user interactions gracefully, mirroring the polished experience users expect from modern applications.
Your primary task is to create a reusable OTPInput React component with the following functionality:
length (number of digits for the OTP) and onChange (a callback function that receives the complete OTP string) as props.length individual input fields, typically represented as distinct boxes, each accepting a single digit.Backspace key is pressed, the focus should move to the previous field.Backspace key is pressed, clear the digit and keep the focus on the current field.onChange callback prop should be triggered with the complete concatenated OTP string whenever any digit is entered, deleted, or pasted.styles.css offers a basic starting point; you are free to enhance it for clarity.aria-label or sr-only text for individual fields) and semantic HTML to aid screen reader users and improve overall usability.OTPInput component renders the correct number of individual input fields based on the length prop.Backspace on an empty field correctly moves focus to the previous field.Backspace on a non-empty field clears its value and keeps focus on that field.onChange callback is invoked with the full, concatenated OTP string on every valid change (input, backspace, paste).Constraints
length prop for the OTP will be an integer between 4 and 8 (inclusive).Loading editor...