Skip to content

Commit

Permalink
fix(module:cascader): fix children state not changed in async demo (N…
Browse files Browse the repository at this point in the history
…G-ZORRO#2985)

* fix(module:cascader): fix children state not changed in async demo

* build: fix ci

* Update components/cascader/nz-cascader-li.component.ts

Co-Authored-By: wendzhue <wendzhue@gmail.com>

close NG-ZORRO#2984
  • Loading branch information
Wendell authored and Ricbet committed Apr 9, 2020
1 parent 4c19cff commit 0b23225
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion components/cascader/nz-cascader-li.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input, Renderer2,
Expand Down Expand Up @@ -27,7 +28,7 @@ export class NzCascaderOptionComponent {
@Input() highlightText: string;
@Input() nzLabelProperty = 'label';

constructor(private sanitizer: DomSanitizer, elementRef: ElementRef, renderer: Renderer2) {
constructor(private sanitizer: DomSanitizer, private cdr: ChangeDetectorRef, elementRef: ElementRef, renderer: Renderer2) {
renderer.addClass(elementRef.nativeElement, 'ant-cascader-menu-item');
}

Expand All @@ -42,4 +43,8 @@ export class NzCascaderOptionComponent {
}
return str.replace(new RegExp(this.highlightText, 'g'), safeHtml);
}

markForCheck(): void {
this.cdr.markForCheck();
}
}
23 changes: 16 additions & 7 deletions components/cascader/nz-cascader.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import {
OnDestroy,
Optional,
Output,
QueryList,
Renderer2,
TemplateRef,
ViewChild,
ViewChildren,
ViewEncapsulation
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
Expand All @@ -26,6 +28,7 @@ import { DEFAULT_CASCADER_POSITIONS } from '../core/overlay/overlay-position';
import { NgClassType } from '../core/types/ng-class';
import { arraysEqual, toArray } from '../core/util/array';
import { InputBoolean } from '../core/util/convert';
import { NzCascaderOptionComponent } from './nz-cascader-li.component';

import {
CascaderOption,
Expand Down Expand Up @@ -76,6 +79,7 @@ export class NzCascaderComponent implements OnDestroy, ControlValueAccessor {
@ViewChild('input') input: ElementRef;
@ViewChild('menu') menu: ElementRef;
@ViewChild(CdkConnectedOverlay) overlay: CdkConnectedOverlay;
@ViewChildren(NzCascaderOptionComponent) cascaderItems: QueryList<NzCascaderOptionComponent>;

@Input() @InputBoolean() nzShowInput = true;
@Input() @InputBoolean() nzShowArrow = true;
Expand Down Expand Up @@ -318,16 +322,17 @@ export class NzCascaderComponent implements OnDestroy, ControlValueAccessor {
this.isLoading = columnIndex < 0;
option.loading = true;
this.nzLoadData(option, columnIndex).then(() => {
option.loading = this.isLoading = false;
if (option.children) {
option.children.forEach(child => child.parent = columnIndex < 0 ? undefined : option);
this.setColumnData(option.children, columnIndex + 1);
this.cdr.detectChanges();
this.reposition();
}
if (success) {
success();
}
option.loading = this.isLoading = false; // Need to check children.
this.checkChildren();
// Reposition in the next tick, because we use markForCheck above.
Promise.resolve().then(() => this.reposition());
}, () => {
option.loading = this.isLoading = false;
option.isLeaf = true;
Expand Down Expand Up @@ -675,12 +680,12 @@ export class NzCascaderComponent implements OnDestroy, ControlValueAccessor {
};

const filter: (inputValue: string, p: CascaderOption[]) => boolean =
this.nzShowSearch instanceof Object && (this.nzShowSearch as NzShowSearchOptions).filter
? (this.nzShowSearch as NzShowSearchOptions).filter
: defaultFilter;
this.nzShowSearch instanceof Object && (this.nzShowSearch as NzShowSearchOptions).filter
? (this.nzShowSearch as NzShowSearchOptions).filter
: defaultFilter;

const sorter: (a: CascaderOption[], b: CascaderOption[], inputValue: string) => number =
this.nzShowSearch instanceof Object && (this.nzShowSearch as NzShowSearchOptions).sorter;
this.nzShowSearch instanceof Object && (this.nzShowSearch as NzShowSearchOptions).sorter;

const loopParent = (node: CascaderOption, forceDisabled = false) => {
const disabled = forceDisabled || node.disabled;
Expand Down Expand Up @@ -864,4 +869,8 @@ export class NzCascaderComponent implements OnDestroy, ControlValueAccessor {
});
}
}

private checkChildren(): void {
this.cascaderItems.forEach(item => item.markForCheck());
}
}
1 change: 1 addition & 0 deletions components/icon/nz-icon-test.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const icons: IconDefinition[] = Object.keys(antDesignIcons).map(key => antDesign
/**
* Include this module in every testing spec, except `nz-icon.spec.ts`.
*/
// @dynamic
@NgModule({
exports: [
NzIconModule
Expand Down
1 change: 1 addition & 0 deletions components/icon/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './nz-icon.module';
export * from './nz-icon-test.module';
export * from './nz-icon.directive';
export * from './nz-icon.service';

0 comments on commit 0b23225

Please sign in to comment.