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
8 changes: 4 additions & 4 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3309,16 +3309,16 @@ export namespace Components {
interface IonTabs {
"getRouteId": () => Promise<RouteID | undefined>;
/**
* Get the currently selected tab. This method is only available for vanilla JavaScript projects. The Angular, React, and Vue implementations of tabs are coupled to each framework's router.
* Get the currently selected tab. In React and Vue, this method only works when tabs are used without a router outlet.
*/
"getSelected": () => Promise<string | undefined>;
/**
* Get a specific tab by the value of its `tab` property or an element reference. This method is only available for vanilla JavaScript projects. The Angular, React, and Vue implementations of tabs are coupled to each framework's router.
* @param tab The tab instance to select. If passed a string, it should be the value of the tab's `tab` property.
* Get a specific tab by the value of its `tab` property or an element reference. In React and Vue, this method only works when tabs are used without a router outlet.
* @param tab The tab instance to get. If passed a string, it should be the value of the tab's `tab` property.
*/
"getTab": (tab: string | HTMLIonTabElement) => Promise<HTMLIonTabElement | undefined>;
/**
* Select a tab by the value of its `tab` property or an element reference. This method is only available for vanilla JavaScript projects. The Angular, React, and Vue implementations of tabs are coupled to each framework's router.
* Select a tab by the value of its `tab` property or an element reference. In React and Vue, this method only works when tabs are used without a router outlet. With a router outlet, tabs are coupled to the framework's router, so navigate with the router instead.
* @param tab The tab instance to select. If passed a string, it should be the value of the tab's `tab` property.
*/
"select": (tab: string | HTMLIonTabElement) => Promise<boolean>;
Expand Down
17 changes: 12 additions & 5 deletions core/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ export class Tabs implements NavOutlet {
}

/**
* Select a tab by the value of its `tab` property or an element reference. This method is only available for vanilla JavaScript projects. The Angular, React, and Vue implementations of tabs are coupled to each framework's router.
* Select a tab by the value of its `tab` property or an element reference.
*
* In React and Vue, this method only works when tabs are used without a router outlet. With a router outlet,
* tabs are coupled to the framework's router, so navigate with the router instead.
*
* @param tab The tab instance to select. If passed a string, it should be the value of the tab's `tab` property.
*/
Expand All @@ -113,17 +116,21 @@ export class Tabs implements NavOutlet {
}

/**
* Get a specific tab by the value of its `tab` property or an element reference. This method is only available for vanilla JavaScript projects. The Angular, React, and Vue implementations of tabs are coupled to each framework's router.
* Get a specific tab by the value of its `tab` property or an element reference.
*
* @param tab The tab instance to select. If passed a string, it should be the value of the tab's `tab` property.
* In React and Vue, this method only works when tabs are used without a router outlet.
*
* @param tab The tab instance to get. If passed a string, it should be the value of the tab's `tab` property.
*/
@Method()
async getTab(tab: string | HTMLIonTabElement): Promise<HTMLIonTabElement | undefined> {
return getTab(this.tabs, tab);
}

/**
* Get the currently selected tab. This method is only available for vanilla JavaScript projects. The Angular, React, and Vue implementations of tabs are coupled to each framework's router.
* Get the currently selected tab.
*
* In React and Vue, this method only works when tabs are used without a router outlet.
*/
@Method()
getSelected(): Promise<string | undefined> {
Expand Down Expand Up @@ -233,7 +240,7 @@ const getTab = (tabs: HTMLIonTabElement[], tab: string | HTMLIonTabElement): HTM
const tabEl = typeof tab === 'string' ? tabs.find((t) => t.tab === tab) : tab;

if (!tabEl) {
printIonError(`[ion-tabs] - Tab with id: "${tabEl}" does not exist`);
printIonError(`[ion-tabs] - Tab with id: "${tab}" does not exist`);
}
return tabEl;
};
27 changes: 27 additions & 0 deletions core/src/components/tabs/test/tabs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { newSpecPage } from '@stencil/core/testing';

import { Tab } from '../../tab/tab';
import { Tabs } from '../tabs';

describe('tabs: getTab', () => {
it('should name the requested tab when it does not exist', async () => {
const page = await newSpecPage({
components: [Tabs, Tab],
html: `
<ion-tabs>
<ion-tab tab="home">Home</ion-tab>
</ion-tabs>
`,
});

const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});

const tabs = page.body.querySelector('ion-tabs')!;
const tab = await tabs.getTab('settings');

expect(tab).toBeUndefined();
expect(consoleError).toHaveBeenCalledWith('[Ionic Error]: [ion-tabs] - Tab with id: "settings" does not exist');

consoleError.mockRestore();
});
});
Loading