Overview
Many modern applications, especially in analytics or booking platforms, require users to select multiple, potentially disjoint, date ranges. This problem challenges you to build a robust and intuitive date picker component that supports this advanced selection model. Successfully implementing this demonstrates strong UI component design, sophisticated state management, and an understanding of complex user interaction patterns.
What you need to do / Task
Your goal is to build a React component for an advanced date picker with the following capabilities:
- Core Calendar Grid: Render a standard monthly calendar grid showing days of the week and days of the current month. Days from previous/next months should be visually distinct or omitted.
- Date Range Selection:
- Allow users to select a single date range by clicking a start date, then clicking an end date. The range should be inclusive.
- If a user clicks the same date again while it is the pending start date (only one date selected), the pending selection should be cleared.
- Multi-Range Support:
- After a range is selected, if the user clicks a new date outside any existing ranges, it should initiate the selection of a new, separate date range.
- If a user clicks a date within an already selected range, that specific range should be deselected.
- The component must maintain and display a list of all selected, disjoint date ranges.
- Month Navigation: Implement controls to navigate to the previous and next months. Crucially, selected ranges must persist visually and logically across month changes.
- Visual Feedback:
- Clearly highlight the currently hovered date.
- Visually distinguish the pending start date of a new range selection.
- Visually distinguish selected dates and entire selected ranges (start, end, and in-between dates).
- When a pending start date exists, highlight the dates between the pending start and the currently hovered date to suggest the potential range.
UI/UX expectations
- Layout: The date picker should display a clear header with the current month and year, along with "Previous" and "Next" buttons. Below the header, a grid of days of the week (Sun-Sat) followed by the calendar days.
- Interactions: All interactions should be primarily mouse-based (click, hover). Ensure the component feels responsive to user input.
- Accessibility: Use basic semantic HTML (e.g.,
<table> for the grid, aria-label for navigation buttons where appropriate) to ensure a foundation for accessibility.
Acceptance criteria
- The calendar correctly displays days for the current month and year.
- Users can navigate to previous and next months using provided controls.
- Clicking two distinct dates correctly selects a single date range, adding it to the list of selected ranges.
- Clicking a date that is part of an existing range correctly deselects only that specific range.
- Users can select and maintain multiple, disjoint date ranges simultaneously.
- Hovering over a date provides distinct visual feedback.
- Selected dates, pending start dates, and potential hover ranges are clearly highlighted with appropriate styling.
- Selected ranges persist correctly when navigating between months.