ng2-bootstrap-base-modified
Version:
Native Angular Bootstrap Components Typeahead modified
78 lines (62 loc) • 2.14 kB
text/typescript
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: any = {
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.
*/
export class ButtonRadioDirective implements ControlValueAccessor, OnInit {
public onChange:any = Function.prototype;
public onTouched:any = Function.prototype;
/** Radio button value, will be set to `ngModel` */
public btnRadio:any;
/** If `true` — radio button can be unchecked */
public uncheckable:boolean;
/** Current value of radio component or group */
public value:any;
protected el: ElementRef;
public get isActive(): boolean {
return this.btnRadio === this.value;
}
public onClick(): void {
if (this.el.nativeElement.attributes.disabled) {
return;
}
if (this.uncheckable && this.btnRadio === this.value) {
this.value = undefined;
} else {
this.value = this.btnRadio;
}
this.onTouched();
this.onChange(this.value);
}
public constructor(el: ElementRef) {
this.el = el;
}
public ngOnInit(): void {
this.uncheckable = typeof this.uncheckable !== 'undefined';
}
public onBlur(): void {
this.onTouched();
}
// ControlValueAccessor
// model -> view
public writeValue(value: any): void {
this.value = value;
}
public registerOnChange(fn: any): void {
this.onChange = fn;
}
public registerOnTouched(fn: any): void {
this.onTouched = fn;
}
}