Skip to content

Commit

Permalink
fix(module:select): fix select reset in form (#3017)
Browse files Browse the repository at this point in the history
close #3014
  • Loading branch information
vthinkxie authored Mar 4, 2019
1 parent 42ed317 commit 30b3d86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
36 changes: 36 additions & 0 deletions components/select/nz-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,37 @@ describe('nz-select component', () => {
fixture.detectChanges();
expect(select.nativeElement.classList).toContain('ant-select-disabled');
}));
/** https://github.com/NG-ZORRO/ng-zorro-antd/issues/3014 **/
it('should reset form works', fakeAsync(() => {
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.formGroup.value.select).toBe('jack');
testComponent.reset();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.formGroup.value.select).toBe(null);
select.nativeElement.click();
fixture.detectChanges();
overlayContainerElement.querySelector('li').click();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.formGroup.value.select).toBe('jack');
testComponent.reset();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.formGroup.value.select).toBe(null);
select.nativeElement.click();
fixture.detectChanges();
overlayContainerElement.querySelector('li').click();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.formGroup.value.select).toBe('jack');
}));
});
describe('option change', () => {
let fixture;
Expand Down Expand Up @@ -401,6 +432,7 @@ export class NzTestSelectTagsComponent {
template: `
<form [formGroup]="formGroup">
<nz-select
nzShowSearch
formControlName="select">
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
Expand All @@ -421,6 +453,10 @@ export class NzTestSelectFormComponent {
disable(): void {
this.formGroup.disable();
}

reset(): void {
this.formGroup.reset();
}
}

@Component({
Expand Down
8 changes: 5 additions & 3 deletions components/select/nz-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,11 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterVie
this.nzSelectService.modelChange$.pipe(
takeUntil(this.destroy$)
).subscribe(modelValue => {
this.value = modelValue;
this.onChange(this.value);
this.updateCdkConnectedOverlayPositions();
if (this.value !== modelValue) {
this.value = modelValue;
this.onChange(this.value);
this.updateCdkConnectedOverlayPositions();
}
});
this.nzSelectService.open$.pipe(
takeUntil(this.destroy$)
Expand Down
3 changes: 1 addition & 2 deletions components/select/nz-select.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export class NzSelectService {
modelValue = selectedList;
}
return modelValue;
}),
distinctUntilChanged()
})
);
searchValue$ = this.searchValueRaw$.pipe(
distinctUntilChanged(),
Expand Down

0 comments on commit 30b3d86

Please sign in to comment.