All files / formly/structural/si-formly-accordion si-formly-accordion.component.ts

100% Statements 12/12
100% Branches 5/5
100% Functions 3/3
100% Lines 10/10

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                            1x   1x 1x 1x 1x     1x 1x         2x 2x 2x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { AfterViewInit, ChangeDetectionStrategy, Component } from '@angular/core';
import { FieldType, FormlyFieldConfig, FormlyModule } from '@ngx-formly/core';
import { SiAccordionComponent, SiCollapsiblePanelComponent } from '@siemens/element-ng/accordion';
 
@Component({
  selector: 'si-formly-accordion',
  imports: [SiCollapsiblePanelComponent, FormlyModule, SiAccordionComponent],
  templateUrl: './si-formly-accordion.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiFormlyAccordionComponent extends FieldType implements AfterViewInit {
  protected panelToggle(toggle: boolean, fieldGroup: FormlyFieldConfig): void {
    if (this.field.fieldGroup) {
      const openPanelField = this.field.fieldGroup?.find(f => f.props?.opened);
      if (openPanelField?.props) {
        openPanelField.props.opened = false;
      }
    }
    if (fieldGroup.props) {
      fieldGroup.props.opened = toggle;
    }
  }
 
  ngAfterViewInit(): void {
    if (this.props?.expandFirstPanel !== false) {
      if (this.field.fieldGroup?.[0].props) {
        this.field.fieldGroup[0].props.opened = true;
      }
    }
  }
}