File

libs/core/dynamic-form/checkbox/src/checkbox.component.ts

Extends

DfBaseComponent

Implements

OnInit DoCheck

Metadata

Index

Properties
Methods
Inputs
Outputs

Inputs

control
Type : UntypedFormControl
Default value : new UntypedFormControl()
Inherited from DfBaseComponent
Defined in DfBaseComponent:63
config
Type : C
Required :  true
Inherited from DfBaseComponent
Defined in DfBaseComponent:50

The configuration object for this formfield.

Note that derived formfield components should extend the DfBaseConfig config interface as needed and expose that their own config interface.

formAclPath
Type : string
Inherited from DfBaseComponent
Defined in DfBaseComponent:75
isAclHandled
Type : boolean
Default value : false
Inherited from DfBaseComponent
Defined in DfBaseComponent:79
isRetailChannel
Type : boolean
Inherited from DfBaseComponent
Defined in DfBaseComponent:77
validationConfigs
Type : ValidationConfig[] | undefined
Inherited from DfBaseComponent
Defined in DfBaseComponent:73

Outputs

formEvent
Type : DfEventPayload
Inherited from DfBaseComponent
Defined in DfBaseComponent:86

Emits when events associated to the form control happen.

The emitted object contains the data necessary to uniquely identify the event (field id and event type). It also contains the event data.

Methods

onBlur
onBlur(event: FocusEvent)
Parameters :
Name Type Optional
event FocusEvent No
Returns : void

Properties

isCheckboxGroup
Type : unknown
Default value : computed(() => this.config().type === 'CHECKBOX_GROUP')
isHorizontal
Type : unknown
Default value : computed(() => !!this.groupLayout()?.horizontal)
componentOrControlInitFinished
Type : unknown
Default value : new ReplaySubject<AbstractControl | undefined>(1)
Inherited from DfBaseComponent

This ReplaySubject is provided to emit the control once it is initialized.

import {
  DfBaseComponent,
  DfOptions,
  DfOptionsProviderService
} from '@allianz/taly-core/dynamic-form';
import { RowJustification } from '@allianz/ng-aquila/grid';
import {
  Component,
  ElementRef,
  input,
  OnInit,
  inject,
  computed,
  Signal,
  signal,
  Injector,
  runInInjectionContext,
  DoCheck
} from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormGroupDirective, NgForm, UntypedFormControl } from '@angular/forms';
import { of } from 'rxjs';
import { CheckboxLabelSize, DfCheckboxConfig, DfCheckboxGroupConfig } from './checkbox.model';
import { ErrorStateMatcher } from '@allianz/ng-aquila/utils';

@Component({
  selector: 'df-checkbox',
  styleUrls: ['./checkbox.component.scss'],
  templateUrl: './checkbox.component.html',
  standalone: false
})
export class DfCheckboxComponent
  extends DfBaseComponent<DfCheckboxConfig | DfCheckboxGroupConfig>
  implements OnInit, DoCheck
{
  private readonly _parentForm = inject(NgForm, { optional: true });
  private readonly _parentFormGroup = inject(FormGroupDirective, { optional: true });
  private readonly errorStateMatcher = inject(ErrorStateMatcher);

  private readonly _elementRef = inject(ElementRef);

  private optionsProviderService = inject(DfOptionsProviderService, { optional: true });
  private injector = inject(Injector);

  private groupLayout = computed(() => {
    const config = this.config();
    return config.type === 'CHECKBOX_GROUP' ? config.layout ?? null : null;
  });

  private readonly DEFAULT_OPTION_COLUMN_SPAN = 4;

  protected errorState = false;
  protected checkboxOptions: Signal<DfOptions[]> = signal([]);
  protected horizontalOptionsAlignment = computed(() => this.computeHorizontalOptionsAlignment());
  protected groupLabelCol = computed(() => this.computeGroupLabelCol());
  protected labelCenter = computed(() => this.isRetailChannel() && this.groupLabelCol() === '12');
  protected optionColumnSpan = computed(() => {
    const layout = this.groupLayout();
    const columnSpan = (layout && layout.optionsColumnSpan) ?? this.DEFAULT_OPTION_COLUMN_SPAN;
    return `12, 12, ${columnSpan}`;
  });

  override control = input<UntypedFormControl>(new UntypedFormControl());
  isHorizontal = computed(() => !!this.groupLayout()?.horizontal);
  isCheckboxGroup = computed(() => this.config().type === 'CHECKBOX_GROUP');
  protected labelSize = computed(() =>
    this.config().labelSizeSmall ? CheckboxLabelSize.Small : CheckboxLabelSize.Large
  );

  override ngOnInit() {
    super.ngOnInit();

    if (this.config().type === 'CHECKBOX_GROUP') {
      this.checkboxOptions = this.getCheckboxOptions();
    }

    this.emitFormControlEventOnValueChanges();
  }

  ngDoCheck(): void {
    const controlValue = this.control();
    if (controlValue) {
      // We follow the Aquila approach with the ErrorStateMatcher here:
      // We need to re-evaluate this on every change detection cycle, because there are some
      // error triggers that we can't subscribe to (e.g. parent form submissions). This means
      // that whatever logic is in here has to be super lean or we risk destroying the performance.
      const newErrorState = this.errorStateMatcher.isErrorState(
        controlValue,
        this._parentFormGroup || this._parentForm
      );

      if (this.errorState !== newErrorState) {
        this.errorState = newErrorState;
      }
    }
  }

  private computeHorizontalOptionsAlignment(): RowJustification {
    const layout = this.groupLayout();

    if (!layout?.horizontal) return 'start';

    const itemsPerRow = Math.floor(
      12 / (layout.optionsColumnSpan || this.DEFAULT_OPTION_COLUMN_SPAN)
    );
    const hasMultipleRows = this.checkboxOptions().length > itemsPerRow;

    return this.isRetailChannel() && !hasMultipleRows ? 'center' : 'start';
  }

  private computeGroupLabelCol(): string {
    const layout = this.groupLayout();

    if (
      layout &&
      this.isRetailChannel() &&
      layout.horizontal !== true &&
      layout.groupLabelLeftAlignInRetail
    ) {
      const columnSpan = layout.optionsColumnSpan || this.DEFAULT_OPTION_COLUMN_SPAN;
      return `12, 12, ${columnSpan}`;
    }

    return '12';
  }

  private getCheckboxOptions(): Signal<DfOptions[]> {
    const groupConfig = this.config();

    if (groupConfig.type !== 'CHECKBOX_GROUP') {
      return signal([]);
    }

    if (Array.isArray(groupConfig.options)) {
      return signal(groupConfig.options);
    }

    if (typeof groupConfig.options === 'string' && this.optionsProviderService) {
      return runInInjectionContext(this.injector, () =>
        toSignal(
          this.optionsProviderService?.getDfOptions(groupConfig.options as string) ?? of([]),
          {
            initialValue: []
          }
        )
      );
    }

    return signal([]);
  }

  onBlur(event: FocusEvent): void {
    const checkboxGroupHasFocus = this._elementRef.nativeElement.contains(event.relatedTarget);

    if (!checkboxGroupHasFocus) {
      this.emitFormEvent('onBlurEvent', this.control().value);
    }
  }
}
<!-- Single Checkbox -->
@if (!isCheckboxGroup()) {
<div class="df-checkbox__container">
  <nx-checkbox
    [formControl]="control()"
    [labelSize]="labelSize()"
    [attr.data-testid]="config().testId"
    class="nx-margin-0"
    (focusout)="emitFormEvent('onBlurEvent', control().value)"
  >
    <span>{{ config().label | interpolateFromStore | async }}</span>
    <br />
    <span>{{ config().hint | interpolateFromStore | async }}</span>
    @if (errorState) {
    <taly-validation-errors
      ngProjectAs="nx-error"
      nxFormfieldError
      [errorMessages]="validationConfigs()"
      [controlErrors]="control().errors"
    >
    </taly-validation-errors>
    }
  </nx-checkbox>
  @if (config().infoIcon) {
  <df-info-icon [config]="config().infoIcon"></df-info-icon>
  }
</div>
} @else {
<!-- Checkbox Group -->
<fieldset nxLayout="grid nopadding" [containerQuery]="true" class="df-checkbox-group__container">
  <div nxRow [rowJustify]="isRetailChannel() ? 'center' : 'start'">
    <div
      [nxCol]="groupLabelCol()"
      [ngClass]="{ 'text-center': labelCenter() }"
      data-testid="groupLabelCol"
    >
      <nx-label
        data-testid="checkboxLabel"
        [size]="isRetailChannel() ? 'large' : 'small'"
        [ngClass]="{ 'nx-font-weight-regular': isRetailChannel(), 'text-center': labelCenter() }"
      >
        {{ config().label | interpolateFromStore | async }}@if (config().infoIcon) {
        <df-info-icon nxFormfieldAppendix [config]="config().infoIcon"></df-info-icon>
        }
      </nx-label>
    </div>
  </div>

  <nx-checkbox-group
    [formControl]="control()"
    [name]="config().id"
    [attr.data-testid]="config().testId"
    (focusout)="onBlur($event)"
  >
    @if (isHorizontal()) {
    <div nxRow [rowJustify]="horizontalOptionsAlignment()" data-testid="rowInHorizontalLayout">
      @for (option of checkboxOptions(); track $index) {
      <div [nxCol]="optionColumnSpan()" data-testid="columnInHorizontalLayout">
        <nx-checkbox
          [value]="option.value"
          [labelSize]="labelSize()"
          [attr.data-testid]="option?.testId"
        >
          <span>{{ option.label | interpolateFromStore | async }}</span>
        </nx-checkbox>
      </div>
      }
    </div>
    } @else { @for (option of checkboxOptions(); track $index) {
    <div
      nxRow
      [rowJustify]="isRetailChannel() ? 'center' : 'start'"
      data-testid="rowInVerticalLayout"
    >
      <div [nxCol]="optionColumnSpan()" data-testid="columnInVerticalLayout">
        <nx-checkbox
          [value]="option.value"
          [labelSize]="labelSize()"
          [attr.data-testid]="option?.testId"
        >
          <span>{{ option.label | interpolateFromStore | async }}</span>
        </nx-checkbox>
      </div>
    </div>
    } } @if (errorState) {
    <taly-validation-errors
      ngProjectAs="nx-error"
      nxFormfieldError
      [errorMessages]="validationConfigs()"
      [controlErrors]="control().errors"
    >
    </taly-validation-errors>
    }
  </nx-checkbox-group>

  @if (config().hint) {
  <br />
  <span>{{ config().hint | interpolateFromStore | async }}</span>
  }
</fieldset>
} @if (config().note && !errorState) {
<nx-message context="info">
  <span>{{ config().note | interpolateFromStore | async }}</span>
</nx-message>
}

./checkbox.component.scss

:host {
  display: block;
}

.df-checkbox__container {
  display: flex;
  flex-direction: row;
  justify-content: start;
  align-items: start;

  df-info-icon {
    margin-left: 8px;
  }
}

.text-center {
  text-align: center;
}

.df-checkbox-group__container {
  nx-checkbox {
    margin-top: var(--vertical-inner-section-spacing);
    margin-bottom: 0;
  }

  df-info-icon {
    display: inline-flex;
    align-items: center;
    padding-left: 8px;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""