libs/core/dynamic-form/dropdown/src/dropdown.component.ts
selector | df-dropdown |
styleUrls | ./dropdown.component.scss |
templateUrl | ./dropdown.component.html |
Properties |
Methods |
constructor(optionsProviderService: DfOptionsProviderService, formfieldDefaultOptions: FormfieldDefaultOptions)
|
|||||||||
Parameters :
|
getDropDownOptions |
getDropDownOptions()
|
Returns :
Observable<DfOptions[]>
|
Public appearanceType |
Type : AppearanceType
|
Default value : 'auto'
|
control |
Default value : input<UntypedFormControl>(new UntypedFormControl())
|
Inherited from
DfBaseComponent
|
Defined in
DfBaseComponent:28
|
dropDownOptions$ |
Type : Observable<DfOptions[]> | undefined
|
Public floatLabelType |
Type : FloatLabelType
|
Default value : 'auto'
|
Optional labelAlwaysFloating |
Type : boolean
|
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
|
Defined in
DfBaseComponent:103
|
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 |
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 {
DfBaseComponent,
DfOptions,
DfOptionsProviderService
} from '@allianz/taly-core/dynamic-form';
import { Component, Inject, input, OnInit, Optional } from '@angular/core';
import { UntypedFormControl } from '@angular/forms';
import {
AppearanceType,
FloatLabelType,
FORMFIELD_DEFAULT_OPTIONS,
FormfieldDefaultOptions
} from '@aposin/ng-aquila/formfield';
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 {
public appearanceType: AppearanceType = 'auto';
public floatLabelType: FloatLabelType = 'auto';
override control = input<UntypedFormControl>(new UntypedFormControl());
dropDownOptions$: Observable<DfOptions[]> | undefined;
preselect$ = new BehaviorSubject<string>('');
labelAlwaysFloating?: boolean;
constructor(
@Optional() private optionsProviderService: DfOptionsProviderService,
@Optional()
@Inject(FORMFIELD_DEFAULT_OPTIONS)
private formfieldDefaultOptions: FormfieldDefaultOptions
) {
super();
// Don't overwrite the value from the InjectionToken, if provided.
// Be aware that the `expert` style always needs the float label.
this.floatLabelType = formfieldDefaultOptions?.nxFloatLabel || 'auto';
}
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('');
}
}
})
);
/* If label is not floating, then we pass the label to the nx-formfield.
** TODO: exchange this logic when NDBX support that with this config is supported multiple lines
*/
this.labelAlwaysFloating = this.formfieldDefaultOptions?.nxFloatLabel === 'always';
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)) {
return of(configValue.options);
}
if (typeof configValue.options === 'string' && this.optionsProviderService) {
return this.optionsProviderService.getDfOptions(configValue.options);
}
return of([]);
}
private setUpExpertMode() {
if (!this.isRetailChannel()) {
this.appearanceType = 'outline';
this.floatLabelType = 'always';
}
}
protected onBlur() {
this.emitFormEvent('onBlurEvent', this.control().value);
}
}
<ng-container *aclTag="aclResource">
<span nxCopytext class="own-label nx-font-weight-semibold" *ngIf="labelAlwaysFloating">
{{ config().label | interpolateFromStore | async }}
<span *ngIf="config().optionalLabel" class="nx-font-weight-regular">
({{ config().optionalLabel | interpolateFromStore | async }})</span
>
</span>
<nx-formfield
[label]="!labelAlwaysFloating ? (config().label | interpolateFromStore | async) : ''"
[floatLabel]="floatLabelType"
[optionalLabel]="
!labelAlwaysFloating ? (config().optionalLabel | interpolateFromStore | async) || '' : ''
"
[appearance]="appearanceType"
>
<span *ngIf="config().inputPrefix" 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) || '' }}"
>
<nx-dropdown-item *ngIf="config().clearOptionLabel && control().value">
<span>{{ config().clearOptionLabel | interpolateFromStore | async }}</span>
</nx-dropdown-item>
<ng-container *ngFor="let optionConfig of dropDownOptions$ | async">
<nx-dropdown-item [value]="optionConfig.value" #thisInput>
<span>{{ optionConfig.label | interpolateFromStore | async }}</span>
</nx-dropdown-item>
</ng-container>
</nx-dropdown>
<span *ngIf="config().inputSuffix" nxFormfieldSuffix>
{{ config().inputSuffix | interpolateFromStore | async }}
</span>
<span *ngIf="config().hint" nxFormfieldHint>
{{ config().hint | interpolateFromStore | async }}
</span>
<df-info-icon
*ngIf="config().infoIcon"
nxFormfieldAppendix
[config]="config().infoIcon"
></df-info-icon>
<taly-validation-errors
nxFormfieldError
[errorMessages]="validationConfigs()"
[controlErrors]="control().errors"
>
</taly-validation-errors>
<nx-message *ngIf="config().note" context="info" nxFormfieldNote>
<span>{{ config().note | interpolateFromStore | async }}</span>
</nx-message>
</nx-formfield>
</ng-container>
./dropdown.component.scss
:host {
display: block;
}
/* Own label management.TODO: Remove when NDBX support multiple lines */
::ng-deep.own-label + * {
.nx-formfield__wrapper {
padding-top: 4px !important;
}
}