diff --git a/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.css b/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.css index 6d1e517..d9da0b3 100644 --- a/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.css +++ b/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.css @@ -22,22 +22,10 @@ margin-left: 100; } -.small-picture { - height: 70; - margin: 20; -} - .yellow-grid { background-color: #F5C518; } -.result-label { - background-color: gray; - color: white; - padding: 10; - margin-right: 20; -} - ListView.picker-field { background-color: green; margin-left: 20; @@ -57,3 +45,7 @@ ActionBar.picker-field { font-size: 20; font-weight: bold; } + +.submit-button { + margin: 20; +} \ No newline at end of file diff --git a/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.html b/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.html index 56d709c..6b6dd4f 100644 --- a/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.html +++ b/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.html @@ -3,8 +3,8 @@ - + @@ -14,13 +14,7 @@ - - - - - - - - + + > \ No newline at end of file diff --git a/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.ts b/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.ts index 57cb841..2678c22 100644 --- a/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.ts +++ b/demo-angular/src/app/examples/reactive-forms/reactive-forms-example.component.ts @@ -1,7 +1,8 @@ -import { Component, OnInit } from "@angular/core"; -import { FormBuilder, FormGroup, Validators } from "@angular/forms"; +import { Component, OnInit, ViewChild } from "@angular/core"; +import { FormBuilder, FormGroup, FormControl, Validators } from "@angular/forms"; import { RouterExtensions } from "nativescript-angular/router"; import { ObservableArray } from "tns-core-modules/data/observable-array/observable-array"; +import { PickerFieldComponent } from "nativescript-picker/angular"; @Component({ selector: "ns-reactive-forms-example", @@ -11,6 +12,7 @@ import { ObservableArray } from "tns-core-modules/data/observable-array/observab }) export class ReactiveFormsExampleComponent implements OnInit { public pickerItems: ObservableArray; + @ViewChild("picker") pickerComp: PickerFieldComponent; constructor(private routerExtensions: RouterExtensions, private fb: FormBuilder) { this.pickerItems = new ObservableArray([ @@ -24,20 +26,31 @@ export class ReactiveFormsExampleComponent implements OnInit { new Movie("One Flew Over the Cuckoo's Nest", 9, 1975, "https://m.media-amazon.com/images/M/MV5BZjA0OWVhOTAtYWQxNi00YzNhLWI4ZjYtNjFjZTEyYjJlNDVlL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_UX182_CR0,0,182,268_AL_.jpg"), new Movie(" Lawrence of Arabia", 10, 1962, "https://m.media-amazon.com/images/M/MV5BYWY5ZjhjNGYtZmI2Ny00ODM0LWFkNzgtZmI1YzA2N2MxMzA0XkEyXkFqcGdeQXVyNjUwNzk3NDc@._V1_UY268_CR2,0,182,268_AL_.jpg"), ]); - this.selectedMovie = this.pickerItems.getItem(0); + + this.movieForm = new FormGroup({ + movie: new FormControl(this.pickerItems.getItem(0).year, Validators.required), + }); } ngOnInit(): void { } - public movieForm: FormGroup = this.fb.group({ - movie: [undefined, Validators.required], - }); - - public selectedMovie: Movie; + public movieForm: FormGroup; - public goBack() { +Í; public goBack() { this.routerExtensions.backToPreviousPage(); } + + public onSubmit() { + let formMovieValue = this.movieForm.get("movie").value; + let selectedValue = this.pickerComp.nativeElement.selectedValue; + console.log("picker selected value: ", selectedValue); + console.log("Forms 'movie' value: ", formMovieValue); + alert({ + title: "Forms 'movie' value:", + message: `Forms 'movie' value: ${formMovieValue}`, + okButtonText: "OK" + }); + } } class Movie { diff --git a/src/picker.common.ts b/src/picker.common.ts index 36b2b4e..49eb40b 100644 --- a/src/picker.common.ts +++ b/src/picker.common.ts @@ -159,12 +159,8 @@ export class PickerField extends TextField implements TemplatedItemsView { if (selectedIndex !== undefined) { let object = this.getDataItem(selectedIndex); this.selectedIndex = selectedIndex; - let value = this.getValueFromField("valueField", this.valueField, object); - this.selectedValue = value === undefined ? object : value; - let textValue = this.getValueFromField("textField", this.textField, object); - textValue = textValue === undefined ? object : textValue; - this.text = textValue; + this._updateSelectedValue(object); } this.disposeModalView(); @@ -415,9 +411,70 @@ export class PickerField extends TextField implements TemplatedItemsView { } } + private _updateSelectedValue(object: any) { + let value = this.getValueFromField("selectedValue", this.valueField, object); + this.selectedValue = value === undefined ? object : value; + } + + private updatePickerText(object: any) { + let textValue = this.getValueFromField("text", this.textField, object); + textValue = textValue === undefined ? object : textValue; + this.text = textValue; + } + protected onModalAnimatedChanged(oldValue: boolean, newValue: boolean) { } - protected onSelectedValueChanged(oldValue: any, newValue: any) { } + protected onSelectedValueChanged(oldValue: any, newValue: any) { + if (this.hasItem(newValue)) { + this.updatePickerText(newValue); + return; + } + + let dataItem = this.getObjectFromValue(this.valueField, newValue); + if (dataItem) { + this.updatePickerText(dataItem); + } else { + this.text = newValue; + } + } + + private getObjectFromValue(propertyName: string, value: any) { + if (!propertyName) { + return undefined; + } + + if (this.items) { + for (let i = 0; i < this.items.length; i++) { + let item = this._getDataItem(i); + if (item.hasOwnProperty(propertyName)) { + let dataItemValue = item[propertyName]; + if (dataItemValue === value) { + return item; + } + } + } + } + + return undefined; + } + + private hasItem(object: any) { + if (this.items) { + for (let i = 0; i < this.items.length; i++) { + let item = this._getDataItem(i); + if (item === object) { + return true; + } + } + } + + return false; + } + + private _getDataItem(index: number): any { + let thisItems = this.items; + return thisItems.getItem ? thisItems.getItem(index) : thisItems[index]; + } protected onValueFieldChanged(oldValue: string, newValue: string) { } diff --git a/src/picker.d.ts b/src/picker.d.ts index b5f0bad..61b1efd 100644 --- a/src/picker.d.ts +++ b/src/picker.d.ts @@ -55,7 +55,7 @@ export class PickerField extends TextField implements TemplatedItemsView { public valueField: string; /** - * Gets the object selected from the list in the modal view. + * Gets or sets the object selected from the list in the modal view. */ public selectedValue: any; @@ -86,8 +86,6 @@ export class PickerField extends TextField implements TemplatedItemsView { refresh(); - _openModalHandler(args): void; - /** * Identifies the {@link modalAnimated} dependency property. */