2019-11-02 21:57:59 +01:00
|
|
|
import { helper } from '@ember/component/helper';
|
2019-11-09 13:20:27 +01:00
|
|
|
import { next } from '@ember/runloop';
|
2019-11-02 21:57:59 +01:00
|
|
|
import { assert } from '@ember/debug';
|
|
|
|
|
|
|
|
function scrollFirstInvalidElementIntoViewPort() {
|
2019-11-09 13:20:27 +01:00
|
|
|
// `schedule('afterRender', function() {})` would be more approperiate but there seems to be a
|
|
|
|
// timing issue in Firefox causing the Browser not scrolling up far enough if doing so
|
|
|
|
// delaying to next runloop therefore
|
|
|
|
next(function() {
|
2019-11-02 21:57:59 +01:00
|
|
|
let invalidInput = document.querySelector('.form-control.is-invalid');
|
|
|
|
assert(
|
|
|
|
'Atleast one form control must be marked as invalid if form submission was rejected as invalid',
|
|
|
|
invalidInput
|
|
|
|
);
|
2019-11-09 13:20:27 +01:00
|
|
|
|
|
|
|
// focus first invalid control
|
2019-11-02 21:57:59 +01:00
|
|
|
invalidInput.focus({ preventScroll: true });
|
|
|
|
|
|
|
|
// scroll to label of first invalid control
|
|
|
|
if (
|
|
|
|
invalidInput.getBoundingClientRect().top <= 0 ||
|
|
|
|
invalidInput.getBoundingClientRect().bottom >= window.innerHeight
|
|
|
|
) {
|
|
|
|
let label = document.querySelector(`label[for="${invalidInput.id}"]`);
|
|
|
|
label.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default helper(function() {
|
|
|
|
return scrollFirstInvalidElementIntoViewPort;
|
|
|
|
});
|