Frontend School

OTP Input Component with Advanced Interaction

Machine Coding
medium

OTP Input Component with Advanced Interaction

Medium
React
Frontend
Component
Forms
Accessibility
TypeScript

Overview

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.

What you need to do / Task

Your primary task is to create a reusable OTPInput React component that handles a numeric OTP.

  1. Component Structure: Design a functional React component OTPInput that accepts length: number (e.g., 6 for a 6-digit OTP) and onChange: (otp: string) => void props.
  2. Input Fields: Render individual input fields (e.g., <input type="text" />) for each digit of the OTP.
  3. Numeric Input: Ensure only numeric digits (0-9) can be entered into each field. Non-numeric input should be ignored or prevented.
  4. Auto-Focus on Next: When a digit is entered into an input field, automatically move focus to the next empty input field.
  5. Backspace/Delete Handling:
    • Pressing Backspace in an empty field should move focus to the previous field and clear its content.
    • Pressing Backspace in a non-empty field should clear its content and remain focused on that field.
    • Pressing Delete should clear the current field and remain focused.
  6. Full OTP Propagation: The 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.
  7. Error State: Implement a basic mechanism to visually indicate an error state (e.g., a red border or text) when an isInvalid: boolean prop is passed to the component.

UI/UX expectations

  • Visual Clarity: Each digit should reside in its own distinct visual box (e.g., a square input field).
  • Focus Indication: Clearly highlight the currently focused input field.
  • Accessibility: Ensure the component is keyboard navigable. Use appropriate ARIA attributes (e.g., aria-label, inputmode="numeric", pattern="[0-9]") for better accessibility.
  • Error Display: When isInvalid is true, the input fields should visually reflect this (e.g., a red border around all fields, or a red error message below).

Acceptance criteria

  • The OTPInput component renders length number of input fields.
  • Typing a digit into an input field correctly populates it and moves focus to the next field.
  • Only numeric characters are accepted; other characters are ignored.
  • Pressing Backspace in an empty field moves focus to the previous field and clears it.
  • Pressing Backspace in a filled field clears it and keeps focus.
  • Pressing Delete clears the current field and keeps focus.
  • The onChange callback is invoked with the current partial or complete OTP string on every change.
  • The component correctly displays a visual error state when the isInvalid prop is true.
  • Keyboard navigation (Tab, Shift+Tab) works as expected between input fields.

Constraints

Constraints & assumptions

  • The solution must be implemented using React (functional components) and TypeScript.
  • The OTP will always consist of numeric digits (0-9).
  • The maximum OTP length will be 8 digits.
  • No external UI libraries (e.g., Material UI, Ant Design) are allowed; build from primitive HTML elements and CSS.
  • The component should manage its own internal state for the OTP value.
  • No backend integration is required for OTP verification.

Out of scope

  • Copy-pasting multiple digits from the clipboard into separate fields.
  • Complex animations or transitions.
  • Internationalization (i18n).
  • Support for non-numeric OTPs.
  • Automatic submission of the OTP form.

What interviewers evaluate

  • Component Design & Reusability: How well the OTPInput component is structured, its props, and its ability to be used in different contexts.
  • State Management & Event Handling: Effective use of React state hooks and efficient handling of keyboard and input events.
  • User Experience & Accessibility: Intuitive interaction patterns (auto-focus, backspace logic) and adherence to accessibility best practices.
  • Code Quality & Readability: Clean, well-organized, and maintainable code with appropriate TypeScript types.

Loading editor...