Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat(tab-bar): Allow activation of tab without previous active tab (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stof authored and patrickrodee committed Apr 22, 2019
1 parent 0826a78 commit 7d4124d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/mdc-tab-bar/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface MDCTabBarAdapter {
* @param index The index of the tab to activate
* @param clientRect The client rect of the previously active Tab Indicator
*/
activateTabAtIndex(index: number, clientRect: ClientRect): void;
activateTabAtIndex(index: number, clientRect?: ClientRect): void;

/**
* Deactivates the tab at the given index
Expand Down
9 changes: 7 additions & 2 deletions packages/mdc-tab-bar/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ export class MDCTabBarFoundation extends MDCFoundation<MDCTabBarAdapter> {
return;
}

this.adapter_.deactivateTabAtIndex(previousActiveIndex);
this.adapter_.activateTabAtIndex(index, this.adapter_.getTabIndicatorClientRectAtIndex(previousActiveIndex));
let previousClientRect;
if (previousActiveIndex !== -1) {
this.adapter_.deactivateTabAtIndex(previousActiveIndex);
previousClientRect = this.adapter_.getTabIndicatorClientRectAtIndex(previousActiveIndex);
}

this.adapter_.activateTabAtIndex(index, previousClientRect);
this.scrollIntoView(index);

this.adapter_.notifyTabActivated(index);
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mdc-tab-bar/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ test('#activateTab() deactivates the previously active tab', () => {
td.verify(mockAdapter.deactivateTabAtIndex(6), {times: 1});
});

test('#activateTab() does not deactivate the previously active tab if there is none', () => {
const {foundation, mockAdapter} = setupActivateTabTest();
td.when(mockAdapter.getTabListLength()).thenReturn(13);
td.when(mockAdapter.getPreviousActiveTabIndex()).thenReturn(-1);
foundation.activateTab(1);
td.verify(mockAdapter.deactivateTabAtIndex(td.matchers.anything()), {times: 0});
});

test('#activateTab() activates the newly active tab with the previously active tab\'s indicatorClientRect', () => {
const {foundation, mockAdapter} = setupActivateTabTest();
td.when(mockAdapter.getTabListLength()).thenReturn(13);
Expand Down

0 comments on commit 7d4124d

Please sign in to comment.