Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/vue/src/components/IonTabBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ export const IonTabBar = defineComponent({

/**
* If the router outlet is not defined, then the tabs are being used
* as a basic tab navigation without the router. In this case, the
* tabs will not emit the `ionTabsDidChange` and `ionTabsWillChange`
* events through the `checkActiveTab` method. Instead, we need to
* handle those events through the tab buttons.
* as a basic tab navigation without the router. History changes do
* not happen in this case, so the selected tab is updated through
* the tab buttons instead of the `checkActiveTab` method.
*/
if (!hasRouterOutlet) {
child.component.props._onClick = (
Expand All @@ -164,11 +163,7 @@ export const IonTabBar = defineComponent({
* and updates the active tab accordingly.
*
* History changes only occur when the router
* outlet is present. Due to this, the
* `ionTabsDidChange` and `ionTabsWillChange`
* events are only emitted when the router
* outlet is present. A different approach must
* be taken for tabs without a router outlet.
* outlet is present.
*
* @param ionRouter
*/
Expand Down Expand Up @@ -277,18 +272,24 @@ export const IonTabBar = defineComponent({
(child: VNode) => isTabButton(child) && child.props?.tab === activeTab
);
const tabBar = this.$refs.ionTabBar as HTMLIonTabBarElement | undefined;
const tabDidChange = activeTab !== prevActiveTab;
/**
* Without a router outlet, `IonTabs` forwards the tab change
* events from core `ion-tabs`, which emits them once the tab
* has switched.
*/
const shouldEmitTabChange =
hasRouterOutlet && activeTab !== prevActiveTab;
if (tabBar) {
if (activeChild) {
tabDidChange && this.$data._tabsWillChange(activeTab);
shouldEmitTabChange && this.$data._tabsWillChange(activeTab);

if (hasRouterOutlet && ionRouter !== null) {
ionRouter.handleSetCurrentTab(activeTab);
}

tabBar.selectedTab = tabState.activeTab = activeTab;

tabDidChange && this.$data._tabsDidChange(activeTab);
shouldEmitTabChange && this.$data._tabsDidChange(activeTab);
} else {
/**
* When going to a tab that does
Expand Down
28 changes: 22 additions & 6 deletions packages/vue/src/components/IonTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ const isTab = (node: VNode): boolean => {
export const IonTabs = /*@__PURE__*/ defineComponent({
name: "IonTabs",
emits: [WILL_CHANGE, DID_CHANGE],
data() {
return {
hasRouterOutlet: false,
};
},
setup(props, { slots, emit }) {
const slottedContent: VNode[] | undefined =
slots.default && slots.default();
Expand All @@ -74,6 +69,8 @@ export const IonTabs = /*@__PURE__*/ defineComponent({
);
}

const hasRouterOutlet = !!routerOutlet;

/**
* Tab bar can be used as a standalone component,
* so it cannot be modified directly through
Expand All @@ -83,7 +80,7 @@ export const IonTabs = /*@__PURE__*/ defineComponent({
provide(
"tabBarData",
shallowRef<TabBarData>({
hasRouterOutlet: !!routerOutlet,
hasRouterOutlet,
_tabsWillChange: (tab) => emit(WILL_CHANGE, { tab }),
_tabsDidChange: (tab) => emit(DID_CHANGE, { tab }),
})
Expand All @@ -93,9 +90,28 @@ export const IonTabs = /*@__PURE__*/ defineComponent({
props,
slots,
emit,
hasRouterOutlet,
};
},
mounted() {
/**
* Without a router outlet, core `ion-tabs` switches tabs itself,
* including from `select()`, and emits the tab change events once
* the switch is done. Forward those, since declaring the same
* names in `emits` keeps them from reaching the app's listeners.
* The listeners are added before the element is defined so the
* events for the first tab are not missed.
*/
if (!this.hasRouterOutlet) {
const tabsEl = this.$el as HTMLIonTabsElement;
tabsEl.addEventListener(WILL_CHANGE, (ev) =>
this.$emit(WILL_CHANGE, { tab: (ev as CustomEvent).detail.tab })
);
tabsEl.addEventListener(DID_CHANGE, (ev) =>
this.$emit(DID_CHANGE, { tab: (ev as CustomEvent).detail.tab })
);
}

/**
* `defineCustomElement` must be called in the `mounted` hook
* to ensure that the custom element is defined after the
Expand Down
21 changes: 19 additions & 2 deletions packages/vue/test/base/src/views/TabsBasic.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<template>
<ion-page data-pageid="tabs">
<ion-content>
<ion-tabs id="tabs" @ionTabsWillChange="onTabWillChange" @ionTabsDidChange="onTabDidChange">
<ion-tabs id="tabs" ref="tabsRef" @ionTabsWillChange="onTabWillChange" @ionTabsDidChange="onTabDidChange">
<div slot="top">
<button id="select-tab3" @click="selectTab3">Select Tab 3</button>
<p>
Active tab on ionTabsDidChange: <span id="did-change-active-tab">{{ didChangeActiveTab }}</span>
</p>
</div>

<ion-tab-bar slot="bottom">
<ion-tab-button
v-for="tab in tabs"
Expand Down Expand Up @@ -45,11 +52,21 @@ export default defineComponent({
console.log('ionTabsWillChange', e.tab);
}

const tabsRef = ref();
const didChangeActiveTab = ref('');

const onTabDidChange = (e: { tab: string }) => {
console.log('ionTabsDidChange', e.tab);

const activeTab = Array.from(document.querySelectorAll('ion-tab')).find((tab) => tab.active);
didChangeActiveTab.value = activeTab?.getAttribute('data-pageid') ?? 'none';
}

const selectTab3 = () => {
tabsRef.value.$el.select('tab3');
}

return { tabs, onTabWillChange, onTabDidChange }
return { tabs, tabsRef, didChangeActiveTab, onTabWillChange, onTabDidChange, selectTab3 }
}
});
</script>
45 changes: 44 additions & 1 deletion packages/vue/test/base/tests/e2e/specs/tabs.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ describe('Tabs', () => {

describe('Without IonRouterOutlet', () => {
beforeEach(() => {
cy.visit('/tabs-basic');
cy.visit('/tabs-basic', {
onBeforeLoad(win) {
cy.spy(win.console, 'log').as('consoleLog');
},
});
});

it('should show correct tab when clicking the tab button', () => {
Expand Down Expand Up @@ -673,6 +677,45 @@ describe('Tabs', () => {

cy.url().should('include', '/tabs-basic');
});

it('should fire tab change events for the first tab on load', () => {
cy.get('#did-change-active-tab').should('have.text', 'tab1');
cy.get('@consoleLog').should((consoleLog) => {
expect(consoleLog.withArgs('ionTabsWillChange', 'tab1')).to.have.been.calledOnce;
expect(consoleLog.withArgs('ionTabsDidChange', 'tab1')).to.have.been.calledOnce;
});
});

it('should fire tab change events once after the tab switches', () => {
cy.get('ion-tab-button#tab-button-tab1').should('have.class', 'tab-selected');

cy.get('ion-tab-button#tab-button-tab2').click();

cy.get('#did-change-active-tab').should('have.text', 'tab2');
cy.get('@consoleLog').should((consoleLog) => {
expect(consoleLog.withArgs('ionTabsWillChange', 'tab2')).to.have.been.calledOnce;
expect(consoleLog.withArgs('ionTabsDidChange', 'tab2')).to.have.been.calledOnce;
});
});

it('should fire tab change events when a tab is selected from code', () => {
cy.get('ion-tab-button#tab-button-tab1').should('have.class', 'tab-selected');

cy.get('#select-tab3').click();

cy.get('#did-change-active-tab').should('have.text', 'tab3');
cy.get('@consoleLog').should((consoleLog) => {
expect(consoleLog.withArgs('ionTabsWillChange', 'tab3')).to.have.been.calledOnce;
expect(consoleLog.withArgs('ionTabsDidChange', 'tab3')).to.have.been.calledOnce;
});

cy.get('ion-tab-button#tab-button-tab2').click();

cy.get('#did-change-active-tab').should('have.text', 'tab2');
cy.get('@consoleLog').should((consoleLog) => {
expect(consoleLog.withArgs('ionTabsDidChange', 'tab2')).to.have.been.calledOnce;
});
});
})

describe('Swipe to Go Back', () => {
Expand Down
Loading