diff --git a/packages/react-native-select-pro/src/components/flat-options-list/flat-options-list.tsx b/packages/react-native-select-pro/src/components/flat-options-list/flat-options-list.tsx index 0f8b0969..ec3f7357 100644 --- a/packages/react-native-select-pro/src/components/flat-options-list/flat-options-list.tsx +++ b/packages/react-native-select-pro/src/components/flat-options-list/flat-options-list.tsx @@ -1,7 +1,8 @@ -import React, { memo } from 'react'; +import React, { memo, useRef } from 'react'; import isEqual from 'react-fast-compare'; import { FlatList } from 'react-native'; +import { ERRORS, logError } from '../../helpers'; import { NoOptions } from '../no-options'; import type { FlatOptionsListProps } from './flat-options-list.types'; @@ -16,6 +17,21 @@ export const FlatOptionsList = memo( accessibilityState, disabled, }: FlatOptionsListProps) => { + const flatListRef = useRef(null); + + const scrollToIndex = () => { + if (flatListRef.current) { + try { + flatListRef.current.scrollToIndex({ + animated: false, + index: initialScrollIndex === -1 ? 0 : initialScrollIndex, + }); + } catch { + logError(ERRORS.SCROLL_TO_LOCATION); + } + } + }; + return ( } - initialScrollIndex={initialScrollIndex} scrollEnabled={!disabled} {...flatListProps} data={resolvedData} getItemLayout={getItemLayout} renderItem={renderItem} keyExtractor={({ value }) => value} + onLayout={scrollToIndex} /> ); },