feat(inputs\multi-checkbox): add multi-checkbox directive

This commit is contained in:
tibing 2016-07-08 17:00:07 +03:00
parent e63812f011
commit 70cdff101f
12 changed files with 270 additions and 94 deletions

View file

@ -1,32 +1,36 @@
import {Component, Provider, forwardRef, Input} from "@angular/core";
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/common";
const BA_CHECKBOX_CONTROL_VALUE_ACCESSOR = new Provider(
NG_VALUE_ACCESSOR, {
useExisting: forwardRef(() => BaCheckbox),
multi: true
});
import {Component, Input, Self} from '@angular/core';
import {ControlValueAccessor, NgModel} from '@angular/forms';
@Component({
selector: 'ba-checkbox',
template: require('./baCheckbox.html'),
providers: [BA_CHECKBOX_CONTROL_VALUE_ACCESSOR]
selector: 'ba-checkbox[ngModel]',
styles: [require('./baCheckbox.scss')],
template: require('./baCheckbox.html')
})
export class BaCheckbox implements ControlValueAccessor {
@Input() disabled:boolean;
@Input() label:string;
@Input() value:string;
@Input() name:string;
@Input() baCheckboxClass:string;
public model: NgModel;
public state: boolean;
onChange(value: any): void {}
onTouch(value: any): void {}
writeValue(value: any): void {
this.state = value;
public constructor(@Self() state:NgModel) {
this.model = state;
state.valueAccessor = this;
}
registerOnChange(fn: any): void { this.onChange = fn; }
registerOnTouched(fn: any): void { this.onTouch = fn; }
public onChange(value: any): void {}
public onTouch(value: any): void {}
public writeValue(state: any): void {
this.state = state;
}
public registerOnChange(fn: any): void {
this.onChange = function(state: boolean) {
this.writeValue(state);
this.model.viewToModelUpdate(state);
}
}
public registerOnTouched(fn: any): void { this.onTouch = fn; }
}