Skip to content

Commit 1436f21

Browse files
feat(module:cascader): support for load options with observable (#8048)
1 parent 1050954 commit 1436f21

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

components/cascader/cascader.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export class NzCascaderComponent
275275

276276
@Input() nzTriggerAction: NzCascaderTriggerType | NzCascaderTriggerType[] = ['click'] as NzCascaderTriggerType[];
277277
@Input() nzChangeOn?: (option: NzCascaderOption, level: number) => boolean;
278-
@Input() nzLoadData?: (node: NzCascaderOption, index: number) => PromiseLike<NzSafeAny>;
278+
@Input() nzLoadData?: (node: NzCascaderOption, index: number) => PromiseLike<NzSafeAny> | Observable<NzSafeAny>;
279279
// TODO: RTL
280280
@Input() nzSuffixIcon: string | TemplateRef<void> = 'down';
281281
@Input() nzExpandIcon: string | TemplateRef<void> = '';

components/cascader/cascader.service.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { Injectable, OnDestroy } from '@angular/core';
7-
import { BehaviorSubject, Subject } from 'rxjs';
7+
import { BehaviorSubject, finalize, from, Subject } from 'rxjs';
88

99
import { NzSafeAny } from 'ng-zorro-antd/core/types';
1010
import { arraysEqual, isNotNil } from 'ng-zorro-antd/core/util';
@@ -401,27 +401,26 @@ export class NzCascaderService implements OnDestroy {
401401
option.loading = true;
402402
}
403403

404-
loadFn(option, columnIndex).then(
405-
() => {
406-
option.loading = false;
407-
if (option.children) {
408-
this.setColumnData(option.children, columnIndex + 1, option);
404+
from(loadFn(option, columnIndex))
405+
.pipe(
406+
finalize(() => {
407+
option.loading = false;
408+
this.$loading.next(false);
409+
this.$redraw.next();
410+
})
411+
)
412+
.subscribe({
413+
next: () => {
414+
if (option.children) {
415+
this.setColumnData(option.children, columnIndex + 1, option);
416+
}
417+
success?.();
418+
},
419+
error: () => {
420+
option.isLeaf = true;
421+
failure?.();
409422
}
410-
if (success) {
411-
success();
412-
}
413-
this.$loading.next(false);
414-
this.$redraw.next();
415-
},
416-
() => {
417-
option.loading = false;
418-
option.isLeaf = true;
419-
if (failure) {
420-
failure();
421-
}
422-
this.$redraw.next();
423-
}
424-
);
423+
});
425424
}
426425
}
427426

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { NzCascaderModule } from 'ng-zorro-antd/cascader';
3939
| `[nzExpandTrigger]` | expand current item when clicked or hovered, one of 'click' 'hover' | `'click'\|'hover'` | `'click'` |
4040
| `[nzLabelProperty]` | the label property name of options | `string` | `'label'` |
4141
| `[nzLabelRender]` | render template of displaying selected options | `TemplateRef<any>` | - |
42-
| `[nzLoadData]` | to load option lazily. Lazy load will be called immediately if the setting is `ngModel` with an array value and `nzOptions` is not set | `(option: any, index?: index) => PromiseLike<any>` | - |
42+
| `[nzLoadData]` | to load option lazily. Lazy load will be called immediately if the setting is `ngModel` with an array value and `nzOptions` is not set | `(option: any, index?: index) => PromiseLike<any> \| Observable<any>` | - |
4343
| `[nzMenuClassName]` | additional className of popup overlay | `string` | - |
4444
| `[nzMenuStyle]` | additional css style of popup overlay | `object` | - |
4545
| `[nzNotFoundContent]` | specify content to show when no result matches | `string\|TemplateRef<void>` | - |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { NzCascaderModule } from 'ng-zorro-antd/cascader';
4040
| `[nzExpandTrigger]` | 次级菜单的展开方式,可选 'click' 和 'hover' | `'click'\|'hover'` | `'click'` |
4141
| `[nzLabelProperty]` | 选项的显示值的属性名 | `string` | `'label'` |
4242
| `[nzLabelRender]` | 选择后展示的渲染模板 | `TemplateRef<any>` | - |
43-
| `[nzLoadData]` | 用于动态加载选项。如果提供了`ngModel`初始值,且未提供`nzOptions`值,则会立即触发动态加载。 | `(option: any, index?: index) => PromiseLike<any>` | - |
43+
| `[nzLoadData]` | 用于动态加载选项。如果提供了`ngModel`初始值,且未提供`nzOptions`值,则会立即触发动态加载。 | `(option: any, index?: index) => PromiseLike<any> \| Observable<any>` | - |
4444
| `[nzMenuClassName]` | 自定义浮层类名 | `string` | - |
4545
| `[nzMenuStyle]` | 自定义浮层样式 | `object` | - |
4646
| `[nzNotFoundContent]` | 当下拉列表为空时显示的内容 | `string\|TemplateRef<void>` | - |

components/cascader/typings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6+
import { Observable } from 'rxjs';
7+
68
import { NzSafeAny } from 'ng-zorro-antd/core/types';
79

810
export type NzCascaderExpandTrigger = 'click' | 'hover';
@@ -51,5 +53,5 @@ export interface NzCascaderComponentAsSource {
5153

5254
nzChangeOn?(option: NzCascaderOption, level: number): boolean;
5355

54-
nzLoadData?(node: NzCascaderOption, index: number): PromiseLike<NzSafeAny>;
56+
nzLoadData?(node: NzCascaderOption, index: number): PromiseLike<NzSafeAny> | Observable<NzSafeAny>;
5557
}

0 commit comments

Comments
 (0)