Skip to content

Commit a2763af

Browse files
authoredMar 3, 2021
fix(vue): correctly remove active state from tab button when navigating away from tab (#23000)
resolves #22597
1 parent 943e3f6 commit a2763af

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed
 

‎packages/vue/src/components/IonTabBar.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,23 @@ export const IonTabBar = defineComponent({
110110
const activeChild = childNodes.find((child: VNode) => isTabButton(child) && child.props?.tab === activeTab);
111111
const tabBar = this.$refs.ionTabBar;
112112
const tabDidChange = activeTab !== prevActiveTab;
113-
if (activeChild && tabBar) {
114-
tabDidChange && this.$props._tabsWillChange(activeTab);
113+
if (tabBar) {
114+
if (activeChild) {
115+
tabDidChange && this.$props._tabsWillChange(activeTab);
115116

116-
ionRouter.handleSetCurrentTab(activeTab);
117-
tabBar.selectedTab = tabState.activeTab = activeTab;
117+
ionRouter.handleSetCurrentTab(activeTab);
118+
tabBar.selectedTab = tabState.activeTab = activeTab;
118119

119-
tabDidChange && this.$props._tabsDidChange(activeTab);
120+
tabDidChange && this.$props._tabsDidChange(activeTab);
121+
/**
122+
* When going to a tab that does
123+
* not have an associated ion-tab-button
124+
* we need to remove the selected state from
125+
* the old tab.
126+
*/
127+
} else {
128+
tabBar.selectedTab = tabState.activeTab = '';
129+
}
120130
}
121131
};
122132

‎packages/vue/test-app/src/router/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ const routes: Array<RouteRecordRaw> = [
107107
next({ path: '/tabs/tab1' });
108108
},
109109
component: () => import('@/views/Tab3.vue')
110+
},
111+
{
112+
path: 'tab4',
113+
component: () => import('@/views/Tab4.vue')
110114
}
111115
]
112116
},
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<template>
2+
<ion-page data-pageid="tab4">
3+
<ion-header>
4+
<ion-toolbar>
5+
<ion-title>Tab 4</ion-title>
6+
</ion-toolbar>
7+
</ion-header>
8+
<ion-content :fullscreen="true">
9+
<ion-header collapse="condense">
10+
<ion-toolbar>
11+
<ion-title size="large">Tab 4</ion-title>
12+
</ion-toolbar>
13+
</ion-header>
14+
15+
<ExploreContainer name="Tab 4 page" />
16+
</ion-content>
17+
</ion-page>
18+
</template>
19+
20+
<script>
21+
import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/vue';
22+
import ExploreContainer from '@/components/ExploreContainer.vue';
23+
24+
export default {
25+
components: { ExploreContainer, IonHeader, IonToolbar, IonTitle, IonContent, IonPage }
26+
}
27+
</script>

‎packages/vue/test-app/tests/e2e/specs/tabs.js

+13
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ describe('Tabs', () => {
207207
cy.ionPageVisible('tab2');
208208
cy.ionPageVisible('tabs');
209209
});
210+
211+
// Verifies fix for https://github.com/ionic-team/ionic-framework/issues/22597
212+
it('should deselect old tab button when going to a tab that does not have a tab button', () => {
213+
cy.visit('http://localhost:8080/tabs/tab1');
214+
215+
cy.get('ion-tab-button#tab-button-tab1').should('have.class', 'tab-selected');
216+
217+
cy.routerPush('/tabs/tab4');
218+
cy.ionPageHidden('tab1');
219+
cy.ionPageVisible('tab4');
220+
221+
cy.get('ion-tab-button#tab-button-tab1').should('not.have.class', 'tab-selected');
222+
});
210223
})
211224

212225
describe('Tabs - Swipe to Go Back', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.