Skip to content

Commit

Permalink
feat(forms): update scroll directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismaestro authored and iramos committed Oct 5, 2018
1 parent 08aeb02 commit 704e1a1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/app/shared/directives/scroll-to-first-invalid.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Directive, ElementRef, HostListener, Input} from '@angular/core';
import {NgForm} from '@angular/forms';
import UtilsHelper from '../../shared/helpers/utils.helper';
import {scrollToElement} from '../helpers/utils.helper';

@Directive({selector: '[appScrollToFirstInvalid]'})
export class ScrollToFirstInvalidDirective {
Expand All @@ -21,10 +21,19 @@ export class ScrollToFirstInvalidDirective {
}
}

const target = this.el.nativeElement.querySelector('.ng-invalid');
if (target) {
UtilsHelper.scrollToElement(target);
const formControlInvalid = this.el.nativeElement.querySelector('.form-control.ng-invalid');

if (formControlInvalid) {
return scrollToElement(formControlInvalid);
} else {
// The first element is the global form and here we are looking for the first nested form
const formGroupInvalid = this.el.nativeElement.querySelectorAll('form.ng-invalid');
if (formGroupInvalid && formGroupInvalid.length > 1) {
return scrollToElement(formGroupInvalid[1]);
}
}

return scrollToElement(this.el.nativeElement);
}
}
}

0 comments on commit 704e1a1

Please sign in to comment.