Skip to content

Commit

Permalink
Model bindings (#29)
Browse files Browse the repository at this point in the history
* bind value to object by default

* write value tests and fixes

* small fixes

* add more tests

* Check if index > -1

* add more tests

* update readme and demo
  • Loading branch information
Tadeuš Varnas committed Sep 19, 2017
1 parent 06b79a6 commit d976c37
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 86 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -74,12 +74,12 @@ map: {
### API
| Input | Type | Default | Required | Description |
| ------------- | ------------- | ------------- | ------------- | ------------- |
| [items] | Array<NgOption> | `[]` | yes | Items array|
| labelKey | string | `label` | no | Bind option display text to object property. Default `label` |
| valueKey | string | `value` | no | Bind selected option model value to property or whole object if used as `valueKey="this"`. Default `value`|
| [clearable] | boolean | `true` | no | Set is allowed to clear selected value. Default `true`|
| placeholder | string | `null` | no | Set placeholder text. Default `null`|
| [typeahead] | Subject | `null` | no | Set custom autocomplete or filter. Default `null`|
| [items] | Array<NgOption> | `[]` | yes | Items array |
| labelKey | string | `label` | no | Object property to use for label. Default `label` |
| valueKey | string | `-` | no | Object property to use for selected model. By default binds to whole object. |
| [clearable] | boolean | `true` | no | Allow to clear selected value. Default `true`|
| placeholder | string | `-` | no | Placeholder text. |
| [typeahead] | Subject | `-` | no | Custom autocomplete or filter. |

| Output | Description |
| ------------- | ------------- |
Expand Down
1 change: 1 addition & 0 deletions src/demo/app/reactive-forms.component.ts
Expand Up @@ -31,6 +31,7 @@ import {HttpClient} from '@angular/common/http';
<div class="form-group">
<label for="state">Age</label>
<ng-select [items]="ages"
valueKey="value"
placeholder="Select age"
formControlName="age">
</ng-select>
Expand Down
1 change: 0 additions & 1 deletion src/demo/app/select-autocomplete.component.ts
Expand Up @@ -8,7 +8,6 @@ import { Observable } from 'rxjs/Observable';
<label>Search with autocomplete in Github accounts</label>
<ng-select [items]="items"
labelKey="login"
valueKey="this"
[typeahead]="typeahead"
[(ngModel)]="githubAccount">
Expand Down
1 change: 0 additions & 1 deletion src/demo/app/select-bindings.component.ts
Expand Up @@ -14,7 +14,6 @@ import {Component} from '@angular/core';
<label>Bind model to object</label>
<ng-select [items]="cities"
labelKey="name"
valueKey="this"
placeholder="Select value"
[clearable]="false"
[(ngModel)]="selectedCity">
Expand Down
26 changes: 15 additions & 11 deletions src/demo/app/select-multi.component.ts
Expand Up @@ -4,23 +4,23 @@ import { NgOption } from '@ng-select/ng-select';
@Component({
template: `
<label>Select multiple elements</label>
<ng-select [items]="companies"
labelKey="name"
valueKey="this"
[multiple]="true"
[(ngModel)]="selectedCompany">
<ng-select
[items]="companies"
labelKey="name"
[multiple]="true"
[(ngModel)]="selectedCompany">
</ng-select>
<p>
Selected value: {{selectedCompany | json}}
</p>
<label>Disabled multiple elements</label>
<ng-select [items]="companies2"
labelKey="name"
valueKey="this"
[multiple]="true"
[disabled]="disable"
[(ngModel)]="selectedCompanyDisabled">
<ng-select
[items]="companies2"
labelKey="name"
[multiple]="true"
[disabled]="disable"
[(ngModel)]="selectedCompanyDisabled">
</ng-select>
<br>
<button class="btn btn-secondary btn-sm" (click)="disable = !disable">Toggle disabled</button>
Expand All @@ -31,6 +31,8 @@ export class SelectMultiComponent {
companies: any[] = [];
companies2: any[] = [];
selectedCompany: any;
selectedCompanyDisabled: any;
disable = true;

/* tslint:disable */
companiesNames = ['Miškas', 'Žalias', 'Flexigen', 'Rooforia', 'Rooblia', 'Tropoli', 'Eargo', 'Gadtron', 'Elentrix', 'Terragen', 'Medalert', 'Xelegyl', 'Bristo', 'Xylar', 'Imperium', 'Kangle', 'Earwax', 'Zanity', 'Portico', 'Tsunamia', 'Kage', 'Comstar', 'Radiantix', 'Bostonic', 'Geekko', 'Eventex', 'Stockpost', 'Silodyne', 'Enersave', 'Perkle', 'Pyramis', 'Accuprint', 'Papricut', 'Pathways', 'Circum', 'Gology', 'Buzzworks', 'Dancerity', 'Zounds', 'Diginetic', 'Snips', 'Chillium', 'Exotechno', 'Accufarm', 'Vidto', 'Signidyne', 'Escenta', 'Sureplex', 'Quarmony', 'Interfind', 'Exoswitch', 'Mondicil', 'Pyramia', 'Digitalus', 'Earthplex', 'Limozen', 'Twiist', 'Tubalum', 'Securia', 'Uni', 'Biospan', 'Zensus', 'Memora'];
Expand All @@ -41,6 +43,8 @@ export class SelectMultiComponent {
this.companies.push({ id: i, name: c });
this.companies2.push({ id: i, name: c });
});

this.selectedCompanyDisabled = [{ id: 0, name: 'Miškas' }, { id: 1, name: 'Žalias' }]
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/demo/app/select-search.component.ts
Expand Up @@ -6,7 +6,6 @@ import { Component, EventEmitter } from '@angular/core';
<label>Search in label text (default)</label>
<ng-select [items]="companies"
labelKey="name"
valueKey="this"
[(ngModel)]="selectedCompany">
</ng-select>
<p>
Expand All @@ -16,7 +15,6 @@ import { Component, EventEmitter } from '@angular/core';
<label>Search using custom filter handler (search for Rooforia)</label>
<ng-select [items]="filteredCompanies2"
labelKey="name"
valueKey="this"
[typeahead]="customFilter"
[(ngModel)]="selectedCompany2">
</ng-select>
Expand Down

0 comments on commit d976c37

Please sign in to comment.