/
index.ts
81 lines (71 loc) · 2.21 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
Directive,
ElementRef,
forwardRef,
HostListener,
Inject,
NgModule
} from '@angular/core';
import { FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
import { registerElement } from 'nativescript-angular/element-registry';
import { BaseValueAccessor } from 'nativescript-angular/forms/value-accessors/base-value-accessor';
import { isBlank } from 'nativescript-angular/lang-facade';
import { View } from 'tns-core-modules/ui/core/view';
function convertToInt(value): number {
let normalizedValue;
if (isBlank(value)) {
normalizedValue = 0;
} else {
if (typeof value === 'number') {
normalizedValue = value;
} else {
let parsedValue = parseInt(value.toString(), 10);
normalizedValue = isNaN(parsedValue) ? 0 : parsedValue;
}
}
return Math.round(normalizedValue);
}
registerElement('CheckBox', () => require('../checkbox').CheckBox);
const CHECKED_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckedValueAccessor),
multi: true
};
export type CheckableView = { checked: boolean } & View;
/**
* The accessor for setting checked property and listening to changes that is used by the
* {@link NgModel} directives.
*
* ### Example
* ```
* <CheckBox [(ngModel)]="model.test">
* ```
*/
@Directive({
selector:
'CheckBox[ngModel], CheckBox[formControlName], CheckBox[formControl], checkBox[ngModel], checkBox[formControlName], checkBox[formControl], check-box[ngModel], check-box[formControlName], check-box[formControl]',
providers: [CHECKED_VALUE_ACCESSOR]
})
export class CheckedValueAccessor extends BaseValueAccessor<CheckableView> {
constructor(@Inject(ElementRef) elementRef: ElementRef) {
super(elementRef.nativeElement);
}
@HostListener('checkedChange', ['$event'])
public checkedChangeListener(event: any) {
this.onChange(event.value);
}
public onTouched = () => {};
public writeValue(value: any): void {
this.view.checked = value;
}
public registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
}
@NgModule({
declarations: [CheckedValueAccessor],
providers: [],
imports: [FormsModule],
exports: [FormsModule, CheckedValueAccessor]
})
export class TNSCheckBoxModule {}