Skip to content
Merged
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
19 changes: 16 additions & 3 deletions src/components/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = React.createClass({
id: PropTypes.string,
selected: PropTypes.bool,
disabled: PropTypes.bool,
activeTabClassName: PropTypes.string,
disabledTabClassName: PropTypes.string,
panelId: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.array,
Expand All @@ -37,6 +39,8 @@ module.exports = React.createClass({
selected: false,
id: null,
panelId: null,
activeTabClassName: 'ReactTabs__Tab--selected',
disabledTabClassName: 'ReactTabs__Tab--disabled',
};
},

Expand All @@ -49,7 +53,16 @@ module.exports = React.createClass({
},

render() {
const { selected, disabled, panelId, className, children, id, ...attributes } = this.props;
const {
selected,
disabled,
panelId,
activeTabClassName,
disabledTabClassName,
className,
children,
id,
...attributes } = this.props;

return (
<li
Expand All @@ -58,8 +71,8 @@ module.exports = React.createClass({
'ReactTabs__Tab',
className,
{
'ReactTabs__Tab--selected': selected,
'ReactTabs__Tab--disabled': disabled,
[activeTabClassName]: selected,
[disabledTabClassName]: disabled,
}
)}
role="tab"
Expand Down
20 changes: 18 additions & 2 deletions src/components/TabList.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import React, { PropTypes } from 'react';
import cx from 'classnames';

function renderChildren(props) {
return React.Children.map(props.children, (child) =>
React.cloneElement(child, {
activeTabClassName: props.activeTabClassName,
disabledTabClassName: props.disabledTabClassName,
})
);
}

module.exports = React.createClass({
displayName: 'TabList',

propTypes: {
className: PropTypes.string,
activeTabClassName: PropTypes.string,
disabledTabClassName: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array,
]),
},

render() {
const { className, children, ...attributes } = this.props;
const {
className,
activeTabClassName,
disabledTabClassName,
children,
...attributes } = this.props;

return (
<ul
Expand All @@ -24,7 +40,7 @@ module.exports = React.createClass({
)}
role="tablist"
>
{children}
{renderChildren({ activeTabClassName, disabledTabClassName, children })}
</ul>
);
},
Expand Down
41 changes: 40 additions & 1 deletion src/components/__tests__/TabList-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* global jest, describe, it, expect */
import React from 'react';
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import Tab from '../Tab';
import TabList from '../TabList';
import Tabs from '../Tabs';

function hasClassAt(wrapper, index, className) {
return wrapper.childAt(index).find('li').hasClass(className);
}

describe('<TabList />', () => {
it('should have sane defaults', () => {
Expand Down Expand Up @@ -30,4 +36,37 @@ describe('<TabList />', () => {

expect(wrapper.prop('role')).toBe('tablist');
});

it('should retain the default classnames for active and disabled tab', () => {
const wrapper = mount(
<Tabs selectedIndex={0}>
<TabList>
<Tab>Foo</Tab>
<Tab disabled>Bar</Tab>
</TabList>
</Tabs>
);

const tabsList = wrapper.childAt(0);
expect(hasClassAt(tabsList, 0, 'ReactTabs__Tab--selected')).toBe(true);
expect(hasClassAt(tabsList, 1, 'ReactTabs__Tab--disabled')).toBe(true);
});

it('should display the custom classnames for active and disabled tab', () => {
const wrapper = mount(
<Tabs selectedIndex={0}>
<TabList activeTabClassName="active" disabledTabClassName="disabled">
<Tab>Foo</Tab>
<Tab disabled>Bar</Tab>
</TabList>
</Tabs>
);

const tabsList = wrapper.childAt(0);
expect(hasClassAt(tabsList, 0, 'ReactTabs__Tab--selected')).toBe(false);
expect(hasClassAt(tabsList, 1, 'ReactTabs__Tab--disabled')).toBe(false);

expect(hasClassAt(tabsList, 0, 'active')).toBe(true);
expect(hasClassAt(tabsList, 1, 'disabled')).toBe(true);
});
});