Skip to content

Commit 7dff5f8

Browse files
crisbetojosephperrott
authored andcommitted
fix(button-toggle): forward tabindex to underlying button (#12538)
1 parent 5623c5b commit 7dff5f8

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/lib/button-toggle/button-toggle.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<button #button class="mat-button-toggle-button"
22
type="button"
33
[id]="buttonId"
4+
[attr.tabindex]="disabled ? -1 : tabIndex"
45
[attr.aria-pressed]="checked"
56
[disabled]="disabled || null"
67
[attr.name]="name || null"

src/lib/button-toggle/button-toggle.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ describe('MatButtonToggle without forms', () => {
211211
ButtonToggleWithAriaLabel,
212212
ButtonToggleWithAriaLabelledby,
213213
RepeatedButtonTogglesWithPreselectedValue,
214+
ButtonToggleWithTabindex,
214215
],
215216
});
216217

@@ -686,6 +687,26 @@ describe('MatButtonToggle without forms', () => {
686687
});
687688
});
688689

690+
describe('with tabindex ', () => {
691+
it('should forward the tabindex to the underlying button', () => {
692+
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
693+
fixture.detectChanges();
694+
695+
const button = fixture.nativeElement.querySelector('.mat-button-toggle button');
696+
697+
expect(button.getAttribute('tabindex')).toBe('3');
698+
});
699+
700+
it('should clear the tabindex from the host element', () => {
701+
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
702+
fixture.detectChanges();
703+
704+
const host = fixture.nativeElement.querySelector('.mat-button-toggle');
705+
706+
expect(host.hasAttribute('tabindex')).toBe(false);
707+
});
708+
});
709+
689710
it('should not throw on init when toggles are repeated and there is an initial value', () => {
690711
const fixture = TestBed.createComponent(RepeatedButtonTogglesWithPreselectedValue);
691712

@@ -855,3 +876,10 @@ class RepeatedButtonTogglesWithPreselectedValue {
855876
possibleValues = ['One', 'Two', 'Three'];
856877
value = 'Two';
857878
}
879+
880+
881+
@Component({
882+
template: `<mat-button-toggle tabindex="3"></mat-button-toggle>`
883+
})
884+
class ButtonToggleWithTabindex {}
885+

src/lib/button-toggle/button-toggle.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
QueryList,
2727
ViewChild,
2828
ViewEncapsulation,
29+
Attribute,
2930
} from '@angular/core';
3031
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
3132
import {
@@ -330,6 +331,8 @@ export const _MatButtonToggleMixinBase = mixinDisableRipple(MatButtonToggleBase)
330331
'[class.mat-button-toggle-checked]': 'checked',
331332
'[class.mat-button-toggle-disabled]': 'disabled',
332333
'class': 'mat-button-toggle',
334+
// Clear out the native tabindex here since we forward it to the underlying button
335+
'[attr.tabindex]': 'null',
333336
'[attr.id]': 'id',
334337
}
335338
})
@@ -370,6 +373,9 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
370373
/** MatButtonToggleGroup reads this to assign its own value. */
371374
@Input() value: any;
372375

376+
/** Tabindex for the toggle. */
377+
@Input() tabIndex: number | null;
378+
373379
/** Whether the button is checked. */
374380
@Input()
375381
get checked(): boolean {
@@ -404,9 +410,13 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
404410
constructor(@Optional() toggleGroup: MatButtonToggleGroup,
405411
private _changeDetectorRef: ChangeDetectorRef,
406412
private _elementRef: ElementRef<HTMLElement>,
407-
private _focusMonitor: FocusMonitor) {
413+
private _focusMonitor: FocusMonitor,
414+
// @breaking-change 8.0.0 `defaultTabIndex` to be made a required parameter.
415+
@Attribute('tabindex') defaultTabIndex: string) {
408416
super();
409417

418+
const parsedTabIndex = Number(defaultTabIndex);
419+
this.tabIndex = (parsedTabIndex || parsedTabIndex === 0) ? parsedTabIndex : null;
410420
this.buttonToggleGroup = toggleGroup;
411421
}
412422

0 commit comments

Comments
 (0)