Skip to content

Commit 6a16284

Browse files
committed
fix(radio-group): correctly check selected items
1 parent fd8dea6 commit 6a16284

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('IgxRadioGroupDirective', () => {
1212
TestBed.configureTestingModule({
1313
declarations: [
1414
RadioGroupComponent,
15+
RadioGroupSimpleComponent,
1516
RadioGroupWithModelComponent,
1617
RadioGroupReactiveFormsComponent,
1718
RadioGroupDeepProjectionComponent
@@ -193,8 +194,37 @@ describe('IgxRadioGroupDirective', () => {
193194
expect(radioGroup.value).toEqual(0);
194195
expect(radioGroup.radioButtons.first.checked).toEqual(true);
195196
}));
197+
198+
it('Updates checked radio button correctly', fakeAsync(() => {
199+
const fixture = TestBed.createComponent(RadioGroupSimpleComponent);
200+
fixture.detectChanges();
201+
tick();
202+
203+
const radioGroup = fixture.componentInstance.radioGroup;
204+
expect(radioGroup.radioButtons.first.checked).toEqual(true);
205+
expect(radioGroup.radioButtons.last.checked).toEqual(false);
206+
207+
radioGroup.radioButtons.last.select();
208+
fixture.detectChanges();
209+
tick();
210+
211+
expect(radioGroup.radioButtons.first.checked).toEqual(false);
212+
expect(radioGroup.radioButtons.last.checked).toEqual(true);
213+
}));
196214
});
197215

216+
@Component({
217+
template: `
218+
<igx-radio-group #radioGroup>
219+
<igx-radio [checked]="true">Option 1</igx-radio>
220+
<igx-radio>Option 2</igx-radio>
221+
</igx-radio-group>
222+
`
223+
})
224+
class RadioGroupSimpleComponent {
225+
@ViewChild('radioGroup', { read: IgxRadioGroupDirective, static: true }) public radioGroup: IgxRadioGroupDirective;
226+
}
227+
198228
@Component({
199229
template: `<igx-radio-group #radioGroup name="radioGroup" value="Baz" required="true" labelPosition="before">
200230
<igx-radio *ngFor="let item of ['Foo', 'Bar', 'Baz']" value="{{item}}">

projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,11 @@ export class IgxRadioGroupDirective implements AfterContentInit, ControlValueAcc
366366
* @internal
367367
*/
368368
private _selectedRadioButtonChanged(args: IChangeRadioEventArgs) {
369-
if (this._selected !== args.radio) {
370-
if (this._selected) {
371-
this._selected.checked = false;
372-
}
373-
this._selected = args.radio;
374-
}
369+
this.radioButtons.forEach(button => {
370+
button.checked = button.id === args.radio.id;
371+
});
375372

373+
this._selected = args.radio;
376374
this._value = args.value;
377375

378376
if (this._isInitialized) {

0 commit comments

Comments
 (0)