UNPKG

quesbook-component

Version:
142 lines (116 loc) 6.19 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _starEmpty3x = require('../assets/image/icon/star-empty@3x.png'); var _starEmpty3x2 = _interopRequireDefault(_starEmpty3x); var _starFilled3x = require('../assets/image/icon/star-filled@3x.png'); var _starFilled3x2 = _interopRequireDefault(_starFilled3x); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } var filledLayerStyle = { position: 'absolute', top: 0, left: 0 }; var QbRateStar = function (_Component) { _inherits(QbRateStar, _Component); function QbRateStar(props) { _classCallCheck(this, QbRateStar); var _this = _possibleConstructorReturn(this, (QbRateStar.__proto__ || Object.getPrototypeOf(QbRateStar)).call(this, props)); _this.starCounts = [0, 1, 2, 3, 4]; return _this; } _createClass(QbRateStar, [{ key: 'render', value: function render() { var _this2 = this; return _react2.default.createElement( 'div', { className: 'qb_rate_star', style: _extends({ position: 'relative', lineHeight: 0 }, this.props.compStyle) }, _react2.default.createElement( 'div', { className: 'qb_rate_star_empty_layer', style: this.layerStyle }, this.starCounts.map(function (item) { return _react2.default.createElement('span', { key: item, style: _extends({}, _this2.spanStyle, { backgroundImage: 'url(' + _starEmpty3x2.default + ')' }) }); }) ), _react2.default.createElement( 'div', { className: 'qb_rate_star_filled_layer', style: _extends({}, this.layerStyle, filledLayerStyle) }, _react2.default.createElement( 'div', { className: 'qb_rate_star_score', style: this.scoreStyle }, _react2.default.createElement( 'div', { style: this.layerStyle }, this.starCounts.map(function (item) { return _react2.default.createElement('span', { key: item, style: _extends({}, _this2.spanStyle, { backgroundImage: 'url(' + _starFilled3x2.default + ')' }) }); }) ) ) ) ); } }, { key: 'layerStyle', get: function get() { var _props = this.props, starWidth = _props.starWidth, gap = _props.gap; return { width: (starWidth + gap) * 5 }; } }, { key: 'spanStyle', get: function get() { return { display: 'inline-block', width: this.props.starWidth, height: this.props.starHeight, marginRight: this.props.gap, backgroundSize: '100%', backgroundRepeat: 'no-repeat' }; } }, { key: 'scoreStyle', get: function get() { // Suppose that the full rate is five points, if not, please convert it manually var rate = this.props.rate ? Number(this.props.rate) : 0; var _props2 = this.props, starWidth = _props2.starWidth, gap = _props2.gap; var integer = Math.floor(rate); var decimal = rate - integer; var width = (starWidth + gap) * integer + starWidth * decimal; return { width: width, overflow: 'hidden' }; } }]); return QbRateStar; }(_react.Component); QbRateStar.propTypes = { compStyle: _react2.default.PropTypes.object, // Suppose that the full rate is five points, if not, please convert it manually rate: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.number]), starWidth: _react2.default.PropTypes.number, starHeight: _react2.default.PropTypes.number }; QbRateStar.defaultProps = { compStyle: {}, rate: 0, starWidth: 15.6, starHeight: 15, gap: 4 }; exports.default = QbRateStar;