-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheckbox.ts
62 lines (48 loc) · 1.39 KB
/
checkbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Component, Input, Output, EventEmitter, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'lsu-checkbox',
template: `
<div class="ui {{type}} checkbox" [ngClass]="{'checked': checked}">
<input type="checkbox" id="{{_id}}" [ngModel]="checked" (ngModelChange)="valueChanged($event)" [disabled]="disabled">
<label for="{{_id}}" style="cursor: pointer">{{ label }}</label>
</div>
`,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckBoxComponent),
multi: true
}]
})
export class CheckBoxComponent implements ControlValueAccessor {
@Input()
public disabled: boolean = false;
@Input()
public type: string;
@Input()
public label: string;
@Output()
onChange: EventEmitter<boolean> = new EventEmitter<boolean>();
checked: boolean = false;
_onChange = (_: any) => { };
_onTouched = () => { };
_id: string;
constructor() {
}
ngOnInit() {
this._id = `lsu_checkbox_${new Date().valueOf()}_${Math.random() * 10000}`;
}
writeValue(value: boolean): void {
this.checked = value;
}
registerOnChange(fn: (_: any) => {}): void {
this._onChange = fn;
}
registerOnTouched(fn: () => {}): void {
this._onTouched = fn;
}
valueChanged(value: boolean) {
this._onChange(value);
this.onChange.emit(value);
}
}