libs/core/dynamic-form/radio/src/radio.component.ts
DfBaseComponent<DfRadioConfig>
| changeDetection | ChangeDetectionStrategy.OnPush |
| selector | df-radio |
| styleUrls | ./radio.component.scss |
| templateUrl | ./radio.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
| control | |
Type : UntypedFormControl
|
|
Default value : new UntypedFormControl()
|
|
|
Inherited from
DfBaseComponent
|
|
| config | |
Type : C
|
|
| Required : true | |
|
Inherited from
DfBaseComponent
|
|
|
The configuration object for this formfield. Note that derived formfield components should extend the |
|
| formAclPath | |
Type : string
|
|
|
Inherited from
DfBaseComponent
|
|
| isAclHandled | |
Type : boolean
|
|
Default value : false
|
|
|
Inherited from
DfBaseComponent
|
|
| isRetailChannel | |
Type : boolean
|
|
|
Inherited from
DfBaseComponent
|
|
| validationConfigs | |
Type : ValidationConfig[] | undefined
|
|
|
Inherited from
DfBaseComponent
|
|
| formEvent | |
Type : DfEventPayload
|
|
|
Inherited from
DfBaseComponent
|
|
|
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. |
|
| getRadioOptions |
getRadioOptions()
|
|
Returns :
Signal<DfOptions[]>
|
| onBlur | ||||||
onBlur(event: FocusEvent)
|
||||||
|
Parameters :
Returns :
void
|
| Readonly groupLabelCol |
Type : unknown
|
Default value : computed(() => this.computeGroupLabelCol())
|
| Readonly horizontalOptionsAlignment |
Type : unknown
|
Default value : computed(() => this.computeHorizontalOptionsAlignment())
|
| Readonly labelCenter |
Type : unknown
|
Default value : computed(() => this.isRetailNonA1() && !this.isGroupLabelLeftAligned())
|
| Readonly radioOptions |
Type : Signal<DfOptions[]>
|
Default value : toSignal(
toObservable(this.config).pipe(
switchMap((configValue) => {
if (Array.isArray(configValue.options)) return of(configValue.options);
if (typeof configValue.options === 'string' && this.optionsProviderService) {
return this.optionsProviderService.getDfOptions(configValue.options);
}
return of([]);
})
),
{ initialValue: [] }
)
|
| 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. |
| errorMessages |
Type : WritableSignal<Observable[]>
|
Default value : signal([])
|
|
Inherited from
DfBaseComponent
|
|
The resolved validation error messages for the current control state. |
| Readonly nxErrorAppearance |
Type : ErrorStyleType
|
Default value : inject(ERROR_DEFAULT_OPTIONS, { optional: true })?.appearance ||
(inject<CHANNEL>(CHANNEL_TOKEN) === CHANNEL.EXPERT ? 'text' : 'message')
|
|
Inherited from
DfBaseComponent
|
import { RowJustification } from '@allianz/ng-aquila/grid';
import { IS_A1 } from '@allianz/taly-core';
import {
CONTAINER_BREAKPOINT_M,
DfBaseComponent,
DfOptions,
DfOptionsProviderService,
FORM_CONTAINER_WIDTH,
VerticalLayout
} from '@allianz/taly-core/dynamic-form';
import {
ChangeDetectionStrategy,
Component,
computed,
ElementRef,
inject,
input,
OnInit,
Signal
} from '@angular/core';
import { toObservable, toSignal } from '@angular/core/rxjs-interop';
import { UntypedFormControl } from '@angular/forms';
import { of, switchMap } from 'rxjs';
import { DfRadioConfig } from './radio.model';
@Component({
selector: 'df-radio',
styleUrls: ['./radio.component.scss'],
templateUrl: './radio.component.html',
standalone: false,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DfRadioComponent extends DfBaseComponent<DfRadioConfig> implements OnInit {
private optionsProviderService = inject(DfOptionsProviderService, { optional: true });
private el = inject(ElementRef);
override control = input<UntypedFormControl>(new UntypedFormControl());
protected isA1 = inject(IS_A1);
protected isRetailNonA1 = computed(() => this.isRetailChannel() && !this.isA1);
private formContainerWidth = inject(FORM_CONTAINER_WIDTH, { optional: true });
readonly radioOptions: Signal<DfOptions[]> = toSignal(
toObservable(this.config).pipe(
switchMap((configValue) => {
if (Array.isArray(configValue.options)) return of(configValue.options);
if (typeof configValue.options === 'string' && this.optionsProviderService) {
return this.optionsProviderService.getDfOptions(configValue.options);
}
return of([]);
})
),
{ initialValue: [] }
);
readonly horizontalOptionsAlignment = computed(() => this.computeHorizontalOptionsAlignment());
private isGroupLabelLeftAligned = computed(
() =>
this.isRetailChannel() &&
!!(this.config().layout as VerticalLayout)?.groupLabelLeftAlignInRetail
);
readonly groupLabelCol = computed(() => this.computeGroupLabelCol());
readonly optionsColumn = computed(() => {
const span = this.config().layout?.optionsColumnSpan || 4;
const containerWidth = this.formContainerWidth?.() ?? Infinity;
return containerWidth <= CONTAINER_BREAKPOINT_M ? '12' : `${span}`;
});
readonly labelCenter = computed(() => this.isRetailNonA1() && !this.isGroupLabelLeftAligned());
override ngOnInit() {
super.ngOnInit();
this.emitFormControlEventOnValueChanges();
}
getRadioOptions(): Signal<DfOptions[]> {
return this.radioOptions;
}
onBlur(event: FocusEvent) {
const radioGroupHasFocus = this.el.nativeElement.contains(event.relatedTarget);
if (radioGroupHasFocus) {
return;
}
this.emitFormEvent('onBlurEvent', this.control().value);
}
private computeHorizontalOptionsAlignment() {
let alignment: RowJustification = 'start';
if (this.config().layout?.horizontal) {
const itemsPerRow = Math.floor(12 / (this.config().layout?.optionsColumnSpan || 4));
const hasMultipleRows = this.radioOptions().length > itemsPerRow;
if (this.isRetailNonA1() && !hasMultipleRows) {
alignment = 'center';
}
}
return alignment;
}
private computeGroupLabelCol() {
if (!this.isGroupLabelLeftAligned()) {
return '12';
}
const span = this.config().layout?.optionsColumnSpan || 4;
const containerWidth = this.formContainerWidth?.() ?? Infinity;
return containerWidth <= CONTAINER_BREAKPOINT_M ? '12' : `${span}`;
}
}
<!--Note: We use <fieldset> and <legend> to mark up this group for better a11y.
This way, assistive tech can associate the label text inside <legend> with the group.
If we use other elements (e.g. <h3> or <p>), it can't and, for example, a screenreader
may not read it out at all.
See: https://accessibility.blog.gov.uk/2016/07/22/using-the-fieldset-and-legend-elements/-->
<fieldset nxLayout="grid nopadding">
<div nxRow [rowJustify]="isRetailNonA1() ? 'center' : 'start'">
<div
[nxCol]="groupLabelCol()"
[ngClass]="{ 'text-center': labelCenter() }"
data-testid="groupLabelCol"
>
<nx-label
data-testid="radioLabel"
[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-radio-group
[formControl]="control()"
[name]="config().id"
[attr.data-testid]="config().testId"
(focusout)="onBlur($event)"
>
@if (config().layout?.horizontal) {
<div nxRow [rowJustify]="horizontalOptionsAlignment()" data-testid="rowInHorizontalLayout">
@for (option of radioOptions(); track $index) {
<div [nxCol]="optionsColumn()" data-testid="columnInHorizontalLayout">
<nx-radio
[labelSize]="isRetailChannel() ? 'big' : 'small'"
[value]="option.value"
[attr.data-testid]="option?.testId"
><span>{{ option.label | interpolateFromStore | async }}</span>
</nx-radio>
</div>
}
</div>
} @else { @for (option of radioOptions(); track $index) {
<div
nxRow
[rowJustify]="isRetailNonA1() ? 'center' : 'start'"
data-testid="rowInVerticalLayout"
>
<div [nxCol]="optionsColumn()" data-testid="columnInVerticalLayout">
<nx-radio
[labelSize]="isRetailChannel() ? 'big' : 'small'"
[value]="option.value"
[attr.data-testid]="option?.testId"
><span>{{ option.label | interpolateFromStore | async }}</span>
</nx-radio>
</div>
</div>
} } @for (message$ of errorMessages(); track $index) {
<nx-error class="nx-margin-top-xs" [appearance]="nxErrorAppearance">{{
message$ | async
}}</nx-error>
}
</nx-radio-group>
@if (config().note && !(control().touched && control().invalid)) {
<nx-message context="info">
<span>{{ config().note | interpolateFromStore | async }}</span>
</nx-message>
}
</fieldset>
./radio.component.scss
@use '../../src/breakpoints.scss' as *;
:host {
display: block;
}
nx-radio {
margin-top: var(--vertical-inner-section-spacing);
}
nx-label {
display: inline-flex;
align-items: center;
}
df-info-icon {
display: inline-flex;
align-items: center;
padding-left: 8px;
}
.text-center {
text-align: center;
}