diff --git a/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.spec.ts b/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.spec.ts index 8a2f7a109d9..d36f8abf852 100644 --- a/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.spec.ts +++ b/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.spec.ts @@ -13,7 +13,8 @@ describe('IgxRadioGroupDirective', () => { declarations: [ RadioGroupComponent, RadioGroupWithModelComponent, - RadioGroupReactiveFormsComponent + RadioGroupReactiveFormsComponent, + RadioGroupDeepProjectionComponent ], imports: [ IgxRadioModule, @@ -182,6 +183,16 @@ describe('IgxRadioGroupDirective', () => { expect(fixture.componentInstance.newModel.name).toEqual(fixture.componentInstance.model.name); expect(fixture.componentInstance.newModel.favoriteSeason).toEqual(fixture.componentInstance.seasons[0]); })); + + it('Properly initialize selection when value is falsy in deep content projection', fakeAsync(() => { + const fixture = TestBed.createComponent(RadioGroupDeepProjectionComponent); + fixture.detectChanges(); + tick(); + + const radioGroup = fixture.componentInstance.radioGroup; + expect(radioGroup.value).toEqual(0); + expect(radioGroup.radioButtons.first.checked).toEqual(true); + })); }); @Component({ @@ -272,3 +283,33 @@ class RadioGroupReactiveFormsComponent { }); } } + +@Component({ + template: ` +
+ +
+

{{ choice }}

+
+
+
+ ` +}) +class RadioGroupDeepProjectionComponent { + + @ViewChild(IgxRadioGroupDirective, { static: true }) + radioGroup: IgxRadioGroupDirective; + + choices = [0, 1, 2]; + group1: FormGroup; + + constructor(private _builder: FormBuilder) { + this._createForm(); + } + + private _createForm() { + this.group1 = this._builder.group({ + favouriteChoice: 0 + }); + } +} diff --git a/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.ts b/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.ts index f4ffc2033f6..457401cac06 100644 --- a/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.ts +++ b/projects/igniteui-angular/src/lib/directives/radio/radio-group.directive.ts @@ -35,6 +35,7 @@ let nextId = 0; * ``` */ @Directive({ + exportAs: 'igxRadioGroup', selector: 'igx-radio-group, [igxRadioGroup]', providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: IgxRadioGroupDirective, multi: true }] }) @@ -46,7 +47,7 @@ export class IgxRadioGroupDirective implements AfterContentInit, ControlValueAcc * ``` * @memberof IgxRadioGroupDirective */ - @ContentChildren(IgxRadioComponent) public radioButtons: QueryList; + @ContentChildren(IgxRadioComponent, { descendants: true }) public radioButtons: QueryList; /** * Sets/gets the `value` attribute. @@ -258,13 +259,11 @@ export class IgxRadioGroupDirective implements AfterContentInit, ControlValueAcc */ private _initRadioButtons() { if (this.radioButtons) { + const props = { name: this._name, labelPosition: this._labelPosition, disabled: this._disabled, required: this._required }; this.radioButtons.forEach((button) => { - button.name = this._name; - button.labelPosition = this._labelPosition; - button.disabled = this._disabled; - button.required = this._required; + Object.assign(button, props); - if (this._value && button.value === this._value) { + if (button.value === this._value) { button.checked = true; this._selected = button; } diff --git a/projects/igniteui-angular/src/lib/radio/radio.component.html b/projects/igniteui-angular/src/lib/radio/radio.component.html index 26a05e7d27a..fd845e4e433 100644 --- a/projects/igniteui-angular/src/lib/radio/radio.component.html +++ b/projects/igniteui-angular/src/lib/radio/radio.component.html @@ -11,8 +11,8 @@ [attr.aria-label]="ariaLabel" (click)="_onRadioClick($event)" (change)="_onRadioChange($event)" - (focus)="onFocus($event)" - (blur)="onBlur($event)" /> + (focus)="onFocus()" + (blur)="onBlur()" />