Skip to content

Commit d3d4b3f

Browse files
committed
fix(tab-bar): use as standalone in vue
1 parent bbcbf5c commit d3d4b3f

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

packages/vue/src/components/IonTabBar.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,31 @@ export const IonTabBar = defineComponent({
3838
/* eslint-disable @typescript-eslint/no-empty-function */
3939
_tabsWillChange: { type: Function, default: () => {} },
4040
_tabsDidChange: { type: Function, default: () => {} },
41-
_hasRouterOutlet: { type: Boolean, default: false },
41+
/**
42+
* This prop is set by the `ion-tabs` component. If
43+
* the value is `undefined`, then the `ion-tab-bar`
44+
* component was not found within the slotted content.
45+
* Most likely, the tab bar was not passed as a direct
46+
* child of `ion-tabs`.
47+
*
48+
* A workaround will be used to determine if the tabs
49+
* are being used as a basic tab navigation or with
50+
* the router.
51+
*/
52+
_hasRouterOutlet: { type: Boolean, default: undefined },
4253
/* eslint-enable @typescript-eslint/no-empty-function */
4354
},
4455
data() {
4556
return {
4657
tabState: {
4758
activeTab: undefined,
4859
tabs: {},
60+
/**
61+
* Passing this prop to each tab button
62+
* lets it be aware of the presence of
63+
* the router outlet.
64+
*/
65+
hasRouterOutlet: false,
4966
},
5067
tabVnodes: [],
5168
};
@@ -55,7 +72,7 @@ export const IonTabBar = defineComponent({
5572
},
5673
methods: {
5774
setupTabState(ionRouter: any) {
58-
const hasRouterOutlet = this.$props._hasRouterOutlet;
75+
const hasRouterOutlet = this.$data.tabState.hasRouterOutlet;
5976
/**
6077
* For each tab, we need to keep track of its
6178
* base href as well as any child page that
@@ -75,13 +92,6 @@ export const IonTabBar = defineComponent({
7592
ref: child,
7693
};
7794

78-
/**
79-
* Passing this prop to each tab button
80-
* lets it be aware of the presence of
81-
* the router outlet.
82-
*/
83-
tabState.hasRouterOutlet = hasRouterOutlet;
84-
8595
/**
8696
* Passing this prop to each tab button
8797
* lets it be aware of the state that
@@ -126,7 +136,7 @@ export const IonTabBar = defineComponent({
126136
* @param ionRouter
127137
*/
128138
checkActiveTab(ionRouter: any) {
129-
const hasRouterOutlet = this.$props._hasRouterOutlet;
139+
const hasRouterOutlet = this.$data.tabState.hasRouterOutlet;
130140
const currentRoute = ionRouter?.getCurrentRouteInfo();
131141
const childNodes = this.$data.tabVnodes;
132142
const { tabs, activeTab: prevActiveTab } = this.$data.tabState;
@@ -216,7 +226,7 @@ export const IonTabBar = defineComponent({
216226
this.tabSwitch(activeTab);
217227
},
218228
tabSwitch(activeTab: string, ionRouter?: any) {
219-
const hasRouterOutlet = this.$props._hasRouterOutlet;
229+
const hasRouterOutlet = this.$data.tabState.hasRouterOutlet;
220230
const childNodes = this.$data.tabVnodes;
221231
const { activeTab: prevActiveTab } = this.$data.tabState;
222232
const tabState = this.$data.tabState;
@@ -250,6 +260,30 @@ export const IonTabBar = defineComponent({
250260
},
251261
mounted() {
252262
const ionRouter: any = inject("navManager", null);
263+
const hasRouterOutlet = this.$props._hasRouterOutlet;
264+
265+
/**
266+
* If `hasRouterOutlet` prop is not provided,
267+
* then the `ion-tab-bar` could not be found
268+
* within the slotted content of `ion-tabs`.
269+
* This means that the tab bar was not passed
270+
* as a direct child of `ion-tabs`.
271+
*
272+
* In this case, the router outlet needs to be
273+
* searched for within the `ion-tab-bar` component.
274+
* This is necessary to determine if the tabs
275+
* are being used as a basic tab navigation or
276+
* with the router.
277+
*/
278+
if (hasRouterOutlet === undefined) {
279+
const ionRouterOutlet = this.$refs.ionTabBar
280+
.closest("ion-tabs")
281+
?.querySelector("ion-router-outlet");
282+
283+
this.$data.tabState.hasRouterOutlet = !!ionRouterOutlet;
284+
} else {
285+
this.$data.tabState.hasRouterOutlet = hasRouterOutlet;
286+
}
253287

254288
this.setupTabState(ionRouter);
255289

0 commit comments

Comments
 (0)