import { Directive , ElementRef , HostBinding , forwardRef , HostListener , Input , OnInit } from '@angular/core' ; import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ; export const RADIO_CONTROL_VALUE_ACCESSOR = { provide : NG_VALUE_ACCESSOR , useExisting : forwardRef ( ( ) => ButtonRadioDirective ) , multi : true } ; /**
* Create radio buttons or groups of buttons.
* A value of a selected button is bound to a variable specified via ngModel.
*/ @ Directive ( { selector : '[mdbRadio]' , providers : [ RADIO_CONTROL_VALUE_ACCESSOR ] } ) export class ButtonRadioDirective implements ControlValueAccessor , OnInit { public onChange : any = Function . prototype ; public onTouched : any = Function . prototype ; /** Radio button value, will be set to `ngModel` */ @ Input ( ) public mdbRadio : any ; /** If `true` — radio button can be unchecked */ @ Input ( ) public uncheckable : boolean ; /** Current value of radio component or group */ @ Input ( ) public value : any ; protected el : ElementRef ; @ HostBinding ( 'class.active' ) public get isActive ( ) : boolean { return this . mdbRadio === this . value ; } @ HostListener ( 'click' ) public onClick ( ) { if ( this . el . nativeElement . attributes . disabled ) { return ; } if ( this . uncheckable && this . mdbRadio === this . value ) { this . value = undefined ; } else { this . value = this . mdbRadio ; } this . onTouched ( ) ; this . onChange ( this . value ) ; } public constructor ( el ) { this . el = el ; } public ngOnInit ( ) { this . uncheckable = typeof this . uncheckable !== 'undefined' ; } public onBlur ( ) { this . onTouched ( ) ; } public writeValue ( value ) { this . value = value ; } public registerOnChange ( fn ) { this . onChange = fn ; } public registerOnTouched ( fn ) { this . onTouched = fn ; } }