Skip to content

Commit

Permalink
fix(forms): default to unsetValue for value accessors (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
sis0k0 authored and Alexander Vakrilov committed Oct 11, 2017
1 parent 164f582 commit 6940955
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ControlValueAccessor } from "@angular/forms";
import { View } from "tns-core-modules/ui/core/view";
import { View, unsetValue } from "tns-core-modules/ui/core/view";

import { isBlank } from "../../lang-facade";

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

writeValue(_: any) { }
writeValue(_: any) {}

protected normalizeValue(value: any): any {
return isBlank(value) ? unsetValue : value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class CheckedValueAccessor extends BaseValueAccessor<Switch> { // tslint:
}

writeValue(value: any): void {
this.view.checked = value;
const normalized = super.normalizeValue(value);
this.view.checked = normalized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class DateValueAccessor extends BaseValueAccessor<DatePicker> { // tslint
}

writeValue(value: any): void {
this.view.date = value;
const normalized = super.normalizeValue(value);
this.view.date = normalized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class NumberValueAccessor extends BaseValueAccessor<Slider> { // tslint:d
}

writeValue(value: any): void {
this.view.value = value;
const normalized = super.normalizeValue(value);
this.view.value = normalized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class SelectedIndexValueAccessor extends BaseValueAccessor<SelectableView
private viewInitialized: boolean;

writeValue(value: any): void {
this.value = value;
const normalized = super.normalizeValue(value);
this.value = normalized;

if (this.viewInitialized) {
this.view.selectedIndex = this.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class TextValueAccessor extends BaseValueAccessor<TextView> { // tslint:d
}

writeValue(value: any): void {
this.view.text = value;
const normalized = super.normalizeValue(value);
this.view.text = normalized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class TimeValueAccessor extends BaseValueAccessor<TimePicker> { // tslint
}

writeValue(value: any): void {
this.view.time = value;
const normalized = super.normalizeValue(value);
this.view.time = normalized;
}
}

0 comments on commit 6940955

Please sign in to comment.