File

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

Extends

DfBaseComponent

Implements

OnInit

Metadata

Index

Properties
Methods

Methods

getDropDownOptions
getDropDownOptions()

Properties

Public appearanceType
Type : AppearanceType
Default value : 'auto'
control
Default value : input<UntypedFormControl>(new UntypedFormControl())
Inherited from DfBaseComponent
Defined in DfBaseComponent:24
dropDownOptions$
Type : Observable<DfOptions[]> | undefined
preselect$
Default value : new BehaviorSubject<string>('')
aclResource
Type : string
Inherited from DfBaseComponent
Defined in DfBaseComponent:56
componentOrControlInitFinished
Default value : new ReplaySubject<AbstractControl | undefined>(1)
Inherited from DfBaseComponent

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

config
Type : InputSignal<C>
Default value : input.required<C>()
Inherited from DfBaseComponent
Defined in DfBaseComponent:64

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
Default value : input<string>()
Inherited from DfBaseComponent
Defined in DfBaseComponent:89
Readonly formEvent
Default value : output<DfEventPayload>()
Inherited from DfBaseComponent
Defined in DfBaseComponent:98

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.

isRetailChannel
Default value : input<boolean>()
Inherited from DfBaseComponent
Defined in DfBaseComponent:91
validationConfigs
Default value : input<ValidationConfig[] | undefined>()
Inherited from DfBaseComponent
Defined in DfBaseComponent:87
import { AppearanceType } from '@allianz/ng-aquila/formfield';
import {
  DfBaseComponent,
  DfOptions,
  DfOptionsProviderService
} from '@allianz/taly-core/dynamic-form';
import { Component, inject, input, OnInit } from '@angular/core';
import { UntypedFormControl } from '@angular/forms';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { delay, tap } from 'rxjs/operators';
import { DfDropdownConfig } from './dropdown.model';

@Component({
  selector: 'df-dropdown',
  styleUrls: ['./dropdown.component.scss'],
  templateUrl: './dropdown.component.html',
  standalone: false
})
export class DfDropdownComponent extends DfBaseComponent<DfDropdownConfig> implements OnInit {
  private optionsProviderService = inject(DfOptionsProviderService, { optional: true });

  public appearanceType: AppearanceType = 'auto';

  override control = input<UntypedFormControl>(new UntypedFormControl());

  dropDownOptions$: Observable<DfOptions[]> | undefined;
  preselect$ = new BehaviorSubject<string>('');

  override ngOnInit() {
    super.ngOnInit();
    this.setUpExpertMode();
    this.dropDownOptions$ = this.getDropDownOptions().pipe(
      delay(0), //fix expression changed after checked
      tap((values) => {
        if (this.config().autoPrefill) {
          if (values) {
            if (values.length === 1) {
              this.preselect$.next(values[0].value);
            } else {
              if (
                this.preselect$.value &&
                !values.map((option) => option.value).includes(this.preselect$.value)
              ) {
                this.preselect$.next('');
              }
            }
          } else {
            this.preselect$.next('');
          }
        }
      })
    );

    this.emitFormControlEventOnValueChanges();
  }

  // TODO: Change this to be an impure pipe (There is already a ticket for that):
  getDropDownOptions(): Observable<DfOptions[]> {
    const configValue = this.config();
    if (Array.isArray(configValue.options)) {
      this.validateNoDuplicateValues(configValue.options);
      return of(configValue.options);
    }

    if (typeof configValue.options === 'string' && this.optionsProviderService) {
      return this.optionsProviderService.getDfOptions(configValue.options).pipe(
        tap((options) => {
          this.validateNoDuplicateValues(options);
        })
      );
    }

    return of([]);
  }

  private validateNoDuplicateValues(options: DfOptions[] | null): void {
    if (!options?.length) {
      return;
    }

    const values = options.map((opt) => opt.value);
    const uniqueValues = new Set(values);
    if (values.length !== uniqueValues.size) {
      throw new Error(
        `Dropdown "${
          this.config().id
        }" contains duplicate option values. Please check your options configuration.`
      );
    }
  }

  private setUpExpertMode() {
    if (!this.isRetailChannel()) {
      this.appearanceType = 'outline';
    }
  }

  protected onBlur() {
    this.emitFormEvent('onBlurEvent', this.control().value);
  }
}
<ng-container *aclTag="aclResource">
  <nx-formfield
    [label]="config().label | interpolateFromStore | async"
    [optionalLabel]="(config().optionalLabel | interpolateFromStore | async) || ''"
    [appearance]="appearanceType"
  >
    @if (config().inputPrefix) {
    <span nxFormfieldPrefix>
      {{ config().inputPrefix | interpolateFromStore | async }}
    </span>
    }

    <nx-dropdown
      [formControl]="control()"
      [id]="config().id"
      [showFilter]="!!config().showFilter"
      [value]="preselect$ | async"
      (valueChange)="preselect$.next($event)"
      (focusOut)="onBlur()"
      [attr.data-testid]="config().testId"
      filterPlaceholder="{{ (config().filterPlaceholder | interpolateFromStore | async) || '' }}"
    >
      @if (config().clearOptionLabel && control().value) {
      <nx-dropdown-item>
        <span>{{ config().clearOptionLabel | interpolateFromStore | async }}</span>
      </nx-dropdown-item>
      } @for (optionConfig of dropDownOptions$ | async; track optionConfig.value) {
      <nx-dropdown-item [value]="optionConfig.value" #thisInput>
        <span>{{ optionConfig.label | interpolateFromStore | async }}</span>
      </nx-dropdown-item>
      }
    </nx-dropdown>

    @if (config().inputSuffix) {
    <span nxFormfieldSuffix>
      {{ config().inputSuffix | interpolateFromStore | async }}
    </span>
    } @if (config().hint) {
    <span nxFormfieldHint>
      {{ config().hint | interpolateFromStore | async }}
    </span>
    } @if (config().infoIcon) {
    <df-info-icon nxFormfieldAppendix [config]="config().infoIcon"></df-info-icon>
    }

    <taly-validation-errors
      nxFormfieldError
      [errorMessages]="validationConfigs()"
      [controlErrors]="control().errors"
    >
    </taly-validation-errors>

    @if (config().note) {
    <nx-message context="info" nxFormfieldNote>
      <span>{{ config().note | interpolateFromStore | async }}</span>
    </nx-message>
    }
  </nx-formfield>
</ng-container>

./dropdown.component.scss

:host {
  display: block;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""