Skip to content

Commit 704e1a1

Browse files
Ismaestroiramos
authored andcommitted
feat(forms): update scroll directive
1 parent 08aeb02 commit 704e1a1

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/app/shared/directives/scroll-to-first-invalid.directive.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Directive, ElementRef, HostListener, Input} from '@angular/core';
22
import {NgForm} from '@angular/forms';
3-
import UtilsHelper from '../../shared/helpers/utils.helper';
3+
import {scrollToElement} from '../helpers/utils.helper';
44

55
@Directive({selector: '[appScrollToFirstInvalid]'})
66
export class ScrollToFirstInvalidDirective {
@@ -21,10 +21,19 @@ export class ScrollToFirstInvalidDirective {
2121
}
2222
}
2323

24-
const target = this.el.nativeElement.querySelector('.ng-invalid');
25-
if (target) {
26-
UtilsHelper.scrollToElement(target);
24+
const formControlInvalid = this.el.nativeElement.querySelector('.form-control.ng-invalid');
25+
26+
if (formControlInvalid) {
27+
return scrollToElement(formControlInvalid);
28+
} else {
29+
// The first element is the global form and here we are looking for the first nested form
30+
const formGroupInvalid = this.el.nativeElement.querySelectorAll('form.ng-invalid');
31+
if (formGroupInvalid && formGroupInvalid.length > 1) {
32+
return scrollToElement(formGroupInvalid[1]);
33+
}
2734
}
35+
36+
return scrollToElement(this.el.nativeElement);
2837
}
2938
}
3039
}

0 commit comments

Comments
 (0)