File

libs/ui/utilities/src/lib/reactive-form-base/reactive-form-base.component.ts

Description

This is the base class for all custom reactive form elements.

Example

export class TsInputComponent extends TsReactiveFormBaseComponent {}

Metadata

Index

Properties
Methods
Inputs
Accessors

Inputs

formControl
Type : FormControl
Default value : new FormControl()

Define the form control to get access to validators

Methods

Public onBlur
onBlur()

Set touched on blur

Returns : void
Protected registerOnChange
registerOnChange(fn: (_: any) => void)

Register onChange callback (from ControlValueAccessor interface)

Parameters :
Name Type Optional
fn function No
Returns : void
Protected registerOnTouched
registerOnTouched(fn: () => void)

Register onTouched callback (from ControlValueAccessor interface)

Parameters :
Name Type Optional
fn function No
Returns : void
Protected writeValue
writeValue(value: any)

Write value to inner value (from ControlValueAccessor interface)

Parameters :
Name Type Optional
value any No
Returns : void

Properties

Protected innerValue
Type : any
Default value : ''

Define the internal data model

Protected onChangeCallback
Type : function
Default value : noop

Define placeholder for callback (provided later by the control value accessor)

Protected onTouchedCallback
Type : function
Default value : noop

Define placeholder for callback (provided later by the control value accessor)

Accessors

value
getvalue()
setvalue(v: any)

Set the accessor and call the onchange callback

Parameters :
Name Type Optional
v any No
Returns : void
import {
  Component,
  Input,
} from '@angular/core';
import { FormControl } from '@angular/forms';

import { noop } from '@terminus/fe-utilities';


/**
 * This is the base class for all custom reactive form elements.
 *
 * @example
 * export class TsInputComponent extends TsReactiveFormBaseComponent {}
 */
// NOTE: OnPush will be enabled in all classes that extend this class.
// eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection, @angular-eslint/use-component-selector
@Component({ template: `` })
export class TsReactiveFormBaseComponent {
  /**
   * Define the internal data model
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  protected innerValue: any = '';

  /**
   * Define placeholder for callback (provided later by the control value accessor)
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  protected onChangeCallback: (_: any) => void = noop;

  /**
   * Define placeholder for callback (provided later by the control value accessor)
   */
  protected onTouchedCallback: () => void = noop;

  /**
   * Define the form control to get access to validators
   */
  @Input()
  public formControl: FormControl = new FormControl();

  /**
   * Set the accessor and call the onchange callback
   *
   * @param v
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  public set value(v: any) {
    if (v !== this.innerValue) {
      this.innerValue = v;
      this.onChangeCallback(v);
    }
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  public get value(): any {
    return this.innerValue;
  }

  /**
   * Set touched on blur
   */
  public onBlur() {
    this.onTouchedCallback();
  }

  /**
   * Register onChange callback (from ControlValueAccessor interface)
   *
   * @param fn
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  protected registerOnChange(fn: (_: any) => void) {
    this.onChangeCallback = fn;
  }

  /**
   * Register onTouched callback (from ControlValueAccessor interface)
   *
   * @param fn
   */
  protected registerOnTouched(fn: () => void) {
    this.onTouchedCallback = fn;
  }

  /**
   * Write value to inner value (from ControlValueAccessor interface)
   *
   * @param value
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  protected writeValue(value: any) {
    // istanbul ignore else
    if (value !== this.innerValue) {
      this.innerValue = value;
    }
  }
}

result-matching ""

    No results matching ""