From 7405bed6b2fe9bb65b8272afb4e9d5504c5d5975 Mon Sep 17 00:00:00 2001 From: ZhiDanLuo Date: Wed, 12 Jun 2019 21:00:20 +0800 Subject: [PATCH] fix(module:select): remove duplicate drop-down menu options when nzMode === 'tags' (#3559) close #3559 * fix(module:select): remove duplicate drop-down menu options when nzMode === 'tags'(#3559) --- components/select/nz-select.service.spec.ts | 13 ++++++++----- components/select/nz-select.service.ts | 7 +++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/components/select/nz-select.service.spec.ts b/components/select/nz-select.service.spec.ts index f87f541e474..3fdb7cb52a3 100644 --- a/components/select/nz-select.service.spec.ts +++ b/components/select/nz-select.service.spec.ts @@ -75,17 +75,20 @@ describe('SelectService', () => { it('should updateListOfTagOption work', () => { service.listOfCachedSelectedOption = [ { nzValue: `option_value_0`, nzLabel: `option_label_0` }, - { nzValue: `option_value_miss`, nzLabel: `option_label_miss` } + { nzValue: `option_value_miss`, nzLabel: `option_label_miss` }, + { nzValue: `option_value_a`, nzLabel: `option_label_a` } // tslint:disable-next-line: no-any ] as any; - service.listOfSelectedValue = [`option_value_1`, `option_value_miss_1`]; + service.listOfSelectedValue = [`option_value_1`, `option_value_miss_1`, `option_value_a`]; service.listOfTemplateOption = createListOfOption(3); service.updateListOfTagOption(); - expect(service.listOfTagOption.length).toEqual(2); + expect(service.listOfTagOption.length).toEqual(3); 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'); + expect(service.listOfTagOption[1].nzValue).toEqual('option_value_a'); + expect(service.listOfTagOption[1].nzLabel).toEqual('option_label_a'); + expect(service.listOfTagOption[2].nzValue).toEqual('option_value_miss_1'); + expect(service.listOfTagOption[2].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 93c6c92d826..bb5184f40ef 100644 --- a/components/select/nz-select.service.ts +++ b/components/select/nz-select.service.ts @@ -182,9 +182,11 @@ export class NzSelectService { // https://github.com/NG-ZORRO/ng-zorro-antd/issues/3424 this.listOfTagOption = [...this.listOfCachedSelectedOption, ...this.listOfSelectedValue].reduce( (options: NzOptionComponent[], componentOrValue) => { + // https://github.com/NG-ZORRO/ng-zorro-antd/issues/3559 if ( typeof componentOrValue === 'string' && - !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, componentOrValue)) + !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, componentOrValue)) && + ![...this.listOfTemplateOption, ...options].find(o => o.nzValue === componentOrValue) ) { const nzOptionComponent = new NzOptionComponent(); nzOptionComponent.nzValue = componentOrValue; @@ -192,7 +194,8 @@ export class NzSelectService { options.push(nzOptionComponent); } else if ( componentOrValue.nzValue && - !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, componentOrValue.nzValue)) + !this.listOfTemplateOption.find(o => this.compareWith(o.nzValue, componentOrValue.nzValue)) && + ![...this.listOfTemplateOption, ...options].find(o => o.nzValue === componentOrValue.nzValue) ) { options.push(componentOrValue); }