Skip to content

Commit 4641c53

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 6ca1e43 commit 4641c53

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

src/material-experimental/mdc-radio/radio.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[id]="inputId"
66
[checked]="checked"
77
[disabled]="disabled"
8-
[tabIndex]="tabIndex"
8+
[tabIndex]="_getInputTabindex()"
99
[attr.name]="name"
1010
[attr.value]="value"
1111
[required]="required"

src/material-experimental/mdc-radio/radio.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('MDC-based MatRadio', () => {
2626
TranscludingWrapper,
2727
RadioButtonWithPredefinedTabindex,
2828
RadioButtonWithPredefinedAriaAttributes,
29+
RadiosInsidePreCheckedRadioGroup,
2930
]
3031
});
3132

@@ -402,6 +403,41 @@ describe('MDC-based MatRadio', () => {
402403
.every(element => element.classList.contains('mat-mdc-focus-indicator'))).toBe(true);
403404
});
404405

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

407443
describe('group with ngModel', () => {
@@ -934,6 +970,19 @@ class RadiosInsideRadioGroup {
934970
}
935971

936972

973+
@Component({
974+
template: `
975+
<mat-radio-group name="test-name">
976+
<mat-radio-button value="fire">Charmander</mat-radio-button>
977+
<mat-radio-button value="water">Squirtle</mat-radio-button>
978+
<mat-radio-button value="leaf" checked>Bulbasaur</mat-radio-button>
979+
</mat-radio-group>
980+
`
981+
})
982+
class RadiosInsidePreCheckedRadioGroup {
983+
}
984+
985+
937986
@Component({
938987
template: `
939988
<mat-radio-button name="season" value="spring">Spring</mat-radio-button>

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', () => {
@@ -908,6 +944,18 @@ class RadiosInsideRadioGroup {
908944
color: string | null;
909945
}
910946

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

912960
@Component({
913961
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 */
@@ -614,6 +615,21 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
614615
}
615616
}
616617

618+
/** Gets the tabindex for the underlying input element. */
619+
_getInputTabindex(): number {
620+
const group = this.radioGroup;
621+
622+
// Implement a roving tabindex if the button is inside a group. For most cases this isn't
623+
// necessary, because the browser handles the tab order for inputs inside a group automatically,
624+
// but we need an explicitly higher tabindex for the selected button in order for things like
625+
// the focus trap to pick it up correctly.
626+
if (!group || !group.selected || this.disabled) {
627+
return this.tabIndex;
628+
}
629+
630+
return group.selected === this ? this.tabIndex : -1;
631+
}
632+
617633
static ngAcceptInputType_checked: BooleanInput;
618634
static ngAcceptInputType_disabled: BooleanInput;
619635
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)