import {TextFieldGridEditor} from "@webbpm/base-package";
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef} from "@angular/core";

@Component({
             moduleId: module.id,
             selector: 'text-field-ervu-grid-editor',
             templateUrl: './../../../../../../src/resources/template/account_applications/component/editablegrid/editors/ErvuAccountTextFieldGridEditor.html',
             changeDetection: ChangeDetectionStrategy.OnPush
           })
export class ErvuAccountTextFieldGridEditor extends TextFieldGridEditor{

  constructor(el: ElementRef, cd: ChangeDetectorRef) {
    super(el, cd);
  }

  onBlur() {
    super.onBlur();
    if (this.getValue() && !this.isValueValid()) {
      this.clearValue();
      this.cd.markForCheck();
    }
  }

  protected isValueValid() {
    if (this.pattern) {
      const regex = new RegExp(this.pattern);
      return regex.test(this.getValue());
    }
    return true;
  }
}
