Skip to content

Instantly share code, notes, and snippets.

@qstrahl
Last active April 3, 2017 20:58
Show Gist options
  • Save qstrahl/6d8bc13045365f7885b4c781f03b7a45 to your computer and use it in GitHub Desktop.
Save qstrahl/6d8bc13045365f7885b4c781f03b7a45 to your computer and use it in GitHub Desktop.
import React, { Children, PropTypes } from 'react';
import cssModules from 'react-css-modules';
import styles from './style.css';
import { omit } from 'lodash';
function makeLabel(child, index) {
const { selected } = this.state;
const onClick = () => this.setState({ selected: index });
const styleName = index === selected ? 'tab-label-selected' : 'tab-label';
return React.cloneElement(child.props.children[0], { onClick, styleName });
}
/**
* Represents a set of tabs, with one tab active at a time, each corresponding to a unique section of content.
*/
export class Tabs extends React.Component {
static propTypes = {
children: PropTypes.node,
};
state = { selected: 0 };
render() {
const passthroughProps = omit(this.props, ['styles']);
const { children } = this.props;
const { selected } = this.state;
const content = children[selected].props.children[1];
return (
<article styleName="wrapper" {...passthroughProps}>
<header styleName="tab-labels">{Children.map(children, makeLabel, this)}</header>
{content}
</article>
);
}
}
export default cssModules(Tabs, styles);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment