Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update radio button directive to work with ReactiveForms #1023

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions components/buttons/button-radio.directive.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
import {
Directive, ElementRef, HostBinding, HostListener, Input, OnInit, Self
Directive, ElementRef, HostBinding, forwardRef, HostListener, Input, OnInit
} from '@angular/core';
import { ControlValueAccessor, NgModel } from '@angular/forms';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

// TODO: if uncheckable, null should be set to ngModel
// if disabled, button should not be checkable
export const RADIO_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ButtonRadioDirective),
multi: true
};

@Directive({selector: '[btnRadio][ngModel]'})
@Directive({ selector: '[btnRadio]', providers: [RADIO_CONTROL_VALUE_ACCESSOR] })
export class ButtonRadioDirective implements ControlValueAccessor, OnInit {
public cd:NgModel;
public el:ElementRef;

public onChange:any = Function.prototype;
public onTouched:any = Function.prototype;
public onChange:any = Function.prototype;
public onTouched:any = Function.prototype;

@Input() private btnRadio:string;
@Input() private uncheckable:boolean;
@Input() private btnRadio:any;
@Input() private uncheckable:boolean;
@Input() private value:any;

@HostBinding('class.active')
public get isActive():boolean {
return this.btnRadio === this.value;
}

@HostListener('click')
public onClick():void {
if (this.uncheckable && this.btnRadio === this.value) {
return this.cd.viewToModelUpdate(void 0);
@HostBinding('class.active')
public get isActive(): boolean {
return this.btnRadio === this.value;
}

this.cd.viewToModelUpdate(this.btnRadio);
}
@HostListener('click')
public onClick(): void {
if (this.el.nativeElement.attributes.disabled) {
return;
}

public constructor(@Self() cd:NgModel, el:ElementRef) {
// hack!
this.cd = cd;
this.el = el;
cd.valueAccessor = this;
}
if (this.uncheckable && this.btnRadio === this.value) {
this.value = undefined;
} else {
this.value = this.btnRadio;
}

public ngOnInit():void {
this.uncheckable = typeof this.uncheckable !== 'undefined';
}
this.onTouched();
this.onChange(this.value);
}

// hack view model!
protected get value():any {
return this.cd.viewModel;
}
public constructor( private el: ElementRef) {
}

protected set value(value:any) {
this.cd.viewModel = value;
}
public ngOnInit(): void {
this.uncheckable = typeof this.uncheckable !== 'undefined';
}

// ControlValueAccessor
// model -> view
public writeValue(value:any):void {
this.value = value;
}
public onBlur(): void {
this.onTouched();
}

public registerOnChange(fn:(_:any) => {}):void {
this.onChange = fn;
}
// ControlValueAccessor
// model -> view
public writeValue(value: any): void {
this.value = value;
}

public registerOnChange(fn: any): void {
this.onChange = fn;
}

public registerOnTouched(fn:() => {}):void {
this.onTouched = fn;
}
public registerOnTouched(fn: any): void {
this.onTouched = fn;
}
}