Skip to content

Commit 8776556

Browse files
authored
fix(react): handle tab back nav better, fixes #19646 (#19647)
1 parent ed98d9e commit 8776556

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { IonTabBarInner } from '../inner-proxies';
66
import { IonTabButton } from '../proxies';
77

88
type Props = LocalJSX.IonTabBar & {
9-
navigate?: (path: string, direction: 'back' | 'none') => void;
109
currentPath?: string;
1110
slot?: 'bottom' | 'top';
1211
};
@@ -22,6 +21,7 @@ interface State {
2221
}
2322

2423
const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Props, State> {
24+
context!: React.ContextType<typeof NavContext>;
2525

2626
constructor(props: Props) {
2727
super(props);
@@ -67,13 +67,15 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
6767
}
6868

6969
private onTabButtonClick = (e: CustomEvent<{ href: string, selected: boolean, tab: string }>) => {
70-
const { navigate } = this.props;
71-
if (navigate) {
72-
if (this.state.activeTab === e.detail.tab) {
73-
navigate(this.state.tabs[e.detail.tab].originalHref, 'back');
70+
if (this.state.activeTab === e.detail.tab) {
71+
const originalHref = this.state.tabs[e.detail.tab].originalHref;
72+
if (this.context.hasIonicRouter()) {
73+
this.context.goBack(originalHref);
7474
} else {
75-
navigate(this.state.tabs[e.detail.tab].currentHref, 'none');
75+
this.context.navigate(originalHref, 'back');
7676
}
77+
} else {
78+
this.context.navigate(this.state.tabs[e.detail.tab].currentHref, 'none');
7779
}
7880
}
7981

@@ -96,16 +98,17 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
9698
</IonTabBarInner>
9799
);
98100
}
101+
102+
static get contextType() {
103+
return NavContext;
104+
}
99105
})();
100106

101107
export const IonTabBar: React.FC<Props> = props => {
102108
const context = useContext(NavContext);
103109
return (
104110
<IonTabBarUnwrapped
105111
{...props as any}
106-
navigate={props.navigate || ((path: string, direction: 'back' | 'none') => {
107-
context.navigate(path, direction);
108-
})}
109112
currentPath={props.currentPath || context.currentPath}
110113
>
111114
{props.children}

0 commit comments

Comments
 (0)