I'm trying to hide my calendar by clicking the react-day-picker-input element again (it will open when you click it by default). For that, I have a state that records true or false when you click the element. The problem is that when I click again to hide it, it gives me the error below:
TypeError: calendarNode.hideDayPicker is not a function
I tried using showOverlay and hideDayPicker. I saw a code that works with buttons, but fails to achieve the results when you apply onClick to the DayPickerInput component (see below). https://codesandbox.io/s/oqm3p4j9rz
Here's my code (summarized):
onKeyPress = (event) => {
event.preventDefault();
}
dateRestriction = () => {
const date = new Date();
const nutrition_offset = date.getTimezoneOffset() + 240;
date.setMinutes(date.getMinutes() + nutrition_offset);
const year = date.getFullYear();
const month = date.getMonth() + 1;
let day = date.getDate();
if ((date.getDay() === 4) || (date.getDay() === 5)) {
if (date.getDate() < 5) {
day = ('0' + (date.getDate() + 5));
} else {
day = date.getDate() + 5;
}
}
if (date.getDay() === 6) {
if (date.getDate() < 6) {
day = ('0' + (date.getDate() + 4));
} else {
day = date.getDate() + 4;
}
}
if ((date.getDay() === 0) || (date.getDay() === 1) || (date.getDay() === 2) || (date.getDay() === 3)) {
if (date.getDate() < 7) {
day = ('0' + (date.getDate() + 3));
} else {
day = date.getDate() + 3;
}
}
let dateRestricted = this.parseDate_(year + '-' + month + '-' + day);
this.setState({
noDay: dateRestricted,
showCalendar: true
});
this.handleDayPickerInputHide();
}
handleDayPickerInputHide = () => {
const calendarNode = document.getElementsByName("date");
if (this.state.showCalendar === false) {
return;
} else {
calendarNode.hideDayPicker();
this.setState = {
showCalendar: false
}
}
}
render () {
const { selectedDay } = this.state;
return (
<div>
<DateObject
inputProps={
{className: 'dib nav pl2 pointer br3 shadow-1 dropdownButtonDate removeCursor bg-transparent pv1 mt2 typefaceFont dropdownText',
onKeyDown: this.onKeyPress,
onClick: this.dateRestriction,
name: 'date',
required: "required"}
}
value={selectedDay}
onDayChange={this.handleDayChange}
dayPickerProps={{
selectedDays: selectedDay,
disabledDays:
[
new Date(2019, 0, 1),
new Date(2019, 11, 24),
new Date(2019, 11, 25),
new Date(2019, 11, 31),
{
daysOfWeek: [0, 6],
},
{
before: new Date(this.state.noDay)
}]
}}
/>
</div>
)
}
Expected: 1. Calendar is hidden initially (default behavior) 2. Click displays calendar (default behavior) 3. Click again to hide the calendar (NEEDED) 4. Click outside hides the calendar as well (default behavior) 5. Choose a date hides the calendar as well (default behavior)
Actual results: 1. Calendar is hidden initially (default behavior) 2. Click displays calendar (default behavior) 3. Click again to hide the calendar (ERROR) 4. Click outside hides the calendar as well (default behavior) 5. Choose a date hides the calendar as well (default behavior)