Skip to content

Instantly share code, notes, and snippets.

@jschwertfeger
Created December 9, 2017 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschwertfeger/85c16c99f1abe7be5fe956046148647e to your computer and use it in GitHub Desktop.
Save jschwertfeger/85c16c99f1abe7be5fe956046148647e to your computer and use it in GitHub Desktop.
class NavTabs extends React.Component {
render() {
return (
<RoutedTabs selectors={[
{tab: 0, path: '/one/:id', exact: true}, // Matches /one/123 exactly
{tab: 1, path: '/one/:id/child', exact: true}, // Matches /one/123/child exactly
{tab: 2, path: '/two'}, // Matches /two/...
]}>
<Tab label="One" comonent={Link} to="/one/123"/>
<Tab label="One Child" comonent={Link} to="/one/123/child"/>
<Tab label="Two" comonent={Link} to="/two"/>
</RoutedTabs>
);
};
}
import React from 'react';
import PropTypes from 'prop-types';
import matchPath from 'react-router-dom/matchPath';
import Tabs from 'material-ui/Tabs';
class RoutedTabs extends React.Component {
render() {
const {
selectors,
children,
...others
} = this.props;
const location = this.props.location || this.context.router.route.location;
const match = selectors.find(selector => !!matchPath(location.pathname, {...selector}));
const selected = match === undefined ? false : match.tab;
return (
<Tabs value={selected} {...others}>
{children}
</Tabs>
);
}
}
RoutedTabs.propTypes = {
selectors: PropTypes.arrayOf(PropTypes.shape({
tab: PropTypes.any.isRequired,
path: PropTypes.string.isRequired,
})).isRequired,
location: PropTypes.object,
children: PropTypes.node.isRequired,
};
RoutedTabs.contextTypes = {
router: PropTypes.shape({
route: PropTypes.object.isRequired,
}),
};
export default RoutedTabs;
@mynameistechno
Copy link

👍

@AlekseiDanilov
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment