diff --git a/packages/components-angular/src/focus.directive.ts b/packages/components-angular/src/focus.directive.ts index f0ec57d962..0b7e3fe78b 100644 --- a/packages/components-angular/src/focus.directive.ts +++ b/packages/components-angular/src/focus.directive.ts @@ -1,12 +1,23 @@ -import { AfterViewChecked, Directive, ElementRef } from '@angular/core' +import { AfterViewInit, Directive, ElementRef } from '@angular/core' @Directive({ selector: '[balAutoFocus]', }) -export class BalAutoFocus implements AfterViewChecked { +export class BalAutoFocus implements AfterViewInit { constructor(private elementRef: ElementRef) {} - ngAfterViewChecked() { - this.elementRef.nativeElement.focus() + ngAfterViewInit() { + this.setFocus() + } + + setFocus() { + const el = this.elementRef.nativeElement + if (el) { + if (el.setFocus) { + el.setFocus() + } else { + el.focus() + } + } } } diff --git a/packages/components/src/stories/implementation/07-form.stories.mdx b/packages/components/src/stories/implementation/07-form.stories.mdx index e393224eb3..940a691284 100644 --- a/packages/components/src/stories/implementation/07-form.stories.mdx +++ b/packages/components/src/stories/implementation/07-form.stories.mdx @@ -152,12 +152,16 @@ npm install @baloise/web-app-validators-angular Below is a basic example to use the reactive form together with the Design System. +Angular package offers `balAutoFocus` directive which brings focus to the element. +Usage can be seen in the following code in bal-field. +In order for it to work please import `BalSharedModule` in each Angular module that uses it. + ```html