Skip to content

Commit

Permalink
Add animation
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Jun 5, 2016
1 parent 45936be commit fa54abe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions addon/components/validatable-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ export default Ember.Component.extend({
*/
isSubmitting: false,

/**
* Scroll to the first input field that does not pass the validation
*
* @returns {void}
*/
scrollToFirstError: function() {
var form = this.get('element');

// We get the first element that fails, and scroll to it
for (var i = 0 ; i !== form.elements.length ; ++i) {
if (!form.elements[i].validity.valid) {
Ember.$('html, body').animate({
scrollTop: Ember.$(form.elements[i]).offset().top - 40
}, 200);

break;
}
}
},

actions: {
/**
* @param {string} inputName
Expand All @@ -58,6 +78,7 @@ export default Ember.Component.extend({
*/
submit(eventName) {
if (!this.get('element').checkValidity()) {
this.scrollToFirstError();
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-cli-html5-validation",
"version": "0.2.2",
"version": "0.2.3",
"description": "The default blueprint for ember-cli addons.",
"directories": {
"doc": "doc",
Expand Down

0 comments on commit fa54abe

Please sign in to comment.