Skip to content

Commit 6940955

Browse files
sis0k0Alexander Vakrilov
authored and
Alexander Vakrilov
committed
fix(forms): default to unsetValue for value accessors (#846)
1 parent 164f582 commit 6940955

7 files changed

+20
-8
lines changed

Diff for: nativescript-angular/forms/value-accessors/base-value-accessor.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ControlValueAccessor } from "@angular/forms";
2-
import { View } from "tns-core-modules/ui/core/view";
2+
import { View, unsetValue } from "tns-core-modules/ui/core/view";
3+
4+
import { isBlank } from "../../lang-facade";
35

46
export class BaseValueAccessor<TView extends View> implements ControlValueAccessor {
57
private pendingChangeNotification: any = 0;
@@ -28,5 +30,9 @@ export class BaseValueAccessor<TView extends View> implements ControlValueAccess
2830
this.view.isEnabled = !isDisabled;
2931
}
3032

31-
writeValue(_: any) { }
33+
writeValue(_: any) {}
34+
35+
protected normalizeValue(value: any): any {
36+
return isBlank(value) ? unsetValue : value;
37+
}
3238
}

Diff for: nativescript-angular/forms/value-accessors/checked-value-accessor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class CheckedValueAccessor extends BaseValueAccessor<Switch> { // tslint:
3434
}
3535

3636
writeValue(value: any): void {
37-
this.view.checked = value;
37+
const normalized = super.normalizeValue(value);
38+
this.view.checked = normalized;
3839
}
3940
}

Diff for: nativescript-angular/forms/value-accessors/date-value-accessor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class DateValueAccessor extends BaseValueAccessor<DatePicker> { // tslint
3434
}
3535

3636
writeValue(value: any): void {
37-
this.view.date = value;
37+
const normalized = super.normalizeValue(value);
38+
this.view.date = normalized;
3839
}
3940
}

Diff for: nativescript-angular/forms/value-accessors/number-value-accessor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class NumberValueAccessor extends BaseValueAccessor<Slider> { // tslint:d
3333
}
3434

3535
writeValue(value: any): void {
36-
this.view.value = value;
36+
const normalized = super.normalizeValue(value);
37+
this.view.value = normalized;
3738
}
3839
}

Diff for: nativescript-angular/forms/value-accessors/selectedIndex-value-accessor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class SelectedIndexValueAccessor extends BaseValueAccessor<SelectableView
5050
private viewInitialized: boolean;
5151

5252
writeValue(value: any): void {
53-
this.value = value;
53+
const normalized = super.normalizeValue(value);
54+
this.value = normalized;
5455

5556
if (this.viewInitialized) {
5657
this.view.selectedIndex = this.value;

Diff for: nativescript-angular/forms/value-accessors/text-value-accessor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class TextValueAccessor extends BaseValueAccessor<TextView> { // tslint:d
4848
}
4949

5050
writeValue(value: any): void {
51-
this.view.text = value;
51+
const normalized = super.normalizeValue(value);
52+
this.view.text = normalized;
5253
}
5354
}

Diff for: nativescript-angular/forms/value-accessors/time-value-accessor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class TimeValueAccessor extends BaseValueAccessor<TimePicker> { // tslint
3636
}
3737

3838
writeValue(value: any): void {
39-
this.view.time = value;
39+
const normalized = super.normalizeValue(value);
40+
this.view.time = normalized;
4041
}
4142
}

0 commit comments

Comments
 (0)