diff --git a/apps/scandic-web/components/DatePicker/Range/Mobile.tsx b/apps/scandic-web/components/DatePicker/Range/Mobile.tsx index efedc4bb0..a477bb97a 100644 --- a/apps/scandic-web/components/DatePicker/Range/Mobile.tsx +++ b/apps/scandic-web/components/DatePicker/Range/Mobile.tsx @@ -1,5 +1,6 @@ "use client" -import { DayPicker } from "react-day-picker" +import { useEffect, useRef, useState } from "react" +import { type DateRange, DayPicker } from "react-day-picker" import { useIntl } from "react-intl" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" @@ -29,15 +30,39 @@ export default function DatePickerRangeMobile({ /** English is default language and doesn't need to be imported */ const locale = lang === Lang.en ? undefined : locales[lang] + const monthsRef = useRef(null) + const [autoScrollEnabled, setAutoScrollEnabled] = useState(true) + const currentDate = dt().toDate() const startOfCurrentMonth = dt(currentDate).set("date", 1).toDate() const yesterday = dt(currentDate).subtract(1, "day").toDate() + useEffect(() => { + if (!monthsRef.current || !selectedRange?.from || !autoScrollEnabled) return + + const selectedDay = monthsRef.current.querySelector( + 'td[aria-selected="true"]:not([data-outside="true"])' + ) + + const targetMonth = selectedDay?.closest(`.${styles.month}`) + + if (targetMonth) { + targetMonth.scrollIntoView({ block: "start" }) + } + }, [selectedRange, autoScrollEnabled]) + + function handleSelectWrapper( + dateRange: DateRange | undefined, + selectedDay: Date + ) { + setAutoScrollEnabled(false) + handleOnSelect(dateRange, selectedDay) + } // Max future date allowed to book kept same as of existing prod. const endDate = dt().add(395, "day").toDate() const endOfLastMonth = dt(endDate).endOf("month").toDate() return ( -
+
)