|
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) { |
33 | 13 | 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; |
37 | 17 | } |
38 | 18 |
|
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 | + } |
42 | 27 |
|
43 | | - componentDidMount: function componentDidMount() { |
44 | | - this._create(this.props); |
45 | | - }, |
| 28 | + setProgress(progress) { |
| 29 | + this.shape.set(progress); |
| 30 | + } |
46 | 31 |
|
47 | | - componentWillUnmount: function componentWillUnmount() { |
48 | | - this._destroy() |
49 | | - }, |
| 32 | + setText(text) { |
| 33 | + if (text) { |
| 34 | + this.shape.setText(text); |
| 35 | + } |
| 36 | + } |
50 | 37 |
|
51 | | - _create: function _create(props, oldProps) { |
52 | | - if (this.state.shape !== null) { |
| 38 | + create(nextProps, oldProps) { |
| 39 | + if (this.shape !== null) { |
53 | 40 | throw new Error('Progressbar is already created'); |
54 | 41 | } |
55 | 42 |
|
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, |
62 | 46 | ); |
63 | 47 |
|
64 | | - if (props.initialAnimate) { |
| 48 | + if (nextProps.initialAnimate) { |
65 | 49 | if (oldProps) { |
66 | | - this._setProgress(oldProps.progress); |
| 50 | + this.setProgress(oldProps.progress); |
67 | 51 | } |
68 | 52 |
|
69 | | - this._animateProgress(props.progress); |
| 53 | + this.animateProgress(nextProps.progress); |
70 | 54 | } 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); |
81 | 56 | } |
82 | | - }, |
83 | 57 |
|
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); |
96 | 59 | } |
97 | | -}); |
98 | 60 |
|
99 | | -var Line = React.createClass({ |
100 | | - render() { |
101 | | - return <Shape {...this.props} ShapeClass={ProgressBar.Line} />; |
| 61 | + animateProgress(progress) { |
| 62 | + this.shape.animate(progress); |
102 | 63 | } |
103 | | -}); |
104 | 64 |
|
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 | + } |
108 | 70 | } |
109 | | -}); |
110 | 71 |
|
111 | | -var SemiCircle = React.createClass({ |
112 | 72 | 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 | + ); |
114 | 84 | } |
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 | +}; |
116 | 96 |
|
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, |
121 | 109 | }; |
| 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