Skip to content

Commit

Permalink
fix(react): adding change events to iontabs, fixes #19665 (#19711)
Browse files Browse the repository at this point in the history
  • Loading branch information
elylucas committed Oct 21, 2019
1 parent ee21d3a commit b7baf24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 9 additions & 1 deletion packages/react/src/components/navigation/IonTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { IonTabBarInner } from '../inner-proxies';
import { IonTabButton } from '../proxies';

type Props = LocalJSX.IonTabBar & {
onIonTabsDidChange?: (event: CustomEvent<{ tab: string }>) => void;
onIonTabsWillChange?: (event: CustomEvent<{ tab: string }>) => void;
currentPath?: string;
slot?: 'bottom' | 'top';
};
Expand Down Expand Up @@ -72,9 +74,15 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
if (this.context.hasIonicRouter()) {
this.context.tabNavigate(originalHref);
} else {
this.context.navigate(originalHref, 'back');
this.context.navigate(originalHref, 'back');
}
} else {
if (this.props.onIonTabsWillChange) {
this.props.onIonTabsWillChange(new CustomEvent('ionTabWillChange', { detail: { tab: e.detail.tab } }));
}
if (this.props.onIonTabsDidChange) {
this.props.onIonTabsDidChange(new CustomEvent('ionTabDidChange', { detail: { tab: e.detail.tab } }));
}
this.context.navigate(this.state.tabs[e.detail.tab].currentHref, 'none');
}
}
Expand Down
16 changes: 7 additions & 9 deletions packages/react/src/components/navigation/IonTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { JSX as LocalJSX } from '@ionic/core';
import React from 'react';

import { NavContext } from '../../contexts/NavContext';
import { IonRouterOutlet } from '../IonRouterOutlet';

import { IonTabBar } from './IonTabBar';

interface Props {
interface Props extends LocalJSX.IonTabs {
children: React.ReactNode;
}

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

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

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

render() {
let outlet: React.ReactElement<{}> | undefined;
let tabBar: React.ReactElement<{ slot: 'bottom' | 'top' }> | undefined;
let tabBar: React.ReactElement | undefined;

React.Children.forEach(this.props.children, (child: any) => {
if (child == null || typeof child !== 'object' || !child.hasOwnProperty('type')) {
Expand All @@ -48,7 +49,8 @@ export const IonTabs = /*@__PURE__*/(() => class extends React.Component<Props>
outlet = child;
}
if (child.type === IonTabBar) {
tabBar = child;
const { onIonTabsDidChange, onIonTabsWillChange } = this.props;
tabBar = React.cloneElement(child, { onIonTabsDidChange, onIonTabsWillChange });
}
});

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

static get displayName() {
return 'IonTabs';
}

static get contextType() {
return NavContext;
}
})();
}

0 comments on commit b7baf24

Please sign in to comment.