Skip to content

Commit 834666c

Browse files
elylucasliamdebeasi
authored andcommitted
fix(react): adding change events to iontabs, fixes #19665 (#19711)
1 parent 5acb58a commit 834666c

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

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

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

88
type Props = LocalJSX.IonTabBar & {
9+
onIonTabsDidChange?: (event: CustomEvent<{ tab: string }>) => void;
10+
onIonTabsWillChange?: (event: CustomEvent<{ tab: string }>) => void;
911
currentPath?: string;
1012
slot?: 'bottom' | 'top';
1113
};
@@ -72,9 +74,15 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
7274
if (this.context.hasIonicRouter()) {
7375
this.context.tabNavigate(originalHref);
7476
} else {
75-
this.context.navigate(originalHref, 'back');
77+
this.context.navigate(originalHref, 'back');
7678
}
7779
} else {
80+
if (this.props.onIonTabsWillChange) {
81+
this.props.onIonTabsWillChange(new CustomEvent('ionTabWillChange', { detail: { tab: e.detail.tab } }));
82+
}
83+
if (this.props.onIonTabsDidChange) {
84+
this.props.onIonTabsDidChange(new CustomEvent('ionTabDidChange', { detail: { tab: e.detail.tab } }));
85+
}
7886
this.context.navigate(this.state.tabs[e.detail.tab].currentHref, 'none');
7987
}
8088
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { JSX as LocalJSX } from '@ionic/core';
12
import React from 'react';
23

34
import { NavContext } from '../../contexts/NavContext';
45
import { IonRouterOutlet } from '../IonRouterOutlet';
56

67
import { IonTabBar } from './IonTabBar';
78

8-
interface Props {
9+
interface Props extends LocalJSX.IonTabs {
910
children: React.ReactNode;
1011
}
1112

@@ -28,7 +29,7 @@ const tabsInner: React.CSSProperties = {
2829
contain: 'layout size style'
2930
};
3031

31-
export const IonTabs = /*@__PURE__*/(() => class extends React.Component<Props> {
32+
export class IonTabs extends React.Component<Props> {
3233
context!: React.ContextType<typeof NavContext>;
3334
routerOutletRef: React.Ref<HTMLIonRouterOutletElement> = React.createRef();
3435

@@ -38,7 +39,7 @@ export const IonTabs = /*@__PURE__*/(() => class extends React.Component<Props>
3839

3940
render() {
4041
let outlet: React.ReactElement<{}> | undefined;
41-
let tabBar: React.ReactElement<{ slot: 'bottom' | 'top' }> | undefined;
42+
let tabBar: React.ReactElement | undefined;
4243

4344
React.Children.forEach(this.props.children, (child: any) => {
4445
if (child == null || typeof child !== 'object' || !child.hasOwnProperty('type')) {
@@ -48,7 +49,8 @@ export const IonTabs = /*@__PURE__*/(() => class extends React.Component<Props>
4849
outlet = child;
4950
}
5051
if (child.type === IonTabBar) {
51-
tabBar = child;
52+
const { onIonTabsDidChange, onIonTabsWillChange } = this.props;
53+
tabBar = React.cloneElement(child, { onIonTabsDidChange, onIonTabsWillChange });
5254
}
5355
});
5456

@@ -71,11 +73,7 @@ export const IonTabs = /*@__PURE__*/(() => class extends React.Component<Props>
7173
);
7274
}
7375

74-
static get displayName() {
75-
return 'IonTabs';
76-
}
77-
7876
static get contextType() {
7977
return NavContext;
8078
}
81-
})();
79+
}

0 commit comments

Comments
 (0)