Date Range Picker Example using React Date Range [Customized]

HOW CUSTOMIZATION IS DONE IN REACT DATE RANGE PICKER

In this blog, React Date Range Picker has been customized so that we can cover as many use cases as possible.

CSS has been customized to change the design of the calendar. You can read the comments given in the source code to understand that why that particular CSS is changed.

I have created two custom fields to show the start date and end date. When any of the fieldĀ  is clicked, calendar gets open to select the date range.

I have used MOMENT JS to convert the date object provided by this package into the string to show the date in the start date and end date text fields.

I have created the responsive design so it can be used on any devices.

There are many use cases which are handled in the handleChange function in the below source code. You can read the comments for better understanding of each part of the code.

ClickAwayListener component is used from Material UI to close the calendar if the user clicks anywhere out of the date range text fields or calendar area.

I have attached the desktop view output and mobile view output of the code below.

HOW TO IMPLEMENT REACT DATERANGE IN YOUR PROJECT

First we have to import three files for this package –

  • Main css file
  • Theme css file
  • DateRange component

DateRange component has many properties which can be used to fulfill as per the requirement. I have used some important properties. You can go through its documentation to read about more properties.

Below are the important properties of Date Range Picker react component used in the code –

  • direction – this is used to change the direction of calendar as per the screen size for responsive design.
  • scroll – to enable scrolling of months when we are viewing calendar on the mobile.
  • minDate – to restrict the user to not to select the date earlier than the minDate.
  • maxDate – to restrict the user to notĀ  to select the date beyond the maxDate.

 

OUTPUT FOR DESKTOP VIEW

date-range-picker-output

OUTPUT FOR MOBILE VIEW

react-date-range-mobile-view

Leave a Reply

Your email address will not be published. Required fields are marked *