Created
April 8, 2017 11:39
-
-
Save gyver98/aba50231c963adfd7d072314d6a8e3f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import './TeslaCounter.css'; | |
class TeslaCounter extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
direction: '' | |
} | |
} | |
componentWillReceiveProps(nextProps) { | |
const { currentValue, initValues } = nextProps; | |
if (this.props.currentValue !== nextProps.currentValue) { | |
const direction = this.props.currentValue < nextProps.currentValue ? 'increase' : 'decrese'; | |
this.setState({ | |
direction | |
}); | |
return; | |
} | |
} | |
render() { | |
const { initValues, currentValue, increment, decrement } = this.props; | |
const { direction } = this.state; | |
const animationClass = direction==='increase'? 'flip-in-hor-top' : 'flip-in-hor-bottom'; | |
return ( | |
<div className="tesla-counter"> | |
<p className="tesla-counter__title">{initValues.title}</p> | |
<div className="tesla-counter__container cf"> | |
<div className="tesla-counter__item"> | |
<div className={`${animationClass}`}> | |
<p className="tesla-counter__number "> | |
{ currentValue } | |
<span>{ initValues.unit }</span> | |
</p> | |
</div> | |
<div className="tesla-counter__controls"> | |
<button | |
onClick={(e) => { | |
e.preventDefault(); | |
increment(currentValue)}} | |
disabled={currentValue >= initValues.max} | |
> | |
</button> | |
<button | |
onClick={(e) => { | |
e.preventDefault(); | |
decrement(currentValue)}} | |
disabled={currentValue <= initValues.min} | |
> | |
</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
) | |
} | |
}; | |
TeslaCounter.propTypes = { | |
currentValue: React.PropTypes.number, | |
increment: React.PropTypes.func, | |
decrement: React.PropTypes.func, | |
initValues: React.PropTypes.object | |
} | |
export default TeslaCounter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment