I want to a create a RadioComponent which populates the input and Label.
I want this component to work with ngModel and reactive forms.
So i created a RadioGroup by implementing ControlValueAccessor, and RadioButton Component which populates Input and Label.
I am calling these component from a parent component where I defined FormGroup but reactive form is not working with this approach.
Can someone please suggest where I am going wrong.
https://stackblitz.com/edit/angular-mipzzw
RadioGroupComponent
import { Component, Input, forwardRef, ContentChildren, QueryList } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor, FormGroup } from '@angular/forms';
import { RadioButtonComponent } from '../radio-button/radio-button.component';
@Component({
selector: 'radio-group',
templateUrl: './radio-group.component.html',
styleUrls: ['./radio-group.component.css'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RadioGroupComponent),
multi: true
}
],
})
export class RadioGroupComponent implements ControlValueAccessor {
constructor() { }
private _value: any = null;
@ContentChildren(forwardRef(() => RadioButtonComponent), { descendants: true })
radios: QueryList<RadioButtonComponent>;
_onChange = (value: any) => {
console.log('onChange=', value);
}
_onTouched = () => { };
writeValue(value: any): void {
console.log('writeValue=', value);
this._onChange(value);
}
registerOnChange(fn: (_: any) => {}): void {
this._onChange = fn;
}
registerOnTouched(fn: () => {}): void {
this._onTouched = fn;
}
get value(): any {
console.log('get value', this._value);
return this._value;
}
set value(val) { // this value is updated by programmatic changes
if (val !== undefined && this._value !== val) {
this._value = val;
this._onChange(val);
this._onTouched();
}
}
}
RadioComponent
import { Component, Input, Output, EventEmitter, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor, FormGroup } from '@angular/forms';
@Component({
selector: 'radio-button',
template: `
<input type="radio"
(change)="changeValue(value)"
class="input-control"
[id]="inputId"
[(ngModel)]="selectedOption"
[value]="value"
[formControlName]="inputName"/>
<label
for="{{ inputId }}">
<span>{{ label | translate }}</span>
</label>
`,
styleUrls: ['./radio.less']
})
export class RadioComponent {
@Input() selectedOption: string;
@Input() inputId: string;
@Input() value: string;
@Input() label: string;
@Input() inputName: string;
@Output() selectedOptionChange: EventEmitter<any> = new EventEmitter<any>();
changeValue(value: any) {
this.selectedOptionChange.emit(value);
}
}
Demo Component
import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'app-radio',
template: `
<div [formGroup]="exampleForm">
<radio-button *ngFor="let option of options"
[inputId] = "option.value"
[label]="option.display"
[value]="option.value"
(selectedOptionChange) = "selectedOptionChange($event)"
formInputName= "gender"
[selectedOption]="options[0].value">
</radio-button>
</div>
</section>
<h3>Selected value = {{selectedOption}} </h3>
`,
})
export class DemoComponent implements OnInit {
constructor(private fb: FormBuilder) { }
exampleForm = this.fb.group({
gender: ['', []],
});
selectedOption: string;
options: Array<{ value: string, display: string }> = [
{ value: 'Male', display: 'Male' },
{ value: 'Female', display: 'Female' }
];
ngOnInit() {
this.selectedOptionChange(this.options[0].value);
}
selectedOptionChange(value: any): void {
this.selectedOption = value;
}
}