Skip to content

Commit

Permalink
update patch for KeyboardAvoidingView
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgrzybowski committed Jun 26, 2023
1 parent b36f102 commit 01dcb62
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions patches/react-native+0.71.2-alpha.3.patch
Original file line number Diff line number Diff line change
@@ -1,15 +1,87 @@
diff --git a/node_modules/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js b/node_modules/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js
index 2f48f9e..f59beee 100644
index 2f48f9e..6418c76 100644
--- a/node_modules/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js
+++ b/node_modules/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js
@@ -128,6 +128,10 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
@@ -65,6 +65,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
_subscriptions: Array<EventSubscription> = [];
viewRef: {current: React.ElementRef<typeof View> | null, ...};
_initialFrameHeight: number = 0;
+ _bottom: number = 0;

constructor(props: Props) {
super(props);
@@ -107,18 +108,20 @@ class KeyboardAvoidingView extends React.Component<Props, State> {

_onKeyboardChange = (event: ?KeyboardEvent) => {
this._keyboardEvent = event;
+ // $FlowFixMe[unused-promise]
this._updateBottomIfNecessary();
};

_updateBottomIfNecessary = async () => {
+ if (!this.props.enabled) {
+ return;
_onLayout = async (event: ViewLayoutEvent) => {
- const wasFrameNull = this._frame == null;
+ const oldFrame = this._frame;
this._frame = event.nativeEvent.layout;
if (!this._initialFrameHeight) {
// save the initial frame height, before the keyboard is visible
this._initialFrameHeight = this._frame.height;
}

- if (wasFrameNull) {
+ // update bottom height for the first time or when the height is changed
+ if (!oldFrame || oldFrame.height !== this._frame.height) {
await this._updateBottomIfNecessary();
}

@@ -127,20 +130,31 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
}
};

+ // Avoid unnecessary renders if the KeyboardAvoidingView is disabled.
+ _setBottom = (value: number) => {
+ const {enabled = true} = this.props;
+ this._bottom = value;
+ if (enabled) {
+ this.setState({bottom: value});
+ }
+ };
+
_updateBottomIfNecessary = async () => {
if (this._keyboardEvent == null) {
this.setState({bottom: 0});
- this.setState({bottom: 0});
+ this._setBottom(0);
return;
}

const {duration, easing, endCoordinates} = this._keyboardEvent;
const height = await this._relativeKeyboardHeight(endCoordinates);

- if (this.state.bottom === height) {
+ if (this._bottom === height) {
return;
}

- if (duration && easing) {
+ this._setBottom(height);
+
+ if (enabled && duration && easing) {
LayoutAnimation.configureNext({
// We have to pass the duration equal to minimal accepted duration defined here: RCTLayoutAnimation.m
duration: duration > 10 ? duration : 10,
@@ -150,9 +164,15 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
},
});
}
- this.setState({bottom: height});
};

+ componentDidUpdate(_: Props, prevState: State): void {
+ const {enabled = true} = this.props;
+ if (enabled && this._bottom !== prevState.bottom) {
+ this.setState({bottom: this._bottom});
+ }
+ }
+
componentDidMount(): void {
if (Platform.OS === 'ios') {
this._subscriptions = [

0 comments on commit 01dcb62

Please sign in to comment.