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
23 changes: 22 additions & 1 deletion 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.childPosition = {};

this.setTouchedInput = this.setTouchedInput.bind(this);
this.validate = this.validate.bind(this);
Expand Down Expand Up @@ -108,6 +109,16 @@ class Form extends React.Component {
return _.first(_.keys(hasStateErrors ? this.state.erorrs : this.props.formState.errorFields));
}

setPosition(element, position) {
if (!element.props.inputID && element.props.children) {
_.forEach(element.props.children, (child) => {
this.setPosition(child, position);
});
} else {
this.childPosition[element.props.inputID] = position;
}
Comment on lines +112 to +119
Copy link
Contributor

Choose a reason for hiding this comment

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

If element.props.inputID is false, but element.props.children is also falsey, then you will enter in the else which I think is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

I put some console logs, and it doesn't happen in the debit card form, but in other forms like opening Personal details it does happen:

element.props.inputID is undefined and element.props.children is also undefined

}

submit() {
// Return early if the form is already submitting to avoid duplicate submission
if (this.props.formState.isLoading) {
Expand Down Expand Up @@ -249,9 +260,18 @@ class Form extends React.Component {
style={[styles.w100, styles.flex1]}
contentContainerStyle={styles.flexGrow1}
keyboardShouldPersistTaps="handled"
ref={el => this.form = el}
>
<View style={[this.props.style]}>
{this.childrenWrapperWithProps(this.props.children)}
{_.map(this.childrenWrapperWithProps(this.props.children), child => (
<View
onLayout={(event) => {
this.setPosition(child, event.nativeEvent.layout.y);
}}
>
{child}
</View>
))}
{this.props.isSubmitButtonVisible && (
<FormAlertWithSubmitButton
buttonText={this.props.submitButtonText}
Expand All @@ -263,6 +283,7 @@ 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));
const focusInput = this.inputRefs[focusKey];
this.form.scrollTo({y: this.childPosition[focusKey], animated: false});
if (focusInput.focus && typeof focusInput.focus === 'function') {
focusInput.focus();
}
Expand Down