Frontend School

Build an Accessible OTP Input Component

Machine Coding
easy

Build an Accessible OTP Input Component

Easy
React
TypeScript
UI
Forms
Accessibility

Overview

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.

What you need to do / Task

Your primary task is to create a reusable OTPInput React component with the following functionality:

  1. Component Structure: The component should accept length (number of digits for the OTP) and onChange (a callback function that receives the complete OTP string) as props.
  2. Individual Input Fields: Render length individual input fields, typically represented as distinct boxes, each accepting a single digit.
  3. Auto-focus on Input: When a user enters a digit into an input field, the focus should automatically shift to the next empty field.
  4. Backspace Navigation:
    • If an input field is empty and the Backspace key is pressed, the focus should move to the previous field.
    • If an input field contains a digit and the Backspace key is pressed, clear the digit and keep the focus on the current field.
  5. Paste Functionality: If a user pastes a string of digits (e.g., from their clipboard), populate the input fields sequentially with the pasted content. If the pasted string is longer than the remaining fields, truncate it.
  6. Value Reporting: The onChange callback prop should be triggered with the complete concatenated OTP string whenever any digit is entered, deleted, or pasted.

UI/UX expectations

  • Each digit should be entered into its own visually distinct input box. The provided styles.css offers a basic starting point; you are free to enhance it for clarity.
  • The active input field should have a clear focus indicator (e.g., a border or shadow) to guide the user.
  • Ensure the component is accessible. Use appropriate ARIA attributes (e.g., aria-label or sr-only text for individual fields) and semantic HTML to aid screen reader users and improve overall usability.
  • Input fields should only accept numeric characters (0-9).

Acceptance criteria

  • The OTPInput component renders the correct number of individual input fields based on the length prop.
  • Entering a digit in any field correctly populates it and automatically moves focus to the next field.
  • Pressing Backspace on an empty field correctly moves focus to the previous field.
  • Pressing Backspace on a non-empty field clears its value and keeps focus on that field.
  • Pasting a string of digits successfully populates multiple input fields sequentially.
  • The onChange callback is invoked with the full, concatenated OTP string on every valid change (input, backspace, paste).
  • Input fields correctly restrict input to only numeric characters (0-9).
  • The component demonstrates basic accessibility considerations for screen readers and keyboard navigation.

Constraints

Constraints & assumptions

  • The length prop for the OTP will be an integer between 4 and 8 (inclusive).
  • Input values will always be expected to be numeric digits (0-9).
  • The component should be built using React and TypeScript.
  • The target environment is a modern web browser.

Out of scope

  • Server-side validation or API integration for OTP submission.
  • Advanced error states beyond basic invalid input (e.g., 'incorrect OTP').
  • Countdown timers for OTP expiry or resend functionality.
  • Complex styling beyond basic layout and clear focus states.

What interviewers evaluate

  • Functional Correctness: Does the OTP input component behave as specified for all required interactions (single digit input, backspace, paste)?
  • Code Quality & Structure: Is the code clean, readable, modular, and does it follow good React practices (e.g., effective state management, use of refs, component composition)?
  • User Experience & Accessibility: Is the component intuitive to use, does it handle focus correctly, and does it incorporate basic accessibility considerations (e.g., ARIA attributes)?
  • Problem-Solving Approach: How effectively did the candidate break down the problem, manage complexity, and implement a robust solution within the given time?

Loading editor...