-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change allows for the use of `ngModel` and `formControlName` when using the drop-downs with Angular. There should be no breaking changes to previous functionality or usage for either Angular or Vanilla NativeScript. Build scripts updated. Compatible with Angular AoT and Webpack. Based on `nativescript-angular/value-accessors/selectedIndex-value-accessor.ts` and `nativescript-angular/nativescript-angular/forms.ts` with some changes to conform to project linting.
- Loading branch information
1 parent
108d1e4
commit 7417fdc
Showing
11 changed files
with
222 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { AfterViewInit, Directive, ElementRef, HostListener, Inject, NgModule, forwardRef } from "@angular/core"; | ||
import { FormsModule, NG_VALUE_ACCESSOR } from "@angular/forms"; | ||
import { BaseValueAccessor, registerElement } from "nativescript-angular"; | ||
import { convertToInt } from "nativescript-angular/common/utils"; | ||
import { View } from "tns-core-modules/ui/core/view"; | ||
|
||
registerElement("DropDown", () => require("../drop-down").DropDown); | ||
|
||
const SELECTED_INDEX_VALUE_ACCESSOR = {provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => SelectedIndexValueAccessor), multi: true}; | ||
|
||
export type SelectableView = {selectedIndex: number} & View; | ||
|
||
/** | ||
* The accessor for setting a selectedIndex and listening to changes that is used by the | ||
* {@link NgModel} directives. | ||
* | ||
* ### Example | ||
* ``` | ||
* <DropDown [(ngModel)]="model.test"> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
// tslint:disable-next-line:max-line-length directive-selector | ||
selector: "DropDown[ngModel], DropDown[formControlName], dropDown[ngModel], dropDown[formControlName], drop-down[ngModel], drop-down[formControlName]", | ||
providers: [SELECTED_INDEX_VALUE_ACCESSOR] | ||
}) | ||
export class SelectedIndexValueAccessor extends BaseValueAccessor<SelectableView> implements AfterViewInit { // tslint:disable-line:max-line-length directive-class-suffix | ||
|
||
private _normalizedValue: number; | ||
private viewInitialized: boolean; | ||
|
||
constructor(@Inject(ElementRef) elementRef: ElementRef) { | ||
super(elementRef.nativeElement); | ||
} | ||
|
||
@HostListener("selectedIndexChange", ["$event"]) | ||
public selectedIndexChangeListener(event: any) { | ||
this.onChange(event.value); | ||
} | ||
|
||
// tslint:disable-next-line:no-empty | ||
public onTouched = () => { }; | ||
|
||
public writeValue(value: any): void { | ||
if (value === undefined || value === null || value === "") { | ||
this._normalizedValue = null; | ||
} | ||
else { | ||
this._normalizedValue = convertToInt(value); | ||
} | ||
|
||
if (this.viewInitialized) { | ||
this.view.selectedIndex = this._normalizedValue; | ||
} | ||
} | ||
|
||
public ngAfterViewInit() { | ||
this.viewInitialized = true; | ||
this.view.selectedIndex = this._normalizedValue; | ||
} | ||
|
||
public registerOnTouched(fn: () => void): void { this.onTouched = fn; } | ||
} | ||
|
||
@NgModule({ | ||
declarations: [ SelectedIndexValueAccessor ], | ||
providers: [], | ||
imports: [ | ||
FormsModule | ||
], | ||
exports: [ | ||
FormsModule, | ||
SelectedIndexValueAccessor | ||
] | ||
}) | ||
export class DropDownModule { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
<ActionBar title="NG Demo"> | ||
</ActionBar> | ||
<ActionBar title="NG Demo"></ActionBar> | ||
<StackLayout> | ||
<GridLayout rows="auto, auto, auto, *" columns="auto, *"> | ||
<DropDown #dd [class] ="cssClass" [items]="items" [selectedIndex]="selectedIndex" [hint]="hint" | ||
(selectedIndexChanged)="onchange($event)" (opened)="onopen()" | ||
row="0" colSpan="2"> | ||
</DropDown> | ||
<Label text="Selected Index:" row="1" col="0" fontSize="18" verticalAlignment="bottom"></Label> | ||
<TextField [text]="selectedIndex" row="1" col="1" ></TextField> | ||
<Button row="2" col="0" colSpan="2" (tap)="changeStyles()" text="Change Styles" style="font-size: 25"></Button> | ||
<GridLayout rows="auto, auto, auto, *" | ||
columns="auto, *"> | ||
<DropDown #dd | ||
[class]="cssClass" | ||
[items]="items" | ||
[(ngModel)]="selectedIndex" | ||
[hint]="hint" | ||
(selectedIndexChanged)="onchange($event)" | ||
(opened)="onopen()" | ||
row="0" | ||
colSpan="2"></DropDown> | ||
<Label text="Selected Index:" | ||
row="1" | ||
col="0" | ||
fontSize="18" | ||
verticalAlignment="bottom"></Label> | ||
<TextField [text]="selectedIndex" | ||
row="1" | ||
col="1"></TextField> | ||
<Button row="2" | ||
col="0" | ||
colSpan="2" | ||
(tap)="changeStyles()" | ||
text="Change Styles" | ||
style="font-size: 25"></Button> | ||
</GridLayout> | ||
</StackLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
// this import should be first in order to load some required settings (like globals and reflect-metadata) | ||
import { platformNativeScriptDynamic } from "nativescript-angular/platform"; | ||
|
||
import { registerElement } from "nativescript-angular/element-registry"; | ||
|
||
import { AppModule } from "./app.module"; | ||
|
||
registerElement("DropDown", () => require("nativescript-drop-down/drop-down").DropDown); | ||
|
||
platformNativeScriptDynamic().bootstrapModule(AppModule); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmitOnError": true, | ||
"noEmitHelpers": true, | ||
"sourceMap": false, | ||
"removeComments": true, | ||
"declaration": true, | ||
"experimentalDecorators": true, | ||
"target": "es5", | ||
"module": "commonjs", | ||
"outDir": "bin/dist", | ||
"lib": ["es6", "dom", "es2015.iterable"], | ||
"rootDir": ".", | ||
"baseUrl": ".", | ||
"paths": { | ||
"*": [ | ||
"./node_modules/tns-core-modules/*", | ||
"./node_modules/*" | ||
] | ||
}, | ||
"emitDecoratorMetadata": true, | ||
"moduleResolution": "node" | ||
}, | ||
"files": [ | ||
"angular/index.ts" | ||
], | ||
"angularCompilerOptions": { | ||
"skipTemplateCodegen": true | ||
} | ||
} |