fix(button-toggle): not forwarding focus to underlying control (#14001)

Fixes the button toggle host element not being able to receive focus which means that it can't be used with the focus trap directives. Also fixes that when it does receive focus, it won't forward it to the underlying `button` element.
This commit is contained in:
Kristiyan Kostadinov
2018-11-07 18:20:26 +00:00
committed by vivian-hu
parent ec4809ff1b
commit 81f8fb9582
2 changed files with 19 additions and 3 deletions
+15 -1
View File
@@ -723,7 +723,21 @@ describe('MatButtonToggle without forms', () => {
const host = fixture.nativeElement.querySelector('.mat-button-toggle');
expect(host.hasAttribute('tabindex')).toBe(false);
expect(host.getAttribute('tabindex')).toBe('-1');
});
it('should forward focus to the underlying button when the host is focused', () => {
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
fixture.detectChanges();
const host = fixture.nativeElement.querySelector('.mat-button-toggle');
const button = host.querySelector('button');
expect(document.activeElement).not.toBe(button);
host.focus();
expect(document.activeElement).toBe(button);
});
});
+4 -2
View File
@@ -365,9 +365,11 @@ export const _MatButtonToggleMixinBase: CanDisableRippleCtor & typeof MatButtonT
'[class.mat-button-toggle-disabled]': 'disabled',
'[class.mat-button-toggle-appearance-standard]': 'appearance === "standard"',
'class': 'mat-button-toggle',
// Clear out the native tabindex here since we forward it to the underlying button
'[attr.tabindex]': 'null',
// Always reset the tabindex to -1 so it doesn't conflict with the one on the `button`,
// but can still receive focus from things like cdkFocusInitial.
'[attr.tabindex]': '-1',
'[attr.id]': 'id',
'(focus)': 'focus()',
}
})
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit,