Skip to content

Commit

Permalink
Merge pull request #55 from peckadesign/scroll-error-into-view
Browse files Browse the repository at this point in the history
#1 Úprava scrollování po validování formuláře
  • Loading branch information
zipper authored May 6, 2024
2 parents 64e1131 + a783bce commit 19b05b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pd-forms",
"title": "pdForms",
"description": "Customization of netteForms for use in PeckaDesign.",
"version": "4.0.1",
"version": "4.1.0",
"author": "PeckaDesign, s.r.o <[email protected]>",
"contributors": [
"Radek Šerý <[email protected]>",
Expand Down
31 changes: 28 additions & 3 deletions src/assets/pdForms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @name pdForms
* @author Radek Šerý <[email protected]>
* @version 4.0.1
* @version 4.1.0
*
* Features:
* - live validation
Expand Down Expand Up @@ -594,6 +594,31 @@
}


/**
* Scroll into first error after validation.
*/
pdForms.afterValidationScroll = function (form, firstErrorElem) {
if (! firstErrorElem) {
form.scrollIntoView();
return;
}

var placeholder = pdForms.getMessagePlaceholder(firstErrorElem)

if (placeholder.isGlobal || firstErrorElem.type === 'hidden') {
(placeholder.elem ?? form).scrollIntoView()

firstErrorElem.focus({
preventScroll: true
});

return;
}

firstErrorElem.focus()
}


/**
* Optional rules are defined using "optional" property in "arg". We have to convert arg into Nette format before
* validating. This means removing all properties but data from arg and storing them elsewhere.
Expand Down Expand Up @@ -644,8 +669,8 @@
}
}

if (focusElem) {
focusElem.focus();
if (errors.length) {
pdForms.afterValidationScroll(form, focusElem);
}
};

Expand Down

0 comments on commit 19b05b4

Please sign in to comment.