Skip to content

Commit b62a3e3

Browse files
committed
Fix for deprecation warnings, use es6 syntax.
1 parent 8b3c551 commit b62a3e3

File tree

2 files changed

+92
-97
lines changed

2 files changed

+92
-97
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
"dependencies": {
3232
"lodash.isequal": "^4.1.4",
3333
"progressbar.js": "^1.0.1",
34-
"react": "^15.0.1",
35-
"react-dom": "^15.0.1"
34+
"prop-types": "^15.5.10",
35+
"react": "^15.6.1",
36+
"react-dom": "^15.6.1"
3637
},
3738
"devDependencies": {
3839
"babel": "^6.5.2",
@@ -72,4 +73,4 @@
7273
"jscs": "jscs ./src ./test",
7374
"eslint": "eslint --ext .js ./src ./test"
7475
}
75-
}
76+
}

src/main.js

Lines changed: 88 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,115 @@
1-
var React = require('react');
2-
var ReactDom = require('react-dom');
3-
var isEqual = require('lodash.isequal');
4-
var ProgressBar = require('progressbar.js');
5-
6-
var Shape = React.createClass({
7-
getDefaultProps: function getDefaultProps() {
8-
return {
9-
ShapeClass: null,
10-
options: {},
11-
progress: 0,
12-
text: null,
13-
initialAnimate: false,
14-
containerStyle: {},
15-
containerClassName: '.progressbar-container'
16-
};
17-
},
18-
19-
getInitialState: function getInitialState() {
20-
return {
21-
shape: null
22-
};
23-
},
24-
25-
render: function render() {
26-
var style = this.props.containerStyle;
27-
var className = this.props.containerClassName;
28-
29-
return <div className={className} style={style} ref="progressBar"></div>;
30-
},
31-
32-
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import isEqual from 'lodash.isequal';
4+
import ProgressBar from 'progressbar.js';
5+
6+
export class Shape extends React.Component {
7+
componentDidMount() {
8+
this.shape = null;
9+
this.create(this.props);
10+
}
11+
12+
componentWillReceiveProps(nextProps) {
3313
if (!isEqual(this.props.options, nextProps.options)) {
34-
this._destroy();
35-
this._create(nextProps, this.props);
36-
return;
14+
this.destroy();
15+
this.create(nextProps, this.props);
16+
return false;
3717
}
3818

39-
this._animateProgress(nextProps.progress);
40-
this._setText(nextProps.text);
41-
},
19+
this.animateProgress(nextProps.progress);
20+
this.setText(nextProps.text);
21+
return true;
22+
}
23+
24+
componentWillUnmount() {
25+
this.destroy();
26+
}
4227

43-
componentDidMount: function componentDidMount() {
44-
this._create(this.props);
45-
},
28+
setProgress(progress) {
29+
this.shape.set(progress);
30+
}
4631

47-
componentWillUnmount: function componentWillUnmount() {
48-
this._destroy()
49-
},
32+
setText(text) {
33+
if (text) {
34+
this.shape.setText(text);
35+
}
36+
}
5037

51-
_create: function _create(props, oldProps) {
52-
if (this.state.shape !== null) {
38+
create(nextProps, oldProps) {
39+
if (this.shape !== null) {
5340
throw new Error('Progressbar is already created');
5441
}
5542

56-
// setState function is not used to prevent a new render cycle
57-
// This handling happens outside of React component's lifecycle
58-
var container = ReactDom.findDOMNode(this.refs.progressBar);
59-
this.state.shape = new props.ShapeClass(
60-
container,
61-
props.options
43+
this.shape = new nextProps.ShapeClass(
44+
this.progressBar,
45+
nextProps.options,
6246
);
6347

64-
if (props.initialAnimate) {
48+
if (nextProps.initialAnimate) {
6549
if (oldProps) {
66-
this._setProgress(oldProps.progress);
50+
this.setProgress(oldProps.progress);
6751
}
6852

69-
this._animateProgress(props.progress);
53+
this.animateProgress(nextProps.progress);
7054
} else {
71-
this._setProgress(props.progress);
72-
}
73-
74-
this._setText(props.text);
75-
},
76-
77-
_destroy: function _destroy() {
78-
if (this.state.shape) {
79-
this.state.shape.destroy();
80-
this.state.shape = null;
55+
this.setProgress(nextProps.progress);
8156
}
82-
},
8357

84-
_animateProgress: function _animateProgress(progress) {
85-
this.state.shape.animate(progress);
86-
},
87-
88-
_setProgress: function _setProgress(progress) {
89-
this.state.shape.set(progress);
90-
},
91-
92-
_setText: function _setText(text) {
93-
if (text) {
94-
this.state.shape.setText(text);
95-
}
58+
this.setText(nextProps.text);
9659
}
97-
});
9860

99-
var Line = React.createClass({
100-
render() {
101-
return <Shape {...this.props} ShapeClass={ProgressBar.Line} />;
61+
animateProgress(progress) {
62+
this.shape.animate(progress);
10263
}
103-
});
10464

105-
var Circle = React.createClass({
106-
render() {
107-
return <Shape {...this.props} ShapeClass={ProgressBar.Circle} />;
65+
destroy() {
66+
if (this.shape) {
67+
this.shape.destroy();
68+
this.shape = null;
69+
}
10870
}
109-
});
11071

111-
var SemiCircle = React.createClass({
11272
render() {
113-
return <Shape {...this.props} ShapeClass={ProgressBar.SemiCircle} />;
73+
const style = this.props.containerStyle;
74+
const className = this.props.containerClassName;
75+
return (
76+
<div
77+
className={className}
78+
style={style}
79+
ref={(node) => {
80+
this.progressBar = node;
81+
}}
82+
/>
83+
);
11484
}
115-
});
85+
}
86+
87+
Shape.defaultProps = {
88+
ShapeClass: null,
89+
options: {},
90+
progress: 0,
91+
text: null,
92+
initialAnimate: false,
93+
containerStyle: {},
94+
containerClassName: '.progressbar-container',
95+
};
11696

117-
module.exports = {
118-
Line: Line,
119-
Circle: Circle,
120-
SemiCircle: SemiCircle
97+
Shape.propTypes = {
98+
ShapeClass: PropTypes.oneOf([ProgressBar.Circle]),
99+
options: PropTypes.objectOf(PropTypes.oneOfType([
100+
PropTypes.number,
101+
PropTypes.string,
102+
PropTypes.object,
103+
])),
104+
progress: PropTypes.number,
105+
text: PropTypes.string,
106+
initialAnimate: PropTypes.bool,
107+
containerStyle: PropTypes.objectOf(PropTypes.string),
108+
containerClassName: PropTypes.string,
121109
};
110+
111+
export const Line = props => <Shape {...props} ShapeClass={ProgressBar.Line} />;
112+
113+
export const Circle = props => <Shape {...props} ShapeClass={ProgressBar.Circle} />;
114+
115+
export const SemiCircle = props => <Shape {...props} ShapeClass={ProgressBar.SemiCircle} />;

0 commit comments

Comments
 (0)