From dc550c8c91fa6b1dc507cdad619076a630cdd325 Mon Sep 17 00:00:00 2001 From: Efstathios Ntonas Date: Thu, 23 Nov 2023 13:52:20 +0200 Subject: [PATCH] fix: FlatList initialScrollIndex (#233) Co-authored-by: Efstathios Ntonas --- .../flat-options-list/flat-options-list.tsx | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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} /> ); },