@@ -33,7 +33,6 @@ import { NothingFound } from './NothingFound'
33
33
import { RightButton } from './RightButton'
34
34
import { ScrollViewListItem } from './ScrollViewListItem'
35
35
import { AutocompleteDropdownContext , AutocompleteDropdownContextProvider } from './AutocompleteDropdownContext'
36
- import { useKeyboardHeight } from './useKeyboardHeight'
37
36
import diacriticless from './diacriticless'
38
37
import { theme } from './theme'
39
38
import type { IAutocompleteDropdownProps , AutocompleteDropdownItem } from './types'
@@ -99,7 +98,6 @@ export const AutocompleteDropdown = memo<
99
98
const initialValueRef = useRef ( initialValueProp )
100
99
const [ dataSet , setDataSet ] = useState ( dataSetProp )
101
100
const matchFromStart = matchFrom === 'start' ? true : false
102
- const kbHeight = useKeyboardHeight ( )
103
101
const {
104
102
content,
105
103
setContent,
@@ -116,17 +114,28 @@ export const AutocompleteDropdown = memo<
116
114
setLoading ( loadingProp )
117
115
} , [ loadingProp ] )
118
116
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
+ )
123
122
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
+ )
130
139
131
140
const onClearPress = useCallback ( ( ) => {
132
141
setSearchText ( '' )
@@ -149,23 +158,20 @@ export const AutocompleteDropdown = memo<
149
158
inputRef . current ?. blur ( )
150
159
} , [ ] )
151
160
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
+ }
164
168
165
- setTimeout ( ( ) => {
166
- setIsOpened ( true )
167
- } , 0 )
168
- } , [ calculateDirection , directionProp , setDirection ] )
169
+ setTimeout ( ( ) => {
170
+ setIsOpened ( true )
171
+ } , 0 )
172
+ } ,
173
+ [ calculateDirection , directionProp , setDirection ] ,
174
+ )
169
175
170
176
const toggle = useCallback ( ( ) => {
171
177
isOpened ? close ( ) : open ( )
@@ -392,7 +398,7 @@ export const AutocompleteDropdown = memo<
392
398
if ( typeof onFocusProp === 'function' ) {
393
399
onFocusProp ( e )
394
400
}
395
- open ( )
401
+ open ( { focused : true } )
396
402
} ,
397
403
[ clearOnFocus , onFocusProp , open ] ,
398
404
)
0 commit comments