Skip to content

Commit

Permalink
fix(module:radio): fix radio in reactive form (#1748)
Browse files Browse the repository at this point in the history
close #1735
  • Loading branch information
vthinkxie authored Jun 28, 2018
1 parent aeb485f commit b7a831d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
4 changes: 3 additions & 1 deletion components/radio/nz-radio-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ export class NzRadioGroupComponent implements AfterContentInit, ControlValueAcce
ngAfterContentInit(): void {
this.syncCheckedValue();
this.updateChildrenName();
this.updateDisabledState();
Promise.resolve().then(() => {
this.updateDisabledState();
});
}

writeValue(value: string): void {
Expand Down
1 change: 1 addition & 0 deletions components/radio/nz-radio.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { NzRadioGroupComponent } from './nz-radio-group.component';
templateUrl : './nz-radio.component.html',
host : {
'[class.ant-radio-wrapper]' : 'true',
'[class.ant-radio-wrapper-checked]' : 'nzChecked',
'[class.ant-radio-wrapper-disabled]': 'nzDisabled'
},
providers : [
Expand Down
47 changes: 42 additions & 5 deletions components/radio/nz-radio.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ViewChild } from '@angular/core';
import { fakeAsync, flush, TestBed } from '@angular/core/testing';
import { Component, OnInit, ViewChild } from '@angular/core';
import { fakeAsync, flush, tick, TestBed } from '@angular/core/testing';
import { FormsModule, FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

Expand All @@ -12,7 +12,7 @@ describe('radio', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports : [ NzRadioModule, FormsModule, ReactiveFormsModule ],
declarations: [ NzTestRadioSingleComponent, NzTestRadioButtonComponent, NzTestRadioGroupComponent, NzTestRadioFormComponent, NzTestRadioGroupFormComponent, NzTestRadioGroupDisabledComponent ]
declarations: [ NzTestRadioSingleComponent, NzTestRadioButtonComponent, NzTestRadioGroupComponent, NzTestRadioFormComponent, NzTestRadioGroupFormComponent, NzTestRadioGroupDisabledComponent, NzTestRadioGroupDisabledFormComponent ]
});
TestBed.compileComponents();
}));
Expand Down Expand Up @@ -139,6 +139,8 @@ describe('radio', () => {
it('should disable work', fakeAsync(() => {
testComponent.disabled = true;
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(testComponent.value).toBe('A');
expect(testComponent.modelChange).toHaveBeenCalledTimes(0);
radios[ 1 ].nativeElement.click();
Expand Down Expand Up @@ -168,6 +170,7 @@ describe('radio', () => {
radioGroup = fixture.debugElement.query(By.directive(NzRadioGroupComponent));
});
it('should group disable work', fakeAsync(() => {
testComponent.disabled = true;
fixture.detectChanges();
expect(testComponent.value).toBe('A');
radios[ 1 ].nativeElement.click();
Expand Down Expand Up @@ -255,12 +258,22 @@ describe('radio', () => {
fixture.detectChanges();
expect(testComponent.formGroup.get('radioGroup').value).toBe('A');
testComponent.disable();
fixture.detectChanges();
tick();
fixture.detectChanges();
radios[ 1 ].nativeElement.click();
fixture.detectChanges();
expect(testComponent.formGroup.get('radioGroup').value).toBe('A');
}));
});

describe('radio group disable form', () => {
it('expect not to thrown error', fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(NzTestRadioGroupDisabledFormComponent);
fixture.detectChanges();
}).not.toThrow();
}));
});
});

@Component({
Expand Down Expand Up @@ -369,7 +382,31 @@ export class NzTestRadioGroupFormComponent {
export class NzTestRadioGroupDisabledComponent {
size = 'default';
value = 'A';
disabled = true;
disabled = false;
name: string;
singleDisabled = false;
}

/** https://github.com/NG-ZORRO/ng-zorro-antd/issues/1735 **/
@Component({
selector: 'nz-test-radio-group-disabled-form',
template: `
<form nz-form [formGroup]="validateForm">
<nz-radio-group formControlName="radio">
<label nz-radio *ngFor="let val of radioValues" [nzValue]="val">{{val}}</label>
</nz-radio-group>
</form>`
})
export class NzTestRadioGroupDisabledFormComponent implements OnInit {
validateForm: FormGroup;
radioValues = [ 'A', 'B', 'C', 'D' ];

constructor(private fb: FormBuilder) {
}

ngOnInit(): void {
this.validateForm = this.fb.group({
radio: [ { value: 'B', disabled: true } ]
});
}
}

0 comments on commit b7a831d

Please sign in to comment.