import { Component, Input, OnInit } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; import { Defaults } from '../../constant/defaults.constant'; import { Helper } from '../../helper'; import { RadioDirection } from './radio-direction.constant'; import { RadioOptions } from './RadioOptions'; @Component({ selector: 'rss-radio', styleUrls: ['./radio.component.scss'], templateUrl: './radio.component.html', }) export class RadioComponent implements OnInit { @Input() config: any; @Input() control: FormControl; @Input() readonly: Boolean; @Input() label: any; @Input() direction: RadioDirection = Defaults.RADIO_DIRECTION; @Input() options: any; @Input() displayKey: string = Defaults.DISPLAY_KEY; @Input() valueKey: string = Defaults.VALUE_KEY; @Input() required: boolean = Defaults.REQUIRED; @Input() requiredError: string = Defaults.REQUIRED_ERROR_MESSAGE; @Input() moreInfo: any[]; directionOptions: any = RadioDirection; findLabel: Function; constructor() { } ngOnInit() { if (this.config) { const config = new RadioOptions(this.config); Helper.merge(this, config); } if (!this.control) { console.error(Defaults.MISSING_FORM_CONTROL_ERROR_MESSAGE); } else if (!this.options) { console.error(Defaults.MISSING_OPTIONS_ERROR_MESSAGE); } else { if (this.required) { this.control.setValidators(Validators.required); } } if (this.readonly) { this.findLabel = Helper.findLabel(this.displayKey, this.valueKey, this.options); } } displayRequiredError(fc: FormControl) { return Helper.displayRequiredError(fc); } }