File

src/ng-xform/field-components/base-dynamic-field.component.ts

Description

Base class for Dynamic Fields

Implements

OnInit OnDestroy

Index

Properties
Methods
Accessors

Constructor

constructor()

Methods

displayFieldCss
displayFieldCss()
Returns : literal type
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

control
control: FormControl
Type : FormControl
Public hideLabelOnEdit
hideLabelOnEdit:
Default value : false
subscriptions
subscriptions:
Default value : new Subscription
Public useFormattedValueOnReadonly
useFormattedValueOnReadonly:
Default value : true

If true, the read-only state will show the value obtained from the formattedValue method; otherwise, will keep the component to manage this behavior.

visible
visible:
Default value : true

Accessors

elementId
getelementId()

Property to be used as the Form Element ID

Returns : string
instance
getinstance()
isOptional
getisOptional()
isEditing
getisEditing()
formattedValue
getformattedValue()
import { Input, OnInit, OnDestroy } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { DynamicField } from '../fields';
import { Subscription, Observable } from 'rxjs';

/**
 * Base class for Dynamic Fields
 */
export class BaseDynamicFieldComponent<T extends DynamicField> implements OnInit, OnDestroy {

  @Input() field: T;
  @Input() form: FormGroup;
  @Input() editing = true;
  @Input() isHorizontal: boolean;
  @Input() labelWidth: number;

  control: FormControl;
  visible = true;
  public hideLabelOnEdit = false;


  subscriptions = new Subscription;


  /** If true, the read-only state will show the value obtained from the formattedValue method;
   * otherwise, will keep the component to manage this behavior.
   */
  public useFormattedValueOnReadonly = true;

  constructor() {
    this.subscriptions = new Subscription();
  }

  ngOnInit() {
    this.control = this.form.controls[this.elementId] as FormControl;
    if (this.field.visibilityFn) {
      const formRoot = this.form.root; // Make sure to get the root form, even for nested FromGroups
      this.subscriptions.add(formRoot.valueChanges.subscribe(val => {
        this.visible = this.field.visibilityFn(val);
        if (!this.visible && !this.field.keepValueWhenHiding) {
          this.control.setValue(null, { emitEvent: false });
        }
      }));
      this.visible = this.field.visibilityFn(formRoot.value);
    }

    if (this.field.onChangeFn) {

      this.subscriptions.add(
        this.control.valueChanges.subscribe(val => {
          this.field.onChangeFn(val);
        })
      );
    }

  }

  ngOnDestroy() {
    if (this.subscriptions) {
      this.subscriptions.unsubscribe();
    }
  }

  /**
   * Property to be used as the Form Element ID
   */
  get elementId(): string {
    return String(this.field.key);
  }

  get instance(): BaseDynamicFieldComponent<T> {
    return this;
  }

  get isOptional(): boolean {
    return !this.field.validators || !this.field.validators.find(el => el === Validators.required);
  }

  get isEditing(): boolean {
    return this.editing && !this.field.readOnly;
  }

  get formattedValue(): string {
    let rawValue = this.form.controls[this.elementId].value;
    return rawValue != null ? rawValue : '-';
  }

  displayFieldCss(): { [k: string]: boolean } {
    return {
      'has-error': this.control.touched && this.control.invalid,
      'has-feedback': this.control.touched && this.control.invalid
    };
  }

}

results matching ""

    No results matching ""