Skip to content

Commit 0c168bd

Browse files
committed
add forceRenderTabPanel prop to Tab
Setting `forceRenderTabPanel` to true will override the default behavior of only rendering the selected tab's contents.
1 parent efaec3b commit 0c168bd

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@ var App = React.createClass({
2727
handleSelect: function (index, last) {
2828
console.log('Selected tab: ' + index + ', Last tab: ' + last);
2929
},
30-
30+
3131
render: function () {
3232
return (
3333
{/*
3434
<Tabs/> is a composite component and acts as the main container.
35-
35+
3636
`onSelect` is called whenever a tab is selected. The handler for
3737
this function will be passed the current index as well as the last index.
38-
38+
3939
`selectedIndex` is the tab to select when first rendered. By default
4040
the first (index 0) tab will be selected.
41+
42+
`forceRenderTabPanel` By default this react-tabs will only render the selected
43+
tab's contents. Setting `forceRenderTabPanel` to `true` allows you to override the
44+
default behavior, which may be useful in some circumstances (such as animating between tabs).
45+
4146
*/}
42-
47+
4348
<Tabs
4449
onSelect={this.handleSelected}
4550
selectedIndex={2}
@@ -58,7 +63,7 @@ var App = React.createClass({
5863
or by using the keyboard tab to give focus then navigating with
5964
the arrow keys (right/down to select tab to the right of selected,
6065
left/up to select tab to the left of selected).
61-
66+
6267
The content of the <Tab/> (this.props.children) will be shown as the label.
6368
*/}
6469

lib/components/TabPanel.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = React.createClass({
1414
])
1515
},
1616

17+
contextTypes: {
18+
forceRenderTabPanel: PropTypes.bool
19+
},
20+
1721
getDefaultProps() {
1822
return {
1923
selected: false,
@@ -23,7 +27,9 @@ module.exports = React.createClass({
2327
},
2428

2529
render() {
26-
const children = this.props.selected ? this.props.children : null;
30+
const children = (this.context.forceRenderTabPanel || this.props.selected) ?
31+
this.props.children :
32+
null;
2733

2834
return (
2935
<div

lib/components/Tabs.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,32 @@ module.exports = React.createClass({
2121
selectedIndex: PropTypes.number,
2222
onSelect: PropTypes.func,
2323
focus: PropTypes.bool,
24-
children: childrenPropType
24+
children: childrenPropType,
25+
forceRenderTabPanel: PropTypes.bool
2526
},
2627

28+
childContextTypes: {
29+
forceRenderTabPanel: PropTypes.bool
30+
},
31+
2732
getDefaultProps() {
2833
return {
2934
selectedIndex: -1,
30-
focus: false
35+
focus: false,
36+
forceRenderTabPanel: false
3137
};
3238
},
3339

3440
getInitialState() {
3541
return this.copyPropsToState(this.props);
3642
},
3743

44+
getChildContext() {
45+
return {
46+
forceRenderTabPanel: this.props.forceRenderTabPanel
47+
};
48+
},
49+
3850
componentWillMount() {
3951
jss(require('../helpers/styles.js'));
4052
},

lib/components/__tests__/Tabs-test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { ok, equal } from 'assert';
44
const TestUtils = React.addons.TestUtils;
55

66
function createTabs(props = {}) {
7-
props.selectedIndex = props.selectedIndex || 0;
8-
props.focus = props.focus || false;
9-
props.onSelect = props.onSelect || null;
7+
const {selectedIndex=0, focus=false, onSelect=null, forceRenderTabPanel=false} = props;
108

119
return (
12-
<Tabs focus={props.focus} selectedIndex={props.selectedIndex} onSelect={props.onSelect}>
10+
<Tabs focus={focus} selectedIndex={selectedIndex} onSelect={onSelect} forceRenderTabPanel={forceRenderTabPanel}>
1311
<TabList>
1412
<Tab>Foo</Tab>
1513
<Tab>Bar</Tab>
@@ -152,6 +150,13 @@ describe('react-tabs', function() {
152150
equal(tabs.getPanel(1).getDOMNode().innerHTML, '');
153151
equal(tabs.getPanel(2).getDOMNode().innerHTML, 'Hello Baz');
154152
});
153+
154+
it('should render all tabs if forceRenderTabPanel is true', function() {
155+
const tabs = TestUtils.renderIntoDocument(createTabs({forceRenderTabPanel: true}));
156+
equal(tabs.getPanel(0).getDOMNode().innerHTML, 'Hello Foo');
157+
equal(tabs.getPanel(1).getDOMNode().innerHTML, 'Hello Bar');
158+
equal(tabs.getPanel(2).getDOMNode().innerHTML, 'Hello Baz');
159+
});
155160
});
156161

157162
describe('validation', function() {

0 commit comments

Comments
 (0)