From a05f5a50d1d4df9a145e0aaa8071c939209033b3 Mon Sep 17 00:00:00 2001 From: Wenqi <1264578441@qq.com> Date: Wed, 5 Jun 2019 20:08:07 +0800 Subject: [PATCH] fix(module:select): display error when in tag and search mode (#3442) close #3424 * fix(module:select): display error when in tag and search mode * fix: concat cached options and selected values --- components/select/nz-select.service.spec.ts | 13 ++++++++-- components/select/nz-select.service.ts | 28 +++++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/components/select/nz-select.service.spec.ts b/components/select/nz-select.service.spec.ts index a18f44db7f8..f87f541e474 100644 --- a/components/select/nz-select.service.spec.ts +++ b/components/select/nz-select.service.spec.ts @@ -73,10 +73,19 @@ describe('SelectService', () => { service.mode = 'tags'; }); it('should updateListOfTagOption work', () => { - service.listOfSelectedValue = [`option_value_0`, `option_value_1`, `option_value_miss`]; + service.listOfCachedSelectedOption = [ + { nzValue: `option_value_0`, nzLabel: `option_label_0` }, + { nzValue: `option_value_miss`, nzLabel: `option_label_miss` } + // tslint:disable-next-line: no-any + ] as any; + service.listOfSelectedValue = [`option_value_1`, `option_value_miss_1`]; service.listOfTemplateOption = createListOfOption(3); service.updateListOfTagOption(); - expect(service.listOfTagOption.length).toEqual(1); + expect(service.listOfTagOption.length).toEqual(2); + expect(service.listOfTagOption[0].nzValue).toEqual('option_value_miss'); + expect(service.listOfTagOption[0].nzLabel).toEqual('option_label_miss'); + expect(service.listOfTagOption[1].nzValue).toEqual('option_value_miss_1'); + expect(service.listOfTagOption[1].nzLabel).toEqual('option_value_miss_1'); }); 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 77e326df71e..93c6c92d826 100644 --- a/components/select/nz-select.service.ts +++ b/components/select/nz-select.service.ts @@ -179,15 +179,27 @@ export class NzSelectService { updateListOfTagOption(): void { if (this.isTagsMode) { - const listOfMissValue = this.listOfSelectedValue.filter( - value => !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, value)) + // https://github.com/NG-ZORRO/ng-zorro-antd/issues/3424 + this.listOfTagOption = [...this.listOfCachedSelectedOption, ...this.listOfSelectedValue].reduce( + (options: NzOptionComponent[], componentOrValue) => { + if ( + typeof componentOrValue === 'string' && + !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, componentOrValue)) + ) { + const nzOptionComponent = new NzOptionComponent(); + nzOptionComponent.nzValue = componentOrValue; + nzOptionComponent.nzLabel = componentOrValue; + options.push(nzOptionComponent); + } else if ( + componentOrValue.nzValue && + !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, componentOrValue.nzValue)) + ) { + options.push(componentOrValue); + } + return options; + }, + [] ); - 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];