@@ -2,11 +2,11 @@ import findIndex from 'lodash/findIndex';
22import PropTypes from 'prop-types' ;
33import XDate from 'xdate' ;
44
5- import React , { forwardRef , useImperativeHandle , useRef , useEffect , useState , useCallback , useMemo } from 'react' ;
6- import { FlatList , View , ViewStyle , FlatListProps } from 'react-native' ;
5+ import React , { forwardRef , useCallback , useEffect , useImperativeHandle , useMemo , useRef , useState } from 'react' ;
6+ import { FlatList , FlatListProps , View , ViewStyle } from 'react-native' ;
77
8- import { extractHeaderProps , extractCalendarProps } from '../componentUpdater' ;
9- import { xdateToData , parseDate , toMarkingFormat } from '../interface' ;
8+ import { extractCalendarProps , extractHeaderProps } from '../componentUpdater' ;
9+ import { parseDate , toMarkingFormat , xdateToData } from '../interface' ;
1010import { page , sameDate , sameMonth } from '../dateutils' ;
1111import constants from '../commons/constants' ;
1212import { useDidUpdate } from '../hooks' ;
@@ -15,6 +15,7 @@ import styleConstructor from './style';
1515import Calendar , { CalendarProps } from '../calendar' ;
1616import CalendarListItem from './item' ;
1717import CalendarHeader from '../calendar/header/index' ;
18+ import isEqual from 'lodash/isEqual' ;
1819
1920const CALENDAR_WIDTH = constants . screenWidth ;
2021const CALENDAR_HEIGHT = 360 ;
@@ -108,13 +109,15 @@ const CalendarList = (props: CalendarListProps & ContextProp, ref: any) => {
108109
109110 const [ currentMonth , setCurrentMonth ] = useState ( parseDate ( current ) ) ;
110111
112+ const shouldUseAndroidRTLFix = useMemo ( ( ) => constants . isAndroidRTL && horizontal , [ horizontal ] ) ;
113+
111114 const style = useRef ( styleConstructor ( theme ) ) ;
112115 const list = useRef ( ) ;
113116 const range = useRef ( horizontal ? 1 : 3 ) ;
114117 const initialDate = useRef ( parseDate ( current ) || new XDate ( ) ) ;
115118 const visibleMonth = useRef ( currentMonth ) ;
116119
117- const items = useMemo ( ( ) => {
120+ const items : XDate [ ] = useMemo ( ( ) => {
118121 const months : any [ ] = [ ] ;
119122 for ( let i = 0 ; i <= pastScrollRange + futureScrollRange ; i ++ ) {
120123 const rangeDate = initialDate . current ?. clone ( ) . addMonths ( i - pastScrollRange , true ) ;
@@ -184,13 +187,13 @@ const CalendarList = (props: CalendarListProps & ContextProp, ref: any) => {
184187 const scrollToMonth = useCallback ( ( date : XDate | string ) => {
185188 const scrollTo = parseDate ( date ) ;
186189 const diffMonths = Math . round ( initialDate ?. current ?. clone ( ) . setDate ( 1 ) . diffMonths ( scrollTo ?. clone ( ) . setDate ( 1 ) ) ) ;
187- const scrollAmount = calendarSize * pastScrollRange + diffMonths * calendarSize ;
190+ const scrollAmount = calendarSize * ( shouldUseAndroidRTLFix ? pastScrollRange - diffMonths : pastScrollRange + diffMonths ) ;
188191
189192 if ( scrollAmount !== 0 ) {
190193 // @ts -expect-error
191194 list ?. current ?. scrollToOffset ( { offset : scrollAmount , animated : animateScroll } ) ;
192195 }
193- } , [ calendarSize ] ) ;
196+ } , [ calendarSize , shouldUseAndroidRTLFix , pastScrollRange , animateScroll ] ) ;
194197
195198 const addMonth = useCallback ( ( count : number ) => {
196199 const day = currentMonth ?. clone ( ) . addMonths ( count , true ) ;
@@ -274,24 +277,32 @@ const CalendarList = (props: CalendarListProps & ContextProp, ref: any) => {
274277
275278 const onViewableItemsChanged = useCallback ( ( { viewableItems} : any ) => {
276279 const newVisibleMonth = parseDate ( viewableItems [ 0 ] ?. item ) ;
277- if ( ! sameDate ( visibleMonth ?. current , newVisibleMonth ) ) {
278- visibleMonth . current = newVisibleMonth ;
280+ if ( shouldUseAndroidRTLFix ) {
281+ const centerIndex = items . findIndex ( ( item ) => isEqual ( parseDate ( current ) , item ) ) ;
282+ const adjustedOffset = centerIndex - items . findIndex ( ( item ) => isEqual ( newVisibleMonth , item ) ) ;
283+ visibleMonth . current = items [ centerIndex + adjustedOffset ] ;
279284 setCurrentMonth ( visibleMonth . current ) ;
285+ } else {
286+ if ( ! sameDate ( visibleMonth ?. current , newVisibleMonth ) ) {
287+ visibleMonth . current = newVisibleMonth ;
288+ setCurrentMonth ( visibleMonth . current ) ;
289+ }
280290 }
281- } , [ ] ) ;
291+ } , [ items , shouldUseAndroidRTLFix , current ] ) ;
282292
283293 const viewabilityConfigCallbackPairs = useRef ( [
284294 {
285295 viewabilityConfig : viewabilityConfig . current ,
286296 onViewableItemsChanged
287297 } ,
288298 ] ) ;
289-
299+
290300 return (
291301 < View style = { style . current . flatListContainer } testID = { testID } >
292302 < FlatList
293303 // @ts -expect-error
294304 ref = { list }
305+ windowSize = { shouldUseAndroidRTLFix ? pastScrollRange + futureScrollRange + 1 : undefined }
295306 style = { listStyle }
296307 showsVerticalScrollIndicator = { showScrollIndicator }
297308 showsHorizontalScrollIndicator = { showScrollIndicator }
0 commit comments