Skip to content

Added TextValueAccessor to support two-way binding via ngModel. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion ng-sample/app/renderer-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Component, Directive, Host, ElementRef, Input} from 'angular2/core';
import { Observable } from 'data/observable';
import { TextValueAccessor } from './nativescript-angular/text-value-accessor';

@Component({
selector: 'templated-component',
Expand All @@ -24,10 +26,12 @@ export class ProgressComponent {

@Component({
selector: 'renderer-test',
directives: [TemplatedComponent, ProgressComponent],
directives: [TemplatedComponent, ProgressComponent, TextValueAccessor],
template: `
<StackLayout orientation='vertical'>
<Progress value="50" style="color: red"></Progress>
<Label [text]='model.test'></Label>
<TextField #name [ngModel]='model.test' (ngModelChange)="model.test=setUpperCase($event)" fontSize='20' padding='20'></TextField>
<Label [class.valid]="isValid" [class.invalid]="!isValid" text='Name' fontSize='20' verticalAlignment='center' padding='20'></Label>
<TextField #name text='John' fontSize='20' padding='20'></TextField>
<Button [text]='buttonText' (tap)='onSave($event, name.text, $el)'></Button>
Expand All @@ -49,12 +53,14 @@ export class RendererTest {
public moreDetailsText: string = "";
public detailLines: Array<string> = [];
public isValid: boolean = true;
public model: Observable;

constructor() {
this.buttonText = 'Save...'
this.showDetails = true;
this.detailsText = 'plain ng-if directive \ndetail 1-2-3...';
this.moreDetailsText = 'More details:';
this.model = new Observable({'test': 'Jack'});

this.detailLines = [
"ngFor inside a ngIf 1",
Expand All @@ -67,8 +73,22 @@ export class RendererTest {
alert(name);
}

testLoaded($event) {
console.log("testLoaded called with event args: " + $event);
}

onToggleDetails() {
console.log('onToggleDetails current: ' + this.showDetails);
this.showDetails = !this.showDetails;
}

setUpperCase($event) {
if ($event.value && $event.value.toUpperCase) {
return $event.value.toUpperCase();
}
if (typeof $event === "string") {
return $event.toUpperCase();
}
return $event;
}
}
38 changes: 38 additions & 0 deletions src/nativescript-angular/text-value-accessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {Directive, ElementRef, Renderer, Self, forwardRef, Provider} from 'angular2/core';
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from 'angular2/src/common/forms/directives/control_value_accessor';
import {isBlank, CONST_EXPR} from 'angular2/src/facade/lang';

const TEXT_VALUE_ACCESSOR = CONST_EXPR(new Provider(
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => TextValueAccessor), multi: true}));

/**
* The accessor for writing a text and listening to changes that is used by the
* {@link NgModel}, {@link NgFormControl}, and {@link NgControlName} directives.
*
* ### Example
* ```
* <TextField [(ngModel)]='model.test'>
* ```
*/
@Directive({
selector: 'TextField[ngModel]',
// TODO: vsavkin replace the above selector with the one below it once
// https://github.com/angular/angular/issues/3011 is implemented
// selector: '[ngControl],[ngModel],[ngFormControl]',
host: {'(textChange)': 'onChange($event.value)'},
bindings: [TEXT_VALUE_ACCESSOR]
})
export class TextValueAccessor implements ControlValueAccessor {
onChange = (_) => {};
onTouched = () => {};

constructor(private _renderer: Renderer, private _elementRef: ElementRef) { }

writeValue(value: any): void {
var normalizedValue = isBlank(value) ? '' : value;
this._renderer.setElementProperty(this._elementRef.nativeElement, 'text', normalizedValue);
}

registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}