11import React from 'react' ;
22import { StyleSheet , View } from 'react-native' ;
3- import { PickerStateContext } from " ../PickerStateProvider" ;
4- import { IOS_MODAL_HEIGHT } from " ../constants" ;
3+ import { PickerStateContext } from ' ../PickerStateProvider' ;
4+ import { IOS_MODAL_ANIMATION_DURATION_MS , IOS_MODAL_HEIGHT } from ' ../constants' ;
55
66const styles = {
77 container : {
@@ -17,6 +17,11 @@ const styles = {
1717 } ,
1818} ;
1919
20+ function schedule ( callback , timeout ) {
21+ const handle = setTimeout ( callback , timeout ) ;
22+ return ( ) => clearTimeout ( handle ) ;
23+ }
24+
2025/**
2126 * PickerAvoidingView is a React component that adjusts the view layout to avoid
2227 * being covered by an open iOS UIPickerView modal. It's meant to be similar to
@@ -33,12 +38,22 @@ export function PickerAvoidingView(props) {
3338 const context = React . useContext ( PickerStateContext ) ;
3439 const isPickerOpen = context && context . isPickerOpen ;
3540
41+ const [ shouldAddSpace , setShouldAddSpace ] = React . useState ( false ) ;
42+
43+ React . useEffect ( ( ) => {
44+ if ( isPickerOpen ) {
45+ return schedule ( ( ) => {
46+ setShouldAddSpace ( true ) ;
47+ } , IOS_MODAL_ANIMATION_DURATION_MS ) ;
48+ } else {
49+ setShouldAddSpace ( false ) ;
50+ }
51+ } , [ isPickerOpen ] ) ;
52+
3653 return (
3754 < View style = { StyleSheet . compose ( props . style , styles . container ) } >
38- < View style = { styles . contentContainer } >
39- { props . children }
40- </ View >
41- { isPickerOpen && < View style = { styles . space } /> }
55+ < View style = { styles . contentContainer } > { props . children } </ View >
56+ { shouldAddSpace && < View style = { styles . space } /> }
4257 </ View >
4358 ) ;
4459}
0 commit comments