File

projects/i-components/src/lib/directives/validationsupport.directive.ts

Description

Support directive for adding validation elements to a component template

Implements

OnChanges OnDestroy

Metadata

Selector [wmValidationSupport]

Index

Properties
Methods
Inputs

Constructor

constructor(elementRef: ElementRef, injectorBase: Injector)

Creates an instance of ValidationSupportDirective.

Parameters :
Name Type Optional
elementRef ElementRef No
injectorBase Injector No

Inputs

wmValidationSupport
Type : [boolean, string, string, string, ]

Pair of values representing the validation state (valid or not) and the validation error message

Methods

ngOnChanges
ngOnChanges(changes: SimpleChanges)

Verifies for changes in the validation state

Parameters :
Name Type Optional
changes SimpleChanges No
Returns : void
ngOnDestroy
ngOnDestroy()

Clean up for the directive

Returns : void

Properties

Private validationHelper
Type : ValidationHelper

Helper used to register/unregister the validation message on the component.

Public wmValidationSupport
Type : [boolean, string, string, string, ]
Decorators :
@Input()

Pair of values representing the validation state (valid or not) and the validation error message

import {
  Directive,
  ElementRef,
  Injector,
  Input,
  OnChanges,
  OnDestroy,
  SimpleChanges,
} from '@angular/core';
import { ValidationHelper } from '../utils/validation-helper';

/**
 * Support directive for adding validation elements to a component template
 *
 * @export
 * @class ValidationSupportDirective
 * @implements {OnChanges}
 * @implements {OnDestroy}
 */
@Directive({
  selector: '[wmValidationSupport]',
})
export class ValidationSupportDirective implements OnChanges, OnDestroy {
  /**
   * Pair of values representing the validation state (valid or not) and the validation error message
   *
   * @type {[boolean, string]}
   * @memberof ValidationSupportDirective
   */
  @Input() public wmValidationSupport: [
    boolean,
    string,
    string,
    string,
    boolean?
  ];

  /**
   * Helper used to register/unregister the validation message
   * on the component.
   *
   * @private
   * @type {ValidationHelper}
   * @memberof ValidationSupportDirective
   */
  private validationHelper: ValidationHelper;

  /**
   * Creates an instance of ValidationSupportDirective.
   *
   * @param {ElementRef} elementRef
   * @param {Injector} injectorBase
   * @memberof ValidationSupportDirective
   */
  constructor(elementRef: ElementRef, injectorBase: Injector) {
    this.validationHelper = new ValidationHelper(elementRef, injectorBase);
  }

  /**
   *  Verifies for changes in the validation state
   *
   * @param {SimpleChanges} changes
   * @memberof ValidationSupportDirective
   */
  ngOnChanges(changes: SimpleChanges): void {
    /* istanbul ignore else */
    if (changes.wmValidationSupport) {
      const [flag, message, applyTo, size, isRounded] =
        this.wmValidationSupport;
      if (flag) {
        this.validationHelper.isRounded = isRounded;
        this.validationHelper.registerValidationMsgService(
          message,
          size,
          applyTo
        );
      } else {
        this.validationHelper.unregisterValidationMsgService();
      }
    }
  }

  /**
   * Clean up for the directive
   *
   * @memberof ValidationSupportDirective
   */
  ngOnDestroy() {
    this.validationHelper.unregisterValidationMsgService();
  }
}

result-matching ""

    No results matching ""