From 65c62686333b4ea4564630b4bcd6183fa8816f6a Mon Sep 17 00:00:00 2001 From: "ethan.zhao" Date: Thu, 6 Sep 2018 16:39:32 +0800 Subject: [PATCH] fix(module:cascader): search correctly when a root node is a leaf node --- components/cascader/nz-cascader.component.ts | 4 +- components/cascader/nz-cascader.spec.ts | 60 ++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/components/cascader/nz-cascader.component.ts b/components/cascader/nz-cascader.component.ts index 8cda1bbebcc..82e576b63d0 100644 --- a/components/cascader/nz-cascader.component.ts +++ b/components/cascader/nz-cascader.component.ts @@ -1227,7 +1227,7 @@ export class NzCascaderComponent implements OnInit, OnDestroy, ControlValueAcces if (!sNode.isLeaf) { loopParent(sNode, disabled); } - if (sNode.isLeaf || !sNode.children) { + if (sNode.isLeaf || !sNode.children || !sNode.children.length) { loopChild(sNode, disabled); } }); @@ -1248,7 +1248,7 @@ export class NzCascaderComponent implements OnInit, OnDestroy, ControlValueAcces path.pop(); }; - this.oldColumnsHolder[ 0 ].forEach(node => loopParent(node)); + this.oldColumnsHolder[ 0 ].forEach(node => (node.isLeaf || !node.children || !node.children.length) ? loopChild(node) : loopParent(node)); if (sorter) { results.sort((a, b) => sorter(a.path, b.path, this._inputValue)); } diff --git a/components/cascader/nz-cascader.spec.ts b/components/cascader/nz-cascader.spec.ts index 1ab9ac0d034..347f31c0617 100644 --- a/components/cascader/nz-cascader.spec.ts +++ b/components/cascader/nz-cascader.spec.ts @@ -1441,6 +1441,32 @@ describe('cascader', () => { fixture.detectChanges(); expect(itemEl1.classList).not.toContain('ant-cascader-menu-item-active'); }); + it('should support search a root node have no children ', (done) => { + fixture.detectChanges(); + testComponent.nzShowSearch = true; + testComponent.nzOptions = options5; + fixture.detectChanges(); + // const input: HTMLElement = cascader.nativeElement.querySelector('.ant-cascader-input'); + const spy = spyOn(testComponent.cascader, 'focus'); + cascader.nativeElement.click(); + fixture.detectChanges(); + // expect(document.activeElement).toBe(input); + expect(spy).toHaveBeenCalled(); + testComponent.cascader.inputValue = 'Roo'; + testComponent.cascader.setMenuVisible(true); + fixture.detectChanges(); + const itemEl1 = overlayContainerElement.querySelector('.ant-cascader-menu:nth-child(1) .ant-cascader-menu-item:nth-child(1)') as HTMLElement; + expect(testComponent.cascader.inSearch).toBe(true); + expect(itemEl1.innerText).toBe('Root'); + itemEl1.click(); + fixture.whenStable().then(() => { + expect(testComponent.cascader.inSearch).toBe(false); + expect(testComponent.cascader.menuVisible).toBe(false); + expect(testComponent.cascader.inputValue).toBe(''); + expect(testComponent.values.join(',')).toBe('root'); + done(); + }); + }); }); describe('load data lazily', () => { @@ -1731,6 +1757,40 @@ const options4 = [ { } ] } ] } ]; + +const options5 = [ { + value : 'zhejiang', + label : 'Zhejiang', + children: [ { + value : 'hangzhou', + label : 'Hangzhou', + children: [ { + value : 'xihu', + label : 'West Lake', + isLeaf: true + } ] + }, { + value : 'ningbo', + label : 'Ningbo', + isLeaf: true + } ] +}, { + value : 'jiangsu', + label : 'Jiangsu', + children: [ { + value : 'nanjing', + label : 'Nanjing', + children: [ { + value : 'zhonghuamen', + label : 'Zhong Hua Men', + isLeaf: true + } ] + } ] +}, { + value : 'root', + label : 'Root', + isLeaf: true +} ]; @Component({ selector: 'nz-demo-cascader-default', template: `