From f20cdab647289fdd53789e1f599f744af55cd3ca Mon Sep 17 00:00:00 2001 From: crisbeto Date: Mon, 18 Feb 2019 20:01:11 +0100 Subject: [PATCH] feat(tabs): add method for programmatically setting focus Adds a method that allows for focus to be moved to a particular tab. This is usually tricky, because all of the DOM elements are hidden away inside the tab group template. Fixes #15007. --- src/material/tabs/tab-group.spec.ts | 17 ++++++++++++++++- src/material/tabs/tab-group.ts | 12 ++++++++++++ tools/public_api_guard/material/tabs.d.ts | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/material/tabs/tab-group.spec.ts b/src/material/tabs/tab-group.spec.ts index c452f6498827..ec6cc8840612 100644 --- a/src/material/tabs/tab-group.spec.ts +++ b/src/material/tabs/tab-group.spec.ts @@ -6,7 +6,7 @@ import {By} from '@angular/platform-browser'; import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations'; import {CommonModule} from '@angular/common'; import {Observable} from 'rxjs'; -import {MatTab, MatTabGroup, MatTabHeaderPosition, MatTabsModule} from './index'; +import {MatTab, MatTabGroup, MatTabHeaderPosition, MatTabsModule, MatTabHeader} from './index'; describe('MatTabGroup', () => { @@ -291,6 +291,21 @@ describe('MatTabGroup', () => { subscription.unsubscribe(); }); + it('should be able to programmatically focus a particular tab', () => { + fixture.detectChanges(); + const tabGroup: MatTabGroup = + fixture.debugElement.query(By.css('mat-tab-group')).componentInstance; + const tabHeader: MatTabHeader = + fixture.debugElement.query(By.css('mat-tab-header')).componentInstance; + + expect(tabHeader.focusIndex).not.toBe(3); + + tabGroup.focusTab(3); + fixture.detectChanges(); + + expect(tabHeader.focusIndex).not.toBe(3); + }); + }); describe('aria labelling', () => { diff --git a/src/material/tabs/tab-group.ts b/src/material/tabs/tab-group.ts index 1236829e8d64..5efadbb8d6c8 100644 --- a/src/material/tabs/tab-group.ts +++ b/src/material/tabs/tab-group.ts @@ -289,6 +289,18 @@ export abstract class _MatTabGroupBase extends _MatTabGroupMixinBase implements } } + /** + * Sets focus to a particular tab. + * @param index Index of the tab to be focused. + */ + focusTab(index: number) { + const header = this._tabHeader; + + if (header) { + header.focusIndex = index; + } + } + _focusChanged(index: number) { this.focusChange.emit(this._createChangeEvent(index)); } diff --git a/tools/public_api_guard/material/tabs.d.ts b/tools/public_api_guard/material/tabs.d.ts index 663fd5ea4712..097794de8d30 100644 --- a/tools/public_api_guard/material/tabs.d.ts +++ b/tools/public_api_guard/material/tabs.d.ts @@ -58,6 +58,7 @@ export declare abstract class _MatTabGroupBase extends _MatTabGroupMixinBase imp _handleClick(tab: MatTab, tabHeader: MatTabGroupBaseHeader, index: number): void; _removeTabBodyWrapperHeight(): void; _setTabBodyWrapperHeight(tabHeight: number): void; + focusTab(index: number): void; ngAfterContentChecked(): void; ngAfterContentInit(): void; ngOnDestroy(): void;