Skip to content

Commit 40b2cf9

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 3d35180 commit 40b2cf9

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
@@ -19,7 +19,7 @@
1919
[id]="inputId"
2020
[checked]="checked"
2121
[disabled]="disabled"
22-
[tabIndex]="tabIndex"
22+
[tabIndex]="_getInputTabindex()"
2323
[attr.name]="name"
2424
[attr.value]="value"
2525
[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
@@ -192,6 +192,7 @@ export class MatRadioGroup implements AfterContentInit, ControlValueAccessor {
192192
this._selected = selected;
193193
this.value = selected ? selected.value : null;
194194
this._checkSelectedRadioButton();
195+
this._markRadiosForCheck();
195196
}
196197

197198
/** Whether the radio group is disabled */
@@ -601,6 +602,21 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
601602
}
602603
}
603604

605+
/** Gets the tabindex for the underlying input element. */
606+
_getInputTabindex(): number {
607+
const group = this.radioGroup;
608+
609+
// Implement a roving tabindex if the button is inside a group. For most cases this isn't
610+
// necessary, because the browser handles the tab order for inputs inside a group automatically,
611+
// but we need an explicitly higher tabindex for the selected button in order for things like
612+
// the focus trap to pick it up correctly.
613+
if (!group || !group.selected || this.disabled) {
614+
return this.tabIndex;
615+
}
616+
617+
return group.selected === this ? this.tabIndex : -1;
618+
}
619+
604620
static ngAcceptInputType_checked: BooleanInput;
605621
static ngAcceptInputType_disabled: BooleanInput;
606622
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
@@ -29,6 +29,7 @@ export declare class MatRadioButton extends _MatRadioButtonMixinBase implements
2929
get value(): any;
3030
set value(value: any);
3131
constructor(radioGroup: MatRadioGroup, elementRef: ElementRef, _changeDetector: ChangeDetectorRef, _focusMonitor: FocusMonitor, _radioDispatcher: UniqueSelectionDispatcher, _animationMode?: string | undefined, _providerOverride?: MatRadioDefaultOptions | undefined);
32+
_getInputTabindex(): number;
3233
_isRippleDisabled(): boolean;
3334
_markForCheck(): void;
3435
_onInputChange(event: Event): void;

0 commit comments

Comments
 (0)