Skip to content

Commit

Permalink
fix: fix cannot be reset & column not drop for leaf node
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendell committed Mar 21, 2019
1 parent 2a02bcd commit 8f7b2e6
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions components/cascader/nz-cascader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class NzCascaderService implements OnDestroy {
/**
* Make sure that value matches what is displayed in the dropdown.
*/
syncOptions(init: boolean = false): void {
syncOptions(first: boolean = false): void {
const values = this.values;
const hasValue = values && values.length;
const lastColumnIndex = values.length - 1;
Expand All @@ -85,6 +85,7 @@ export class NzCascaderService implements OnDestroy {
const currentValue = values[columnIndex];

if (!currentValue) {
this.$redraw.next();
return;
}

Expand Down Expand Up @@ -116,7 +117,10 @@ export class NzCascaderService implements OnDestroy {
}
};

if (init && this.cascaderComponent.nzLoadData && !hasValue) {
this.activatedOptions = [];
this.selectedOptions = [];

if (first && this.cascaderComponent.nzLoadData && !hasValue) {
return;
} else {
initColumnWithIndex(0);
Expand All @@ -134,8 +138,7 @@ export class NzCascaderService implements OnDestroy {
* Reset all options. Rebuild searching options if in searching mode.
*/
withOptions(options: CascaderOption[] | null): void {
this.columnsSnapshot = this.columns =
options && options.length ? [options] : [];
this.columnsSnapshot = this.columns = options && options.length ? [options] : [];

if (this.inSearchingMode) {
this.prepareSearchOptions(this.cascaderComponent.inputValue);
Expand Down Expand Up @@ -173,7 +176,7 @@ export class NzCascaderService implements OnDestroy {
} else if (!option.isLeaf && loadingChildren) {
// Parent option that should try to load children asynchronously.
this.loadChildren(option, columnIndex);
} else if (!option.isLeaf) {
} else if (option.isLeaf) {
// Leaf option.
this.dropBehindColumns(columnIndex);
}
Expand Down Expand Up @@ -221,14 +224,8 @@ export class NzCascaderService implements OnDestroy {
});
};
const showSearch = this.cascaderComponent.nzShowSearch;
const filter =
isShowSearchObject(showSearch) && showSearch.filter
? showSearch.filter
: defaultFilter;
const sorter =
isShowSearchObject(showSearch) && showSearch.sorter
? showSearch.sorter
: null;
const filter = isShowSearchObject(showSearch) && showSearch.filter ? showSearch.filter : defaultFilter;
const sorter = isShowSearchObject(showSearch) && showSearch.sorter ? showSearch.sorter : null;
const loopChild = (node: CascaderOption, forceDisabled = false) => {
path.push(node);
const cPath = Array.from(path);
Expand All @@ -238,9 +235,7 @@ export class NzCascaderService implements OnDestroy {
disabled,
isLeaf: true,
path: cPath,
[this.cascaderComponent.nzLabelProperty]: cPath
.map(p => this.getOptionLabel(p))
.join(' / ')
[this.cascaderComponent.nzLabelProperty]: cPath.map(p => this.getOptionLabel(p)).join(' / ')
};
results.push(option);
}
Expand Down Expand Up @@ -268,9 +263,7 @@ export class NzCascaderService implements OnDestroy {
return;
}

this.columnsSnapshot[0].forEach(o =>
isChildOption(o) ? loopChild(o) : loopParent(o)
);
this.columnsSnapshot[0].forEach(o => (isChildOption(o) ? loopChild(o) : loopParent(o)));

if (sorter) {
results.sort((a, b) => sorter(a.path, b.path, searchValue));
Expand Down Expand Up @@ -307,11 +300,7 @@ export class NzCascaderService implements OnDestroy {
return typeof changeOn === 'function' ? changeOn(o, i) : false;
};

if (
option.isLeaf ||
this.cascaderComponent.nzChangeOnSelect ||
shouldPerformSelection(option, index)
) {
if (option.isLeaf || this.cascaderComponent.nzChangeOnSelect || shouldPerformSelection(option, index)) {
this.selectedOptions = [...this.activatedOptions];
this.prepareEmitValue();
this.$redraw.next();
Expand Down Expand Up @@ -346,11 +335,7 @@ export class NzCascaderService implements OnDestroy {
* @param options Options to insert
* @param columnIndex Position
*/
private setColumnData(
options: CascaderOption[],
columnIndex: number,
parent: CascaderOption
): void {
private setColumnData(options: CascaderOption[], columnIndex: number, parent: CascaderOption): void {
const existingOptions = this.columns[columnIndex];
if (!arraysEqual(existingOptions, options)) {
options.forEach(o => (o.parent = parent));
Expand All @@ -371,10 +356,7 @@ export class NzCascaderService implements OnDestroy {
}

private dropBehindActivatedOptions(lastReserveIndex: number): void {
this.activatedOptions = this.activatedOptions.splice(
0,
lastReserveIndex + 1
);
this.activatedOptions = this.activatedOptions.splice(0, lastReserveIndex + 1);
}

private dropBehindColumns(lastReserveIndex: number): void {
Expand Down

0 comments on commit 8f7b2e6

Please sign in to comment.