Frontend School

Advanced OTP Input Component with Auto-Focus and Paste Handling

Machine Coding
hard

Advanced OTP Input Component with Auto-Focus and Paste Handling

Hard
React
Frontend
UI
OTP
Accessibility
TypeScript
Forms

Overview

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.

What you need to do / Task

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.

  1. Component Structure: Create a functional OtpInput component that accepts a length prop (number of digits, e.g., 4 or 6). It should render individual input fields for each digit.
  2. Digit Input & Auto-Focus: Implement logic such that typing a digit in one field automatically moves the focus to the next empty field. If the last field is filled, focus should not move further.
  3. Backspace Navigation: When the user presses 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.
  4. Paste Functionality: The component must intelligently handle pasting an OTP string (e.g., '123456'). When a user pastes, the component should parse the pasted string and populate the input fields accordingly, moving focus to the next available field or the last field if all are filled.
  5. Validation: Ensure only numeric digits can be entered into the fields. Non-numeric input should be ignored or prevented. The component should also expose an isError prop to visually indicate an error state (e.g., invalid OTP).
  6. Callbacks: Provide an onChange callback that is triggered whenever the OTP value changes, and an onComplete callback that fires when all fields are filled with valid digits.
  7. Accessibility: Implement basic accessibility features for screen readers and keyboard navigation.

UI/UX expectations

  • The component should visually represent each digit as a distinct input field (e.g., small squares). Use a flexible layout (e.g., flexbox) to arrange them.
  • A clear visual indication of the currently focused input field is expected (e.g., a border or background change).
  • When an error state is active (via isError prop), the input fields should display a distinct visual cue (e.g., red border).
  • Input fields should be appropriately sized and spaced for optimal readability and touch interaction.

Acceptance criteria

  • The OtpInput component renders length number of individual input fields.
  • Typing a numeric character in an input field correctly populates it and automatically moves focus to the next field.
  • Pressing Backspace in an empty field moves focus to the previous field.
  • Pressing Backspace in a filled field clears its value and keeps focus on that field, then moves focus on subsequent Backspace press.
  • Pasting a numeric string into any field correctly populates the subsequent fields and moves focus to the end of the input sequence.
  • Non-numeric input is prevented or ignored.
  • The onChange(otp: string) callback is invoked with the current OTP string whenever any digit changes.
  • The onComplete(otp: string) callback is invoked only when all length fields are filled with numeric digits.
  • The component visually reflects an error state when the isError prop is true.
  • Keyboard navigation (Tab/Shift+Tab) works as expected between fields.
  • Each input field has appropriate type, inputmode, and aria-label attributes for accessibility.

Constraints

Constraints & assumptions

  • You should use React functional components and hooks.
  • TypeScript is preferred for type safety and clarity.
  • The OTP length will be between 4 and 6 digits (inclusive).
  • The OTP will always consist of numeric characters only.
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed; build from scratch using native HTML elements and CSS.
  • The solution should be self-contained within the provided Sandpack environment.

Out of scope

  • Backend API integration for OTP verification.
  • Advanced accessibility features beyond basic keyboard navigation and ARIA attributes.
  • Complex animations or transition effects.
  • Global state management (e.g., Redux, Zustand) beyond the component's internal state.

What interviewers evaluate

  • Component Design & Reusability: How well the OtpInput component is structured, its API (props, callbacks), and its reusability.
  • Complex Interaction Handling: Proficiency in managing focus, keyboard events (type, backspace), and paste events for a fluid user experience.
  • State Management & Side Effects: Effective use of React hooks (useState, useRef, useEffect) to manage component state and interactions.
  • Accessibility & UI/UX: Attention to detail in providing a good user experience and basic accessibility for diverse users.
  • Code Quality & Maintainability: Readability, maintainability, type safety (if TypeScript is used), and adherence to best practices.

Loading editor...