Skip to content

Commit

Permalink
fix(module:tree-select): unable close when used OnPush (#2028)
Browse files Browse the repository at this point in the history
close #2012
  • Loading branch information
hsuanxyz authored and wen committed Aug 31, 2018
1 parent 30bccd1 commit fb83354
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
8 changes: 5 additions & 3 deletions components/tree-select/nz-tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DOCUMENT } from '@angular/common';
import {
forwardRef,
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Expand Down Expand Up @@ -78,7 +79,6 @@ import { NzTreeComponent } from '../tree/nz-tree.component';
export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, AfterViewInit, OnDestroy {

private nodes = [];
isInit = false;
isComposing = false;
isDestroy = true;
inputValue = '';
Expand Down Expand Up @@ -119,7 +119,7 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte
@Input()
set nzNodes(value: NzTreeNode[]) {
this.nodes = value;
if (this.isInit) {
if (this.treeRef) {
setTimeout(() => this.updateSelectedNodes(), 0);
}
}
Expand Down Expand Up @@ -173,6 +173,7 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte
@Optional() @Inject(DOCUMENT) private document: any, // tslint:disable-line:no-any
@Optional() private element: ElementRef,
private renderer: Renderer2,
private cdr: ChangeDetectorRef,
private overlay: Overlay,
private viewContainerRef: ViewContainerRef) {
}
Expand Down Expand Up @@ -204,6 +205,7 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte
this.nzOpen = false;
this.nzOpenChange.emit(this.nzOpen);
this.updateCdkConnectedOverlayStatus();
this.cdr.markForCheck();
}

onKeyDownInput(e: KeyboardEvent): void {
Expand Down Expand Up @@ -263,6 +265,7 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte
this.portal = new TemplatePortal(this.dropdownTemplate, this.viewContainerRef);
this.overlayRef = this.overlay.create(this.getOverlayConfig());
this.overlayRef.attach(this.portal);
this.cdr.detectChanges();
this.overlayBackdropClickSubscription = this.subscribeOverlayBackdropClick();
}

Expand Down Expand Up @@ -434,7 +437,6 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte

ngAfterViewInit(): void {
this.attachOverlay();
this.isInit = true;
}

setDisabledState(isDisabled: boolean): void {
Expand Down
61 changes: 59 additions & 2 deletions components/tree-select/nz-tree-select.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BACKSPACE } from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component, ViewChild } from '@angular/core';
import { ChangeDetectionStrategy, Component, ViewChild } from '@angular/core';
import { async, fakeAsync, flush, inject, tick, TestBed } from '@angular/core/testing';
import { FormsModule, FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
Expand All @@ -22,7 +22,7 @@ describe('tree-select component', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [ NzTreeSelectModule, NoopAnimationsModule, FormsModule, ReactiveFormsModule ],
declarations: [ NzTestTreeSelectBasicComponent, NzTestTreeSelectCheckableComponent, NzTestTreeSelectFormComponent, NzTestTreeSelectAsyncNodesComponent ]
declarations: [ NzTestTreeSelectBasicComponent, NzTestTreeSelectCheckableComponent, NzTestTreeSelectFormComponent, NzTestTreeSelectAsyncNodesComponent, NzTestTreeSelectOnPushComponent ]
});
TestBed.compileComponents();
inject([ OverlayContainer ], (oc: OverlayContainer) => {
Expand All @@ -35,6 +35,38 @@ describe('tree-select component', () => {
overlayContainer.ngOnDestroy();
}));

describe('OnPush', () => {
let fixture;
let testComponent: NzTestTreeSelectOnPushComponent;
let treeSelectComponent: NzTreeSelectComponent;
let treeSelect;

beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(NzTestTreeSelectOnPushComponent);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
treeSelect = fixture.debugElement.query(By.directive(NzTreeSelectComponent));
treeSelectComponent = treeSelect.componentInstance;
fixture.detectChanges();
flush();
tick(200);
fixture.detectChanges();
}));

it('should close when the outside clicks', fakeAsync(() => {
treeSelect.nativeElement.click();
fixture.detectChanges();
expect(treeSelectComponent.nzOpen).toBe(true);
tick(200);
expect((overlayContainerElement.querySelector('.ant-select-dropdown') as HTMLElement).style.opacity).toBe('1');
dispatchFakeEvent(overlayContainerElement.querySelector('.cdk-overlay-backdrop'), 'click');
fixture.detectChanges();
tick();
expect(treeSelectComponent.nzOpen).toBe(false);
expect((overlayContainerElement.querySelector('.ant-select-dropdown') as HTMLElement).style.opacity).toBe('0');
}));
});

describe('basic', () => {
let fixture;
let testComponent: NzTestTreeSelectBasicComponent;
Expand Down Expand Up @@ -668,3 +700,28 @@ export class NzTestTreeSelectAsyncNodesComponent {
});
}
}

@Component({
selector: 'nz-test-tree-select-on-push',
template: `
<nz-tree-select
[nzNodes]="nodes"
[(ngModel)]="value">
</nz-tree-select>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})

export class NzTestTreeSelectOnPushComponent {
value: string = '1001';
nodes = [
new NzTreeNode({
title : 'root1',
key : '1001'
}),
new NzTreeNode({
title : 'root2',
key : '1002'
})
];
}

0 comments on commit fb83354

Please sign in to comment.