Skip to content

Commit

Permalink
fix(module:select): fix select init touched state error when disabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
vthinkxie authored and Ricbet committed Apr 9, 2020
1 parent fdec23d commit d28b2fd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
41 changes: 39 additions & 2 deletions components/select/nz-select.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DOWN_ARROW, ENTER, ESCAPE, SPACE, TAB, UP_ARROW } from '@angular/cdk/keycodes';
import { Component, DebugElement } from '@angular/core';
import { async, fakeAsync, flush, inject, tick, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { FormsModule, FormBuilder, FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { dispatchKeyboardEvent } from '../core/testing';
Expand All @@ -18,7 +18,7 @@ describe('nz-select component', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [ NzSelectModule, NoopAnimationsModule, FormsModule, ReactiveFormsModule ],
declarations: [ NzTestSelectDefaultComponent, NzTestSelectTagsComponent, NzTestSelectFormComponent, NzTestOptionChangeComponent ]
declarations: [ NzTestSelectDefaultComponent, NzTestSelectTagsComponent, NzTestSelectFormComponent, NzTestOptionChangeComponent, NzTestSelectFormDisabledTouchedComponent ]
});
TestBed.compileComponents();
inject([ OverlayContainer ], (oc: OverlayContainer) => {
Expand Down Expand Up @@ -361,6 +361,24 @@ describe('nz-select component', () => {
expect(changeSpy).toHaveBeenCalledTimes(4);
});
});

describe('form init state', () => {
let fixture: ComponentFixture<NzTestSelectFormDisabledTouchedComponent>;
let testComponent: NzTestSelectFormDisabledTouchedComponent;
beforeEach(() => {
fixture = TestBed.createComponent(NzTestSelectFormDisabledTouchedComponent);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
});
/** https://github.com/NG-ZORRO/ng-zorro-antd/issues/3059 **/
it('should init disabled state with touched false', fakeAsync(() => {
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.formGroup.controls.select.touched).toBe(false);
}));
});

});

@Component({
Expand Down Expand Up @@ -465,6 +483,25 @@ export class NzTestSelectFormComponent {
}
}

@Component({
template: `
<form [formGroup]="formGroup">
<nz-select
formControlName="select">
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-select>
</form>
`
})
export class NzTestSelectFormDisabledTouchedComponent {
formGroup: FormGroup;

constructor() {
this.formGroup = new FormGroup({ select: new FormControl({ value: 'lucy', disabled: true }) });
}
}

@Component({
template: `
<nz-select>
Expand Down
4 changes: 3 additions & 1 deletion components/select/nz-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterVie
triggerWidth: number;
private _disabled = false;
private _autoFocus = false;
private isInit = false;
private destroy$ = new Subject();
@ViewChild(CdkOverlayOrigin) cdkOverlayOrigin: CdkOverlayOrigin;
@ViewChild(CdkConnectedOverlay) cdkConnectedOverlay: CdkConnectedOverlay;
Expand Down Expand Up @@ -167,7 +168,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterVie
this._disabled = toBoolean(value);
this.nzSelectService.disabled = this._disabled;
this.nzSelectService.check();
if (this.nzDisabled) {
if (this.nzDisabled && this.isInit) {
this.closeDropDown();
}
}
Expand Down Expand Up @@ -308,6 +309,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterVie

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

ngAfterContentInit(): void {
Expand Down

0 comments on commit d28b2fd

Please sign in to comment.