Skip to content

Commit

Permalink
fix(module:tree-select): should not close when the selectable is false (
Browse files Browse the repository at this point in the history
NG-ZORRO#3843)

* fix(module:tree-select): should not close when the selectable is false

close NG-ZORRO#3833

* test(module:tree-select): test getter
  • Loading branch information
hsuanxyz authored and Ricbet committed Apr 9, 2020
1 parent 1973b9a commit a7d4d0a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/tree-select/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
| `[nzAsyncData]` | Load data asynchronously (should be used with NzTreeNode.addChildren(...)) | `boolean` | `false` |
| `[nzNodes]` | Data of the treeNodes | `NzTreeNodeOptions[]` | `[]` |
| `[nzDefaultExpandAll]` | Whether to expand all treeNodes by default | `boolean` | `false` |
| `[nzDefaultExpandedKeys]` | Default expanded treeNodes | `string[]` | - |
| `[nzExpandedKeys]` | Default expanded treeNodes | `string[]` | - |
| `[nzDisplayWith]` | How to display the selected node value in the trigger | `(node: NzTreeNode) => string` | `(node: NzTreeNode) => node.title` |
| `[nzMaxTagCount]` | Max tag count to show| number | - |
| `[nzMaxTagPlaceholder]` | Placeholder for not showing tags | TemplateRef<{ $implicit: NzTreeNode[] }> | - |
Expand Down
2 changes: 1 addition & 1 deletion components/tree-select/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
| `[nzAsyncData]` | 是否异步加载(显示加载状态) | `boolean` | `false` |
| `[nzNodes]` | treeNodes 数据 | `NzTreeNodeOptions[]` | `[]` |
| `[nzDefaultExpandAll]` | 默认展开所有树节点 | `boolean` | `false` |
| `[nzDefaultExpandedKeys]` | 默认展开指定的树节点 | `string[]` | - |
| `[nzExpandedKeys]` | 默认展开指定的树节点 | `string[]` | - |
| `[nzDisplayWith]` | 如何在输入框显示所选的节点值的方法 | `(node: NzTreeNode) => string` | `(node: NzTreeNode) => node.title` |
| `[nzMaxTagCount]` | 最多显示多少个 tag | number | - |
| `[nzMaxTagPlaceholder]` | 隐藏 tag 时显示的内容 | TemplateRef<{ $implicit: NzTreeNode[] }> | - |
Expand Down
2 changes: 1 addition & 1 deletion components/tree-select/nz-tree-select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
[nzShowLine]="nzShowLine"
[nzExpandedIcon]="nzExpandedIcon"
[nzExpandAll]="nzDefaultExpandAll"
[nzExpandedKeys]="nzDefaultExpandedKeys"
[nzExpandedKeys]="expandedKeys"
[nzCheckedKeys]="nzCheckable ? value : []"
[nzSelectedKeys]="!nzCheckable ? value : []"
[nzTreeTemplate]="nzTreeTemplate"
Expand Down
27 changes: 24 additions & 3 deletions components/tree-select/nz-tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { filter, tap } from 'rxjs/operators';
import {
isNotNil,
slideMotion,
warnDeprecation,
zoomMotion,
InputBoolean,
NzFormatEmitEvent,
Expand Down Expand Up @@ -117,7 +118,26 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
@Input() nzSize: NzSizeLDSType = 'default';
@Input() nzPlaceHolder = '';
@Input() nzDropdownStyle: { [key: string]: string };
@Input() nzDefaultExpandedKeys: string[] = [];
/**
* @deprecated 9.0.0 - use `nzExpandedKeys` instead.
*/
@Input()
set nzDefaultExpandedKeys(value: string[]) {
warnDeprecation(`'nzDefaultExpandedKeys' would be removed in 9.0.0. Please use 'nzExpandedKeys' instead.`);
this.expandedKeys = value;
}
get nzDefaultExpandedKeys(): string[] {
return this.expandedKeys;
}

@Input()
set nzExpandedKeys(value: string[]) {
this.expandedKeys = value;
}
get nzExpandedKeys(): string[] {
return this.expandedKeys;
}

@Input() nzDisplayWith: (node: NzTreeNode) => string | undefined = (node: NzTreeNode) => node.title;
@Input() nzMaxTagCount: number;
@Input() nzMaxTagPlaceholder: TemplateRef<{ $implicit: NzTreeNode[] }>;
Expand All @@ -143,6 +163,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
dropDownPosition: 'top' | 'center' | 'bottom' = 'bottom';
selectionChangeSubscription: Subscription;
selectedNodes: NzTreeNode[] = [];
expandedKeys: string[] = [];
value: string[] = [];

onChange: (value: string[] | string | null) => void;
Expand Down Expand Up @@ -285,7 +306,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc

onExpandedKeysChange(value: NzFormatEmitEvent): void {
this.nzExpandChange.emit(value);
this.nzDefaultExpandedKeys = [...value.keys!];
this.expandedKeys = [...value.keys!];
}

setInputValue(value: string): void {
Expand Down Expand Up @@ -336,7 +357,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
}),
filter((event: NzFormatEmitEvent) => {
const node = event.node!;
return this.nzCheckable ? !node.isDisabled && !node.isDisableCheckbox : !node.isDisabled;
return this.nzCheckable ? !node.isDisabled && !node.isDisableCheckbox : !node.isDisabled && node.isSelectable;
})
),
this.nzCheckable ? this.nzTreeCheckBoxChange : observableOf(),
Expand Down
30 changes: 27 additions & 3 deletions components/tree-select/nz-tree-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
dispatchFakeEvent,
dispatchMouseEvent,
typeInElement,
NzTreeNode
NzTreeNode,
NzTreeNodeOptions
} from 'ng-zorro-antd/core';

import { NzTreeSelectComponent } from './nz-tree-select.component';
Expand Down Expand Up @@ -247,6 +248,26 @@ describe('tree-select component', () => {
`+ ${testComponent.value.length - testComponent.maxTagCount} ...`
);
}));

it('should set selectable', fakeAsync(() => {
treeSelect.nativeElement.click();
fixture.detectChanges();
expect(treeSelectComponent.nzOpen).toBe(true);
let node = overlayContainerElement.querySelector('nz-tree-node')!;
dispatchMouseEvent(node, 'click');
fixture.detectChanges();
flush();
expect(treeSelectComponent.nzOpen).toBe(false);
testComponent.nodes[0].selectable = false;
treeSelect.nativeElement.click();
fixture.detectChanges();
expect(treeSelectComponent.nzOpen).toBe(true);
node = overlayContainerElement.querySelector('nz-tree-node')!;
dispatchMouseEvent(node, 'click');
fixture.detectChanges();
flush();
expect(treeSelectComponent.nzOpen).toBe(true);
}));
});

describe('checkable', () => {
Expand Down Expand Up @@ -444,6 +465,7 @@ describe('tree-select component', () => {
treeSelect.nativeElement.click();
fixture.detectChanges();
expect(treeSelectComponent.nzDefaultExpandedKeys.length === 0).toBe(true);
expect(treeSelectComponent.nzExpandedKeys.length === 0).toBe(true);
expect(treeSelectComponent.nzOpen).toBe(true);
let targetSwitcher = overlayContainerElement.querySelector('.ant-select-tree-switcher')!;
expect(targetSwitcher.classList.contains('ant-select-tree-switcher_close')).toBe(true);
Expand All @@ -452,6 +474,7 @@ describe('tree-select component', () => {
fixture.detectChanges();
expect(targetSwitcher.classList.contains('ant-select-tree-switcher_open')).toBe(true);
expect(treeSelectComponent.nzDefaultExpandedKeys[0] === '1001').toBe(true);
expect(treeSelectComponent.nzExpandedKeys[0] === '1001').toBe(true);
treeSelect.nativeElement.click();
fixture.detectChanges();
expect(treeSelectComponent.nzOpen).toBe(false);
Expand All @@ -461,6 +484,7 @@ describe('tree-select component', () => {
expect(treeSelectComponent.nzOpen).toBe(true);
expect(targetSwitcher.classList.contains('ant-select-tree-switcher_open')).toBe(true);
expect(treeSelectComponent.nzDefaultExpandedKeys[0] === '1001').toBe(true);
expect(treeSelectComponent.nzExpandedKeys[0] === '1001').toBe(true);
});
});

Expand All @@ -486,7 +510,7 @@ describe('tree-select component', () => {
<nz-tree-select
style="width:250px;position: relative;display: block;"
nzPlaceHolder="Please select"
[nzDefaultExpandedKeys]="expandKeys"
[nzExpandedKeys]="expandKeys"
[nzNodes]="nodes"
[(ngModel)]="value"
[nzSize]="size"
Expand All @@ -512,7 +536,7 @@ export class NzTestTreeSelectBasicComponent {
dropdownMatchSelectWidth = true;
multiple = false;
maxTagCount = Infinity;
nodes = [
nodes: NzTreeNodeOptions[] = [
{
title: 'root1',
key: '1001',
Expand Down

0 comments on commit a7d4d0a

Please sign in to comment.