From fcd6e40b0d4a6c2c6146deaf7996a6bc37a2d489 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Thu, 24 Sep 2020 00:12:03 +0200 Subject: [PATCH] do not bail out if start and end are unordered --- components/wizard/calendar.tsx | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/components/wizard/calendar.tsx b/components/wizard/calendar.tsx index e216fa9..ff7cfd0 100644 --- a/components/wizard/calendar.tsx +++ b/components/wizard/calendar.tsx @@ -23,6 +23,22 @@ export default function MyCalendar() { const prevBooked = inSelection && getNextSmaller(daysBooked, start) const nextBooked = inSelection && getNextBigger(daysBooked, start) + function isSelected(date: Date) { + if (!startDate) { + return false + } + + // if end is before start, it is not within + if (endDate && !isAfter(endDate, startDate)) { + return false + } + + return isWithinInterval(date, { + start: startDate, + end: endDate || startDate, + }) + } + function tileClassName({ date, view }) { const isMonthView = view === 'month' const isDaysBookedLoaded = !!daysBooked @@ -37,12 +53,7 @@ export default function MyCalendar() { 'react-calendar__tile--selected-start': isSameDay(date, startDate), 'react-calendar__tile--selected-end': isSameDay(date, endDate), 'react-calendar__tile--selected': - (startDate && - isWithinInterval(date, { - start: startDate, - end: endDate || startDate, - })) || - isSameDay(date, startDate), + isSelected(date) || isSameDay(date, startDate), 'react-calendar__tile--unselectable': inSelection && ((prevBooked && date < new Date(prevBooked)) ||