Skip to content

Commit

Permalink
refactor(tabs): use default value for navManager
Browse files Browse the repository at this point in the history
  • Loading branch information
thetaPC committed Sep 11, 2024
1 parent 641303c commit cdbab6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 9 additions & 10 deletions packages/vue/src/components/IonTabBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const IonTabBar = defineComponent({
};
},
updated() {
this.setupTabState(inject("navManager"));
this.setupTabState(inject("navManager", null));
},
methods: {
setupTabState(ionRouter: any) {
Expand Down Expand Up @@ -133,7 +133,7 @@ export const IonTabBar = defineComponent({
const tabKeys = Object.keys(tabs);
let activeTab = tabKeys.find((key) => {
const href = tabs[key].originalHref;
return currentRoute && currentRoute.pathname.startsWith(href);
return currentRoute?.pathname.startsWith(href);
});

/**
Expand Down Expand Up @@ -171,19 +171,19 @@ export const IonTabBar = defineComponent({
* land on /tabs/tab1/child instead of /tabs/tab1.
*/
if (
currentRoute &&
(activeTab !== prevActiveTab || prevHref !== currentRoute.pathname)
activeTab !== prevActiveTab ||
prevHref !== currentRoute?.pathname
) {
/**
* By default the search is `undefined` in Ionic Vue,
* but Vue Router can set the search to the empty string.
* We check for truthy here because empty string is falsy
* and currentRoute.search cannot ever be a boolean.
*/
const search = currentRoute.search ? `?${currentRoute.search}` : "";
const search = currentRoute?.search ? `?${currentRoute.search}` : "";
tabs[activeTab] = {
...tabs[activeTab],
currentHref: currentRoute.pathname + search,
currentHref: currentRoute?.pathname + search,
};
}

Expand All @@ -192,8 +192,7 @@ export const IonTabBar = defineComponent({
* set the previous tab back to its original href.
*/
if (
currentRoute &&
currentRoute.routerAction === "pop" &&
currentRoute?.routerAction === "pop" &&
activeTab !== prevActiveTab
) {
tabs[prevActiveTab] = {
Expand Down Expand Up @@ -230,7 +229,7 @@ export const IonTabBar = defineComponent({
if (activeChild) {
tabDidChange && this.$props._tabsWillChange(activeTab);

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

Expand All @@ -250,7 +249,7 @@ export const IonTabBar = defineComponent({
},
},
mounted() {
const ionRouter: any = inject("navManager");
const ionRouter: any = inject("navManager", null);

this.setupTabState(ionRouter);

Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/components/IonTabButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const IonTabButton = /*@__PURE__*/ defineComponent({
defineCustomElement();

// TODO(FW-2969): type
const ionRouter: any = inject("navManager");
const ionRouter: any = inject("navManager", null);
const onClick = (ev: Event) => {
if (ev.cancelable) {
ev.preventDefault();
Expand Down Expand Up @@ -72,7 +72,7 @@ export const IonTabButton = /*@__PURE__*/ defineComponent({
* should direct users back to the root
* of the tab.
*/
if (ionRouter) {
if (ionRouter !== null) {
if (prevActiveTab === tab) {
if (originalHref !== currentHref) {
ionRouter.resetTab(tab);
Expand Down

0 comments on commit cdbab6f

Please sign in to comment.