Introduction
The react-dates is a plugin written in React.js to initialize the React Date range picker component in React forms.
Open a command prompt. Create a directory for the SPFx solution.
md spfx-ReactDateRange
Navigate to the above-created directory.
cd spfx-ReactDateRange
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have the default name (spfx-ReactDateRangein this case) or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
Permissions to access web APIs
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice - ReactDateRangePicker
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
NPM Packages used,
- npm install --save react-dates
- npm i moment
Necessary imports,
- import * as Moment from 'moment';
- import 'react-dates/initialize';
- import 'react-dates/lib/css/_datepicker.css';
- import { DateRangePicker, SingleDatePicker, DayPickerRangeController } from 'react-dates';
To initialize DateRangePicker component, insert the below tag in render method,
- <DateRangePicker/>
To set Max and Min date use the following code as a sample
- public isOutsideRange = day =>day.isAfter(Moment().add(10, "days")) || day.isBefore(Moment().subtract(1, "days"));
To adjust arrow mark hidden in ui element use the below method
- (document.querySelector(".DateRangePickerInput_arrow_svg") as HTMLDivElement).style.padding='0 0 0 20px';
In this plugin we have lot of daterangepicker components as per different needs:
- DateRangePicker (DRP)
- SingleDatePicker (SDP)
- DayPickerRangeController
- DayPickerSingleDateController
- DayPicker
- PresetDateRangePicker
Full Code
in ReactDateRangePicker.tsx
- import * as React from 'react';
- import * as Moment from 'moment';
- import { IReactDateRangePickerProps } from './IReactDateRangePickerProps';
- import 'react-dates/initialize';
- import 'react-dates/lib/css/_datepicker.css';
- import { DateRangePicker, SingleDatePicker, DayPickerRangeController } from 'react-dates';
- export interface IDateRangeState {
- startDate: any;
- endDate: any;
- focusedInput: any;
- }
- const divStyle = {
- padding: '0 0 0 20px'
- };
- export default class ReactDateRangePicker extends React.Component<IReactDateRangePickerProps, IDateRangeState> {
- constructor(props) {
- super(props);
- this.state = {
- startDate: null,
- endDate: null,
- focusedInput: null,
- };
- this.isOutsideRange = this.isOutsideRange.bind(this);
- }
- public componentDidMount(){
- (document.querySelector(".DateRangePickerInput_arrow_svg") as HTMLDivElement).style.padding='0 0 0 20px';
- }
- public isOutsideRange = day =>day.isAfter(Moment().add(10, "days")) || day.isBefore(Moment().subtract(1, "days"));
- public render(): React.ReactElement<IReactDateRangePickerProps> {
- // const {focusedInput, startDate, endDate} = this.state;
- return (
- <div>
- <DateRangePicker
- regular={true}
- anchorDirection="left"
- block={true}
- customArrowIcon={null}
- customCloseIcon={null}
- customInputIcon={null}
- disabled={false}
- enableOutsideDays={true}
- endDatePlaceholderText="End"
- startDatePlaceholderText="Start"
- horizontalMargin={0}
- isRTL={false}
- keepOpenOnDateSelect={false}
- minimumNights={2}
- monthFormat="MMMM YYYY"
- navNext={null}
- navPosition="navPositionTop"
- navPrev={null}
- showClearDates={true}
- showDefaultInputIcon={true}
- displayFormat={() => "DD/MM/YYYY"}
- numberOfMonths={2}
- daySize={50}
- //withPortal={true}
- isOutsideRange={this.isOutsideRange}
- // isDayBlocked={ () => true}
- //isDayHighlighted={ () => true}
- startDateId="startDate"
- endDateId="endDate"
- startDate={this.state.startDate}
- endDate={this.state.endDate}
- onDatesChange={({ startDate, endDate }) => { this.setState({ startDate, endDate });}}
- focusedInput={this.state.focusedInput}
- onFocusChange={(focusedInput) => { this.setState({ focusedInput });}}
- />
- </div>
- );
- }
- }
Available Properties
- regular - size of the picker
- small -size of the picker
- anchorDirection - direction of the picker
- block -fixed width of the picker
- customArrowIcon -customized Arrow Icon
- customCloseIcon -customized Close Icon
- customInputIcon=customized Input Icon
- disabled -to disable the picker
- enableOutsideDays -enabling outside days
- endDatePlaceholderText -Placeholder for end date
- startDatePlaceholderText -Placeholder for start date
- horizontalMargin -Margin from horizontal direction
- isRTL -useful for arabic lang,to open picker in right side
- keepOpenOnDateSelect - after clicking both dates picker never disappear
- minimumNights - how much days want to selected as mandatory
- monthFormat -format of month
- displayFormat - display format whether as dd/mm/yy and etc...
- numberOfMonths={2}
- daySize -how long days need to visible from start and end date
- withPortal - open as full indexed view
- isOutsideRange - to set min and max date to visible
- isDayBlocked - to blocking the days
- isDayHighlighted=to highlight the days
- startDateId=html id for start date
- endDateId=html id for end date
- startDate=default value for start date
- endDate=default value for enddate
- onDatesChange -date change event
- onFocusChange -focus change event
and a lot more options are available as per the picker you have choosen
Event Types
onDatesChange - sets the value to the state when the Date is changed.
- onDatesChange = {({ startDate, endDate }) => { this.setState({ startDate, endDate });}}
onFocusChange - sets the value to the state when the Focus is changed.
- onFocusChange = {(focusedInput) => { this.setState({ focusedInput });}}
Sample Output

Conclusion
Hence we learned about initializing React Date Range Picker component in Spfx forms using react-dates plugin. I hope this helps someone. Happy coding :)

vamsidhar madunuriPosted Sep 17, 2024, 5:07 AM
Can't Wait we calculate difference between 2 dates