Skip to content

Instantly share code, notes, and snippets.

@mariodev12
Created September 15, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariodev12/e549817408df70b0c0613fadf4883ca3 to your computer and use it in GitHub Desktop.
Save mariodev12/e549817408df70b0c0613fadf4883ca3 to your computer and use it in GitHub Desktop.
import React, {Component} from 'react'
class Star extends Component {
render(){
var s = 'fa fa-star';
if(!this.props.selected){
s+='-o'
}
return (
<i {...this.props} className={s}></i>
)
}
}
class Rating extends Component {
constructor(props){
super(props)
this.state = {
rating: 0,
hover: null
}
}
handleOver(id){
this.setState({
hover: id + 1
})
}
handleOut(id){
this.setState({
hover: null
})
}
handleClick(id){
this.setState({
rating: id + 1
})
}
render(){
var lengthStars = 5;
var stars = [];
for (var i = 0; i < lengthStars; i++) {
var r = this.state.hover != null ? this.state.hover : this.state.rating;
var selected = (i < r);
stars.push(
<Star key={i} selected={selected}
onMouseOver={this.handleOver.bind(this, i)}
onMouseOut={this.handleOut.bind(this, i)}
onClick={this.handleClick.bind(this, i)}
/>
)
}
return (<div>
{stars}
</div>)
}
}
export default Rating
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment