Skip to content

Commit f1c3e2c

Browse files
devversionjelbourn
authored andcommitted
fix(radio): add support for tabindex on radio-buttons (#9467)
Fixes #9427
1 parent 01867ad commit f1c3e2c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/lib/radio/radio.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
[id]="inputId"
1919
[checked]="checked"
2020
[disabled]="disabled"
21+
[tabIndex]="tabIndex"
2122
[attr.name]="name"
2223
[required]="required"
2324
[attr.aria-label]="ariaLabel"

src/lib/radio/radio.spec.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,26 @@ describe('MatRadio', () => {
657657
let inputEl = fixture.debugElement.query(By.css('.mat-radio-input')).nativeElement;
658658

659659
radioButtonEl.focus();
660-
// Focus events don't always fire in tests, so we needc to fake it.
660+
// Focus events don't always fire in tests, so we need to fake it.
661661
dispatchFakeEvent(radioButtonEl, 'focus');
662662
fixture.detectChanges();
663663

664664
expect(document.activeElement).toBe(inputEl);
665665
});
666+
667+
it('should allow specifying an explicit tabindex for a single radio-button', () => {
668+
const radioButtonInput = fixture.debugElement
669+
.query(By.css('.mat-radio-button input')).nativeElement as HTMLInputElement;
670+
671+
expect(radioButtonInput.tabIndex)
672+
.toBe(0, 'Expected the tabindex to be set to "0" by default.');
673+
674+
fixture.componentInstance.tabIndex = 4;
675+
fixture.detectChanges();
676+
677+
expect(radioButtonInput.tabIndex)
678+
.toBe(4, 'Expected the tabindex to be set to "4".');
679+
});
666680
});
667681

668682
describe('group interspersed with other tags', () => {
@@ -777,9 +791,11 @@ class RadioGroupWithFormControl {
777791
}
778792

779793
@Component({
780-
template: `<mat-radio-button tabindex="-1"></mat-radio-button>`
794+
template: `<mat-radio-button [tabIndex]="tabIndex"></mat-radio-button>`
781795
})
782-
class FocusableRadioButton {}
796+
class FocusableRadioButton {
797+
tabIndex: number;
798+
}
783799

784800
@Component({
785801
template: `

src/lib/radio/radio.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ import {
3434
CanColor,
3535
CanDisable,
3636
CanDisableRipple,
37+
HasTabIndex,
3738
MatRipple,
3839
mixinColor,
3940
mixinDisabled,
4041
mixinDisableRipple,
42+
mixinTabIndex,
4143
RippleConfig,
4244
RippleRef,
4345
} from '@angular/material/core';
@@ -313,12 +315,17 @@ export class MatRadioGroup extends _MatRadioGroupMixinBase
313315
// Boilerplate for applying mixins to MatRadioButton.
314316
/** @docs-private */
315317
export class MatRadioButtonBase {
318+
// Since the disabled property is manually defined for the MatRadioButton and isn't set up in
319+
// the mixin base class. To be able to use the tabindex mixin, a disabled property must be
320+
// defined to properly work.
321+
disabled: boolean;
322+
316323
constructor(public _elementRef: ElementRef) {}
317324
}
318325
// As per Material design specifications the selection control radio should use the accent color
319326
// palette by default. https://material.io/guidelines/components/selection-controls.html
320327
export const _MatRadioButtonMixinBase =
321-
mixinColor(mixinDisableRipple(MatRadioButtonBase), 'accent');
328+
mixinColor(mixinDisableRipple(mixinTabIndex(MatRadioButtonBase)), 'accent');
322329

323330
/**
324331
* A Material design radio-button. Typically placed inside of `<mat-radio-group>` elements.
@@ -328,7 +335,7 @@ export const _MatRadioButtonMixinBase =
328335
selector: 'mat-radio-button',
329336
templateUrl: 'radio.html',
330337
styleUrls: ['radio.css'],
331-
inputs: ['color', 'disableRipple'],
338+
inputs: ['color', 'disableRipple', 'tabIndex'],
332339
encapsulation: ViewEncapsulation.None,
333340
preserveWhitespaces: false,
334341
exportAs: 'matRadioButton',
@@ -345,7 +352,7 @@ export const _MatRadioButtonMixinBase =
345352
changeDetection: ChangeDetectionStrategy.OnPush,
346353
})
347354
export class MatRadioButton extends _MatRadioButtonMixinBase
348-
implements OnInit, AfterViewInit, OnDestroy, CanColor, CanDisableRipple {
355+
implements OnInit, AfterViewInit, OnDestroy, CanColor, CanDisableRipple, HasTabIndex {
349356

350357
private _uniqueId: string = `mat-radio-${++nextUniqueId}`;
351358

0 commit comments

Comments
 (0)