UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

183 lines (155 loc) 6.08 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import * as React from 'react'; import classNames from 'classnames'; import Star from '../icon/svgs/Star'; import StarO from '../icon/svgs/StarO'; var Rate = /*#__PURE__*/function (_React$PureComponent) { _inherits(Rate, _React$PureComponent); var _super = _createSuper(Rate); function Rate(props) { var _this; _classCallCheck(this, Rate); _this = _super.call(this, props); _this.onChange = _this.onChange.bind(_assertThisInitialized(_this)); _this.onStarClick = _this.onStarClick.bind(_assertThisInitialized(_this)); _this.renderStars = _this.renderStars.bind(_assertThisInitialized(_this)); _this.state = { value: props.value }; return _this; } _createClass(Rate, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var value = nextProps.value; if (value != null && value !== this.state.value) { this.setState({ value: value }); } } }, { key: "onChange", value: function onChange(inputValue) { var _this$props = this.props, editing = _this$props.editing, value = _this$props.value; if (!editing) { return; } // do not update internal state based on input value if prop passed if (value != null) { return; } this.setState({ value: inputValue }); } }, { key: "onStarClick", value: function onStarClick(index, value, name, e) { e.stopPropagation(); var _this$props2 = this.props, onStarClick = _this$props2.onStarClick, editing = _this$props2.editing; if (!editing) { return; } onStarClick && onStarClick(index, value, name, e); } }, { key: "renderStars", value: function renderStars() { var _this2 = this; var _this$props3 = this.props, name = _this$props3.name, starCount = _this$props3.starCount, prefixCls = _this$props3.prefixCls; var value = this.state.value; var radioStyles = { display: 'none', position: 'absolute', marginLeft: -9999 }; // populate stars var starNodes = []; var _loop = function _loop(i) { var id = "".concat(name, "_").concat(i); var starNodeInput = /*#__PURE__*/React.createElement("input", { key: "input_".concat(id), style: radioStyles, className: "dv-star-rating-input", type: "radio", name: name, id: id, value: i, checked: value === i, onChange: function onChange() { return _this2.onChange(i); } }); var starNodeLabel = /*#__PURE__*/React.createElement("label", { key: "label_".concat(id), className: classNames("".concat(prefixCls, "-star"), "".concat(prefixCls, "-").concat(value >= i ? 'full' : 'empty', "-star")), htmlFor: id, onClick: function onClick(e) { return _this2.onStarClick(i, value, name, e); } }, _this2.renderIcon(i, value, name, id)); //@ts-ignore starNodes.push(starNodeInput); //@ts-ignore starNodes.push(starNodeLabel); }; for (var i = starCount; i > 0; i--) { _loop(i); } return starNodes.length ? starNodes : null; } }, { key: "renderIcon", value: function renderIcon(index, value, name, id) { var _this$props4 = this.props, renderStarIcon = _this$props4.renderStarIcon, renderStarIconHalf = _this$props4.renderStarIconHalf; if (typeof renderStarIconHalf === 'function' && Math.ceil(value) === index && value % 1 !== 0) { return renderStarIconHalf(index, value, name, id); } if (typeof renderStarIcon === 'function') { return renderStarIcon(index, value, name, id); } return value >= index ? /*#__PURE__*/React.createElement(Star, { className: "Yep-icon-xs" }) : /*#__PURE__*/React.createElement(StarO, { className: "Yep-icon-xs" }); } }, { key: "render", value: function render() { var _this$props5 = this.props, editing = _this$props5.editing, prefixCls = _this$props5.prefixCls, className = _this$props5.className, style = _this$props5.style; var cls = classNames(prefixCls, className, _defineProperty({}, "".concat(prefixCls, "-non-editable"), !editing)); return /*#__PURE__*/React.createElement("div", { style: style, className: cls }, this.renderStars()); } }]); return Rate; }(React.PureComponent); export { Rate as default }; Rate.defaultProps = { prefixCls: 'Yep-rate', style: {}, starCount: 5, editing: true, value: 0 };