import { useTheme } from '@emotion/react'; import React, { useState } from 'react'; import type { SectionList, SectionListProps } from 'react-native'; import { Dimensions, View } from 'react-native'; import SectionHeading from '../SectionHeading'; import Spinner from '../Spinner'; import { OptionSpacer, SectionSpacer, StyledSectionList } from './StyledSelect'; import type { SelectOptionType, SelectProps, SectionType } from './types'; export type BaseOptionListProps> = Pick< SelectProps, 'keyExtractor' | 'loading' | 'onEndReached' | 'onQueryChange' > & { sectionListRef?: React.RefObject | null>; } & SectionListProps; const BaseOptionList = >({ keyExtractor, loading, onEndReached, onQueryChange, sections, renderItem, sectionListRef, ...rest }: BaseOptionListProps) => { const theme = useTheme(); const [onEndReachedCalled, setOnEndReachedCalled] = useState(false); return ( ref={sectionListRef} style={{ ...(onQueryChange ? { height: Dimensions.get('screen').height } : {}), }} sections={sections} keyExtractor={keyExtractor} onEndReachedThreshold={0.1} onEndReached={() => setOnEndReachedCalled(true)} onMomentumScrollBegin={() => { if (onEndReached && onEndReachedCalled && !loading) onEndReached(); setOnEndReachedCalled(false); }} ListFooterComponent={ loading ? ( ) : null } renderSectionHeader={({ section: { category } }) => category !== '' ? ( ) : null } renderItem={renderItem} ItemSeparatorComponent={OptionSpacer} SectionSeparatorComponent={SectionSpacer} {...rest} /> ); }; export default BaseOptionList;