diff --git a/packages/vue/src/components/IonTabBar.ts b/packages/vue/src/components/IonTabBar.ts index b8dd334e21..259c64cb48 100644 --- a/packages/vue/src/components/IonTabBar.ts +++ b/packages/vue/src/components/IonTabBar.ts @@ -110,13 +110,23 @@ export const IonTabBar = defineComponent({ const activeChild = childNodes.find((child: VNode) => isTabButton(child) && child.props?.tab === activeTab); const tabBar = this.$refs.ionTabBar; const tabDidChange = activeTab !== prevActiveTab; - if (activeChild && tabBar) { - tabDidChange && this.$props._tabsWillChange(activeTab); + if (tabBar) { + if (activeChild) { + tabDidChange && this.$props._tabsWillChange(activeTab); - ionRouter.handleSetCurrentTab(activeTab); - tabBar.selectedTab = tabState.activeTab = activeTab; + ionRouter.handleSetCurrentTab(activeTab); + tabBar.selectedTab = tabState.activeTab = activeTab; - tabDidChange && this.$props._tabsDidChange(activeTab); + tabDidChange && this.$props._tabsDidChange(activeTab); + /** + * When going to a tab that does + * not have an associated ion-tab-button + * we need to remove the selected state from + * the old tab. + */ + } else { + tabBar.selectedTab = tabState.activeTab = ''; + } } }; diff --git a/packages/vue/test-app/src/router/index.ts b/packages/vue/test-app/src/router/index.ts index 0ac97bdd76..1af585aa66 100644 --- a/packages/vue/test-app/src/router/index.ts +++ b/packages/vue/test-app/src/router/index.ts @@ -107,6 +107,10 @@ const routes: Array = [ next({ path: '/tabs/tab1' }); }, component: () => import('@/views/Tab3.vue') + }, + { + path: 'tab4', + component: () => import('@/views/Tab4.vue') } ] }, diff --git a/packages/vue/test-app/src/views/Tab4.vue b/packages/vue/test-app/src/views/Tab4.vue new file mode 100644 index 0000000000..9fe2d643d3 --- /dev/null +++ b/packages/vue/test-app/src/views/Tab4.vue @@ -0,0 +1,27 @@ + + + diff --git a/packages/vue/test-app/tests/e2e/specs/tabs.js b/packages/vue/test-app/tests/e2e/specs/tabs.js index 020af3fa5e..5bb3205c1b 100644 --- a/packages/vue/test-app/tests/e2e/specs/tabs.js +++ b/packages/vue/test-app/tests/e2e/specs/tabs.js @@ -207,6 +207,19 @@ describe('Tabs', () => { cy.ionPageVisible('tab2'); cy.ionPageVisible('tabs'); }); + + // Verifies fix for https://github.com/ionic-team/ionic-framework/issues/22597 + it('should deselect old tab button when going to a tab that does not have a tab button', () => { + cy.visit('http://localhost:8080/tabs/tab1'); + + cy.get('ion-tab-button#tab-button-tab1').should('have.class', 'tab-selected'); + + cy.routerPush('/tabs/tab4'); + cy.ionPageHidden('tab1'); + cy.ionPageVisible('tab4'); + + cy.get('ion-tab-button#tab-button-tab1').should('not.have.class', 'tab-selected'); + }); }) describe('Tabs - Swipe to Go Back', () => {