ghg-react
Version:
A library of React components for my own use.
101 lines (88 loc) • 3.71 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './Rating.css';
var Rating = function (_Component) {
_inherits(Rating, _Component);
function Rating(props) {
_classCallCheck(this, Rating);
var _this = _possibleConstructorReturn(this, (Rating.__proto__ || Object.getPrototypeOf(Rating)).call(this, props));
_this.state = {
rating: props.defaultValue,
tmpRating: props.defaultValue
};
return _this;
}
_createClass(Rating, [{
key: 'getValue',
value: function getValue() {
return this.state.rating;
}
}, {
key: 'setTemp',
value: function setTemp(rating) {
this.setState({ tmpRating: rating });
}
}, {
key: 'setRating',
value: function setRating(rating) {
this.setState({
tmpRating: rating,
rating: rating
});
}
}, {
key: 'reset',
value: function reset() {
this.setTemp(this.state.rating);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.setRating(nextProps.defaultValue);
}
}, {
key: 'render',
value: function render() {
var stars = [];
for (var i = 1; i <= this.props.max; i++) {
stars.push(React.createElement(
'span',
{
className: i <= this.state.tmpRating ? 'RatingOn' : null,
key: i,
onClick: !this.props.readonly && this.setRating.bind(this, i),
onMouseOver: !this.props.readonly && this.setTemp.bind(this, i)
},
'\u2606'
));
}
return React.createElement(
'div',
{
className: 'Rating ' + (this.props.readonly ? 'RatingReadonly' : null),
onMouseOut: this.reset.bind(this)
},
stars,
this.props.readonly || !this.props.id ? null : React.createElement('input', {
type: 'hidden',
id: this.props.id,
value: this.state.rating })
);
}
}]);
return Rating;
}(Component);
Rating.propTypes = {
defaultValue: PropTypes.number,
readonly: PropTypes.bool,
max: PropTypes.number
};
Rating.defaultProps = {
defaultValue: 0,
max: 5
};
export default Rating;