import {NgModule,Component,Input,Output,ElementRef,EventEmitter,forwardRef,ViewChild,ChangeDetectorRef,ChangeDetectionStrategy} from '@angular/core'; import {CommonModule} from '@angular/common'; import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms'; export const RADIO_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => RadioButton), multi: true }; @Component({ selector: 'p-radioButton', template: `
`, providers: [RADIO_VALUE_ACCESSOR], changeDetection: ChangeDetectionStrategy.OnPush }) export class RadioButton implements ControlValueAccessor { @Input() value: any; @Input() name: string; @Input() disabled: boolean; @Input() label: string; @Input() tabindex: number; @Input() inputId: string; @Input() ariaLabelledBy: string; @Input() style: any; @Input() styleClass: string; @Input() labelStyleClass: string; @Output() onClick: EventEmitter = new EventEmitter(); @Output() onFocus: EventEmitter = new EventEmitter(); @Output() onBlur: EventEmitter = new EventEmitter(); @ViewChild('rb') inputViewChild: ElementRef; public onModelChange: Function = () => {}; public onModelTouched: Function = () => {}; public checked: boolean; public focused: boolean; constructor(private cd: ChangeDetectorRef) {} handleClick(event, radioButton, focus) { event.preventDefault(); if (this.disabled) { return; } this.select(event); if (focus) { radioButton.focus(); } } select(event) { if (!this.disabled) { this.inputViewChild.nativeElement.checked = true; this.checked = true; this.onModelChange(this.value); this.onClick.emit(event); } } writeValue(value: any) : void { this.checked = (value == this.value); if (this.inputViewChild && this.inputViewChild.nativeElement) { this.inputViewChild.nativeElement.checked = this.checked; } this.cd.markForCheck(); } registerOnChange(fn: Function): void { this.onModelChange = fn; } registerOnTouched(fn: Function): void { this.onModelTouched = fn; } setDisabledState(val: boolean): void { this.disabled = val; } onInputFocus(event) { this.focused = true; this.onFocus.emit(event); } onInputBlur(event) { this.focused = false; this.onModelTouched(); this.onBlur.emit(event); } onChange(event) { this.select(event); } focus() { this.inputViewChild.nativeElement.focus(); } } @NgModule({ imports: [CommonModule], exports: [RadioButton], declarations: [RadioButton] }) export class RadioButtonModule { }