Skip to content

Commit

Permalink
fix(angular-focus-directive): call setFocus method and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mladenplaninicic committed Nov 14, 2022
1 parent f10c212 commit 8b7a858
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 15 additions & 4 deletions packages/components-angular/src/focus.directive.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
<form [formGroup]="form" (ngSubmit)="onSubmit()" class="columns is-multiline mt-0">
<bal-field class="column is-full py-0" required [disabled]="form.get('email')?.disabled">
<bal-field-label required>Email</bal-field-label>
<bal-field-control>
<bal-input name="email" placeholder="Enter your email" formControlName="email"></bal-input>
<bal-input name="email" placeholder="Enter your email" formControlName="email" balAutoFocus></bal-input>
</bal-field-control>
<bal-field-message color="danger">
<bal-ng-error controlName="email" error="isRequired">This field is required</bal-ng-error>
Expand Down

0 comments on commit 8b7a858

Please sign in to comment.