libs/core/dynamic-form/dropdown/src/dropdown.component.ts
| selector | df-dropdown |
| styleUrls | ./dropdown.component.scss |
| templateUrl | ./dropdown.component.html |
Properties |
Methods |
| getDropDownOptions |
getDropDownOptions()
|
|
Returns :
Observable<DfOptions[]>
|
| control |
Default value : input<UntypedFormControl>(new UntypedFormControl())
|
|
Inherited from
DfBaseComponent
|
|
Defined in
DfBaseComponent:23
|
| dropDownOptions$ |
Type : Observable<DfOptions[]> | undefined
|
| interpolatedDropDownOptions$ |
Type : Observable<DfOptions[]> | undefined
|
| componentOrControlInitFinished |
Default value : new ReplaySubject<AbstractControl | undefined>(1)
|
|
Inherited from
DfBaseComponent
|
|
Defined in
DfBaseComponent:91
|
|
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:50
|
|
The configuration object for this formfield. Note that derived formfield components should extend the |
| formAclPath |
Default value : input<string>()
|
|
Inherited from
DfBaseComponent
|
|
Defined in
DfBaseComponent:75
|
| Readonly formEvent |
Default value : output<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. |
| isAclHandled |
Default value : input<boolean>(false)
|
|
Inherited from
DfBaseComponent
|
|
Defined in
DfBaseComponent:79
|
| isRetailChannel |
Default value : input<boolean>()
|
|
Inherited from
DfBaseComponent
|
|
Defined in
DfBaseComponent:77
|
| validationConfigs |
Default value : input<ValidationConfig[] | undefined>()
|
|
Inherited from
DfBaseComponent
|
|
Defined in
DfBaseComponent:73
|
import {
DfBaseComponent,
DfOptions,
DfOptionsProviderService
} from '@allianz/taly-core/dynamic-form';
import { Component, inject, input, OnInit } from '@angular/core';
import { UntypedFormControl } from '@angular/forms';
import { combineLatest, Observable, of } from 'rxjs';
import { delay, map, switchMap, tap } from 'rxjs/operators';
import { DfDropdownConfig } from './dropdown.model';
import { TalyStateService } from '@allianz/taly-core';
@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 });
private talyStateService = inject(TalyStateService, { optional: true });
override control = input<UntypedFormControl>(new UntypedFormControl());
dropDownOptions$: Observable<DfOptions[]> | undefined;
interpolatedDropDownOptions$: Observable<DfOptions[]> | undefined;
override ngOnInit() {
super.ngOnInit();
this.dropDownOptions$ = this.getDropDownOptions().pipe(
delay(0), //fix expression changed after checked
tap((values) => {
const optionValues = values.map((option) => option.value);
const currentValue = this.control().value;
if (this.config().autoPrefill && values.length === 1) {
const singleValue = values[0].value;
this.control().setValue(this.config().multiSelect ? [singleValue] : singleValue);
return;
}
if (currentValue != null) {
// To prevent invalid values in state
if (this.config().multiSelect) {
const isValid =
Array.isArray(currentValue) &&
currentValue.length > 0 &&
currentValue.every((val) => optionValues.includes(val));
if (!isValid) {
this.control().setValue([]);
}
} else {
if (!optionValues.includes(currentValue)) {
this.control().setValue('');
}
}
}
})
);
this.interpolatedDropDownOptions$ = this.createInterpolatedOptions();
this.emitFormControlEventOnValueChanges();
}
private createInterpolatedOptions(): Observable<DfOptions[]> {
if (!this.dropDownOptions$) {
return of([]);
}
return this.dropDownOptions$.pipe(
switchMap((options) => {
if (!options || options.length === 0) {
return of([]);
}
const talyStateService = this.talyStateService;
if (!talyStateService) {
return of(options);
}
const interpolatedOptions$ = options.map((option) =>
talyStateService.interpolateFromStore$(option.label).pipe(
map((interpolatedLabel) => ({
...option,
label: interpolatedLabel
}))
)
);
return combineLatest(interpolatedOptions$);
})
);
}
// 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.`
);
}
}
protected onBlur() {
this.emitFormEvent('onBlurEvent', this.control().value);
}
}
<nx-formfield
[label]="config().label | interpolateFromStore | async"
[optionalLabel]="(config().optionalLabel | interpolateFromStore | async) || ''"
>
@if (config().inputPrefix) {
<span nxFormfieldPrefix>
{{ config().inputPrefix | interpolateFromStore | async }}
</span>
} @if (config().inputPrefix) {
<span nxFormfieldPrefix>
{{ config().inputPrefix | interpolateFromStore | async }}
</span>
} @if (config().multiSelect) {
<nx-multi-select
[formControl]="control()"
[filter]="!!config().showFilter"
[placeholder]="(config().placeholder | interpolateFromStore | async) || ''"
(focusOut)="onBlur()"
[attr.data-testid]="config().testId"
[filterPlaceholder]="(config().filterPlaceholder | interpolateFromStore | async) || ''"
[options]="(interpolatedDropDownOptions$ | async) || []"
selectValue="value"
selectLabel="label"
>
</nx-multi-select>
} @else {
<nx-dropdown
[formControl]="control()"
[showFilter]="!!config().showFilter"
(focusOut)="onBlur()"
[attr.data-testid]="config().testId"
[placeholder]="(config().placeholder | interpolateFromStore | async) || ''"
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 interpolatedDropDownOptions$ | async; track optionConfig.value) {
<nx-dropdown-item [value]="optionConfig.value" #thisInput>
<span>{{ optionConfig.label }}</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>
} @if (config().note) {
<nx-message context="info" nxFormfieldNote>
<span>{{ config().note | interpolateFromStore | async }}</span>
</nx-message>
}
<taly-validation-errors
nxFormfieldError
[errorMessages]="validationConfigs()"
[controlErrors]="control().errors"
>
</taly-validation-errors>
</nx-formfield>
./dropdown.component.scss
:host {
display: block;
}