Skip to content

Commit

Permalink
fix(module:cascader): search correctly when a root node is a leaf node
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan.zhao committed Sep 7, 2018
1 parent cca1fdd commit 65c6268
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions components/cascader/nz-cascader.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand All @@ -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));
}
Expand Down
60 changes: 60 additions & 0 deletions components/cascader/nz-cascader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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: `
Expand Down

0 comments on commit 65c6268

Please sign in to comment.