Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CalendarList - fix - update months doesn't apply to the initial month #1995

Merged
merged 3 commits into from
Aug 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/calendar-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
const style = useRef(styleConstructor(theme));
const list = useRef();
const range = useRef(horizontal ? 1 : 3);
const initialDate = useRef(parseDate(current));
const initialDate = useRef(parseDate(current) || new XDate());
const visibleMonth = useRef(currentMonth);

const items = useMemo(() => {
const months = [];
const date: XDate = initialDate?.current || new XDate();
for (let i = 0; i <= pastScrollRange + futureScrollRange; i++) {
const rangeDate = date.clone().addMonths(i - pastScrollRange, true);
const rangeDate = initialDate.current?.clone().addMonths(i - pastScrollRange, true);
months.push(rangeDate);
}
return months;
Expand All @@ -128,9 +127,8 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
}, [propsStyle]);

const initialDateIndex = useMemo(() => {
const date: XDate = initialDate?.current || new XDate();
return findIndex(items, function(item) {
return item.toString() === date.toString();
return item.toString() === initialDate.current?.toString();
});
}, [items]);

Expand All @@ -149,7 +147,6 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
const scrollToDay = (date: XDate | string, offset: number, animated: boolean) => {
const scrollTo = parseDate(date);
const diffMonths = Math.round(initialDate?.current?.clone().setDate(1).diffMonths(scrollTo?.clone().setDate(1)));

let scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize + (offset || 0);

if (!horizontal) {
Expand All @@ -171,12 +168,10 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
const scrollToMonth = useCallback((date: XDate | string) => {
const scrollTo = parseDate(date);
const diffMonths = Math.round(initialDate?.current?.clone().setDate(1).diffMonths(scrollTo?.clone().setDate(1)));
if (diffMonths !== 0) {
const scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize;

// @ts-expect-error
list?.current?.scrollToOffset({offset: scrollAmount, animated: animateScroll});
}
const scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize;
ethanshar marked this conversation as resolved.
Show resolved Hide resolved

// @ts-expect-error
list?.current?.scrollToOffset({offset: scrollAmount, animated: animateScroll});
}, [calendarSize]);

const addMonth = useCallback((count: number) => {
Expand All @@ -186,7 +181,7 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
}
scrollToMonth(day);
setCurrentMonth(day);
}, [currentMonth]);
}, [currentMonth, scrollToMonth]);

const getMarkedDatesForItem = useCallback((item?: XDate) => {
if (markedDates && item) {
Expand Down