KatInterview

Build Interdependent Month Dropdowns in React

BehavioralModel answer
MediumModel answerBehavioralreactformsvalidation

Last updated

Create two controlled month selector dropdowns with validation logic that prevents selecting invalid date ranges and past months.

You're building a booking form in React that needs month selection functionality. Your task is to implement two connected dropdown menus for selecting months with specific validation rules.

Requirements:

  1. Create two month dropdowns that integrate with an existing form structure. Each dropdown should display all twelve months (January through December) as options.

  2. Implement validation constraints:

    • Neither dropdown should allow selection of months that have already passed (relative to the current month)
    • The second dropdown must not allow selection of any month that comes before the month chosen in the first dropdown
    • These constraints should be enforced by disabling invalid options in the UI
  3. Maintain controlled component patterns: The dropdowns should not manage their own state. Instead, they should receive values through props and report changes through callbacks, allowing the parent component to remain the source of truth.

  4. Handle validation in two modes:

    • During user interaction: show validation errors when constraints are violated, but allow incomplete selections (only one month chosen)
    • On form submission: require both months to be selected and block submission if any validation rule is violated
  5. Ensure accessibility: Use native HTML <select> elements with proper labels, ARIA attributes for invalid states, and clear error messaging connected to form controls.

  6. Write unit tests that verify:

    • Disabled options appear correctly based on current month and interdependencies
    • State updates propagate correctly to parent components
    • Validation errors display at appropriate times
    • Form submission is blocked when validation fails

What to deliver:

  • A reusable month selector component
  • A form component that integrates two month selectors
  • A pure validation function that encapsulates all constraint logic
  • Unit tests demonstrating correct behavior

Assume the booking is for the current calendar year only.

Hints

Hint 1

Use a single pure function to encode all validation rules so UI disabled states and error messages stay consistent

Hint 2

Native select elements provide keyboard navigation and screen reader support automatically—avoid rebuilding these behaviors from scratch

Hint 3

Consider how state can become invalid not just through direct selection, but when one dropdown's value changes and invalidates the other