Skip to content

Commit

Permalink
feat: scroll the window to the top of the form after submitting it su…
Browse files Browse the repository at this point in the history
…ccesfully (#43)

* feat: scroll the window to the top of the form after submitting it succesfully

* fix: guard against null values
  • Loading branch information
erral authored Oct 7, 2022
1 parent b032edc commit 3c26486
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/FormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const FormView = ({
resetFormState,
resetFormOnError,
captcha,
id,
}) => {
const intl = useIntl();

Expand Down Expand Up @@ -91,6 +92,7 @@ const FormView = ({
</Message>
) : (
<Form
id={id}
loading={formState.loading}
onSubmit={onSubmit}
autoComplete="off"
Expand Down
13 changes: 13 additions & 0 deletions src/components/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,25 @@ const View = ({ data, id, path }) => {
onChangeFormData,
});

const formid = `form-${id}`;

useEffect(() => {
if (submitResults?.loaded) {
setFormState({
type: FORM_STATES.success,
result: intl.formatMessage(messages.formSubmitted),
});
captcha.reset();
const formItem = document.getElementById(formid);
if (formItem !== null) {
const formItemPosition = formItem.getBoundingClientRect();
formItemPosition !== null &&
window.scrollTo({
top: formItemPosition.x,
left: formItemPosition.y,
behavior: 'smooth',
});
}
} else if (submitResults?.error) {
let errorDescription = `${
JSON.parse(submitResults.error.response?.text ?? '{}')?.message
Expand All @@ -227,6 +239,7 @@ const View = ({ data, id, path }) => {

return (
<FormView
id={formid}
formState={formState}
formErrors={formErrors}
formData={formData}
Expand Down

0 comments on commit 3c26486

Please sign in to comment.