@@ -38,12 +38,12 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
38
38
const [ show , setShow ] = useState ( false )
39
39
const [ dropdownHeight , setDropdownHeight ] = useState ( 0 )
40
40
const [ inputMeasurements , setInputMeasurements ] = useState <
41
- { x : number ; y : number ; width : number ; height : number } | undefined
41
+ { x : number ; topY : number ; bottomY : number ; width : number ; height : number } | undefined
42
42
> ( )
43
43
const [ opacity , setOpacity ] = useState ( 0 )
44
- const [ contentStyles , setContentStyles ] = useState < { top : number ; left : number ; width ?: number } | undefined > (
45
- undefined ,
46
- )
44
+ const [ contentStyles , setContentStyles ] = useState <
45
+ { top ?: number ; left : number ; width ?: number ; bottom ?: number } | undefined
46
+ > ( undefined )
47
47
const activeInputContainerRef = useRef < View > ( null )
48
48
const wrapperRef = useRef < View > ( null )
49
49
const activeControllerRef = useRef < IAutocompleteDropdownRef | null > ( null )
@@ -58,58 +58,58 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
58
58
59
59
if ( dropdownHeight && direction === 'up' ) {
60
60
setContentStyles ( {
61
- top : inputMeasurements . y - dropdownHeight - 10 - headerOffset ,
61
+ bottom : inputMeasurements . bottomY + 5 + headerOffset ,
62
+ top : undefined ,
62
63
left : inputMeasurements . x ,
63
64
width : inputMeasurements . width ,
64
65
} )
65
66
setOpacity ( 1 )
66
67
} else if ( direction === 'down' ) {
67
68
setContentStyles ( {
68
- top : inputMeasurements . y + inputMeasurements . height + 5 + headerOffset ,
69
+ top : inputMeasurements . topY + inputMeasurements . height + 5 + headerOffset ,
70
+ bottom : undefined ,
69
71
left : inputMeasurements . x ,
70
72
width : inputMeasurements . width ,
71
73
} )
72
74
setOpacity ( 1 )
73
75
}
74
- } , [
75
- direction ,
76
- dropdownHeight ,
77
- headerOffset ,
78
- inputMeasurements ?. height ,
79
- inputMeasurements ?. width ,
80
- inputMeasurements ?. x ,
81
- inputMeasurements ?. y ,
82
- ] )
76
+ } , [ direction , dropdownHeight , headerOffset , inputMeasurements ] )
77
+
78
+ const recalculatePosition = useCallback ( ( showAfterCalculation = false ) => {
79
+ activeInputContainerRef ?. current ?. measure ( ( x , y , width , height , inputPageX , inputPageY ) => {
80
+ wrapperRef . current ?. measure ( ( wrapperX , wrapperY , wrapperW , wrapperH , wrapperPageX , wrapperPageY ) => {
81
+ const currentMeasurement = {
82
+ width,
83
+ height,
84
+ x : inputPageX ,
85
+ topY : inputPageY - wrapperPageY ,
86
+ bottomY : wrapperH - inputPageY + wrapperPageY ,
87
+ }
88
+ setInputMeasurements ( prev =>
89
+ JSON . stringify ( prev ) === JSON . stringify ( currentMeasurement ) ? prev : currentMeasurement ,
90
+ )
91
+ showAfterCalculation && setShow ( true )
92
+ } )
93
+ } )
94
+ } , [ ] )
83
95
84
96
useEffect ( ( ) => {
85
97
if ( content ) {
86
- activeInputContainerRef ?. current ?. measure ( ( x , y , width , height , pageX , pageY ) => {
87
- wrapperRef . current ?. measure ( ( wrapperX , wrapperY , wrapperW , wrapperH , wrapperPageX , wrapperPageY ) => {
88
- setInputMeasurements ( { x : pageX , y : pageY - wrapperPageY , width, height } )
89
- setShow ( true )
90
- } )
91
- } )
98
+ recalculatePosition ( true )
92
99
} else {
93
100
setInputMeasurements ( undefined )
94
101
setDropdownHeight ( 0 )
95
102
setOpacity ( 0 )
96
103
setContentStyles ( undefined )
97
104
setShow ( false )
98
105
}
99
- } , [ content ] )
106
+ } , [ content , recalculatePosition ] )
100
107
101
108
useEffect ( ( ) => {
102
109
if ( show && ! ! opacity ) {
103
110
positionTrackingIntervalRef . current = setInterval ( ( ) => {
104
111
requestAnimationFrame ( ( ) => {
105
- activeInputContainerRef ?. current ?. measure ( ( _x , _y , width , height , inputPageX , inputPageY ) => {
106
- wrapperRef . current ?. measure ( ( wrapperX , wrapperY , wrapperW , wrapperH , wrapperPageX , wrapperPageY ) => {
107
- const currentMeasurement = { x : inputPageX , y : inputPageY - wrapperPageY , width, height }
108
- setInputMeasurements ( prev =>
109
- JSON . stringify ( prev ) === JSON . stringify ( currentMeasurement ) ? prev : currentMeasurement ,
110
- )
111
- } )
112
- } )
112
+ recalculatePosition ( )
113
113
} )
114
114
} , 16 )
115
115
} else {
@@ -119,7 +119,7 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
119
119
return ( ) => {
120
120
clearInterval ( positionTrackingIntervalRef . current )
121
121
}
122
- } , [ opacity , show ] )
122
+ } , [ recalculatePosition , opacity , show ] )
123
123
124
124
const onLayout : ViewProps [ 'onLayout' ] = useCallback ( ( e : LayoutChangeEvent ) => {
125
125
setDropdownHeight ( e . nativeEvent . layout . height )
0 commit comments