Created
December 9, 2017 10:53
-
-
Save jschwertfeger/85c16c99f1abe7be5fe956046148647e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍