All files / formly si-formly.component.ts

100% Statements 39/39
100% Branches 10/10
100% Functions 6/6
100% Lines 39/39

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                                  1x           13x   13x                 13x                 13x         13x             13x   13x     13x   13x   20x 20x 20x   8x 4x 4x   8x     20x 4x   16x   20x 9x   20x 20x     13x 13x     13x 11x 11x         9x 20x 28x 28x 28x 10x   28x 28x 1x         9x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  Component,
  computed,
  inject,
  input,
  model,
  OnInit,
  output,
  viewChild
} from '@angular/core';
import { AbstractControl, FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyForm, FormlyFormOptions } from '@ngx-formly/core';
import { FormlyJsonschema } from '@ngx-formly/core/json-schema';
import { JSONSchema7 } from 'json-schema';
 
@Component({
  selector: 'si-formly',
  // eslint-disable-next-line @angular-eslint/prefer-standalone
  standalone: false,
  templateUrl: './si-formly.component.html'
})
export class SiFormlyComponent<TControl extends { [K in keyof TControl]: AbstractControl }>
  implements OnInit
{
  /**
   * Formly main container to provide modelChange subscriptions.
   */
  readonly formlyForm = viewChild(FormlyForm);
 
  readonly form = model<FormGroup<TControl>>();
  /**
   * Mapping of field name and its value.
   *
   * @defaultValue
   * ```
   * {}
   * ```
   */
  readonly model = model({});
  /**
   * Define FormlyFormOptions.
   *
   * @defaultValue
   * ```
   * {}
   * ```
   */
  readonly options = input<FormlyFormOptions>({});
 
  /**
   * JSONSchema7 can be used instead of FormlyFieldConfig array for defining form fields.
   * */
  readonly schema = model<JSONSchema7>();
 
  /**
   * Define all form fields with FormlyFieldConfig array.
   *
   * @defaultValue []
   */
  readonly fields = input<FormlyFieldConfig[]>([]);
 
  readonly fieldsChange = output<FormlyFieldConfig[]>();
 
  /** Define width for field labels in pixel */
  readonly labelWidth = input<number | undefined>();
 
  protected readonly fieldConfig = computed<FormlyFieldConfig[]>(() => {
    let formlyFieldConfig!: FormlyFieldConfig[];
    const schema = this.schema();
    const labelWidth = this.labelWidth();
    const opts = {
      map: (field: FormlyFieldConfig, s: JSONSchema7): FormlyFieldConfig => {
        if (labelWidth) {
          field.props ??= {};
          field.props.labelWidth = labelWidth;
        }
        return field;
      }
    };
    if (schema) {
      formlyFieldConfig = [this.formlyJsonschema.toFieldConfig(schema, opts)];
    } else {
      formlyFieldConfig = this.fields();
    }
    if (formlyFieldConfig && formlyFieldConfig.length > 0) {
      this.applyLabelWidth(formlyFieldConfig);
    }
    this.fieldsChange.emit(formlyFieldConfig);
    return formlyFieldConfig;
  });
 
  protected ownForm = false;
  private readonly formlyJsonschema = inject(FormlyJsonschema);
 
  ngOnInit(): void {
    if (!this.form()) {
      this.ownForm = true;
      this.form.set(new FormGroup<TControl>({} as TControl));
    }
  }
 
  private applyLabelWidth(formlyFieldConfig: FormlyFieldConfig[]): void {
    const apply = (cfg: FormlyFieldConfig[]): void => {
      cfg.forEach(field => {
        field.props ??= {};
        field.props.labelWidth = this.labelWidth();
        if (Array.isArray(field.fieldGroup)) {
          apply(field.fieldGroup);
        }
        if (typeof field.fieldArray !== 'function') {
          if (Array.isArray(field.fieldArray?.fieldGroup)) {
            apply(field.fieldArray!.fieldGroup);
          }
        }
      });
    };
    apply(formlyFieldConfig);
  }
}