1- import React , { PureComponent } from 'react' ;
1+ import React , { PureComponent , useEffect } from 'react' ;
22import {
33 Dimensions ,
44 Keyboard ,
@@ -16,6 +16,12 @@ import { defaultStyles } from './styles';
1616import { PickerAvoidingView } from './PickerAvoidingView' ;
1717import { PickerStateContext , PickerStateProvider } from './PickerStateProvider' ;
1818import { 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+ }
1925
2026const preserveSpaces = ( label ) => {
2127 return label . replace ( / / g, '\u00a0' ) ;
@@ -253,13 +259,19 @@ export default class RNPickerSelect extends PureComponent {
253259
254260 // If TextInput is below picker modal, scroll up
255261 if ( textInputBottomY > modalY ) {
262+ const scrollView = this . props . scrollViewRef . current ;
263+ const scrollViewContentOffsetY = this . props . scrollViewContentOffsetY ;
264+
256265 // Wait until the modal animation finishes, so the scrolling is effective when PickerAvoidingView is
257266 // used
258- setTimeout ( ( ) => {
259- this . props . scrollViewRef . current . scrollTo ( {
260- y : textInputBottomY - modalY + 10 + this . props . scrollViewContentOffsetY ,
261- } ) ;
262- } , IOS_MODAL_ANIMATION_DURATION_MS + 50 ) ;
267+ this . setState ( {
268+ scrollToInputEffect : ( ) =>
269+ schedule ( ( ) => {
270+ scrollView . scrollTo ( {
271+ y : textInputBottomY - modalY + 10 + scrollViewContentOffsetY ,
272+ } ) ;
273+ } , IOS_MODAL_ANIMATION_DURATION_MS + 50 ) ,
274+ } ) ;
263275 }
264276 } ) ;
265277 }
@@ -634,7 +646,7 @@ export default class RNPickerSelect extends PureComponent {
634646 ) ;
635647 }
636648
637- render ( ) {
649+ renderForPlatform ( ) {
638650 const { children, useNativeAndroidPickerStyle } = this . props ;
639651
640652 if ( Platform . OS === 'ios' ) {
@@ -651,6 +663,14 @@ export default class RNPickerSelect extends PureComponent {
651663
652664 return this . renderAndroidNativePickerStyle ( ) ;
653665 }
666+
667+ render ( ) {
668+ return (
669+ < EffectRunner effect = { this . state . scrollToInputEffect } >
670+ { this . renderForPlatform ( ) }
671+ </ EffectRunner >
672+ ) ;
673+ }
654674}
655675
656676export { defaultStyles , PickerStateProvider , PickerAvoidingView } ;
0 commit comments