Skip to content

Commit

Permalink
Make KeyboardAvoidingView with behavior="height" resize on keyboard c…
Browse files Browse the repository at this point in the history
…lose (#18889)

Summary:
<!--
  Required: Write your motivation here.
  If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged.
-->
Fixes #13754
Pull Request resolved: #18889

Differential Revision: D14486115

Pulled By: PeteTheHeat

fbshipit-source-id: 7b8b4fa9d2c99fc5d6145fed4681afdc4bb14fb8
  • Loading branch information
WaldoJeffers authored and grabbou committed May 6, 2019
1 parent 0c42064 commit 7140a7f
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions Libraries/Components/Keyboard/KeyboardAvoidingView.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
_frame: ?ViewLayout = null;
_subscriptions: Array<EmitterSubscription> = [];
viewRef: {current: React.ElementRef<any> | null};
_initialFrameHeight: number = 0;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -113,19 +114,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 @@ -166,7 +159,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
// 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,
};
}
Expand Down

0 comments on commit 7140a7f

Please sign in to comment.