Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';