Skip to content

Commit 156ebdd

Browse files
committed
Added forwardRef to AgendaList (#2588)
* Refactor AgendaList to use forwardRef for improved component forwarding * fix(AgendaList): useCombinedRefs for improved ref handling * fix(AgendaList): add TypeScript error comment for ref typings * fix(AgendaList): update sectionStyle prop type to include number and add TypeScript error comment
1 parent 20d89d0 commit 156ebdd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/expandableCalendar/agendaList.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import debounce from 'lodash/debounce';
88

99
import XDate from 'xdate';
1010

11-
import React, {useCallback, useContext, useEffect, useMemo, useRef} from 'react';
11+
import React, {forwardRef, useCallback, useContext, useEffect, useMemo, useRef} from 'react';
1212
import {
1313
SectionList,
1414
DefaultSectionT,
@@ -19,7 +19,7 @@ import {
1919
ViewToken,
2020
} from 'react-native';
2121

22-
import {useDidUpdate} from '../hooks';
22+
import {useDidUpdate, useCombinedRefs} from '../hooks';
2323
import {getMoment} from '../momentResolver';
2424
import {isToday, isGTE, sameDate} from '../dateutils';
2525
import {parseDate} from '../interface';
@@ -41,7 +41,7 @@ const viewabilityConfig = {
4141
* @extends: SectionList
4242
* @example: https://github.com/wix/react-native-calendars/blob/master/example/src/screens/expandableCalendar.js
4343
*/
44-
const AgendaList = (props: AgendaListProps) => {
44+
const AgendaList = forwardRef((props: AgendaListProps, ref: any) => {
4545
if (props.infiniteListProps) {
4646
return <InfiniteAgendaList {...props} />;
4747
}
@@ -69,7 +69,7 @@ const AgendaList = (props: AgendaListProps) => {
6969
const {date, updateSource, setDate, setDisabled} = useContext(Context);
7070

7171
const style = useRef(styleConstructor(theme));
72-
const list = useRef<any>();
72+
const list = useCombinedRefs(ref);
7373
const _topSection = useRef(sections[0]?.title);
7474
const didScroll = useRef(false);
7575
const sectionScroll = useRef(false);
@@ -151,6 +151,7 @@ const AgendaList = (props: AgendaListProps) => {
151151
sectionScroll.current = true; // to avoid setDate() in onViewableItemsChanged
152152
_topSection.current = sections[sectionIndex]?.title;
153153

154+
// @ts-expect-error should be fixed when we fix the typings of the ref.
154155
list?.current.scrollToLocation({
155156
animated: true,
156157
sectionIndex: sectionIndex,
@@ -228,6 +229,7 @@ const AgendaList = (props: AgendaListProps) => {
228229
<SectionList
229230
stickySectionHeadersEnabled
230231
{...props}
232+
// @ts-expect-error
231233
ref={list}
232234
keyExtractor={_keyExtractor}
233235
showsVerticalScrollIndicator={false}
@@ -245,7 +247,7 @@ const AgendaList = (props: AgendaListProps) => {
245247
// _getItemLayout = (data, index) => {
246248
// return {length: constants.screenWidth, offset: constants.screenWidth * index, index};
247249
// }
248-
};
250+
});
249251

250252
export default AgendaList;
251253

@@ -255,6 +257,7 @@ AgendaList.propTypes = {
255257
dayFormatter: PropTypes.func,
256258
useMoment: PropTypes.bool,
257259
markToday: PropTypes.bool,
260+
// @ts-expect-error TODO Figure out why forwardRef causes error about the number type
258261
sectionStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
259262
avoidDateUpdates: PropTypes.bool
260263
};

0 commit comments

Comments
 (0)