Skip to content

Commit 75708bf

Browse files
committed
Merge pull request react-bootstrap#261 from jquense/fade-mixin-root-element-fix
fade does include the root element
2 parents c7a2cbf + d0ca680 commit 75708bf

2 files changed

Lines changed: 85 additions & 7 deletions

File tree

src/FadeMixin.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
var React = require('react');
2-
1+
/*global document */
32
// TODO: listen for onTransitionEnd to remove el
3+
function getElementsAndSelf (root, classes){
4+
var els = root.querySelectorAll('.' + classes.join('.'));
5+
6+
els = [].map.call(els, function(e){ return e; });
7+
8+
for(var i = 0; i < classes.length; i++){
9+
if( !root.className.match(new RegExp('\\b' + classes[i] + '\\b'))){
10+
return els;
11+
}
12+
}
13+
els.unshift(root);
14+
return els;
15+
}
16+
417
module.exports = {
518
_fadeIn: function () {
619
var els;
720

821
if (this.isMounted()) {
9-
els = this.getDOMNode().querySelectorAll('.fade');
22+
els = getElementsAndSelf(this.getDOMNode(), ['fade']);
23+
1024
if (els.length) {
11-
Array.prototype.forEach.call(els, function (el) {
25+
els.forEach(function (el) {
1226
el.className += ' in';
1327
});
1428
}
1529
}
1630
},
1731

1832
_fadeOut: function () {
19-
var els = this._fadeOutEl.querySelectorAll('.fade.in');
33+
var els = getElementsAndSelf(this._fadeOutEl, ['fade', 'in']);
2034

2135
if (els.length) {
22-
Array.prototype.forEach.call(els, function (el) {
36+
els.forEach(function (el) {
2337
el.className = el.className.replace(/\bin\b/, '');
2438
});
2539
}
@@ -41,8 +55,9 @@ module.exports = {
4155
},
4256

4357
componentWillUnmount: function () {
44-
var els = this.getDOMNode().querySelectorAll('.fade'),
58+
var els = getElementsAndSelf(this.getDOMNode(), ['fade']),
4559
container = (this.props.container && this.props.container.getDOMNode()) || document.body;
60+
4661
if (els.length) {
4762
this._fadeOutEl = document.createElement('div');
4863
container.appendChild(this._fadeOutEl);

test/FadeMixinSpec.jsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/** @jsx React.DOM */
2+
/*global describe, beforeEach, afterEach, it, assert */
3+
4+
var React = require('react');
5+
var ReactTestUtils = require('react/lib/ReactTestUtils');
6+
var FadeMixin = require('../cjs/FadeMixin');
7+
8+
var Component;
9+
10+
describe('FadeMixin', function () {
11+
beforeEach(function() {
12+
Component = React.createClass({
13+
mixins: [ FadeMixin ],
14+
15+
render: function () {
16+
return this.transferPropsTo(
17+
React.DOM.div({ className: 'fade' },
18+
React.DOM.span({ className: 'fade' })
19+
)
20+
);
21+
}
22+
});
23+
});
24+
25+
it('Should add the in class to all elements', function (done) {
26+
var instance = ReactTestUtils.renderIntoDocument(
27+
Component()
28+
);
29+
30+
var child = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'span')
31+
32+
setTimeout(function(){
33+
assert.ok(instance.getDOMNode().className.match(/\bin\b/));
34+
assert.ok(instance.getDOMNode().className.match(/\bfade\b/));
35+
assert.ok(child.getDOMNode().className.match(/\bin\b/));
36+
assert.ok(child.getDOMNode().className.match(/\bfade\b/));
37+
done()
38+
}, 25)
39+
});
40+
41+
it('Should remove the in class for all elements', function (done) {
42+
var instance = ReactTestUtils.renderIntoDocument(
43+
Component()
44+
);
45+
46+
setTimeout(function(){
47+
instance.componentWillUnmount()
48+
var element = instance._fadeOutEl.children[0]
49+
var child = element.children[0]
50+
51+
assert.ok(element.className.match(/\bin\b/));
52+
assert.ok(child.className.match(/\bin\b/));
53+
54+
setTimeout(function(){
55+
assert.ok(!element.className.match(/\bin\b/));
56+
assert.ok(element.className.match(/\bfade\b/));
57+
assert.ok(!child.className.match(/\bin\b/));
58+
assert.ok(child.className.match(/\bfade\b/));
59+
done()
60+
}, 25)
61+
}, 25)
62+
});
63+
});

0 commit comments

Comments
 (0)