fix(react): fix regression on IonTabsContext not working properly (#21751)

This commit is contained in:
Ely Lucas
2020-07-14 13:26:04 -06:00
committed by GitHub
parent 88f1828bd8
commit 1351b2eafc
5 changed files with 129 additions and 2 deletions

View File

@ -66,10 +66,27 @@ class IonTabBarUnwrapped extends React.PureComponent<InternalProps, IonTabBarSta
this.onTabButtonClick = this.onTabButtonClick.bind(this);
this.renderTabButton = this.renderTabButton.bind(this);
this.setActiveTabOnContext = this.setActiveTabOnContext.bind(this);
this.selectTab = this.selectTab.bind(this);
}
setActiveTabOnContext = (_tab: string) => { };
selectTab(tab: string) {
const tabUrl = this.state.tabs[tab];
if (tabUrl) {
this.onTabButtonClick(new CustomEvent('ionTabButtonClick', {
detail: {
href: tabUrl.currentHref,
tab,
selected: tab === this.state.activeTab,
routeOptions: undefined
}
}));
return true;
}
return false;
}
static getDerivedStateFromProps(props: InternalProps, state: IonTabBarState) {
const tabs = { ...state.tabs };
const tabKeys = Object.keys(state.tabs);
@ -77,7 +94,7 @@ class IonTabBarUnwrapped extends React.PureComponent<InternalProps, IonTabBarSta
.find(key => {
const href = state.tabs[key].originalHref;
return props.routeInfo!.pathname.startsWith(href);
});
}) || tabKeys[0];
// Check to see if the tab button href has changed, and if so, update it in the tabs state
React.Children.forEach((props as any).children, (child: any) => {
@ -142,7 +159,7 @@ class IonTabBarUnwrapped extends React.PureComponent<InternalProps, IonTabBarSta
if (this.props.onIonTabsDidChange) {
this.props.onIonTabsDidChange(new CustomEvent('ionTabDidChange', { detail: { tab: e.detail.tab } }));
}
this.setActiveTabOnContext(e.detail.tab);
this.context.changeTab(e.detail.tab, currentHref, e.detail.routeOptions);
}
}