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
8 changes: 8 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.inputPosition = 0;

this.setTouchedInput = this.setTouchedInput.bind(this);
this.validate = this.validate.bind(this);
Expand Down Expand Up @@ -245,6 +246,10 @@ class Form extends React.Component {
style={[styles.w100, styles.flex1]}
contentContainerStyle={styles.flexGrow1}
keyboardShouldPersistTaps="handled"
ref={el => this.form = el}
onLayout={(event) => {
this.inputPosition = event.nativeEvent.layout.y + 1;
Copy link
Member

Choose a reason for hiding this comment

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

@Gonals why the +1?

suggestion: add a comment for 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.

Yep, adding a comment.

Without the +1, we'll scroll right to the border of the component, potentially covering the upper border

Copy link
Member

Choose a reason for hiding this comment

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

thanks, that makes sense!

}}
>
<View style={[this.props.style]}>
{this.childrenWrapperWithProps(this.props.children)}
Expand All @@ -259,6 +264,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.inputPosition, animated: false});
});
}}
containerStyles={[styles.mh0, styles.mt5]}
enabledWhenOffline={this.props.enabledWhenOffline}
Expand Down