All files / formly/fields/button si-formly-button.component.ts

95.23% Statements 20/21
90% Branches 9/10
100% Functions 1/1
95% Lines 19/20

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                            1x   8x 8x 8x 1x   8x       8x 5x   5x 5x 5x 4x   1x     1x     5x     3x 3x 3x   1x                
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { NgClass } from '@angular/common';
import { Component } from '@angular/core';
import { FieldType, FormlyModule } from '@ngx-formly/core';
import { SiTranslatePipe } from '@siemens/element-translate-ng/translate';
 
@Component({
  selector: 'si-formly-button',
  imports: [NgClass, SiTranslatePipe, FormlyModule],
  templateUrl: './si-formly-button.component.html'
})
export class SiFormlyButtonComponent extends FieldType {
  protected click(): void {
    const listener = this.props.clickListener;
    let args = this.props.clickArgs ?? [];
    if (args && !Array.isArray(args)) {
      args = [args];
    }
    Iif (!listener) {
      return;
    }
 
    if (typeof listener === 'string') {
      try {
        /* eslint-disable */
        const getFn = Function('formState', 'model', 'field', `return ${listener};`);
        const fn = getFn.apply(this, [this.formState, this.model, this.field]);
        if (typeof fn === 'function') {
          fn.apply(this, args);
        } else {
          console.warn(`The dyn ui button ${this.key} has no valid click listener`);
        }
      } catch (error) {
        console.warn(`Error while executing dyn ui button "${this.key}" click listener.`, error);
      }
      /* eslint-enable */
      return;
    }
 
    if (typeof listener === 'function') {
      try {
        listener.apply(this, args);
      } catch (error) {
        console.warn(
          `Error while executing dyn ui button "${this.key}" direct click listener.`,
          error
        );
      }
    }
  }
}