Select a range of days
This example shows a possible implementation for a component selecting a range of days.
- Use
selectedDays={{ from, to }}
to display a range of days as selected - Use the provided
addDayToRange
utility to add or remove days to the range when the user clicks/taps on them - Add
start
andend
modifiers to style the first and last day in a different way.
Code
import React from 'react';
import Helmet from 'react-helmet';
import DayPicker, { DateUtils } from 'react-day-picker';
import 'react-day-picker/lib/style.css';
export default class Example extends React.Component {
static defaultProps = {
numberOfMonths: 2,
};
constructor(props) {
super(props);
this.handleDayClick = this.handleDayClick.bind(this);
this.handleResetClick = this.handleResetClick.bind(this);
this.state = this.getInitialState();
}
getInitialState() {
return {
from: undefined,
to: undefined,
};
}
handleDayClick(day) {
const range = DateUtils.addDayToRange(day, this.state);
this.setState(range);
}
handleResetClick() {
this.setState(this.getInitialState());
}
render() {
const { from, to } = this.state;
const modifiers = { start: from, end: to };
return (
<div className="RangeExample">
<p>
{!from && !to && 'Please select the first day.'}
{from && !to && 'Please select the last day.'}
{from &&
to &&
`Selected from ${from.toLocaleDateString()} to
${to.toLocaleDateString()}`}{' '}
{from && to && (
<button className="link" onClick={this.handleResetClick}>
Reset
</button>
)}
</p>
<DayPicker
className="Selectable"
numberOfMonths={this.props.numberOfMonths}
selectedDays={[from, { from, to }]}
modifiers={modifiers}
onDayClick={this.handleDayClick}
/>
<Helmet>
<style>{`
.Selectable .DayPicker-Day--selected:not(.DayPicker-Day--start):not(.DayPicker-Day--end):not(.DayPicker-Day--outside) {
background-color: #f0f8ff !important;
color: #4a90e2;
}
.Selectable .DayPicker-Day {
border-radius: 0 !important;
}
.Selectable .DayPicker-Day--start {
border-top-left-radius: 50% !important;
border-bottom-left-radius: 50% !important;
}
.Selectable .DayPicker-Day--end {
border-top-right-radius: 50% !important;
border-bottom-right-radius: 50% !important;
}
`}</style>
</Helmet>
</div>
);
}
}
Result
Please select the first day.
March 2021
Su
Mo
Tu
We
Th
Fr
Sa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
April 2021
Su
Mo
Tu
We
Th
Fr
Sa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30