Frontend School

Build a Passenger Selector Modal

Machine Coding
medium

Build a Passenger Selector Modal

Medium
React
Modal
Accessibility
State Management

Overview

In travel booking platforms like Ixigo, selecting the number and type of passengers is a crucial step. This selection often happens within a dedicated modal dialog to provide a focused user experience without cluttering the main booking form. For this problem, you will build a robust and user-friendly passenger selection modal.

What you need to do / Task

Your primary task is to implement a PassengerSelectorModal component and integrate it into App.tsx.

  1. Modal Trigger: Render a button on the main App component that, when clicked, opens the PassengerSelectorModal.
  2. Passenger Type Counters: Inside the modal, display three distinct sections for:
    • Adults (12+ years)
    • Children (2-12 years)
    • Infants (under 2 years, lap-held) Each section must have:
    • A label (e.g., "Adults")
    • A current count display
    • Two buttons: one to decrement the count (-) and one to increment the count (+).
  3. Apply Button: Implement an "Apply" button within the modal. Clicking this button should save the currently selected passenger counts and close the modal.
  4. Cancel/Close Functionality:
    • Implement a "Cancel" button that discards any changes and closes the modal.
    • Implement a close button (e.g., an 'X' icon) in the modal header that also discards changes and closes the modal.
  5. Display Selected Passengers: After applying changes, the main App component should display the total selected passengers and their breakdown (e.g., "3 Passengers (2 Adults, 1 Child, 0 Infants)").

UI/UX expectations

  • Layout: The modal should appear centered on the screen, overlaying the main content. The main content should be visually subdued (e.g., dimmed) when the modal is open.
  • Controls: Increment/decrement buttons should be clearly visible and functional. The current count should be displayed between them.
  • Accessibility:
    • The modal should be keyboard navigable (e.g., Tab key to move between controls, Esc key to close the modal).
    • Appropriate ARIA attributes (e.g., role="dialog", aria-modal="true", aria-labelledby) should be used for the modal itself and its interactive elements.
    • Focus should be trapped within the modal when it's open.
  • Visual Feedback: Buttons should provide hover/focus states. Disabled buttons (due to constraints) should be visually distinct.

Acceptance criteria

  • The "Select Passengers" button correctly opens the modal.
  • The modal displays sections for Adults, Children, and Infants with +/- buttons and current counts.
  • Incrementing/decrementing counts works correctly for each passenger type.
  • Constraints are correctly applied:
    • Adults count is always >= 1.
    • Total passengers (Adults + Children) <= 9.
    • Infants count <= Adults count.
  • The "Apply" button saves the selection, closes the modal, and updates the display in App.tsx.
  • The "Cancel" button closes the modal without saving changes.
  • The 'X' close button closes the modal without saving changes.
  • Pressing Esc key closes the modal without saving changes.
  • Modal is keyboard navigable and traps focus.
  • Modal has appropriate ARIA attributes.

Constraints

Constraints & assumptions

  • Technology: You are expected to use React for building the components.
  • Styling: Use plain CSS or CSS Modules (styles.css is provided). No external UI libraries (e.g., Material UI, Ant Design) are allowed for core component logic, but you can use simple utility classes if needed.
  • Passenger Limits:
    • Minimum Adults: 1
    • Maximum Total Passengers (Adults + Children): 9
    • Maximum Infants: Equal to the number of Adults.
  • Initial State: The modal should initially open with 1 Adult, 0 Children, 0 Infants if no prior selection exists, or reflect the last applied selection.
  • Time Budget: Aim to complete the core functionality within ~45 minutes.

Out of scope

  • Full-fledged internationalization (i18n).
  • Deep linking or URL routing.
  • Server-side data persistence.
  • Extensive styling beyond basic layout and visual feedback.

What interviewers evaluate

  • Component Design: Modularity, reusability, and clear separation of concerns.
  • State Management: Effective use of React state (e.g., useState, useReducer) to manage modal visibility and passenger counts.
  • Logic & Constraints: Correct implementation of passenger count logic and validation rules.
  • User Experience: Intuitive interactions and responsiveness to user input.
  • Accessibility: Adherence to basic accessibility best practices for modal dialogs.

Loading editor...