PeterStaev / NativeScript-Drop-Down

A NativeScript DropDown widget.
Apache License 2.0
105 stars 65 forks source link

Angular Forms Directive #88

Closed DickSmith closed 7 years ago

DickSmith commented 7 years ago

This change allows for the use of ngModel and formControlName when using the nativescript-drop-down with Angular.

To use the new directives remove registerElement("DropDown", () => require("nativescript-drop-down/drop-down").DropDown); from the application and import the module as usual in ngModule:

import { DropDownModule } from “nativescript-drop-down/angular”;
…
@NgModule({
    …
    imports: [
        …
        DropDownModule,
        …
    ],
    …
})

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.

PeterStaev commented 7 years ago

@DickSmith , amazing work! Can you please see to also add the module usage to the readme and the demo-ng project, so it is clear to users. Also seems you are missing a dev dep to @angular/forms.

DickSmith commented 7 years ago

Per discussion:

Let me know if there’s anything else. I would maybe also add a peerDependency for tns-core-modules to be >= to 3.0 to make it clearer to users during npm install that the new plugin will only work once they update, but I’ll leave that up to you. :D

DickSmith commented 7 years ago

Fix for unselected state Currently the value is being forced to int (based on what I copied from the main nativescript-angular repo), which results in null/undefined/empty string being forced to 0, meaning the hint is never shown and there isn’t the possibility to default to an unselected state. (This also is occurring with the ListPicker from the main repo, with the default state always being forced to index 0).

It’s a preference, I think, but since I’m using this with reactive FormControls and I often default new fields to an empty string, I added empty string to also result in an unselected/null state, but I can remove it if you have a preference for otherwise (and just set the value to null in own my forms by default), but since this value is an index, it seems to me an empty string should be treated as null rather than 0.

DickSmith commented 7 years ago

Removing Angular from uglify excludes.

Moving initial setSelection for Android to initNativeView to solve error in some contexts of Cannot read property ‘setSelection’ of undefined, undefined.

PeterStaev commented 7 years ago

Once again thanks for your awesome work @DickSmith ! Published the new RC version on npm.

Make sure you contact Jen Looper on the Nativescript Slack channel to get a {N} swag for your contribution 👏