Skip to content

Commit ec11e41

Browse files
committed
fix(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 264c20d commit ec11e41

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', () => {
@@ -892,6 +928,18 @@ class RadiosInsideRadioGroup {
892928
color: string | null;
893929
}
894930

931+
@Component({
932+
template: `
933+
<mat-radio-group name="test-name">
934+
<mat-radio-button value="fire">Charmander</mat-radio-button>
935+
<mat-radio-button value="water">Squirtle</mat-radio-button>
936+
<mat-radio-button value="leaf" checked>Bulbasaur</mat-radio-button>
937+
</mat-radio-group>
938+
`
939+
})
940+
class RadiosInsidePreCheckedRadioGroup {
941+
}
942+
895943

896944
@Component({
897945
template: `

src/material/radio/radio.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export abstract class _MatRadioGroupBase<T extends _MatRadioButtonBase> implemen
186186
this._selected = selected;
187187
this.value = selected ? selected.value : null;
188188
this._checkSelectedRadioButton();
189+
this._markRadiosForCheck();
189190
}
190191

191192
/** Whether the radio group is disabled */
@@ -587,6 +588,21 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
587588
}
588589
}
589590

591+
/** Gets the tabindex for the underlying input element. */
592+
_getInputTabindex(): number {
593+
const group = this.radioGroup;
594+
595+
// Implement a roving tabindex if the button is inside a group. For most cases this isn't
596+
// necessary, because the browser handles the tab order for inputs inside a group automatically,
597+
// but we need an explicitly higher tabindex for the selected button in order for things like
598+
// the focus trap to pick it up correctly.
599+
if (!group || !group.selected || this.disabled) {
600+
return this.tabIndex;
601+
}
602+
603+
return group.selected === this ? this.tabIndex : -1;
604+
}
605+
590606
static ngAcceptInputType_checked: BooleanInput;
591607
static ngAcceptInputType_disabled: BooleanInput;
592608
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);
26+
_getInputTabindex(): number;
2627
_isRippleDisabled(): boolean;
2728
_markForCheck(): void;
2829
_onInputChange(event: Event): void;

0 commit comments

Comments
 (0)