Skip to content

Commit 1b7a5da

Browse files
committed
feat: Nativescript-Angular 4.4.x support
1 parent dc7514d commit 1b7a5da

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

angular/index.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
import { Directive, ElementRef, HostListener, Inject, NgModule, forwardRef } from "@angular/core";
1+
import {
2+
Directive,
3+
ElementRef,
4+
HostListener,
5+
Inject,
6+
NgModule,
7+
forwardRef
8+
} from "@angular/core";
29
import { FormsModule, NG_VALUE_ACCESSOR } from "@angular/forms";
310
import { registerElement } from "nativescript-angular/element-registry";
4-
import { BaseValueAccessor } from "nativescript-angular/value-accessors/base-value-accessor";
5-
import { convertToInt } from "nativescript-angular/common/utils";
11+
import { BaseValueAccessor } from "nativescript-angular/forms/value-accessors/base-value-accessor";
612
import { View } from "tns-core-modules/ui/core/view";
13+
import { isBlank } from "nativescript-angular/lang-facade";
14+
15+
function convertToInt(value): number {
16+
let normalizedValue;
17+
if (isBlank(value)) {
18+
normalizedValue = 0;
19+
} else {
20+
if (typeof value === "number") {
21+
normalizedValue = value;
22+
} else {
23+
let parsedValue = parseInt(value.toString(), 10);
24+
normalizedValue = isNaN(parsedValue) ? 0 : parsedValue;
25+
}
26+
}
27+
return Math.round(normalizedValue);
28+
}
729

830
registerElement("CheckBox", () => require("../checkbox").CheckBox);
931

1032
const CHECKED_VALUE_ACCESSOR = {
1133
provide: NG_VALUE_ACCESSOR,
12-
useExisting: forwardRef(() => CheckedValueAccessor), multi: true
34+
useExisting: forwardRef(() => CheckedValueAccessor),
35+
multi: true
1336
};
1437

15-
export type CheckableView = {checked: boolean} & View;
38+
export type CheckableView = { checked: boolean } & View;
1639

1740
/**
1841
* The accessor for setting checked property and listening to changes that is used by the
@@ -24,41 +47,35 @@ export type CheckableView = {checked: boolean} & View;
2447
* ```
2548
*/
2649
@Directive({
27-
selector: "CheckBox[ngModel], CheckBox[formControlName], CheckBox[formControl], checkBox[ngModel], checkBox[formControlName], checkBox[formControl], check-box[ngModel], check-box[formControlName], check-box[formControl]",
28-
providers: [CHECKED_VALUE_ACCESSOR]
50+
selector:
51+
"CheckBox[ngModel], CheckBox[formControlName], CheckBox[formControl], checkBox[ngModel], checkBox[formControlName], checkBox[formControl], check-box[ngModel], check-box[formControlName], check-box[formControl]",
52+
providers: [CHECKED_VALUE_ACCESSOR]
2953
})
3054
export class CheckedValueAccessor extends BaseValueAccessor<CheckableView> {
55+
constructor(@Inject(ElementRef) elementRef: ElementRef) {
56+
super(elementRef.nativeElement);
57+
}
3158

32-
constructor(@Inject(ElementRef) elementRef: ElementRef) {
33-
super(elementRef.nativeElement);
34-
}
59+
@HostListener("checkedChange", ["$event"])
60+
public checkedChangeListener(event: any) {
61+
this.onChange(event.value);
62+
}
3563

36-
@HostListener("checkedChange", ["$event"])
37-
public checkedChangeListener(event: any) {
38-
this.onChange(event.value);
39-
}
40-
41-
public onTouched = () => { };
64+
public onTouched = () => {};
4265

43-
public writeValue(value: any): void {
44-
this.view.checked = value;
45-
}
66+
public writeValue(value: any): void {
67+
this.view.checked = value;
68+
}
4669

47-
public registerOnTouched(fn: () => void): void { this.onTouched = fn; }
70+
public registerOnTouched(fn: () => void): void {
71+
this.onTouched = fn;
72+
}
4873
}
4974

5075
@NgModule({
51-
declarations: [
52-
CheckedValueAccessor
53-
],
54-
providers: [],
55-
imports: [
56-
FormsModule
57-
],
58-
exports: [
59-
FormsModule,
60-
CheckedValueAccessor
61-
]
76+
declarations: [CheckedValueAccessor],
77+
providers: [],
78+
imports: [FormsModule],
79+
exports: [FormsModule, CheckedValueAccessor]
6280
})
63-
export class TNSCheckBoxModule {
64-
}
81+
export class TNSCheckBoxModule {}

0 commit comments

Comments
 (0)