Skip to content

Commit 87c6c6c

Browse files
committed
Sync date range calendar with start date
1 parent dadc3da commit 87c6c6c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/DateRangeCalendar.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { For, createEffect, createMemo, createSignal } from "solid-js";
22
import { Divider, Grid, Paper } from "@suid/material";
33
import { extractCommonCalendarProps } from "./MonthCalendar";
4-
import { CommonCalendarProps, DateRange } from "./models";
5-
import { addMonths, getToday } from "./dateUtils";
4+
import { CommonCalendarProps, DateRange, Optional } from "./models";
5+
import { addMonths, getToday, isSameDay } from "./dateUtils";
66
import DateRangeMonthCalendar from "./DateRangeMonthCalendar";
77

88
type CalendarCount = 1 | 2 | 3;
@@ -26,9 +26,18 @@ export default function DateRangeCalendar(props: DateRangeCalendarProps) {
2626

2727
const calendarCount = createMemo(() => props.calendars || 2);
2828

29-
createEffect(() => {
29+
createEffect((prevStartDate: Optional<Date>) => {
3030
setRange(props.value);
31-
});
31+
32+
const startDate = props.value[0];
33+
34+
// When the start date changes, update the calendar date.
35+
if (prevStartDate && startDate && !isSameDay(prevStartDate, startDate)) {
36+
setCalendarDate(startDate);
37+
}
38+
39+
return startDate;
40+
}, props.value[0]);
3241

3342
const calendarDates = createMemo(() =>
3443
Array.from({ length: calendarCount() }, (_, i) => addMonths(calendarDate(), i)),

0 commit comments

Comments
 (0)