Skip to content

Commit 44461dd

Browse files
authored
fix(tab-bar): fire tab change events on tab click (#31262)
Issue number: resolves #30145 --------- ## What is the current behavior? `ionic-react`'s `IonTabBar` tries to reference `this.props.onIonTabsWillChange` and `this.props.onIonTabsDidChange`, which do not exist, causing the `onIonTabsWillChange` and `onIonTabsDidChange` events to never fire. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Update usage of `this.props.onIonTabsWillChange` to `this.props.tabsContext.tabBarProps.onIonTabsWillChange` - Update usage of `this.props.onIonTabsDidChange` to `this.props.tabsContext.tabBarProps.onIonTabsDidChange` - Add check to make sure `this.props.tabsContext` is truthy before using it - `onIonTabsWillChange` and `onIonTabsDidChange` fire as expected ## Does this introduce a breaking change? - [ ] Yes - [x] No
1 parent 4f2ab73 commit 44461dd

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

packages/react/src/components/navigation/IonTabBar.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,20 @@ class IonTabBarUnwrapped extends React.PureComponent<InternalProps, IonTabBarSta
224224
this.context.resetTab(e.detail.tab, originalHref, tappedTab.originalRouteOptions);
225225
}
226226
} else {
227-
if (this.props.onIonTabsWillChange) {
228-
this.props.onIonTabsWillChange(new CustomEvent('ionTabWillChange', { detail: { tab: e.detail.tab } }));
227+
let onWillChange = this.props.onIonTabsWillChange;
228+
let onDidChange = this.props.onIonTabsDidChange;
229+
if (this.props.tabsContext) {
230+
onWillChange = this.props.tabsContext?.tabBarProps.onIonTabsWillChange ?? this.props.onIonTabsWillChange;
231+
onDidChange = this.props.tabsContext?.tabBarProps.onIonTabsDidChange ?? this.props.onIonTabsDidChange;
229232
}
230-
if (this.props.onIonTabsDidChange) {
231-
this.props.onIonTabsDidChange(new CustomEvent('ionTabDidChange', { detail: { tab: e.detail.tab } }));
233+
234+
if (onWillChange) {
235+
onWillChange(new CustomEvent('ionTabWillChange', { detail: { tab: e.detail.tab } }));
236+
}
237+
if (onDidChange) {
238+
onDidChange(new CustomEvent('ionTabDidChange', { detail: { tab: e.detail.tab } }));
232239
}
240+
233241
if (hasRouterOutlet) {
234242
this.setActiveTabOnContext(e.detail.tab);
235243
this.context.changeTab(e.detail.tab, currentHref, e.detail.routeOptions);

packages/react/test/base/tests/e2e/specs/tabs/tabs.cy.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe('IonTabs', () => {
22
/**
33
* Verifies that tabs with similar route prefixes (e.g., /home, /home2, /home3)
44
* correctly select the matching tab instead of the first prefix match.
5-
*
5+
*
66
* @see https://github.com/ionic-team/ionic-framework/issues/30448
77
*/
88
describe('Similar Route Prefixes', () => {
@@ -58,7 +58,11 @@ describe('IonTabs', () => {
5858

5959
describe('Without IonRouterOutlet', () => {
6060
beforeEach(() => {
61-
cy.visit('/tabs-basic');
61+
cy.visit('/tabs-basic', {
62+
onBeforeLoad(win) {
63+
cy.spy(win.console, 'log').as('consoleLog');
64+
},
65+
});
6266
});
6367

6468
it.skip('should show correct tab when clicking the tab button', () => {
@@ -83,5 +87,12 @@ describe('IonTabs', () => {
8387

8488
cy.url().should('include', '/tabs-basic');
8589
});
90+
91+
it('should fire tab change events on tab click', () => {
92+
cy.get('ion-tab-button[tab="tab2"]').click();
93+
94+
cy.get('@consoleLog').should('be.calledWith', 'onIonTabsWillChange', 'tab2');
95+
cy.get('@consoleLog').should('be.calledWith', 'onIonTabsDidChange:', 'tab2');
96+
});
8697
});
8798
});

0 commit comments

Comments
 (0)