All files / src/app/shared/forms/components/form-control-feedback form-control-feedback.component.ts

100% Statements 11/11
85.71% Branches 6/7
100% Functions 5/5
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 393x 3x                     3x   5x         8x 4x         4x 1x     3x   3x       3x      
import { ChangeDetectionStrategy, Component, DoCheck, Input } from '@angular/core';
import { AbstractControl } from '@angular/forms';
 
interface FormErrorMessages {
  [key: string]: string;
}
 
@Component({
  selector: 'ish-form-control-feedback',
  templateUrl: './form-control-feedback.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class FormControlFeedbackComponent implements DoCheck {
  @Input({ required: true }) control: AbstractControl;
  @Input() messages: FormErrorMessages = {};
 
  errors: string[];
 
  ngDoCheck() {
    if (this.control.dirty) {
      this.errors = this.getErrorList();
    }
  }
 
  private getErrorList(): string[] {
    if (!this.control.errors) {
      return [];
    }
 
    return Object.keys(this.control.errors)
      .map(key =>
        this.messages && key in this.messages && this.messages[key]
          ? this.messages[key]
          : this.control.errors.customError
      )
      .filter(locString => !!locString);
  }
}