Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): setting active tab properly on mount, closes #21830 #21833

Merged
merged 4 commits into from Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-router/test-app/build.sh
Expand Up @@ -3,7 +3,7 @@ cd ../../react
npm run build
npm pack
cd ../react-router/test-app
npm i ../../react/ionic-react-5.2.3.tgz
npm i ../../react/ionic-react-5.3.0.tgz
npm run start


Expand Up @@ -241,6 +241,15 @@ describe('Navigation Tests', () => {
cy.ionPageVisible('settings-page')
})

it('/routing/tabs/redirect > Should be on settings page > Home Tab > Should be on home page', () => {
// tests that a redirect going to a tab other than the first tab works
// fixes bug https://github.com/ionic-team/ionic-framework/issues/21830
cy.visit(`http://localhost:${port}/routing/tabs/redirect`)
cy.ionPageVisible('settings-page')
cy.ionTabClick('Home')
cy.ionPageVisible('home-page')
})

/*
Tests to add:
Test that lifecycle events fire
Expand Down
21 changes: 13 additions & 8 deletions packages/react-router/test-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/react-router/test-app/src/pages/routing/Tabs.tsx
Expand Up @@ -12,7 +12,7 @@ interface TabsProps {
}

const Tabs: React.FC<TabsProps> = () => {

return (
<IonTabs>
<IonRouterOutlet id="tabs">
Expand All @@ -25,6 +25,7 @@ const Tabs: React.FC<TabsProps> = () => {
<Route path="/routing/tabs/settings/details/:id" component={SettingsDetails} exact={true} />
<Route path="/routing/tabs/tab3" component={Tab3} />
<Route path="/routing/tabs" render={() => <Redirect to="/routing/tabs/home" />} exact={true} />
<Route path="/routing/tabs/redirect" render={() => <Redirect to="/routing/tabs/settings" />} exact={true} />
{/* <Route path="/routing/tabs" render={() => <Route render={() => <Redirect to="/tabs/home" />} />} /> */}
</IonRouterOutlet>
<IonTabBar slot="bottom">
Expand All @@ -45,4 +46,4 @@ const Tabs: React.FC<TabsProps> = () => {
);
};

export default Tabs;
export default Tabs;
35 changes: 25 additions & 10 deletions packages/react/src/components/navigation/IonTabBar.tsx
Expand Up @@ -30,7 +30,7 @@ interface TabUrls {
}

interface IonTabBarState {
activeTab: string | undefined;
activeTab?: string;
tabs: { [key: string]: TabUrls; };
}

Expand All @@ -51,15 +51,8 @@ class IonTabBarUnwrapped extends React.PureComponent<InternalProps, IonTabBarSta
}
});

const tabKeys = Object.keys(tabs);
const activeTab = tabKeys
.find(key => {
const href = tabs[key].originalHref;
return props.routeInfo!.pathname.startsWith(href);
}) || tabKeys[0];

this.state = {
activeTab,
// activeTab,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed thanks

tabs
};

Expand All @@ -69,6 +62,28 @@ class IonTabBarUnwrapped extends React.PureComponent<InternalProps, IonTabBarSta
this.selectTab = this.selectTab.bind(this);
}

componentDidMount() {
const tabs = this.state.tabs;
const tabKeys = Object.keys(tabs);
const activeTab = tabKeys
.find(key => {
const href = tabs[key].originalHref;
return this.props.routeInfo!.pathname.startsWith(href);
});

if (activeTab) {
this.setState({
activeTab
});
}
}

componentDidUpdate() {
if (this.state.activeTab) {
this.setActiveTabOnContext(this.state.activeTab);
}
}

setActiveTabOnContext = (_tab: string) => { };

selectTab(tab: string) {
Expand All @@ -94,7 +109,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) => {
Expand Down