Skip to content

Commit 8f0aa96

Browse files
committed
Merge pull request #7 from jayfunk/improvements
Improvements
2 parents 6044bd8 + 727accd commit 8f0aa96

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
npm-debug.log
3+
.idea

karma.conf.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = function (config) {
2+
config.set({
3+
frameworks: [
4+
'mocha',
5+
'chai',
6+
'sinon',
7+
'browserify'
8+
],
9+
10+
files: [
11+
'test/**/*.js'
12+
],
13+
14+
browserify: {
15+
debug: true
16+
},
17+
18+
preprocessors: {
19+
'test/**/*.js': ['browserify']
20+
},
21+
22+
reporters: ['progress'],
23+
24+
browsers: ['Chrome'],
25+
26+
client: {
27+
mocha: {
28+
reporter: 'html'
29+
}
30+
}
31+
});
32+
};

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "React component to react on resize event using scroll trick",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "karma start --single-run"
88
},
99
"repository": {
1010
"type": "git",
@@ -23,7 +23,17 @@
2323
"object-assign": "^1.0.0"
2424
},
2525
"devDependencies": {
26-
"react": ">=0.12.0"
26+
"react": ">=0.12.0",
27+
"chai": "^2.2.0",
28+
"karma": "^0.12.31",
29+
"karma-browserify": "^4.1.2",
30+
"karma-chai": "^0.1.0",
31+
"karma-chrome-launcher": "^0.1.7",
32+
"karma-mocha": "^0.1.10",
33+
"karma-sinon": "^1.0.4",
34+
"mocha": "^2.2.4",
35+
"react": "^0.12.0",
36+
"sinon": "^1.14.1"
2737
},
2838
"peerDependencies": {
2939
"react": ">=0.12.0"

src/component.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ var React = require('react');
66
var objectAssign = require('object-assign');
77

88
var Resizeable = React.createClass({
9+
10+
lastDimensions: {
11+
width: null,
12+
height: null
13+
},
14+
915
propTypes: {
1016
triggersClass: React.PropTypes.string,
1117
expandClass: React.PropTypes.string,
1218
contractClass: React.PropTypes.string,
1319
embedCss: React.PropTypes.bool,
1420
onResize: React.PropTypes.func.isRequired
1521
},
22+
1623
getDefaultProps: function () {
1724
return {
1825
triggersClass: 'resize-triggers',
@@ -32,8 +39,13 @@ var Resizeable = React.createClass({
3239

3340
componentDidMount: function () {
3441
this.resetTriggers();
35-
setTimeout(this.resetTriggers, 1000);
42+
this.initialResetTriggersTimeout = setTimeout(this.resetTriggers, 1000);
3643
},
44+
45+
componentWillUnmount: function () {
46+
clearTimeout(this.initialResetTriggersTimeout);
47+
},
48+
3749
componentDidUpdate: function () {
3850
this.resetTriggers();
3951
},
@@ -51,28 +63,29 @@ var Resizeable = React.createClass({
5163
expand.scrollTop = expand.scrollHeight;
5264
},
5365

54-
checkTriggers: function () {
55-
var element = this.refs.resizable.getDOMNode();
56-
return element.offsetWidth != this.lastWidth || element.offsetHeight != this.lastHeight;
57-
},
58-
5966
onScroll: function () {
6067
if (this.r) this.cancelFrame(this.r);
6168
this.r = this.requestFrame(function () {
62-
if (!this.checkTriggers())
63-
return;
69+
var dimensions = this.getDimensions();
6470

65-
var el = this.refs.resizable.getDOMNode();
71+
if (this.haveDimensionsChanged(dimensions)) {
72+
this.lastDimensions = dimensions;
73+
this.props.onResize(dimensions);
74+
}
75+
}.bind(this));
76+
},
6677

67-
this.lastWidth = el.offsetWidth;
68-
this.lastHeight = el.offsetHeight;
78+
getDimensions: function () {
79+
var el = this.refs.resizable.getDOMNode();
6980

70-
this.props.onResize({
71-
width: this.lastWidth,
72-
height: this.lastHeight
73-
});
81+
return {
82+
width: el.offsetWidth,
83+
height: el.offsetHeight
84+
};
85+
},
7486

75-
}.bind(this));
87+
haveDimensionsChanged: function (dimensions) {
88+
return dimensions.width != this.lastDimensions.width || dimensions.height != this.lastDimensions.height;
7689
},
7790

7891
render: function() {

test/ResizableTest.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var React = require('react/addons');
2+
var Resizable = require('../src/component');
3+
var ReactTestUtils = require('react/lib/ReactTestUtils');
4+
5+
describe('Resizable', function () {
6+
7+
it('should mount', function () {
8+
var root = ReactTestUtils.renderIntoDocument(React.createElement(Resizable, {
9+
onResize: function () {}
10+
}));
11+
12+
root.should.exist;
13+
});
14+
15+
it('should clean up timeouts on unmount', function () {
16+
var clock = sinon.useFakeTimers();
17+
var container = document.createElement('div');
18+
19+
React.render(React.createElement(Resizable, {
20+
onResize: function () {}
21+
}), container);
22+
23+
React.unmountComponentAtNode(container);
24+
25+
clock.tick(1000);
26+
clock.restore();
27+
});
28+
29+
it('should have default value for last dimensions when first rendered', function (done) {
30+
var node = ReactTestUtils.renderIntoDocument(React.createElement(Resizable, {
31+
onResize: function () {
32+
done();
33+
}
34+
}));
35+
36+
ReactTestUtils.Simulate.scroll(node.refs.resizable);
37+
});
38+
39+
});

0 commit comments

Comments
 (0)