Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll to the right spot when clicking "Fix Errors" #12954

Merged
merged 14 commits into from
Dec 5, 2022
10 changes: 10 additions & 0 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Form extends React.Component {

this.inputRefs = {};
this.touchedInputs = {};
this.viewPosition = 0;

this.setTouchedInput = this.setTouchedInput.bind(this);
this.validate = this.validate.bind(this);
Expand Down Expand Up @@ -245,6 +246,12 @@ class Form extends React.Component {
style={[styles.w100, styles.flex1]}
contentContainerStyle={styles.flexGrow1}
keyboardShouldPersistTaps="handled"
ref={el => this.form = el}
onLayout={(event) => {
// Store the parent component position to be used for scrolling. The +1 is to avoid covering
// the upper border of the element we scroll to
this.viewPosition = event.nativeEvent.layout.y + 1;
}}
>
<View style={[this.props.style]}>
{this.childrenWrapperWithProps(this.props.children)}
Expand All @@ -259,6 +266,9 @@ class Form extends React.Component {
const errors = !_.isEmpty(this.state.errors) ? this.state.errors : this.props.formState.errorFields;
const focusKey = _.find(_.keys(this.inputRefs), key => _.keys(errors).includes(key));
this.inputRefs[focusKey].focus();
this.inputRefs[focusKey].measure((fx, fy, width, height, px, py) => {
this.form.scrollTo({y: py - this.viewPosition, animated: false});
Copy link
Member

@rushatgabhane rushatgabhane Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danieldoglas @Gonals I'm not sure how to handle this, but we need an enhancement.

Problem: On mobile, when the keyboard is open we scroll to a position that's hidden under the keyboard.

Solution 1: Always scroll to a position so that the input is just above the keyboard.
Solution 2: Always scroll to a position so that the input is at the very top of screen.
...??

Screen.Recording.2022-11-23.at.9.32.49.PM.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice catch! Looking into it

Copy link
Member

@rushatgabhane rushatgabhane Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's too complicated to calculate the scroll postion / detect if the keyboard is open, we might need keyboard avoiding view experts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I changed things around a little bit and I think it is working more smoothly now!
Quick thing, I see you added a bunch of Name fields for testing. Make sure you give them different inputID, as those are involved when determining where to scroll and might be the reason of the weird behavior you saw

});
}}
containerStyles={[styles.mh0, styles.mt5]}
enabledWhenOffline={this.props.enabledWhenOffline}
Expand Down