Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active August 29, 2015 14:13
Show Gist options
  • Save ishiduca/25d27f0f088c7cc6abc4 to your computer and use it in GitHub Desktop.
Save ishiduca/25d27f0f088c7cc6abc4 to your computer and use it in GitHub Desktop.
React.jsでRateする
<!doctype html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css" />
<style>
* {
margin : 0;
padding : 0;
}
main: {
display : block;
text-align : center;
}
.star-box {
margin: 24px;
width : 600px;
text-align : center;
}
.star-box-count,
.star-box-stars{
display: inline-block;
margin: 0 12px;
}
.star,
.trash {
display: inline-block;
margin: 0 3px;
padding: 0 3px;
color: #77ff33;
border-radius : 18pt;
font-size: 40pt;
}
.star-focus-on {
background-color : #ffffaa;
}
</style>
<script src="http://fb.me/react-0.12.2.js"></script>
<script src="http://fb.me/JSXTransformer-0.12.2.js"></script>
</head>
<body>
<main id="main"></main>
<script type="text/jsx">
'use strict'
var Count = React.createClass({
render: function () {
return (
<div className="star-box-count">{this.props.count}</div>
)
}
})
var Star = React.createClass({
render: function () {
var rateFlg = this.props.rate > this.props.val
var focusFlg = this.props.isStarFocus
return (
<i className={
"fa " + (rateFlg ? "fa-star" : "fa-star-o") +
" star star-focus-" + (focusFlg ? 'on' : 'off')
}
onClick={this.handleClick}
onMouseOver={this.handleFocus}
onMouseOut={this.handleBlur}
></i>
)
}
, handleClick: function () {
this.props.onStarClick(this.props.val)
}
, handleFocus: function () {
this.props.onStarFocus(this.props.val)
}
, handleBlur: function () {
this.props.onStarFocus(null)
}
})
var Stars = React.createClass({
render: function () {
return (
<div className="star-box-stars">{this.createStars()}</div>
)
}
, getInitialState: function () {
return {focusRange: null}
}
, createStars: function () {
var stars = [], i = 0, max = this.props.max
var focusRange = this.state.focusRange
for (; i < max; i++) {
stars[i] = (
<Star
key={"id-" + i}
rate={this.props.rate}
val={i}
isStarFocus={null !== focusRange && focusRange >= i ? true : false}
onStarClick={this.handleStarClick}
onStarFocus={this.handleStarFocus}
/>
)
}
return stars
}
, handleStarClick: function (n) {
this.props.onStarsClick(n)
}
, handleStarFocus: function (n) {
this.setState({focusRange: n})
}
})
var RemoveStar = React.createClass({
render: function () {
return (
<i className="fa fa-trash-o trash"
onClick={this.handleClick}
></i>
)
}
, handleClick: function (ev) {
ev.preventDefault()
this.props.onRemoveStarClick()
}
})
var StarBox = React.createClass({
render: function () {
return (
<div className="star-box">
<Count count={this.state.rate} />
<Stars rate={this.state.rate} max={this.state.max} onStarsClick={this.handleStarsClick} />
<RemoveStar onRemoveStarClick={this.handleRemoveStar} />
</div>
)
}
, getInitialState: function () {
var rate = this.props.rate || 0
return {
max: 5
, rate: this.props.rate || 0
}
}
, handleRemoveStar: function () {
this.setState({rate: 0})
}
, handleStarsClick: function (n) {
this.setState({rate: n + 1})
}
})
var rate = 3
React.render(
<StarBox rate={rate} />
, document.querySelector('#main')
)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment