Skip to content

Commit 354c7cf

Browse files
authored
feat(module:cascader): add nzOpen to control visibility (#9339)
1 parent ce611e5 commit 354c7cf

7 files changed

Lines changed: 105 additions & 2 deletions

File tree

components/cascader/cascader.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ export class NzCascaderComponent
347347
@ViewChild(CdkConnectedOverlay, { static: false }) overlay!: CdkConnectedOverlay;
348348
@ViewChildren(NzCascaderOptionComponent) cascaderItems!: QueryList<NzCascaderOptionComponent>;
349349

350+
@Input() nzOpen: boolean = false;
350351
@Input() nzOptions: NzCascaderOption[] | null = [];
351352
@Input() nzOptionRender: TemplateRef<{ $implicit: NzCascaderOption; index: number }> | null = null;
352353
@Input({ transform: booleanAttribute }) nzShowInput = true;
@@ -582,7 +583,10 @@ export class NzCascaderComponent
582583
}
583584

584585
ngOnChanges(changes: SimpleChanges): void {
585-
const { nzStatus, nzSize, nzPlacement, nzOptions } = changes;
586+
const { nzOpen, nzStatus, nzSize, nzPlacement, nzOptions } = changes;
587+
if (nzOpen) {
588+
this.setMenuVisible(this.nzOpen);
589+
}
586590
if (nzOptions) {
587591
this.updateOptions();
588592
}

components/cascader/cascader.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,23 @@ describe('cascader', () => {
17541754
fixture.detectChanges();
17551755
expect(testComponent.values).toEqual(['zhejiang', 'hangzhou', 'xihu']);
17561756
}));
1757+
1758+
it('should nzOpen works', fakeAsync(() => {
1759+
fixture.detectChanges();
1760+
expect(testComponent.cascader.menuVisible).toBe(false);
1761+
expect(testComponent.nzDisabled).toBe(false);
1762+
testComponent.nzOpen = true;
1763+
tick(200);
1764+
fixture.detectChanges();
1765+
expect(testComponent.cascader.menuVisible).toBe(true);
1766+
expect(testComponent.onVisibleChange).toHaveBeenCalledTimes(1);
1767+
1768+
testComponent.nzOpen = false;
1769+
tick(200);
1770+
fixture.detectChanges();
1771+
expect(testComponent.cascader.menuVisible).toBe(false);
1772+
expect(testComponent.onVisibleChange).toHaveBeenCalledTimes(2);
1773+
}));
17571774
});
17581775

17591776
describe('multiple', () => {
@@ -2443,6 +2460,7 @@ const options5: NzSafeAny[] = [];
24432460
template: `
24442461
<nz-cascader
24452462
[(ngModel)]="values"
2463+
[nzOpen]="nzOpen"
24462464
[nzOptions]="nzOptions"
24472465
[nzAllowClear]="nzAllowClear"
24482466
[nzAutoFocus]="nzAutoFocus"
@@ -2489,6 +2507,7 @@ export class NzDemoCascaderDefaultComponent {
24892507
nzOptions: NzSafeAny[] | null = options1;
24902508
values: string[] | number[] | null = null;
24912509

2510+
nzOpen = false;
24922511
nzAllowClear = true;
24932512
nzAutoFocus = false;
24942513
nzMenuClassName = 'menu-classA menu-classB';

components/cascader/demo/open.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
order: 21
3+
title:
4+
zh-CN: 控制浮层显隐
5+
en-US: Controlled Opening
6+
---
7+
8+
## zh-CN
9+
10+
使用 `nzOpen` 控制菜单浮层显隐。
11+
12+
## en-US
13+
14+
Use `nzOpen` to control whether the menu overlay is displayed.

components/cascader/demo/open.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Component } from '@angular/core';
2+
import { FormsModule } from '@angular/forms';
3+
4+
import { NzCascaderModule, NzCascaderOption } from 'ng-zorro-antd/cascader';
5+
import { NzFlexModule } from 'ng-zorro-antd/flex';
6+
import { NzSwitchModule } from 'ng-zorro-antd/switch';
7+
8+
const options: NzCascaderOption[] = [
9+
{
10+
value: 'zhejiang',
11+
label: 'Zhejiang',
12+
children: [
13+
{
14+
value: 'hangzhou',
15+
label: 'Hangzhou',
16+
children: [
17+
{
18+
value: 'xihu',
19+
label: 'West Lake',
20+
isLeaf: true
21+
}
22+
]
23+
},
24+
{
25+
value: 'ningbo',
26+
label: 'Ningbo',
27+
isLeaf: true
28+
}
29+
]
30+
},
31+
{
32+
value: 'jiangsu',
33+
label: 'Jiangsu',
34+
children: [
35+
{
36+
value: 'nanjing',
37+
label: 'Nanjing',
38+
children: [
39+
{
40+
value: 'zhonghuamen',
41+
label: 'Zhong Hua Men',
42+
isLeaf: true
43+
}
44+
]
45+
}
46+
]
47+
}
48+
];
49+
50+
@Component({
51+
selector: 'nz-demo-cascader-open',
52+
imports: [FormsModule, NzCascaderModule, NzFlexModule, NzSwitchModule],
53+
template: `
54+
<div nz-flex nzVertical nzGap="small">
55+
<nz-switch nzSize="small" [(ngModel)]="open" nzCheckedChildren="open" nzUnCheckedChildren="close"></nz-switch>
56+
<nz-cascader [nzOptions]="nzOptions" [ngModel]="values" [nzOpen]="open"></nz-cascader>
57+
</div>
58+
`
59+
})
60+
export class NzDemoCascaderOpenComponent {
61+
nzOptions = options;
62+
values = ['zhejiang', 'hangzhou', 'xihu'];
63+
open = false;
64+
}

components/cascader/demo/variant.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
order: 21
2+
order: 20
33
title:
44
zh-CN: 形态变体
55
en-US: Variants

components/cascader/doc/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ description: Cascade selection box.
4343
| `[nzNotFoundContent]` | specify content to show when no result matches | `string \| TemplateRef<void>` | - |
4444
| `[nzOptionRender]` | render template of cascader options | `TemplateRef<{ $implicit: NzCascaderOption, index: number }>` | |
4545
| `[nzOptions]` | data options of cascade | `object[]` | - |
46+
| `[nzOpen]` | Set visible of cascader popup | `boolean` | `false` |
4647
| `[nzPlaceHolder]` | input placeholder | `string` | `'Please select'` |
4748
| `[nzPlacement]` | popup placement | `'bottomLeft' \| 'bottomRight' \| 'topLeft' \| 'topRight'` | `'bottomLeft'` |
4849
| `[nzShowArrow]` | whether show arrow | `boolean` | `true` |

components/cascader/doc/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ description: 级联选择框。
4444
| `[nzNotFoundContent]` | 当下拉列表为空时显示的内容 | `string \| TemplateRef<void>` | - |
4545
| `[nzOptionRender]` | 选项的渲染模板 | `TemplateRef<{ $implicit: NzCascaderOption, index: number }>` | |
4646
| `[nzOptions]` | 可选项数据源 | `object[]` | - |
47+
| `[nzOpen]` | 控制浮层显隐 | `boolean` | `false` |
4748
| `[nzPlaceHolder]` | 输入框占位文本 | `string` | `'请选择'` |
4849
| `[nzPlacement]` | 浮层弹出位置 | `'bottomLeft' \| 'bottomRight' \| 'topLeft' \| 'topRight'` | `'bottomLeft'` |
4950
| `[nzShowArrow]` | 是否显示箭头 | `boolean` | `true` |

0 commit comments

Comments
 (0)