From b0cd7d8b5fefe8915a0d275cc5c16e7f3f0f0c0c Mon Sep 17 00:00:00 2001 From: wenqi73 <1264578441@qq.com> Date: Sun, 12 May 2019 20:01:42 +0800 Subject: [PATCH] fix: use component insteadof value --- components/select/demo/select-users.ts | 15 +++++++-------- components/select/nz-select.service.spec.ts | 11 +++++++++-- components/select/nz-select.service.ts | 13 +++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/components/select/demo/select-users.ts b/components/select/demo/select-users.ts index fb770d2f0c2..3bf53ab38f2 100644 --- a/components/select/demo/select-users.ts +++ b/components/select/demo/select-users.ts @@ -1,15 +1,14 @@ import { HttpClient } from '@angular/common/http'; import { Component, OnInit } from '@angular/core'; -import { BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, Observable } from 'rxjs'; import { debounceTime, map, switchMap } from 'rxjs/operators'; @Component({ selector: 'nz-demo-select-select-users', template: ` -

{{ selectedUser | json }}

- + Loading Data... @@ -36,7 +35,7 @@ import { debounceTime, map, switchMap } from 'rxjs/operators'; export class NzDemoSelectSelectUsersComponent implements OnInit { randomUserUrl = 'https://api.randomuser.me/?results=5'; searchChange$ = new BehaviorSubject(''); - optionList: Array<{ text: string; id: number }> = []; + optionList: string[] = []; selectedUser: string; isLoading = false; @@ -55,12 +54,12 @@ export class NzDemoSelectSelectUsersComponent implements OnInit { .pipe(map((res: any) => res.results)) .pipe( map((list: any) => { - return list.map((item: any, index: number) => ({ text: `${item.name.first} ${name}`, id: index + 1 })); + return list.map((item: any) => `${item.name.first} ${name}`); }) ); - const optionList$ = this.searchChange$ + const optionList$: Observable = this.searchChange$ .asObservable() - .pipe(debounceTime(100)) + .pipe(debounceTime(500)) .pipe(switchMap(getRandomNameList)); optionList$.subscribe(data => { this.optionList = data; diff --git a/components/select/nz-select.service.spec.ts b/components/select/nz-select.service.spec.ts index a18f44db7f8..59c2f8f858f 100644 --- a/components/select/nz-select.service.spec.ts +++ b/components/select/nz-select.service.spec.ts @@ -72,11 +72,18 @@ describe('SelectService', () => { beforeEach(() => { service.mode = 'tags'; }); - it('should updateListOfTagOption work', () => { - service.listOfSelectedValue = [`option_value_0`, `option_value_1`, `option_value_miss`]; + fit('should updateListOfTagOption work', () => { + service.listOfCachedSelectedOption = [ + { nzValue: `option_value_0`, nzLabel: `option_label_0` }, + { nzValue: `option_value_1`, nzLabel: `option_label_1` }, + { nzValue: `option_value_miss`, nzLabel: `option_label_miss` } + // tslint:disable-next-line: no-any + ] as any; service.listOfTemplateOption = createListOfOption(3); service.updateListOfTagOption(); expect(service.listOfTagOption.length).toEqual(1); + expect(service.listOfTagOption[0].nzValue).toEqual('option_value_miss'); + expect(service.listOfTagOption[0].nzLabel).toEqual('option_label_miss'); }); it('should updateAddTagOption work', () => { service.listOfSelectedValue = [`option_value_0`, `option_value_1`]; diff --git a/components/select/nz-select.service.ts b/components/select/nz-select.service.ts index daf58df5035..13fbb11b3a1 100644 --- a/components/select/nz-select.service.ts +++ b/components/select/nz-select.service.ts @@ -167,7 +167,7 @@ export class NzSelectService { } else { const listOfCachedSelectedOption: NzOptionComponent[] = []; this.listOfSelectedValue.forEach(v => { - const listOfMixedOption = [...this.listOfCachedSelectedOption, ...this.listOfTagAndTemplateOption]; + const listOfMixedOption = [...this.listOfTagAndTemplateOption, ...this.listOfCachedSelectedOption]; const option = listOfMixedOption.find(o => this.compareWith(o.nzValue, v)); if (option) { listOfCachedSelectedOption.push(option); @@ -175,20 +175,13 @@ export class NzSelectService { }); this.listOfCachedSelectedOption = listOfCachedSelectedOption; } - console.log(this.listOfCachedSelectedOption); } updateListOfTagOption(): void { if (this.isTagsMode) { - const listOfMissValue = this.listOfSelectedValue.filter( - value => !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, value)) + this.listOfTagOption = this.listOfCachedSelectedOption.filter( + comp => !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, comp.nzValue)) ); - this.listOfTagOption = listOfMissValue.map(value => { - const nzOptionComponent = new NzOptionComponent(); - nzOptionComponent.nzValue = value; - nzOptionComponent.nzLabel = value; - return nzOptionComponent; - }); this.listOfTagAndTemplateOption = [...this.listOfTemplateOption.concat(this.listOfTagOption)]; } else { this.listOfTagAndTemplateOption = [...this.listOfTemplateOption];