Skip to content

Commit 0d3ce7b

Browse files
committed
CalendarList - fix - navigation back to initial month doesn't display it
1 parent ec007eb commit 0d3ce7b

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/calendar-list/index.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,13 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
106106
const style = useRef(styleConstructor(theme));
107107
const list = useRef();
108108
const range = useRef(horizontal ? 1 : 3);
109-
const initialDate = useRef(parseDate(current));
109+
const initialDate = useRef(parseDate(current) || new XDate());
110110
const visibleMonth = useRef(currentMonth);
111111

112112
const items = useMemo(() => {
113113
const months = [];
114-
const date: XDate = initialDate?.current || new XDate();
115114
for (let i = 0; i <= pastScrollRange + futureScrollRange; i++) {
116-
const rangeDate = date.clone().addMonths(i - pastScrollRange, true);
115+
const rangeDate = initialDate.current?.clone().addMonths(i - pastScrollRange, true);
117116
months.push(rangeDate);
118117
}
119118
return months;
@@ -128,9 +127,8 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
128127
}, [propsStyle]);
129128

130129
const initialDateIndex = useMemo(() => {
131-
const date: XDate = initialDate?.current || new XDate();
132130
return findIndex(items, function(item) {
133-
return item.toString() === date.toString();
131+
return item.toString() === initialDate.current?.toString();
134132
});
135133
}, [items]);
136134

@@ -149,7 +147,6 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
149147
const scrollToDay = (date: XDate | string, offset: number, animated: boolean) => {
150148
const scrollTo = parseDate(date);
151149
const diffMonths = Math.round(initialDate?.current?.clone().setDate(1).diffMonths(scrollTo?.clone().setDate(1)));
152-
153150
let scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize + (offset || 0);
154151

155152
if (!horizontal) {
@@ -164,16 +161,18 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
164161
}
165162
}
166163

167-
// @ts-expect-error
168-
list?.current?.scrollToOffset({offset: scrollAmount, animated});
164+
if (scrollAmount !== 0) {
165+
// @ts-expect-error
166+
list?.current?.scrollToOffset({offset: scrollAmount, animated});
167+
}
169168
};
170169

171170
const scrollToMonth = useCallback((date: XDate | string) => {
172171
const scrollTo = parseDate(date);
173172
const diffMonths = Math.round(initialDate?.current?.clone().setDate(1).diffMonths(scrollTo?.clone().setDate(1)));
174-
if (diffMonths !== 0) {
175-
const scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize;
176-
173+
const scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize;
174+
175+
if (scrollAmount !== 0) {
177176
// @ts-expect-error
178177
list?.current?.scrollToOffset({offset: scrollAmount, animated: animateScroll});
179178
}
@@ -186,7 +185,7 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
186185
}
187186
scrollToMonth(day);
188187
setCurrentMonth(day);
189-
}, [currentMonth]);
188+
}, [currentMonth, scrollToMonth]);
190189

191190
const getMarkedDatesForItem = useCallback((item?: XDate) => {
192191
if (markedDates && item) {

0 commit comments

Comments
 (0)