I have array of days.
JavaScript
x
2
1
const availableDates = ["2019-02-01", "2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07", "2019-02-11", "2019-02-12", "2019-02-13", "2019-02-14", "2019-02-15", "2019-02-19", "2019-02-20", "2019-02-21", "2019-02-22", "2019-02-23", "2019-02-25", "2019-02-26", "2019-02-27", "2019-02-28", "2019-03-01", "2019-03-04", "2019-03-05", "2019-03-06", "2019-03-07", "2019-03-08", "2019-03-09", "2019-03-11", "2019-03-12"]
2
I only want to show the above selected days in the airbnb datepicker and disable the other ones.
JavaScript
1
11
11
1
<SingleDatePicker
2
date={moment()}
3
onDateChange={date => this.setState({ date })}
4
focused={this.state.focused}
5
onFocusChange={({ focused }) => this.setState({ focused })}
6
id="your_unique_id"
7
numberOfMonths={1}
8
renderCalendarDay={() => availableDates.map(d => moment(d).format(d))}
9
className="select-btn selectbtn-picker"
10
/>
11
How can I do that? Thank you
Advertisement
Answer
You will have to use the isDayBlocked
prop of the date picker. The following function will find if a day is contained inside your array, and return true
if it does not find any :
JavaScript
1
5
1
isBlocked = day => {
2
const availableDates = ["2019-02-01", "2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07", "2019-02-11", "2019-02-12", "2019-02-13", "2019-02-14", "2019-02-15", "2019-02-19", "2019-02-20", "2019-02-21", "2019-02-22", "2019-02-23", "2019-02-25", "2019-02-26", "2019-02-27", "2019-02-28", "2019-03-01", "2019-03-04", "2019-03-05", "2019-03-06", "2019-03-07", "2019-03-08", "2019-03-09", "2019-03-11", "2019-03-12"]
3
return !availableDates.some(date => day.isSame(date), 'day')
4
}
5
It uses the isSame
function of moment.js : https://momentjs.com/docs/#/query/is-same/
Then bind your function :
JavaScript
1
12
12
1
<SingleDatePicker
2
date={moment()}
3
onDateChange={date => this.setState({ date })}
4
focused={this.state.focused}
5
onFocusChange={({ focused }) => this.setState({ focused })}
6
id="your_unique_id"
7
numberOfMonths={1}
8
renderCalendarDay={() => availableDates.map(d => moment(d).format(d))}
9
className="select-btn selectbtn-picker"
10
isDayBlocked={this.isBlocked}
11
/>
12