Skip to content

Commit a85c103

Browse files
committed
Support unmounting a TabPane when animation is false
1 parent e77bba7 commit a85c103

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/TabContent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let TabContent = React.createClass({
3030
]),
3131

3232
/**
33-
* Unmounts the content (remove it from the DOM) when animation is not `false` and it is faded out
33+
* Unmount the tab (remove it from the DOM) when it is no longer visible
3434
*/
3535
unmountOnExit: PropTypes.bool,
3636
},

src/TabPane.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ let TabPane = React.createClass({
6565
onExited: PropTypes.func,
6666

6767
/**
68-
* Unmounts the tab (remove it from the DOM) when animation is not `false` and it is faded out
68+
* Unmount the tab (remove it from the DOM) when it is no longer visible
6969
*/
7070
unmountOnExit: PropTypes.bool
7171
},
@@ -140,10 +140,10 @@ let TabPane = React.createClass({
140140
: context.animation;
141141
},
142142

143-
getUnmountOnExit(props = this.props, context = this.context) {
144-
context = this.getContext('$bs_tabcontent', context);
145-
return props.unmountOnExit != null
146-
? props.unmountOnExit
143+
getUnmountOnExit() {
144+
let context = this.getContext('$bs_tabcontent', this.context);
145+
return this.props.unmountOnExit != null
146+
? this.props.unmountOnExit
147147
: context.unmountOnExit;
148148
},
149149

@@ -159,6 +159,10 @@ let TabPane = React.createClass({
159159

160160
let Transition = this.getTransition();
161161

162+
if (!visible && !Transition && this.getUnmountOnExit()) {
163+
return null;
164+
}
165+
162166
let classes = {
163167
active: visible,
164168
[prefix({ bsClass }, 'pane')]: true

src/Tabs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const Tabs = React.createClass({
6565
onSelect: React.PropTypes.func,
6666

6767
/**
68-
* Unmount tabs (remove it from the DOM) when animation is not `false` and it is faded out
68+
* Unmount tabs (remove it from the DOM) when it is no longer visible
6969
*/
7070
unmountOnExit: React.PropTypes.bool,
7171

test/TabsSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,45 @@ describe('Tabs', () => {
155155
assert.equal(tabs.refs.tabs.context.$bs_tabcontainer.activeKey, 1);
156156
});
157157

158+
it('Should mount initial tab and no others when unmountOnExit is true and animation is false', () => {
159+
let tab1 = <span className="tab1">Tab 1</span>;
160+
let instance = ReactTestUtils.renderIntoDocument(
161+
<Tabs id='test' defaultActiveKey={1} animation={false} unmountOnExit={true}>
162+
<Tab title={tab1} eventKey={1}>Tab 1 content</Tab>
163+
<Tab title="Tab 2" eventKey={2}>Tab 2 content</Tab>
164+
<Tab title="Tab 3" eventKey={3}>Tab 3 content</Tab>
165+
</Tabs>
166+
);
167+
168+
let panes = ReactTestUtils.scryRenderedComponentsWithType(instance, TabPane);
169+
170+
expect(ReactDOM.findDOMNode(panes[0])).to.exist;
171+
expect(ReactDOM.findDOMNode(panes[1])).to.not.exist;
172+
expect(ReactDOM.findDOMNode(panes[2])).to.not.exist;
173+
});
174+
175+
it('Should mount the correct tab when selected and unmount the previous when unmountOnExit is true and animation is false', () => {
176+
let tab1 = <span className="tab1">Tab 1</span>;
177+
let instance = ReactTestUtils.renderIntoDocument(
178+
<Tabs id='test' defaultActiveKey={2} animation={false} unmountOnExit={true}>
179+
<Tab title={tab1} eventKey={1}>Tab 1 content</Tab>
180+
<Tab title="Tab 2" eventKey={2}>Tab 2 content</Tab>
181+
</Tabs>
182+
);
183+
184+
let tabs = instance.refs.inner;
185+
let panes = ReactTestUtils.scryRenderedComponentsWithType(instance, TabPane);
186+
187+
ReactTestUtils.Simulate.click(
188+
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'tab1')
189+
);
190+
191+
expect(ReactDOM.findDOMNode(panes[0])).to.exist;
192+
expect(ReactDOM.findDOMNode(panes[1])).to.not.exist;
193+
194+
assert.equal(tabs.refs.tabs.context.$bs_tabcontainer.activeKey, 1);
195+
});
196+
158197
it('Should treat active key of null as nothing selected', () => {
159198
const instance = ReactTestUtils.renderIntoDocument(
160199
<Tabs id='test' activeKey={null} onSelect={()=>{}}>

0 commit comments

Comments
 (0)