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
48 changes: 46 additions & 2 deletions packages/react/src/components/navigation/IonTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class IonTabs extends React.Component<Props> {
routerOutletRef: React.Ref<Components.IonRouterOutlet> = React.createRef();
selectTabHandler?: (tag: string) => boolean;
tabBarRef = React.createRef<any>();
tabsElRef = React.createRef<HTMLIonTabsElement>();

ionTabContextState: IonTabsContextState = {
activeTab: undefined,
Expand All @@ -81,7 +82,12 @@ export class IonTabs extends React.Component<Props> {
}

componentDidMount() {
if (this.tabBarRef.current) {
/**
* Without a router outlet, the tab bar does not switch tabs,
* so its `selectTab` and active tab must not replace the ones
* set for `ion-tabs` in `render`.
*/
if (this.ionTabContextState.hasRouterOutlet && this.tabBarRef.current) {
// Grab initial value
this.ionTabContextState.activeTab = this.tabBarRef.current.state.activeTab;
// Override method
Expand All @@ -92,6 +98,30 @@ export class IonTabs extends React.Component<Props> {
}
}

/**
* Selects a tab on `ion-tabs` when it is used without a router outlet.
* Returns `false` when no `ion-tab` has the given `tab`.
*/
selectTabWithoutRouter = (tab: string) => {
const tabsEl = this.tabsElRef.current;
if (!tabsEl) {
return false;
}

const hasTab = Array.from(tabsEl.querySelectorAll('ion-tab')).some((tabEl) => tabEl.tab === tab);
if (!hasTab) {
return false;
}

tabsEl.select(tab);
return true;
};

onTabsDidChangeWithoutRouter = (event: CustomEvent<{ tab: string }>) => {
this.ionTabContextState.activeTab = event.detail.tab;
this.props.onIonTabsDidChange?.(event);
};

renderTabsInner(children: React.ReactNode, outlet: React.ReactElement<{}> | undefined) {
return (
<IonTabsInner {...this.props}>
Expand Down Expand Up @@ -182,7 +212,21 @@ export class IonTabs extends React.Component<Props> {
}

if (hasTab) {
return <IonTabsInner {...this.props}></IonTabsInner>;
/**
* Core `ion-tabs` switches tabs and emits the tab change events
* itself, so the tab bar gets no change handlers. Otherwise they
* would run a second time from the tab bar's click handler.
*/
this.ionTabContextState.tabBarProps = { ref: this.tabBarRef };
this.ionTabContextState.selectTab = this.selectTabWithoutRouter;

return (
<IonTabsContext.Provider value={this.ionTabContextState}>
<IonTabsInner {...this.props} ref={this.tabsElRef} onIonTabsDidChange={this.onTabsDidChangeWithoutRouter}>
{children}
</IonTabsInner>
</IonTabsContext.Provider>
);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/react/test/base/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Inputs from './pages/Inputs';
import Main from './pages/Main';
import Tabs from './pages/Tabs';
import TabsBasic from './pages/TabsBasic';
import TabsFunctionChildren from './pages/TabsFunctionChildren';
import NavComponent from './pages/navigation/NavComponent';
import NavModalComponent from './pages/navigation/NavModalComponent';
import TabsDirectNavigation from './pages/TabsDirectNavigation';
Expand Down Expand Up @@ -70,6 +71,7 @@ const App: React.FC = () => (
<Route path="/navigation-modal" element={<NavModalComponent />} />
<Route path="/tabs/*" element={<Tabs />} />
<Route path="/tabs-basic/*" element={<TabsBasic />} />
<Route path="/tabs-function-children/*" element={<TabsFunctionChildren />} />
<Route path="/tabs-direct-navigation/*" element={<TabsDirectNavigation />} />
<Route path="/tabs-similar-prefixes/*" element={<TabsSimilarPrefixes />} />
<Route path="/icons" element={<Icons />} />
Expand Down
33 changes: 31 additions & 2 deletions packages/react/test/base/src/pages/TabsBasic.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import React from 'react';
import { IonLabel, IonTabBar, IonTabButton, IonTabs, IonTab, IonPage } from '@ionic/react';
import React, { useContext, useState } from 'react';
import { IonLabel, IonTabBar, IonTabButton, IonTabs, IonTab, IonPage, IonTabsContext } from '@ionic/react';

interface TabsProps {}

const TabSwitcher: React.FC = () => {
const tabsContext = useContext(IonTabsContext);
const [selectResult, setSelectResult] = useState('');
const [activeTab, setActiveTab] = useState('');

return (
<div>
<button id="select-tab2" onClick={() => setSelectResult(String(tabsContext.selectTab('tab2')))}>
Select Tab 2
</button>
<button id="select-missing-tab" onClick={() => setSelectResult(String(tabsContext.selectTab('missing')))}>
Select Missing Tab
</button>
<button id="read-active-tab" onClick={() => setActiveTab(String(tabsContext.activeTab))}>
Read Active Tab
</button>
<p>
selectTab result: <span id="select-result">{selectResult}</span>
</p>
<p>
activeTab: <span id="active-tab">{activeTab}</span>
</p>
</div>
);
};

const TabsBasic: React.FC<TabsProps> = () => {
const onTabWillChange = (event: CustomEvent) => {
console.log('onIonTabsWillChange', event.detail.tab);
Expand All @@ -15,6 +41,9 @@ const TabsBasic: React.FC<TabsProps> = () => {
return (
<IonPage>
<IonTabs onIonTabsWillChange={onTabWillChange} onIonTabsDidChange={onTabDidChange}>
<div slot="top">
<TabSwitcher />
</div>
<IonTab tab="tab1">
<IonLabel>Tab 1 Content</IonLabel>
</IonTab>
Expand Down
32 changes: 32 additions & 0 deletions packages/react/test/base/src/pages/TabsFunctionChildren.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { IonLabel, IonTabBar, IonTabButton, IonTabs, IonTab, IonPage } from '@ionic/react';

const TabsFunctionChildren: React.FC = () => {
return (
<IonPage>
<IonTabs>
{
// The children type doesn't accept a function, but IonTabs still calls one with the tabs context.
(() => [
<IonTab tab="tab1" key="tab1">
<IonLabel>Tab 1 Content</IonLabel>
</IonTab>,
<IonTab tab="tab2" key="tab2">
<IonLabel>Tab 2 Content</IonLabel>
</IonTab>,
<IonTabBar slot="bottom" key="tab-bar">
<IonTabButton tab="tab1">
<IonLabel>Tab 1</IonLabel>
</IonTabButton>
<IonTabButton tab="tab2">
<IonLabel>Tab 2</IonLabel>
</IonTabButton>
</IonTabBar>,
]) as any
}
</IonTabs>
</IonPage>
);
};

export default TabsFunctionChildren;
56 changes: 53 additions & 3 deletions packages/react/test/base/tests/e2e/specs/tabs/tabs.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,61 @@ describe('IonTabs', () => {
cy.url().should('include', '/tabs-basic');
});

it('should fire tab change events on tab click', () => {
it('should fire tab change events once on tab click', () => {
cy.get('ion-tab-button[tab="tab2"]').click();

cy.get('@consoleLog').should('be.calledWith', 'onIonTabsWillChange', 'tab2');
cy.get('@consoleLog').should('be.calledWith', 'onIonTabsDidChange:', 'tab2');
cy.get('@consoleLog').should((consoleLog: any) => {
expect(consoleLog.withArgs('onIonTabsWillChange', 'tab2')).to.have.been.calledOnce;
expect(consoleLog.withArgs('onIonTabsDidChange:', 'tab2')).to.have.been.calledOnce;
});
});

// Issue: https://github.com/ionic-team/ionic-framework/issues/30037
it('should select a tab with selectTab from IonTabsContext', () => {
cy.get('#select-tab2').click();

cy.get('#select-result').should('have.text', 'true');
cy.get('ion-tab').eq(1).should('not.have.class', 'tab-hidden');
cy.get('ion-tab').eq(0).should('have.class', 'tab-hidden');
cy.get('ion-tab-button[tab="tab2"]').should('have.class', 'tab-selected');
cy.get('@consoleLog').should((consoleLog: any) => {
expect(consoleLog.withArgs('onIonTabsWillChange', 'tab2')).to.have.been.calledOnce;
expect(consoleLog.withArgs('onIonTabsDidChange:', 'tab2')).to.have.been.calledOnce;
});
});

it('should return false from selectTab for a tab that does not exist', () => {
cy.get('#select-missing-tab').click();

cy.get('#select-result').should('have.text', 'false');
cy.get('ion-tab').eq(0).should('not.have.class', 'tab-hidden');
});

it('should keep activeTab on IonTabsContext in sync with the selected tab', () => {
cy.get('#read-active-tab').click();
cy.get('#active-tab').should('have.text', 'tab1');

cy.get('ion-tab-button[tab="tab2"]').click();
cy.get('ion-tab').eq(1).should('not.have.class', 'tab-hidden');

cy.get('#read-active-tab').click();
cy.get('#active-tab').should('have.text', 'tab2');
});
});

describe('Function Children Without IonRouterOutlet', () => {
beforeEach(() => {
cy.visit('/tabs-function-children');
});

it('should render the tabs returned by the function', () => {
cy.get('ion-tab').eq(0).should('not.have.class', 'tab-hidden');
cy.get('ion-tab-button[tab="tab1"]').should('have.class', 'tab-selected');

cy.get('ion-tab-button[tab="tab2"]').click();

cy.get('ion-tab').eq(1).should('not.have.class', 'tab-hidden');
cy.get('ion-tab').eq(0).should('have.class', 'tab-hidden');
});
});
});
Loading