@@ -33,7 +33,6 @@ import { NothingFound } from './NothingFound'
3333import { RightButton } from './RightButton'
3434import { ScrollViewListItem } from './ScrollViewListItem'
3535import { AutocompleteDropdownContext , AutocompleteDropdownContextProvider } from './AutocompleteDropdownContext'
36- import { useKeyboardHeight } from './useKeyboardHeight'
3736import diacriticless from './diacriticless'
3837import { theme } from './theme'
3938import type { IAutocompleteDropdownProps , AutocompleteDropdownItem } from './types'
@@ -99,7 +98,6 @@ export const AutocompleteDropdown = memo<
9998 const initialValueRef = useRef ( initialValueProp )
10099 const [ dataSet , setDataSet ] = useState ( dataSetProp )
101100 const matchFromStart = matchFrom === 'start' ? true : false
102- const kbHeight = useKeyboardHeight ( )
103101 const {
104102 content,
105103 setContent,
@@ -116,17 +114,28 @@ export const AutocompleteDropdown = memo<
116114 setLoading ( loadingProp )
117115 } , [ loadingProp ] )
118116
119- const calculateDirection = useCallback ( async ( ) => {
120- const [ , positionY ] = await new Promise < [ x : number , y : number , width : number , height : number ] > ( resolve =>
121- containerRef . current ?. measureInWindow ( ( ...rect ) => resolve ( rect ) ) ,
122- )
117+ const calculateDirection = useCallback (
118+ async ( { waitForKeyboard } : { waitForKeyboard : boolean } ) => {
119+ const [ , positionY ] = await new Promise < [ x : number , y : number , width : number , height : number ] > ( resolve =>
120+ containerRef . current ?. measureInWindow ( ( ...rect ) => resolve ( rect ) ) ,
121+ )
123122
124- const screenHeight = Dimensions . get ( 'window' ) . height
125- setDirection ( ( screenHeight - kbHeight ) / 2 > positionY ? 'down' : 'up' )
126- return new Promise < void > ( resolve => {
127- setTimeout ( resolve , 1 )
128- } )
129- } , [ kbHeight , setDirection ] )
123+ return new Promise < void > ( resolve => {
124+ setTimeout (
125+ ( ) => {
126+ Keyboard . isVisible ( )
127+ const kbHeight = Keyboard . metrics ( ) ?. height || 0
128+ console . log ( { kbHeight } )
129+ const screenHeight = Dimensions . get ( 'window' ) . height
130+ setDirection ( ( screenHeight - kbHeight ) / 2 > positionY ? 'down' : 'up' )
131+ resolve ( )
132+ } ,
133+ waitForKeyboard ? Platform . select ( { ios : 600 , android : 150 , default : 1 } ) : 1 , // wait for keyboard to show
134+ )
135+ } )
136+ } ,
137+ [ setDirection ] ,
138+ )
130139
131140 const onClearPress = useCallback ( ( ) => {
132141 setSearchText ( '' )
@@ -149,23 +158,20 @@ export const AutocompleteDropdown = memo<
149158 inputRef . current ?. blur ( )
150159 } , [ ] )
151160
152- // useEffect(() => {
153- // if (kbHeight && !direction) {
154- // calculateDirection()
155- // }
156- // }, [kbHeight, direction])
157-
158- const open = useCallback ( async ( ) => {
159- if ( directionProp ) {
160- setDirection ( directionProp )
161- } else {
162- await calculateDirection ( )
163- }
161+ const open = useCallback (
162+ async ( { focused } = { focused : false } ) => {
163+ if ( directionProp ) {
164+ setDirection ( directionProp )
165+ } else {
166+ await calculateDirection ( { waitForKeyboard : focused } )
167+ }
164168
165- setTimeout ( ( ) => {
166- setIsOpened ( true )
167- } , 0 )
168- } , [ calculateDirection , directionProp , setDirection ] )
169+ setTimeout ( ( ) => {
170+ setIsOpened ( true )
171+ } , 0 )
172+ } ,
173+ [ calculateDirection , directionProp , setDirection ] ,
174+ )
169175
170176 const toggle = useCallback ( ( ) => {
171177 isOpened ? close ( ) : open ( )
@@ -392,7 +398,7 @@ export const AutocompleteDropdown = memo<
392398 if ( typeof onFocusProp === 'function' ) {
393399 onFocusProp ( e )
394400 }
395- open ( )
401+ open ( { focused : true } )
396402 } ,
397403 [ clearOnFocus , onFocusProp , open ] ,
398404 )
0 commit comments