novo-elements
Version:
Bullhorn's NOVO Element Repository for Angular 2
83 lines (73 loc) • 2.51 kB
text/typescript
// NG2
import { Component, EventEmitter, forwardRef, Input, Output } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
// APP
import { Helpers } from '../../utils/Helpers';
// Value accessor for the component (supports ngModel)
const RADIO_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NovoRadioElement),
multi: true
};
export class NovoRadioGroup { }
export class NovoRadioElement implements ControlValueAccessor {
name: string;
value: any;
checked: boolean;
vertical: boolean;
label: string;
button: boolean = false;
theme: string = 'secondary';
icon: string;
change: EventEmitter<any> = new EventEmitter();
model: any;
onModelChange: Function = () => {
};
onModelTouched: Function = () => {
};
/**
* Handles the select of the radio button, will only change if a new radio is selected
* @param event
* @param radio
*/
select(event, radio) {
Helpers.swallowEvent(event);
// Only change the checked state if this is a new radio, they are not toggle buttons
if (!radio.checked) {
radio.checked = !radio.checked;
this.change.emit(this.value);
this.onModelChange(this.value);
}
}
writeValue(model: any): void {
this.model = model;
}
registerOnChange(fn: Function): void {
this.onModelChange = fn;
}
registerOnTouched(fn: Function): void {
this.onModelTouched = fn;
}
}