All files / formly/structural/si-formly-object-grid si-formly-object-grid.component.ts

96.29% Statements 26/27
76.19% Branches 16/21
100% Functions 6/6
96.15% Lines 25/26

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                                1x 4x           4x     2x 1x   1x       2x 2x       2x       2x   2x                 2x 2x 2x 4x   4x 6x       6x 6x 4x   6x   4x 4x 2x     4x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FieldType, FormlyModule } from '@ngx-formly/core';
import { SiFormContainerComponent } from '@siemens/element-ng/form';
 
import { GridColumnConfig, GridRow, ToGridRowConfig } from './si-formly-object-grid.model';
 
@Component({
  selector: 'si-formly-object-grid',
  imports: [FormlyModule, SiFormContainerComponent],
  templateUrl: './si-formly-object-grid.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiFormlyObjectGridComponent extends FieldType implements OnInit {
  protected rows: GridRow[] = [];
 
  /**
   * Template option to suppress displaying error messages that relate
   * to the formControl of this grid (see formControl.errors).
   */
  private suppressFormErrorDisplay = false;
 
  protected get containerClass(): string | string[] {
    if (Array.isArray(this.props.containerClass) && this.props.containerClass.length > 0) {
      return this.props.containerClass;
    }
    return typeof this.props.containerClass === 'string' ? this.props.containerClass : 'container';
  }
 
  ngOnInit(): void {
    this.setRows();
    this.suppressFormErrorDisplay = this.props.suppressFormErrorDisplay === true;
  }
 
  protected get displayErrorMessages(): boolean {
    return this.showError && !!this.formControl.errors && !this.suppressFormErrorDisplay;
  }
 
  private setRows(): void {
    this.rows.length = 0;
 
    Iif (!Array.isArray(this.props.gridConfig) || this.props.gridConfig.length === 0) {
      this.rows = [
        {
          classes: ['row'],
          columns: [{ classes: ['col-sm'], fields: this.field.fieldGroup }]
        }
      ];
    }
 
    const gridConfig = this.props.gridConfig as ToGridRowConfig[];
    const fieldGroup = [...(this.field.fieldGroup ?? [])];
    gridConfig.forEach(rowConfig => {
      const columns: GridColumnConfig[] = [];
 
      rowConfig.columns.forEach(config => {
        const fields = fieldGroup.splice(
          0,
          config.fieldCount < 0 ? fieldGroup.length : config.fieldCount
        );
        const colClasses = config.classes ?? [];
        if (colClasses.length === 0) {
          colClasses.push('col');
        }
        columns.push({ fields, classes: colClasses });
      });
      const rowClasses = rowConfig.classes ?? [];
      if (rowClasses.length === 0) {
        rowClasses.push('row');
      }
 
      this.rows.push({ classes: rowClasses, columns });
    });
  }
}