Skip to main content
100%

DatePicker

✓ Published0🌍 Public
Aabonello
Last edited Dec 30, 2021
Created on Dec 30, 2021

A React-based date picker component that supports both single-date selection and date-range selection. The DatePicker button displays the selected date or date range, and clicking it opens a modal dialog containing an interactive calendar. The calendar is built with date-fns utilities and displays a month grid with weekday headers, highlighting today, selected dates, and range intervals. Users can navigate between months and select dates, with validation against optional min and max date constraints. The range picker shows a continuous highlighted range between the start and end dates, with distinct styling for the range endpoints.

AI-generated description

DatePicker

The DatePicker component allows you to pick a single date, or date range (from -> to).

This component is built on top of my Dialog as a promise component and uses Bulma.

It uses the SASS from Bulma Calendar, converted to SCSS.

The Javascript has been rewritten from scratch for use in React and uses the date-fns library.

Required Props

  • onChange - When you select a date or a date range, this returns the selection (see below).

Optional Props

  • name - The data name value which will be included in the onChange callback (see below)
  • startDate - The date that you want to be pre-selected in a single date or range. The default is today.
  • isRange - If you want the DatePicker to act as a range picker, include this boolean prop (see below).
  • endDate - If the component is a range picker, this is the end date you want to be pre-selected. The default is tomorrow.
  • minDate - Optional minimum threshold for only allowing dates from this date or after.
  • maxDate - Optional maximum threshold for only allowing dates from this date or before.
  • dateFormat - What is displayed on the button, as per the date-fns format documentation. The default is YYYY-MM-DD.
  • className - A custom className for styling.
const onChange = ({name, startDate, endDate}) => {
    console.log({name, startDate, endDate});
};

<DatePicker
    name="availabilityWindow"
    startDate={new Date(2018, 2, 5)} // March 5th, 2018
    endDate={new Date(2018, 3, 12)} // April 12th, 2018
    minDate={new Date(2018, 1, 1)} // Feb 1st, 2018
    maxDate={new Date(2018, 4, 31)} // May 31st, 2018
    onChange={onChange}
    isRange 
/>

Similar vizzes