Skip to content

Commit 0f503e0

Browse files
authored
fix(infiniteAgendaList): fixes (#2311)
* Fix _onMomentumScrollEnd not called after first scroll, prevent scrollToIndex if same date as current * Fix _onMomentumScrollEnd not called after first scroll, prevent scrollToIndex if same date as current * Fix _onMomentumScrollEnd not called after first scroll, prevent scrollToIndex if same date as current * Revert "Allow passing listStyle to the infiniteAgendaList to allow control the RecyclerListView style (#2286)" This reverts commit c4bd90f. * Prevent jump on onEndReach
1 parent c4bd90f commit 0f503e0

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/expandableCalendar/agendaList.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import {
2121
NativeScrollEvent,
2222
LayoutChangeEvent,
2323
ViewToken,
24-
TextProps,
25-
StyleProp
24+
TextProps
2625
} from 'react-native';
2726

2827
import {useDidUpdate} from '../hooks';
@@ -70,7 +69,6 @@ export interface AgendaListProps extends SectionListProps<any, DefaultSectionT>
7069
titleHeight?: number;
7170
visibleIndicesChangedDebounce?: number;
7271
renderFooter?: () => React.ReactElement | null;
73-
style?: StyleProp<ViewStyle>;
7472
};
7573
}
7674

src/expandableCalendar/infiniteAgendaList.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
22

33
import isUndefined from 'lodash/isUndefined';
44
import debounce from 'lodash/debounce';
5-
import InfiniteList, {InfiniteListProps} from '../infinite-list';
5+
import InfiniteList from '../infinite-list';
66

77
import XDate from 'xdate';
88

@@ -58,12 +58,14 @@ const InfiniteAgendaList = (props: AgendaListProps) => {
5858
const didScroll = useRef(false);
5959
const sectionScroll = useRef(false);
6060
const [data, setData] = useState([] as any[]);
61+
const dataRef = useRef(data);
6162

6263
useEffect(() => {
6364
const items = sections.reduce((acc: any, cur: any) => {
6465
return [...acc, {title: cur.title, isTitle: true}, ...cur.data];
6566
}, []);
6667
setData(items);
68+
dataRef.current = items;
6769

6870
if (date !== _topSection.current) {
6971
setTimeout(() => {
@@ -128,29 +130,34 @@ const InfiniteAgendaList = (props: AgendaListProps) => {
128130
return sectionTitle;
129131
}, []);
130132

131-
const scrollToSection = useCallback(debounce((d) => {
132-
const sectionIndex = scrollToNextEvent ? getNextSectionIndex(d) : getSectionIndex(d);
133+
const scrollToSection = useCallback(debounce((requestedDate) => {
134+
const sectionIndex = scrollToNextEvent ? getNextSectionIndex(requestedDate) : getSectionIndex(requestedDate);
133135
if (isUndefined(sectionIndex)) {
134136
return;
135137
}
136138

137139
if (list?.current && sectionIndex !== undefined) {
138140
sectionScroll.current = true; // to avoid setDate() in _onVisibleIndicesChanged
139-
_topSection.current = sections[findItemTitleIndex(sectionIndex)]?.title;
141+
if (requestedDate !== _topSection.current) {
142+
_topSection.current = sections[findItemTitleIndex(sectionIndex)]?.title;
143+
list.current?.scrollToIndex(sectionIndex, true);
144+
}
140145

141-
list.current?.scrollToIndex(sectionIndex, true);
146+
setTimeout(() => {
147+
_onMomentumScrollEnd(); // the RecyclerListView doesn't trigger onMomentumScrollEnd when calling scrollToSection
148+
}, 500);
142149
}
143-
}, 1000, {leading: false, trailing: true}), [ sections]);
150+
}, 1000, {leading: false, trailing: true}), [sections]);
144151

145152
const layoutProvider = useMemo(
146153
() => new LayoutProvider(
147-
(index) => data[index]?.isTitle ? 'title': 'page',
154+
(index) => dataRef.current[index]?.isTitle ? 'title': 'page',
148155
(type, dim) => {
149156
dim.width = constants.screenWidth;
150157
dim.height = type === 'title' ? infiniteListProps?.titleHeight ?? 60 : infiniteListProps?.itemHeight ?? 80;
151158
}
152159
),
153-
[data]
160+
[]
154161
);
155162

156163
const _onScroll = useCallback((rawEvent: any) => {
@@ -233,7 +240,7 @@ const InfiniteAgendaList = (props: AgendaListProps) => {
233240
ref={list}
234241
renderItem={_renderItem}
235242
data={data}
236-
style={infiniteListProps?.style as InfiniteListProps['style']}
243+
style={style.current.container}
237244
layoutProvider={layoutProvider}
238245
onScroll={_onScroll}
239246
onVisibleIndicesChanged={_onVisibleIndicesChanged}

src/infinite-list/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const InfiniteList = (props: InfiniteListProps, ref: any) => {
5353
onScroll,
5454
onEndReached,
5555
renderFooter,
56-
style,
5756
} = props;
5857

5958
const dataProvider = useMemo(() => {
@@ -161,9 +160,9 @@ const InfiniteList = (props: InfiniteListProps, ref: any) => {
161160
};
162161
}, [onScrollBeginDrag, onMomentumScrollEnd, scrollViewProps, isHorizontal]);
163162

164-
const _style = useMemo(() => {
165-
return [{height: pageHeight}, style];
166-
}, [pageHeight, style]);
163+
const style = useMemo(() => {
164+
return {height: pageHeight};
165+
}, [pageHeight]);
167166

168167
return (
169168
<RecyclerListView
@@ -177,7 +176,7 @@ const InfiniteList = (props: InfiniteListProps, ref: any) => {
177176
initialRenderIndex={initialPageIndex}
178177
renderAheadOffset={5 * pageWidth}
179178
onScroll={_onScroll}
180-
style={_style}
179+
style={style}
181180
scrollViewProps={scrollViewPropsMemo}
182181
onEndReached={onEndReached}
183182
onEndReachedThreshold={onEndReachedThreshold}

0 commit comments

Comments
 (0)