1
- import React , { PureComponent } from 'react' ;
1
+ import React , { PureComponent , useEffect } from 'react' ;
2
2
import {
3
3
Dimensions ,
4
4
Keyboard ,
@@ -16,6 +16,12 @@ import { defaultStyles } from './styles';
16
16
import { PickerAvoidingView } from './PickerAvoidingView' ;
17
17
import { PickerStateContext , PickerStateProvider } from './PickerStateProvider' ;
18
18
import { IOS_MODAL_ANIMATION_DURATION_MS , IOS_MODAL_HEIGHT } from './constants' ;
19
+ import { schedule } from './PickerAvoidingView/utils' ;
20
+
21
+ function EffectRunner ( { effect, children } ) {
22
+ useEffect ( effect ?? ( ( ) => { } ) , [ effect ] ) ;
23
+ return children ;
24
+ }
19
25
20
26
export default class RNPickerSelect extends PureComponent {
21
27
static contextType = PickerStateContext ;
@@ -248,13 +254,19 @@ export default class RNPickerSelect extends PureComponent {
248
254
249
255
// If TextInput is below picker modal, scroll up
250
256
if ( textInputBottomY > modalY ) {
257
+ const scrollView = this . props . scrollViewRef . current ;
258
+ const scrollViewContentOffsetY = this . props . scrollViewContentOffsetY ;
259
+
251
260
// Wait until the modal animation finishes, so the scrolling is effective when PickerAvoidingView is
252
261
// used
253
- setTimeout ( ( ) => {
254
- this . props . scrollViewRef . current . scrollTo ( {
255
- y : textInputBottomY - modalY + 10 + this . props . scrollViewContentOffsetY ,
256
- } ) ;
257
- } , IOS_MODAL_ANIMATION_DURATION_MS + 50 ) ;
262
+ this . setState ( {
263
+ scrollToInputEffect : ( ) =>
264
+ schedule ( ( ) => {
265
+ scrollView . scrollTo ( {
266
+ y : textInputBottomY - modalY + 10 + scrollViewContentOffsetY ,
267
+ } ) ;
268
+ } , IOS_MODAL_ANIMATION_DURATION_MS + 50 ) ,
269
+ } ) ;
258
270
}
259
271
} ) ;
260
272
}
@@ -615,7 +627,7 @@ export default class RNPickerSelect extends PureComponent {
615
627
) ;
616
628
}
617
629
618
- render ( ) {
630
+ renderForPlatform ( ) {
619
631
const { children, useNativeAndroidPickerStyle } = this . props ;
620
632
621
633
if ( Platform . OS === 'ios' ) {
@@ -632,6 +644,14 @@ export default class RNPickerSelect extends PureComponent {
632
644
633
645
return this . renderAndroidNativePickerStyle ( ) ;
634
646
}
647
+
648
+ render ( ) {
649
+ return (
650
+ < EffectRunner effect = { this . state . scrollToInputEffect } >
651
+ { this . renderForPlatform ( ) }
652
+ </ EffectRunner >
653
+ ) ;
654
+ }
635
655
}
636
656
637
657
export { defaultStyles , PickerStateProvider , PickerAvoidingView } ;
0 commit comments