A responsive and accessible date range picker component built with React
npx @tessl/cli install tessl/npm-react-dates@21.8.0React Dates is a comprehensive, responsive, and accessible date picker library built with React. It provides three main date selection components with extensive customization options, accessibility features, and internationalization support through Moment.js integration.
npm install react-datesimport {
DateRangePicker,
SingleDatePicker,
DayPickerRangeController
} from "react-dates";For CommonJS:
const {
DateRangePicker,
SingleDatePicker,
DayPickerRangeController
} = require("react-dates");import React, { useState } from "react";
import {
DateRangePicker,
SingleDatePicker
} from "react-dates";
import moment from "moment";
// Required: Initialize CSS interface
import "react-dates/initialize";
// Required: Import CSS styles
import "react-dates/lib/css/_datepicker.css";
// Date Range Picker
function MyDateRangePicker() {
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null);
const [focusedInput, setFocusedInput] = useState(null);
return (
<DateRangePicker
startDate={startDate}
startDateId="your_unique_start_date_id"
endDate={endDate}
endDateId="your_unique_end_date_id"
onDatesChange={({ startDate, endDate }) => {
setStartDate(startDate);
setEndDate(endDate);
}}
focusedInput={focusedInput}
onFocusChange={setFocusedInput}
/>
);
}
// Single Date Picker
function MySingleDatePicker() {
const [date, setDate] = useState(null);
const [focused, setFocused] = useState(false);
return (
<SingleDatePicker
date={date}
onDateChange={setDate}
focused={focused}
onFocusChange={({ focused }) => setFocused(focused)}
id="your_unique_id"
/>
);
}React Dates is built around several key components:
The library emphasizes accessibility with screen reader support, keyboard navigation, and ARIA labeling. It supports responsive design with multiple orientations, portal rendering, and mobile-optimized layouts.
Complete date picker components with input fields and calendar dropdowns. Perfect for forms and user interfaces requiring date selection.
// Date range selection component
function DateRangePicker(props: DateRangePickerProps): ReactElement;
// Single date selection component
function SingleDatePicker(props: SingleDatePickerProps): ReactElement;Headless calendar components without input fields, ideal for custom implementations and embedded calendars.
// Calendar-only date range controller
function DayPickerRangeController(props: DayPickerRangeControllerProps): ReactElement;
// Calendar-only single date controller
function DayPickerSingleDateController(props: DayPickerSingleDateControllerProps): ReactElement;Standalone input field components for custom layouts and advanced use cases.
// Date range input fields
function DateRangePickerInput(props: DateRangePickerInputProps): ReactElement;
function DateRangePickerInputController(props: DateRangePickerInputControllerProps): ReactElement;
// Single date input field
function SingleDatePickerInput(props: SingleDatePickerInputProps): ReactElement;Low-level calendar rendering components for building custom date picker interfaces.
// Complete calendar component with navigation
function DayPicker(props: DayPickerProps): ReactElement;
// Month grid layout for multiple months
function CalendarMonthGrid(props: CalendarMonthGridProps): ReactElement;
// Individual month view
function CalendarMonth(props: CalendarMonthProps): ReactElement;
// Individual day cell
function CalendarDay(props: CalendarDayProps): ReactElement;Date comparison and conversion utilities for working with Moment.js objects.
// Date comparison functions
function isSameDay(a: moment.Moment, b: moment.Moment): boolean;
function isInclusivelyAfterDay(a: moment.Moment, b: moment.Moment): boolean;
function isInclusivelyBeforeDay(a: moment.Moment, b: moment.Moment): boolean;
function isNextDay(a: moment.Moment, b: moment.Moment): boolean;
// Date conversion functions
function toISODateString(date: moment.Moment | string, currentFormat?: string): string;
function toLocalizedDateString(date: moment.Moment | string, currentFormat?: string): string;
function toMomentObject(dateString: string, customFormat?: string): moment.Moment | null;React Dates exports several constants for configuration:
// Import constants from react-dates
import {
START_DATE,
END_DATE,
HORIZONTAL_ORIENTATION,
VERTICAL_ORIENTATION,
VERTICAL_SCROLLABLE,
ICON_BEFORE_POSITION,
ICON_AFTER_POSITION,
ANCHOR_LEFT,
ANCHOR_RIGHT,
OPEN_DOWN,
OPEN_UP,
NAV_POSITION_TOP,
NAV_POSITION_BOTTOM,
INFO_POSITION_TOP,
INFO_POSITION_BOTTOM,
INFO_POSITION_BEFORE,
INFO_POSITION_AFTER,
DISPLAY_FORMAT,
ISO_FORMAT,
DAY_SIZE,
WEEKDAYS
} from "react-dates";
// Focus states for date range picker
START_DATE: "startDate";
END_DATE: "endDate";
// Orientation options
HORIZONTAL_ORIENTATION: "horizontal";
VERTICAL_ORIENTATION: "vertical";
VERTICAL_SCROLLABLE: "verticalScrollable";
// Icon positions
ICON_BEFORE_POSITION: "before";
ICON_AFTER_POSITION: "after";
// Anchor directions
ANCHOR_LEFT: "left";
ANCHOR_RIGHT: "right";
// Open directions
OPEN_DOWN: "down";
OPEN_UP: "up";
// Navigation positions
NAV_POSITION_TOP: "navPositionTop";
NAV_POSITION_BOTTOM: "navPositionBottom";
// Info panel positions
INFO_POSITION_TOP: "top";
INFO_POSITION_BOTTOM: "bottom";
INFO_POSITION_BEFORE: "before";
INFO_POSITION_AFTER: "after";
// Date formats
DISPLAY_FORMAT: "L";
ISO_FORMAT: "YYYY-MM-DD";
// Layout constants
DAY_SIZE: 39;
WEEKDAYS: [0, 1, 2, 3, 4, 5, 6];
// Validation function types
type DayValidationFunction = (day: moment.Moment) => boolean;
// Date change handlers
type DateRangeChangeHandler = ({ startDate, endDate }: {
startDate: moment.Moment | null;
endDate: moment.Moment | null;
}) => void;
type SingleDateChangeHandler = (date: moment.Moment | null) => void;
// Focus change handlers
type FocusChangeHandler = (focusedInput: 'startDate' | 'endDate' | null) => void;
type SingleFocusChangeHandler = ({ focused }: { focused: boolean }) => void;React Dates requires CSS interface initialization before use:
// Import and run initialization (required)
import "react-dates/initialize";
// Or manually initialize
import { registerCSSInterfaceWithDefaultTheme } from "react-dates/lib/initialize";
registerCSSInterfaceWithDefaultTheme();You must also import the required CSS styles:
@import '~react-dates/lib/css/_datepicker.css';Or include it in your HTML:
<link rel="stylesheet" type="text/css" href="./node_modules/react-dates/lib/css/_datepicker.css" />// Block past dates
const isOutsideRange = (day) => moment().diff(day) > 0;
// Block specific dates
const isDayBlocked = (day) => {
const blockedDates = ['2023-12-25', '2023-01-01'];
return blockedDates.includes(day.format('YYYY-MM-DD'));
};
// Highlight special dates
const isDayHighlighted = (day) => {
const holidays = ['2023-12-25', '2023-07-04'];
return holidays.includes(day.format('YYYY-MM-DD'));
};The library handles validation through callback functions rather than throwing exceptions. Invalid states are communicated through:
isDayBlocked and isOutsideRange functions