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

Add scroll on the first erroneous field after a failed validation #1544

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/mixin/built-in-components.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Dependencies
import React from 'react';
import ReactDOM from 'react-dom';
import { changeMode } from 'focus-core/application';
import assign from 'object-assign';

import result from 'lodash/object/result';
import find from 'lodash/collection/find';
import defaultsDeep from 'lodash/object/defaultsDeep';
import isFunction from 'lodash/lang/isFunction';

// Components
import { component as Field } from './field';
Expand Down Expand Up @@ -161,7 +163,19 @@ export default {
this.clearError();
if (this._validate()) {
this.action.save.call(this, this._getEntity());
}
} else if (this.preventFocusOnValidationFailed !== true) {
for (let inptKey in this.refs) {
const refElt = this.refs[inptKey];
if (isFunction(refElt.validate) || isFunction(refElt._validate)) {
const validationRes = isFunction(refElt.validate) ? refElt.validate() : refElt._validate();
if (validationRes !== undefined && validationRes !== true) {
const elementPosition = ReactDOM.findDOMNode(refElt).getBoundingClientRect().top;
window.scrollTo(0, elementPosition + window.scrollY - window.innerHeight/2);
break;
}
}
}
}
};
return (
<Button
Expand Down