Skip to content

Commit f42033c

Browse files
committed
fix(material/radio): set tabindex based on selected state
Currently all radio buttons inside a radio group have a `tabindex` of 0, unless they're disabled, and we leave it up to the browser to focus the proper button based on its selected state when the user is tabbing. This works for the most part, but it breaks down with something like our focus trap which determines which element to focus based on its `tabindex` since all buttons have the same `tabindex`. These changes fix the issue by setting a `tabindex` of 0 only on the selected radio button. Fixes #17876.
1 parent 029136e commit f42033c

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/material/radio/radio.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[id]="inputId"
1010
[checked]="checked"
1111
[disabled]="disabled"
12-
[tabIndex]="tabIndex"
12+
[tabIndex]="_getInputTabindex()"
1313
[attr.name]="name"
1414
[attr.value]="value"
1515
[required]="required"

src/material/radio/radio.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('MatRadio', () => {
2323
TranscludingWrapper,
2424
RadioButtonWithPredefinedTabindex,
2525
RadioButtonWithPredefinedAriaAttributes,
26+
RadiosInsidePreCheckedRadioGroup,
2627
]
2728
});
2829

@@ -395,6 +396,41 @@ describe('MatRadio', () => {
395396
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
396397
});
397398

399+
it('should set the input tabindex based on the selected radio button', () => {
400+
const getTabIndexes = () => {
401+
return radioInputElements.map(element => parseInt(element.getAttribute('tabindex') || ''));
402+
};
403+
404+
expect(getTabIndexes()).toEqual([0, 0, 0]);
405+
406+
radioLabelElements[0].click();
407+
fixture.detectChanges();
408+
409+
expect(getTabIndexes()).toEqual([0, -1, -1]);
410+
411+
radioLabelElements[1].click();
412+
fixture.detectChanges();
413+
414+
expect(getTabIndexes()).toEqual([-1, 0, -1]);
415+
416+
radioLabelElements[2].click();
417+
fixture.detectChanges();
418+
419+
expect(getTabIndexes()).toEqual([-1, -1, 0]);
420+
});
421+
422+
it('should set the input tabindex correctly with a pre-checked radio button', () => {
423+
const precheckedFixture = TestBed.createComponent(RadiosInsidePreCheckedRadioGroup);
424+
precheckedFixture.detectChanges();
425+
426+
const radios: NodeListOf<HTMLElement> =
427+
precheckedFixture.nativeElement.querySelectorAll('mat-radio-button input');
428+
429+
expect(Array.from(radios).map(radio => {
430+
return radio.getAttribute('tabindex');
431+
})).toEqual(['-1', '-1', '0']);
432+
});
433+
398434
});
399435

400436
describe('group with ngModel', () => {
@@ -911,6 +947,18 @@ class RadiosInsideRadioGroup {
911947
color: string | null;
912948
}
913949

950+
@Component({
951+
template: `
952+
<mat-radio-group name="test-name">
953+
<mat-radio-button value="fire">Charmander</mat-radio-button>
954+
<mat-radio-button value="water">Squirtle</mat-radio-button>
955+
<mat-radio-button value="leaf" checked>Bulbasaur</mat-radio-button>
956+
</mat-radio-group>
957+
`
958+
})
959+
class RadiosInsidePreCheckedRadioGroup {
960+
}
961+
914962

915963
@Component({
916964
template: `

src/material/radio/radio.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export abstract class _MatRadioGroupBase<T extends _MatRadioButtonBase> implemen
199199
this._selected = selected;
200200
this.value = selected ? selected.value : null;
201201
this._checkSelectedRadioButton();
202+
this._markRadiosForCheck();
202203
}
203204

204205
/** Whether the radio group is disabled */
@@ -609,6 +610,21 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
609610
}
610611
}
611612

613+
/** Gets the tabindex for the underlying input element. */
614+
_getInputTabindex(): number {
615+
const group = this.radioGroup;
616+
617+
// Implement a roving tabindex if the button is inside a group. For most cases this isn't
618+
// necessary, because the browser handles the tab order for inputs inside a group automatically,
619+
// but we need an explicitly higher tabindex for the selected button in order for things like
620+
// the focus trap to pick it up correctly.
621+
if (!group || !group.selected || this.disabled) {
622+
return this.tabIndex;
623+
}
624+
625+
return group.selected === this ? this.tabIndex : -1;
626+
}
627+
612628
static ngAcceptInputType_checked: BooleanInput;
613629
static ngAcceptInputType_disabled: BooleanInput;
614630
static ngAcceptInputType_required: BooleanInput;

tools/public_api_guard/material/radio.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export declare abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBa
2323
get value(): any;
2424
set value(value: any);
2525
constructor(radioGroup: _MatRadioGroupBase<_MatRadioButtonBase>, elementRef: ElementRef, _changeDetector: ChangeDetectorRef, _focusMonitor: FocusMonitor, _radioDispatcher: UniqueSelectionDispatcher, _animationMode?: string | undefined, _providerOverride?: MatRadioDefaultOptions | undefined, tabIndex?: string);
26+
_getInputTabindex(): number;
2627
_isRippleDisabled(): boolean;
2728
_markForCheck(): void;
2829
_onInputChange(event: Event): void;

0 commit comments

Comments
 (0)