Skip to content

Commit 35ab525

Browse files
committed
Update focusable radio dynamically
1 parent 9a82864 commit 35ab525

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/radio/radio-group.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export class RadioGroup implements OnInit, AfterContentInit, ControlValueAccesso
193193
this.markRadiosForCheck();
194194
}
195195

196-
/**
196+
/**
197197
* Binds 'radiogroup' value to the role attribute for `RadioGroup`.
198198
* @memberof RadioGroup
199199
*/
@@ -357,6 +357,22 @@ export class RadioGroup implements OnInit, AfterContentInit, ControlValueAccesso
357357
// possibly be set by NgModel on RadioGroup, and it is possible that the OnInit of the
358358
// NgModel occurs *after* the OnInit of the RadioGroup.
359359
this.isInitialized = true;
360+
this.updateFocusableRadio();
361+
362+
this.radios.changes.subscribe(updatedRadios => {
363+
this.radios = updatedRadios;
364+
this.updateFocusableRadio();
365+
});
366+
}
367+
368+
updateFocusableRadio() {
369+
if (this.radios &&
370+
!this.radios.some(radio => radio.checked)) {
371+
this.radios.forEach(radio => radio.needsToBeFocusable = false);
372+
373+
this.radios.toArray()[0].needsToBeFocusable = true;
374+
this.radios.forEach(radio => radio.changeDetectorRef.detectChanges());
375+
}
360376
}
361377

362378
/**

src/radio/radio.component.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class RadioComponent extends CheckboxComponent implements OnInit {
143143
* @memberof RadioComponent
144144
*/
145145
constructor(@Optional() radioGroup: RadioGroup,
146-
protected changeDetectorRef: ChangeDetectorRef, private elementRef: ElementRef, private renderer: Renderer2) {
146+
public changeDetectorRef: ChangeDetectorRef, private elementRef: ElementRef, private renderer: Renderer2) {
147147
super(changeDetectorRef);
148148
RadioComponent.radioCount++;
149149
this.radioGroup = radioGroup;
@@ -159,14 +159,6 @@ export class RadioComponent extends CheckboxComponent implements OnInit {
159159
// if in group check if it needs checked and use group name
160160
this.checked = this.radioGroup.value === this._value;
161161
this.name = this.radioGroup.name;
162-
163-
setTimeout(() => {
164-
if (this.radioGroup.radios &&
165-
!this.radioGroup.radios.some(radio => radio.checked) &&
166-
this.radioGroup.radios.first === this) {
167-
this.needsToBeFocusable = true;
168-
}
169-
}, 0);
170162
}
171163
}
172164

@@ -190,7 +182,6 @@ export class RadioComponent extends CheckboxComponent implements OnInit {
190182

191183
let groupValueChanged = this.radioGroup && this.value !== this.radioGroup.value;
192184
this.checked = true;
193-
this.radioGroup.radios.first.needsToBeFocusable = false;
194185
this.emitChangeEvent();
195186

196187
if (this.radioGroup) {

0 commit comments

Comments
 (0)