diff --git a/src/TabbedTable/Tab.jsx b/src/TabbedTable/Tab.jsx new file mode 100644 index 000000000..d1ee2707c --- /dev/null +++ b/src/TabbedTable/Tab.jsx @@ -0,0 +1,13 @@ +import * as PropTypes from "prop-types"; +import {Table} from "clever-components"; +import MorePropTypes from "clever-components/dist/utils/MorePropTypes"; + + +export default function Tab() { + throw new Error("Configuration component - not meant to be rendered."); +} + +Tab.propTypes = { + children: MorePropTypes.instanceOfComponent(Table), + name: PropTypes.string.isRequired, +}; diff --git a/src/TabbedTable/TabbedTable.jsx b/src/TabbedTable/TabbedTable.jsx new file mode 100644 index 000000000..4b42ec4d5 --- /dev/null +++ b/src/TabbedTable/TabbedTable.jsx @@ -0,0 +1,80 @@ +import * as PropTypes from "prop-types"; +import * as React from "react"; +import * as classnames from "classnames"; +import MorePropTypes from "clever-components/dist/utils/MorePropTypes"; + +import Tab from "./Tab"; + +import "./TabbedTable.less"; + + +export default class TabbedTable extends React.PureComponent { + static cssClass = { + TAB_BAR: "TabbedTable--TabBar", + TITLE: "TabbedTable--Title", + TABS: "TabbedTable--Tabs", + TAB_NAME: "TabbedTable--TabName", + SELECTED_TAB_NAME: "TabbedTable--SelectedTabName", + }; + + static propTypes = { + children: PropTypes.arrayOf(PropTypes.oneOfType([ + MorePropTypes.instanceOfComponent(Tab), + PropTypes.oneOf([null, false]), // allow for conditionally including tabs + ])).isRequired, + title: PropTypes.string, + }; + + constructor(props) { + super(props); + + this.state = { + selectedTabIndex: 0, + }; + } + + _onSelect(e, tabIndex) { + e.preventDefault(); + this.setState({selectedTabIndex: tabIndex}); + } + + render() { + const {cssClass} = TabbedTable; + const {children, title} = this.props; + const {selectedTabIndex} = this.state; + + const tabLinks = React.Children.map(children, (tab, i) => { + if (!tab) { + return null; + } + return ( + this._onSelect(e, i)} + key={tab.props.name} + > + {tab.props.name} + + ); + }); + + const selectedTab = children[selectedTabIndex]; + const tableToRender = React.Children.only(selectedTab.props.children); + return ( +