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

[WIP] Add a TabbedTable component #225

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/TabbedTable/Tab.jsx
Original file line number Diff line number Diff line change
@@ -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,
};
80 changes: 80 additions & 0 deletions src/TabbedTable/TabbedTable.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<a
className={classnames(cssClass.TAB_NAME, {
[cssClass.SELECTED_TAB_NAME]: i === selectedTabIndex,
})}
onClick={e => this._onSelect(e, i)}
key={tab.props.name}
>
{tab.props.name}
</a>
);
});

const selectedTab = children[selectedTabIndex];
const tableToRender = React.Children.only(selectedTab.props.children);
return (
<div>
<div className={cssClass.TAB_BAR}>
{title && <h2 className={cssClass.TITLE}>{title}</h2>}
<span className={cssClass.TABS}>
<label>view:</label>
{tabLinks}
</span>
</div>
{tableToRender}
</div>
);
}
}

TabbedTable.Tab = Tab;
34 changes: 34 additions & 0 deletions src/TabbedTable/TabbedTable.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@import (reference) "~clever-components/dist/less/index";

.TabbedTable--TabBar {
.margin--bottom--m;
}

.TabbedTable--Title {
display: inline-block;
border-right: @size_4xs solid @neutral_gray;
color: @neutral_black;
padding: @size_2xs @size_m;
.margin--right--m;
.text--large;
.text--semi-bold;
}

.TabbedTable--Tabs {
font-size: @size_s;
color: @neutral_dark_gray;
}

.TabbedTable--TabName {
.margin--left--s;
color: @neutral_dark_gray;
}

.TabbedTable--TabName:hover {
cursor: pointer;
}

.TabbedTable--SelectedTabName {
color: @primary_blue;
.text--semi-bold;
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ export {Number};

import Tooltip from "./Tooltip";
export {Tooltip};
export {TabbedTable} from "./TabbedTable/TabbedTable";
1 change: 1 addition & 0 deletions src/less/components.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
@import "../Icon/Icon";
@import "../LeftNav/LeftNav";
@import "../AlertBox/AlertBox";
@import "../TabbedTable/TabbedTable";
21 changes: 21 additions & 0 deletions test/TabbedTable_test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from "assert";
import React from "react";
import sinon from "sinon";
import {shallow} from "enzyme";
import {TabbedTable} from "../src";

describe("TabbedTable", () => {
it("renders (something)", () => {
/*
Write tests here that assert qualities about
TabbedTable's type, classes, properties,
and event handling.

Refer to enzyme's API and examples for Mocha:
http://airbnb.io/enzyme/

Refer to sinon's API for mocking event functions:
http://sinonjs.org/docs/
*/
});
});