Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions Libraries/Components/Keyboard/KeyboardAvoidingView.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Props = $ReadOnly<{|
* Specify how to react to the presence of the keyboard.
*/
behavior?: ?('height' | 'position' | 'padding'),

/**
* Style of the content container when `behavior` is 'position'.
*/
Expand Down Expand Up @@ -66,6 +65,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {

_frame: ?ViewLayout = null;
_subscriptions: Array<EmitterSubscription> = [];
_initialFrameHeight: number = 0;

state = {
bottom: 0,
Expand Down Expand Up @@ -111,19 +111,11 @@ class KeyboardAvoidingView extends React.Component<Props, State> {

_onLayout = (event: ViewLayoutEvent) => {
this._frame = event.nativeEvent.layout;
};

UNSAFE_componentWillUpdate(nextProps: Props, nextState: State): void {
if (
nextState.bottom === this.state.bottom &&
this.props.behavior === 'height' &&
nextProps.behavior === 'height'
) {
// If the component rerenders without an internal state change, e.g.
// triggered by parent component re-rendering, no need for bottom to change.
nextState.bottom = 0;
if (!this._initialFrameHeight) {
// save the initial frame height, before the keyboard is visible
this._initialFrameHeight = this._frame.height;
}
}
};

componentDidMount(): void {
if (Platform.OS === 'ios') {
Expand Down Expand Up @@ -158,23 +150,20 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
switch (behavior) {
case 'height':
let heightStyle;
if (this._frame != null) {
if (this._frame != null && this.state.bottom > 0) {
// Note that we only apply a height change when there is keyboard present,
// i.e. this.state.bottom is greater than 0. If we remove that condition,
// this.frame.height will never go back to its original value.
// When height changes, we need to disable flex.
heightStyle = {
height: this._frame.height - bottomHeight,
height: this._initialFrameHeight - bottomHeight,
flex: 0,
};
}
return (
<View
ref={viewRef}
style={StyleSheet.compose(
style,
heightStyle,
)}
style={StyleSheet.compose(style, heightStyle)}
onLayout={this._onLayout}
{...props}>
{children}
Expand All @@ -189,12 +178,9 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
onLayout={this._onLayout}
{...props}>
<View
style={StyleSheet.compose(
contentContainerStyle,
{
bottom: bottomHeight,
},
)}>
style={StyleSheet.compose(contentContainerStyle, {
bottom: bottomHeight,
})}>
{children}
</View>
</View>
Expand All @@ -204,10 +190,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
return (
<View
ref={viewRef}
style={StyleSheet.compose(
style,
{paddingBottom: bottomHeight},
)}
style={StyleSheet.compose(style, {paddingBottom: bottomHeight})}
onLayout={this._onLayout}
{...props}>
{children}
Expand Down