Frontend School

Stripe Date Picker Component

Machine Coding
medium

Stripe Date Picker Component

Medium
Calendar
Date Picker
UI
React
Frontend

Overview

Stripe users often need to schedule payments, subscriptions, or generate reports for specific dates. A robust and intuitive date picker is crucial for a smooth user experience, ensuring accuracy and reducing errors in financial operations. This problem focuses on building a core DatePicker component that allows users to select a single date from a calendar view.

What you need to do / Task

Your task is to implement a DatePicker React component with the following features:

  1. Input Field & Popover: The DatePicker should render as an input field. Clicking or focusing on this input field should reveal a calendar UI in a popover.
  2. Calendar Display: The calendar popover should display days for the currently viewed month. Include controls for navigating to the previous and next months.
  3. Date Selection: Users must be able to click on a day within the calendar to select it. The selected date should then update the input field's value (formatted as YYYY-MM-DD) and close the popover.
  4. Visual Feedback: Implement clear visual distinctions for:
    • The current day (today).
    • The currently selected date.
    • Days that belong to the previous or next month (should be visually deemphasized or distinct).
  5. Constraints: Dates in the past (before today) should be visually disabled and not selectable.

UI/UX expectations

  • Layout: The DatePicker should primarily consist of an <input type="text" readOnly /> element. The calendar UI should appear as a popover positioned relative to the input when active.
  • Interactions:
    • Clicking the input field toggles the calendar popover's visibility.
    • Clicking the 'Previous Month' and 'Next Month' buttons updates the calendar view.
    • Clicking a valid day in the calendar selects it, updates the input, and closes the popover.
    • Clicking outside the calendar popover (when open) should close it.
    • Pressing the Escape key (when popover is open) should close it.
  • Accessibility (Basic): The input field should be readOnly to prevent manual date entry and encourage calendar usage. Focus management for the popover is not strictly required beyond closing on Escape and outside clicks.

Acceptance criteria

  • Clicking the input field correctly opens and closes the calendar popover.
  • The calendar initially displays the current month and year.
  • The 'Previous Month' and 'Next Month' buttons correctly navigate the calendar view.
  • 'Today's date is visually highlighted in the calendar.
  • Selecting a date updates the input field with the date in YYYY-MM-DD format and visually highlights the selected date in the calendar.
  • Dates in the past are visually disabled and cannot be selected.
  • Clicking outside the calendar popover closes it.
  • Pressing the Escape key closes the calendar popover.

Constraints

Constraints & assumptions

  • You should use native JavaScript Date objects for all date calculations and manipulations. Avoid using external date utility libraries like moment.js or date-fns.
  • Assume a standard Gregorian calendar. No need to account for different calendar systems.
  • The component should handle single date selection only.
  • Focus on functional correctness and clean component architecture. Basic styling to differentiate states is sufficient; pixel-perfect design is not required.
  • Month and day names can be displayed in English (e.g., 'January', 'Mon').

Out of scope

  • Time selection (e.g., hours, minutes).
  • Date range selection.
  • Year/decade view navigation.
  • Complex recurring event logic.
  • Full ARIA compliance for screen readers beyond basic keyboard interactions mentioned in UI/UX.
  • Internationalization beyond English month/day names.

What interviewers evaluate

  • Component Design: How well the component is structured, its API, and the use of React state and props.
  • Date Logic: Correctness and efficiency of date calculations using native Date objects for navigation, comparisons, and formatting.
  • UI/UX Implementation: Responsiveness to user interactions, clear visual feedback for different date states, and adherence to basic accessibility requirements.
  • Code Quality: Readability, maintainability, and handling of edge cases.

Loading editor...