Skip to content

Commit 7ef0b88

Browse files
authored
Merge pull request #6235 from IgniteUI/rkaraivanov/radio-group-fix
fix(igx-radio): Initial selection with falsy values
2 parents 7565799 + 18729d6 commit 7ef0b88

File tree

7 files changed

+111
-15
lines changed

7 files changed

+111
-15
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe('IgxRadioGroupDirective', () => {
1313
declarations: [
1414
RadioGroupComponent,
1515
RadioGroupWithModelComponent,
16-
RadioGroupReactiveFormsComponent
16+
RadioGroupReactiveFormsComponent,
17+
RadioGroupDeepProjectionComponent
1718
],
1819
imports: [
1920
IgxRadioModule,
@@ -182,6 +183,16 @@ describe('IgxRadioGroupDirective', () => {
182183
expect(fixture.componentInstance.newModel.name).toEqual(fixture.componentInstance.model.name);
183184
expect(fixture.componentInstance.newModel.favoriteSeason).toEqual(fixture.componentInstance.seasons[0]);
184185
}));
186+
187+
it('Properly initialize selection when value is falsy in deep content projection', fakeAsync(() => {
188+
const fixture = TestBed.createComponent(RadioGroupDeepProjectionComponent);
189+
fixture.detectChanges();
190+
tick();
191+
192+
const radioGroup = fixture.componentInstance.radioGroup;
193+
expect(radioGroup.value).toEqual(0);
194+
expect(radioGroup.radioButtons.first.checked).toEqual(true);
195+
}));
185196
});
186197

187198
@Component({
@@ -272,3 +283,33 @@ class RadioGroupReactiveFormsComponent {
272283
});
273284
}
274285
}
286+
287+
@Component({
288+
template: `
289+
<form [formGroup]="group1">
290+
<igx-radio-group formControlName="favouriteChoice" name="radioGroupReactive">
291+
<div *ngFor="let choice of choices">
292+
<p><igx-radio [value]="choice">{{ choice }}</igx-radio></p>
293+
</div>
294+
</igx-radio-group>
295+
</form>
296+
`
297+
})
298+
class RadioGroupDeepProjectionComponent {
299+
300+
@ViewChild(IgxRadioGroupDirective, { static: true })
301+
radioGroup: IgxRadioGroupDirective;
302+
303+
choices = [0, 1, 2];
304+
group1: FormGroup;
305+
306+
constructor(private _builder: FormBuilder) {
307+
this._createForm();
308+
}
309+
310+
private _createForm() {
311+
this.group1 = this._builder.group({
312+
favouriteChoice: 0
313+
});
314+
}
315+
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let nextId = 0;
3535
* ```
3636
*/
3737
@Directive({
38+
exportAs: 'igxRadioGroup',
3839
selector: 'igx-radio-group, [igxRadioGroup]',
3940
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: IgxRadioGroupDirective, multi: true }]
4041
})
@@ -46,7 +47,7 @@ export class IgxRadioGroupDirective implements AfterContentInit, ControlValueAcc
4647
* ```
4748
* @memberof IgxRadioGroupDirective
4849
*/
49-
@ContentChildren(IgxRadioComponent) public radioButtons: QueryList<IgxRadioComponent>;
50+
@ContentChildren(IgxRadioComponent, { descendants: true }) public radioButtons: QueryList<IgxRadioComponent>;
5051

5152
/**
5253
* Sets/gets the `value` attribute.
@@ -258,13 +259,11 @@ export class IgxRadioGroupDirective implements AfterContentInit, ControlValueAcc
258259
*/
259260
private _initRadioButtons() {
260261
if (this.radioButtons) {
262+
const props = { name: this._name, labelPosition: this._labelPosition, disabled: this._disabled, required: this._required };
261263
this.radioButtons.forEach((button) => {
262-
button.name = this._name;
263-
button.labelPosition = this._labelPosition;
264-
button.disabled = this._disabled;
265-
button.required = this._required;
264+
Object.assign(button, props);
266265

267-
if (this._value && button.value === this._value) {
266+
if (button.value === this._value) {
268267
button.checked = true;
269268
this._selected = button;
270269
}

projects/igniteui-angular/src/lib/radio/radio.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
[attr.aria-label]="ariaLabel"
1212
(click)="_onRadioClick($event)"
1313
(change)="_onRadioChange($event)"
14-
(focus)="onFocus($event)"
15-
(blur)="onBlur($event)" />
14+
(focus)="onFocus()"
15+
(blur)="onBlur()" />
1616

1717
<label #nativeLabel class="igx-radio__composite" igxRipple
1818
igxRippleTarget=".igx-radio__ripple"

projects/igniteui-angular/src/lib/radio/radio.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,13 @@ export class IgxRadioComponent implements ControlValueAccessor, EditorProvider {
330330
/**
331331
*@hidden
332332
*/
333-
public onFocus(event) {
333+
public onFocus() {
334334
this.focused = true;
335335
}
336336
/**
337337
*@hidden
338338
*/
339-
public onBlur(event) {
339+
public onBlur() {
340340
this.focused = false;
341341
this._onTouchedCallback();
342342
}

src/app/radio/radio.sample.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@
66
padding: 6px;
77
}
88

9+
.promotion {
10+
opacity: .75;
11+
margin: .5rem;
12+
}
13+
14+
.promotion-selected {
15+
opacity: 1;
16+
animation: promo-selected 500ms cubic-bezier(0.19, 1, 0.22, 1);
17+
}
18+
19+
20+
@keyframes promo-selected {
21+
50% {
22+
transform: translateY(5%) scale(1.05, 1.05);
23+
}
24+
100% {
25+
transform: none;
26+
}
27+
}

src/app/radio/radio.sample.html

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22
<app-page-header title="Radio Group">
33
Demonstrates a group of radio buttons.
44
</app-page-header>
5+
<section class="sample-content">
6+
<article class="sample-column">
7+
<igx-radio-group #group="igxRadioGroup" [(ngModel)]="initial">
8+
<div igxLayout>
9+
<igx-card class="promotion" layout="justify" *ngFor="let opt of options"
10+
[type]="group.value != opt ? 'outlined' : 'default'"
11+
[class.promotion-selected]="group.value == opt">
12+
<igx-card-header>
13+
<h6>Reserve your dream vacation at {{ opt }}</h6>
14+
</igx-card-header>
15+
<igx-divider></igx-divider>
16+
<igx-card-content>
17+
<p>
18+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
19+
labore et dolore magna
20+
aliqua. Elit pellentesque habitant morbi tristique senectus et netus et. Aliquet bibendum
21+
enim facilisis gravida. Rutrum
22+
tellus pellentesque eu tincidunt tortor aliquam nulla. Amet consectetur adipiscing elit
23+
pellentesque habitant morbi
24+
tristique. Massa placerat duis ultricies lacus sed. In ornare quam viverra orci sagittis.
25+
Pulvinar elementum integer
26+
enim neque volutpat ac tincidunt. At in tellus integer feugiat scelerisque. Adipiscing vitae
27+
proin sagittis nisl rhoncus
28+
mattis rhoncus urna neque. Proin sagittis nisl rhoncus mattis rhoncus urna neque viverra.
29+
</p>
30+
</igx-card-content>
31+
<igx-divider></igx-divider>
32+
<igx-card-actions layout="justify">
33+
<button igxRipple igxButton="outlined" [disabled]="group.value != opt">Reserve item
34+
{{ opt }}</button>
35+
<igx-radio [value]="opt">Pick choice {{ opt }}</igx-radio>
36+
</igx-card-actions>
37+
</igx-card>
38+
</div>
39+
</igx-radio-group>
40+
<p>Selected: {{ initial }}</p>
41+
</article>
42+
</section>
543
<section class="sample-content">
644
<article class="sample-column">
745
<h4 class="sample-title">Radio group without data model</h4>
@@ -48,15 +86,11 @@ <h4 class="sample-title">Radio group in reactive form</h4>
4886
<input formControlName="name">
4987
</label>
5088
<br>
51-
<!-- TODO: igx-radio-group is not working properly when hosted in label element
52-
When the radio button label is clicked a click event on the first item is triggered -->
53-
<!-- <label>Favorite Season -->
5489
<igx-radio-group formControlName="favoriteSeason" name="radioGroupReactive">
5590
<igx-radio class="radio-sample" *ngFor="let item of seasons" value="{{item}}">
5691
{{item}}
5792
</igx-radio>
5893
</igx-radio-group>
59-
<!-- </label> -->
6094
<br>
6195
<button igxButton type="button" (click)="onUpdateBtnClick($event)" style="width: 200px">Update model</button>
6296
</form>

src/app/radio/radio.sample.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export class RadioSampleComponent implements AfterContentInit {
2222

2323
selectedValue: any;
2424

25+
options = [0, 1, 2, 3, 4];
26+
initial = this.options[0];
27+
2528
seasons = [
2629
'Winter',
2730
'Spring',

0 commit comments

Comments
 (0)