Skip to content

Commit f811a81

Browse files
committed
Full ES6 update, all examples functioning again - ready to merge
1 parent a012ce2 commit f811a81

10 files changed

+121
-128
lines changed

.babelrc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"stage": 0
2+
"stage": 0,
3+
"env": {
4+
"test": {
5+
"plugins": [
6+
"typecheck"
7+
]
8+
}
9+
}
310
}

TODO.md

-7
This file was deleted.

index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';
2-
module.exports = function() {
3-
throw new Error("Don't instantiate Resizable directly! Use require('react-resizable').Resizable");
1+
export default function() {
2+
throw new Error("Don't instantiate Resizable directly! Use require('react-resizable').Resizable or " +
3+
"import {Resizable} from 'react-resizable'.");
44
};
55

6-
module.exports.Resizable = require('./build/Resizable');
7-
module.exports.ResizableBox = require('./build/ResizableBox');
6+
export Resizable from './build/Resizable';
7+
export ResizableBox from './build/ResizableBox';

lib/Resizable.jsx

+55-47
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
1-
'use strict';
2-
var React = require('react');
3-
var DraggableCore = require('react-draggable').DraggableCore;
4-
var assign = require('object-assign');
5-
var cloneElement = require('./cloneElement');
1+
import {default as React, PropTypes} from 'react';
2+
import {DraggableCore} from 'react-draggable';
3+
import assign from 'object-assign';
4+
import cloneElement from './cloneElement';
65

7-
var Resizable = module.exports = React.createClass({
8-
displayName: 'Resizable',
6+
export default class Resizable extends React.Component {
7+
static propTypes = {
8+
//
9+
// Required Props
10+
//
911

10-
propTypes: {
1112
// Require that one and only one child be present.
12-
children: React.PropTypes.element.isRequired,
13-
// Functions
14-
onResizeStop: React.PropTypes.func,
15-
onResizeStart: React.PropTypes.func,
16-
onResize: React.PropTypes.func,
17-
18-
width: React.PropTypes.number.isRequired,
19-
height: React.PropTypes.number.isRequired,
13+
children: PropTypes.element.isRequired,
14+
15+
// Initial w/h
16+
width: PropTypes.number.isRequired,
17+
height: PropTypes.number.isRequired,
18+
19+
//
20+
// Optional props
21+
//
22+
2023
// If you change this, be sure to update your css
21-
handleSize: React.PropTypes.array,
22-
// These will be passed wholesale to react-draggable
23-
draggableOpts: React.PropTypes.object
24-
},
24+
handleSize: PropTypes.array,
2525

26-
getDefaultProps() {
27-
return {
28-
handleSize: [20, 20]
29-
};
30-
},
26+
// Min/max size
27+
minConstraints: PropTypes.arrayOf(PropTypes.number),
28+
maxConstraints: PropTypes.arrayOf(PropTypes.number),
3129

32-
getInitialState() {
33-
return {
34-
bounds: this.constraintsToBounds(),
35-
width: this.props.width,
36-
height: this.props.height
37-
};
38-
},
30+
// Callbacks
31+
onResizeStop: PropTypes.func,
32+
onResizeStart: PropTypes.func,
33+
onResize: PropTypes.func,
34+
35+
// These will be passed wholesale to react-draggable's DraggableCore
36+
draggableOpts: PropTypes.object
37+
};
38+
39+
static defaultProps = {
40+
handleSize: [20, 20]
41+
};
42+
43+
state = {
44+
bounds: this.constraintsToBounds(),
45+
width: this.props.width,
46+
height: this.props.height
47+
};
3948

4049
componentWillReceiveProps(props: Object) {
4150
if (!this.state.resizing) {
@@ -45,19 +54,19 @@ var Resizable = module.exports = React.createClass({
4554
bounds: this.constraintsToBounds()
4655
});
4756
}
48-
},
57+
}
4958

5059
constraintsToBounds() {
51-
var p = this.props;
52-
var mins = p.minConstraints || p.handleSize;
53-
var maxes = p.maxConstraints || [Infinity, Infinity];
60+
let p = this.props;
61+
let mins = p.minConstraints || p.handleSize;
62+
let maxes = p.maxConstraints || [Infinity, Infinity];
5463
return {
5564
left: mins[0] - p.width,
5665
top: mins[1] - p.height,
5766
right: maxes[0] - p.width,
5867
bottom: maxes[1] - p.height
5968
};
60-
},
69+
}
6170

6271
/**
6372
* Wrapper around drag events to provide more useful data.
@@ -66,23 +75,22 @@ var Resizable = module.exports = React.createClass({
6675
* @return {Function} Handler function.
6776
*/
6877
resizeHandler(handlerName: string) {
69-
var me = this;
70-
return function(e, {node, position}) {
71-
let width = me.state.width + position.deltaX, height = me.state.height + position.deltaY;
72-
me.props[handlerName] && me.props[handlerName](e, {node, size: {width, height}});
78+
return (e, {node, position}) => {
79+
let width = this.state.width + position.deltaX, height = this.state.height + position.deltaY;
80+
this.props[handlerName] && this.props[handlerName](e, {node, size: {width, height}});
7381

7482
if (handlerName === 'onResizeStart') {
75-
me.setState({resizing: true});
83+
this.setState({resizing: true});
7684
} else if (handlerName === 'onResizeStop') {
77-
me.setState({resizing: false});
85+
this.setState({resizing: false});
7886
} else {
79-
me.setState({width, height});
87+
this.setState({width, height});
8088
}
8189
};
82-
},
90+
}
8391

8492
render() {
85-
var p = this.props, s = this.state;
93+
let p = this.props;
8694

8795
// What we're doing here is getting the child of this element, and cloning it with this element's props.
8896
// We are then defining its children as:
@@ -104,4 +112,4 @@ var Resizable = module.exports = React.createClass({
104112
]
105113
}));
106114
}
107-
});
115+
}

lib/ResizableBox.jsx

+25-30
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1-
'use strict';
2-
var React = require('react');
3-
var PropTypes = React.PropTypes;
4-
var Resizable = require('./Resizable');
1+
import {default as React, PropTypes} from 'react';
2+
import Resizable from './Resizable';
53

6-
// An example use of Resizable.
74
type Size = {width: number, height: number};
85
type ResizeData = {element: Element, size: Size};
9-
var ResizableBox = module.exports = React.createClass({
10-
displayName: 'ResizableBox',
116

12-
propTypes: {
7+
// An example use of Resizable.
8+
export default class ResizableBox extends React.Component {
9+
static propTypes = {
1310
lockAspectRatio: PropTypes.bool,
1411
minConstraints: PropTypes.arrayOf(PropTypes.number),
1512
maxConstraints: PropTypes.arrayOf(PropTypes.number),
1613
height: PropTypes.number,
1714
width: PropTypes.number
18-
},
15+
};
1916

20-
getDefaultProps() {
21-
return {
22-
lockAspectRatio: false,
23-
handleSize: [20,20]
24-
};
25-
},
17+
static defaultProps = {
18+
lockAspectRatio: false,
19+
handleSize: [20,20]
20+
};
2621

27-
getInitialState() {
28-
return {
29-
width: this.props.width,
30-
height: this.props.height,
31-
aspectRatio: this.props.width / this.props.height
32-
};
33-
},
22+
state = {
23+
width: this.props.width,
24+
height: this.props.height,
25+
aspectRatio: this.props.width / this.props.height
26+
};
3427

35-
onResize(event: Event, {element, size}: ResizeData) {
36-
var {width, height} = size;
37-
var widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
28+
// TODO data is ResizeData type, but that doesn't work in babel-typecheck pre-babel6
29+
onResize = (event: Event, data: Object) => {
30+
let {element, size} = data;
31+
let {width, height} = size;
32+
let widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
3833
if (!widthChanged && !heightChanged) return;
3934

4035
[width, height] = this.runConstraints(width, height);
@@ -44,11 +39,11 @@ var ResizableBox = module.exports = React.createClass({
4439
this.props.onResize(event, {element, size: {width, height}});
4540
}
4641
});
47-
},
42+
}
4843

4944
// If you do this, be careful of constraints
5045
runConstraints(width: number, height: number) {
51-
var [min, max] = [this.props.minConstraints, this.props.maxConstraints];
46+
let [min, max] = [this.props.minConstraints, this.props.maxConstraints];
5247

5348
if (this.props.lockAspectRatio) {
5449
height = width / this.state.aspectRatio;
@@ -64,13 +59,13 @@ var ResizableBox = module.exports = React.createClass({
6459
height = Math.min(max[1], height);
6560
}
6661
return [width, height];
67-
},
62+
}
6863

6964
render() {
7065
// Basic wrapper around a Resizable instance.
7166
// If you use Resizable directly, you are responsible for updating the component
7267
// with a new width and height.
73-
var {handleSize, minConstraints, maxConstraints, ...props} = this.props;
68+
let {handleSize, minConstraints, maxConstraints, ...props} = this.props;
7469
return (
7570
<Resizable
7671
minConstraints={minConstraints}
@@ -89,4 +84,4 @@ var ResizableBox = module.exports = React.createClass({
8984
</Resizable>
9085
);
9186
}
92-
});
87+
}

lib/cloneElement.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
var assign = require('object-assign');
4-
var React = require('react');
1+
import assign from 'object-assign';
2+
import React from 'react';
53

64
// React.addons.cloneWithProps look-alike that merges style & className.
75
module.exports = function cloneElement(element: React.Component, props: Object) {

package.json

+9-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"build": "bash build.sh",
1010
"build-example": "webpack",
11-
"dev": "echo 'Open http://localhost:4002/examples/1.html'; webpack-dev-server --config webpack-dev-server.config.js --hot --progress --colors --port 4002 --content-base .",
11+
"dev": "echo 'Open http://localhost:4002/examples/1.html'; NODE_ENV=test webpack-dev-server --config webpack-dev-server.config.js --hot --progress --colors --port 4002 --content-base .",
1212
"prepublish": "npm run build",
1313
"validate": "npm ls"
1414
},
@@ -33,27 +33,26 @@
3333
"babel-eslint": "^4.1.3",
3434
"babel-loader": "^5.3.2",
3535
"babel-plugin-typecheck": "^1.3.0",
36-
"css-loader": "^0.21.0",
37-
"eslint": "^1.7.3",
38-
"eslint-plugin-react": "^3.6.3",
36+
"css-loader": "^0.23.0",
37+
"eslint": "^1.9.0",
38+
"eslint-plugin-react": "^3.8.0",
3939
"lodash": "^3.10.1",
4040
"pre-commit": "^1.1.2",
41-
"react": "^0.14.0",
42-
"react-hot-loader": "^1.3.0",
41+
"react": "^0.14.2",
42+
"react-dom": "^0.14.2",
4343
"style-loader": "^0.13.0",
44-
"webpack": "^1.12.2",
44+
"webpack": "^1.12.6",
4545
"webpack-dev-server": "^1.12.1"
4646
},
4747
"dependencies": {
4848
"object-assign": "^4.0.1",
49-
"react-draggable": "^1.0.0"
49+
"react-draggable": "^1.1.0"
5050
},
5151
"publishConfig": {
5252
"registry": "https://registry.npmjs.org"
5353
},
5454
"pre-commit": [
5555
"lint",
56-
"validate",
57-
"test"
56+
"validate"
5857
]
5958
}

test/TestLayout.jsx

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
'use strict';
2-
var React = require('react');
3-
var _ = require('lodash');
4-
var ResizableBox = require('../lib/ResizableBox.jsx');
5-
var Resizable = require('../lib/Resizable.jsx');
6-
require('style!css!../css/styles.css');
1+
import React from 'react';
2+
import Resizable from '../lib/Resizable';
3+
import ResizableBox from '../lib/ResizableBox';
4+
import 'style!css!../css/styles.css';
75

8-
var TestLayout = module.exports = React.createClass({
9-
displayName: 'TestLayout',
6+
export default class TestLayout extends React.Component {
7+
state = {width: 200, height: 200};
108

11-
getInitialState() {
12-
return {width: 200, height: 200};
13-
},
14-
15-
onClick() {
9+
onClick = () => {
1610
this.setState({width: 200, height: 200});
17-
},
11+
};
1812

19-
onResize(event, {element, size}) {
13+
onResize = (event, {element, size}) => {
2014
this.setState({width: size.width, height: size.height});
21-
},
15+
};
2216

2317
render() {
2418
return (
@@ -51,4 +45,4 @@ var TestLayout = module.exports = React.createClass({
5145
</div>
5246
);
5347
}
54-
});
48+
}

test/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
'use strict';
2-
var Layout = require('./TestLayout.jsx');
3-
var React = require('react');
4-
var ReactDOM = require('react-dom');
1+
import TestLayout from './TestLayout';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
55
document.addEventListener("DOMContentLoaded", function(event) {
66
var contentDiv = document.getElementById('content');
7-
ReactDOM.render(React.createElement(Layout), contentDiv);
7+
ReactDOM.render(React.createElement(TestLayout), contentDiv);
88
});

webpack-dev-server.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ module.exports = {
1212
},
1313
module: {
1414
loaders: [
15-
{test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader?stage=0'},
16-
{test: /\.jsx$/, exclude: /node_modules/, loader: 'react-hot-loader'}
15+
{test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader'}
1716
]
1817
},
1918
debug: true,

0 commit comments

Comments
 (0)