diff --git a/src/components/Dropdown/index.tsx b/src/components/Dropdown/index.tsx index eef6ed8..a0fd43d 100644 --- a/src/components/Dropdown/index.tsx +++ b/src/components/Dropdown/index.tsx @@ -18,13 +18,13 @@ import { Keyboard, KeyboardEvent, Modal, + StatusBar, StyleSheet, Text, TouchableHighlight, TouchableWithoutFeedback, View, ViewStyle, - StatusBar, } from 'react-native'; import { useDetectDevice } from '../../toolkits'; import { useDeviceOrientation } from '../../useDeviceOrientation'; @@ -225,22 +225,22 @@ const DropdownComponent: ( }, [_measure]); useEffect(() => { - const susbcriptionKeyboardDidShow = Keyboard.addListener( + const subscriptionKeyboardDidShow = Keyboard.addListener( 'keyboardDidShow', onKeyboardDidShow ); - const susbcriptionKeyboardDidHide = Keyboard.addListener( + const subscriptionKeyboardDidHide = Keyboard.addListener( 'keyboardDidHide', onKeyboardDidHide ); return () => { - if (typeof susbcriptionKeyboardDidShow?.remove === 'function') { - susbcriptionKeyboardDidShow.remove(); + if (typeof subscriptionKeyboardDidShow?.remove === 'function') { + subscriptionKeyboardDidShow.remove(); } - if (typeof susbcriptionKeyboardDidHide?.remove === 'function') { - susbcriptionKeyboardDidHide.remove(); + if (typeof subscriptionKeyboardDidHide?.remove === 'function') { + subscriptionKeyboardDidHide.remove(); } }; }, [onKeyboardDidHide, onKeyboardDidShow]); @@ -265,24 +265,35 @@ const DropdownComponent: ( }, [value, data, getValue]); const scrollIndex = useCallback(() => { - if (autoScroll && data.length > 0 && listData.length === data.length) { + if (autoScroll && data.length > 0 && listData?.length === data?.length) { setTimeout(() => { if (refList && refList?.current) { const defaultValue = typeof value === 'object' ? _.get(value, valueField) : value; - const index = _.findIndex(listData, (e: any) => + const index = _.findIndex(listData, (e) => _.isEqual(defaultValue, _.get(e, valueField)) ); + if ( - listData.length > 0 && index > -1 && + !_.isEmpty(listData.length) && index <= listData.length - 1 ) { - refList?.current?.scrollToIndex({ - index: index, - animated: false, - }); + try { + refList.current.scrollToIndex({ + index: index, + animated: false, + }); + } catch (error) { + console.warn(`scrollToIndex error: ${error}`); + } + } else { + console.warn( + `scrollToIndex out of range: requested index ${index} is out of 0 to ${ + listData.length - 1 + }` + ); } } }, 200);