Skip to content

Commit 0ca7749

Browse files
authored
ENG-2305: Scroll-to-error UX improvement (#377)
* Fix scroll function itself * Only scroll to errors when attempting to submit invalid form * Remove unnecessary variable
1 parent e40a085 commit 0ca7749

2 files changed

Lines changed: 11 additions & 25 deletions

File tree

src/pages/GiftAid/GiftAid.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import axios from 'axios';
77
import UpdateForm from './UpdateForm';
88
import SubmitForm from './SubmitForm';
99

10-
1110
// context
1211
import AppContext from '../../context/AppContext';
1312

@@ -28,9 +27,6 @@ import {
2827
getRoute,
2928
} from './utils/Utils';
3029

31-
32-
let scrollTimeout;
33-
3430
function GiftAid(props) {
3531

3632
// initialise and get props from context
@@ -99,7 +95,7 @@ function GiftAid(props) {
9995
* Handle validity state on component update
10096
*/
10197
useEffect(() => {
102-
// if validation fails, scroll to error
98+
// Update validation accordingly on update
10399
if ((formValidityState.showErrorMessages && !formValidityState.formValidity
104100
&& formValidityState.validating) || formValidityState.urlTransactionId.valid === false ) {
105101
// update validation state
@@ -110,27 +106,12 @@ function GiftAid(props) {
110106
}
111107
}, []);
112108

113-
/**
114-
* Handle scroll to error on component update
115-
*/
116-
useEffect(() => {
117-
scrollTimeout = setTimeout(() => {
118-
scrollToError(formValidityState);
119-
}, 500);
120-
return () => {
121-
// clear timeout on component unmount
122-
clearTimeout(scrollTimeout);
123-
}
124-
});
125-
126-
127109
/**
128110
* Updates validation state for form fields
129111
* @param childState
130112
* @param name
131113
*/
132114
const setFieldValidity = (childState, name) => {
133-
134115
const prevStateField = fieldValidation[name];
135116
const fieldUndefined = prevStateField === undefined;
136117
const valueUndefined = typeof prevStateField !== 'undefined' && prevStateField.value === undefined;
@@ -202,6 +183,10 @@ function GiftAid(props) {
202183
pathname: pathParams.sorryPath, // redirect to failure page
203184
});
204185
});
186+
} else {
187+
setTimeout(() => {
188+
scrollToError(formValidityState);
189+
}, 500);
205190
}
206191
return null;
207192
};

src/pages/GiftAid/utils/Utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export const scrollToError = (state = {}) => {
1414
// Scroll to transactionId field / url parameter error message
1515
// if present
1616
if (state.urlTransactionId.valid === false) {
17-
document.querySelector('#field-error--urlTransID').scrollIntoView('smooth');
17+
document.querySelector('#field-error--urlTransID').scrollIntoView({ behavior: 'smooth' });
1818
}
1919
// Scroll to the first erroring field and focus on its input field
2020
const errorWrapper = document.querySelectorAll('.form__field--erroring')[0];
21-
const errorField = document.querySelectorAll('.form__field--erroring > div input')[0];
22-
if (errorWrapper !== undefined) {
23-
errorWrapper.scrollIntoView('smooth');
24-
errorField.focus();
21+
if (errorWrapper) {
22+
errorWrapper.scrollIntoView({ behavior: 'smooth' });
23+
setTimeout(() => {
24+
document.querySelectorAll('.form__field--erroring > div input')[0].focus();
25+
}, 500);
2526
}
2627
};
2728

0 commit comments

Comments
 (0)