choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
290 lines (242 loc) • 7.35 kB
JavaScript
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 _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import noop from 'lodash/noop';
import KeyCode from '../../_util/KeyCode';
import { getOffsetLeft } from './util';
import Star from './Star';
var Rate = /*#__PURE__*/function (_Component) {
_inherits(Rate, _Component);
var _super = _createSuper(Rate);
function Rate(props) {
var _this;
_classCallCheck(this, Rate);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onHover", function (event, index) {
var hoverValue = _this.getStarValue(index, event.pageX);
var cleanedValue = _this.state.cleanedValue;
if (hoverValue !== cleanedValue) {
_this.setState({
hoverValue: hoverValue,
cleanedValue: null
});
}
_this.props.onHoverChange(hoverValue);
});
_defineProperty(_assertThisInitialized(_this), "onMouseLeave", function () {
_this.setState({
hoverValue: undefined,
cleanedValue: null
});
_this.props.onHoverChange(undefined);
});
_defineProperty(_assertThisInitialized(_this), "onClick", function (event, index) {
var value = _this.getStarValue(index, event.pageX);
var isReset = false;
if (_this.props.allowClear) {
isReset = value === _this.state.value;
}
_this.onMouseLeave(true);
_this.changeValue(isReset ? 0 : value);
_this.setState({
cleanedValue: isReset ? value : null
});
});
_defineProperty(_assertThisInitialized(_this), "onFocus", function () {
var onFocus = _this.props.onFocus;
_this.setState({
focused: true
});
if (onFocus) {
onFocus();
}
});
_defineProperty(_assertThisInitialized(_this), "onBlur", function () {
var onBlur = _this.props.onBlur;
_this.setState({
focused: false
});
if (onBlur) {
onBlur();
}
});
_defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
var keyCode = event.keyCode;
var _this$props = _this.props,
count = _this$props.count,
allowHalf = _this$props.allowHalf,
onKeyDown = _this$props.onKeyDown;
var value = _this.state.value;
if (keyCode === KeyCode.RIGHT && value < count) {
if (allowHalf) {
value += 0.5;
} else {
value += 1;
}
_this.changeValue(value);
event.preventDefault();
} else if (keyCode === KeyCode.LEFT && value > 0) {
if (allowHalf) {
value -= 0.5;
} else {
value -= 1;
}
_this.changeValue(value);
event.preventDefault();
}
if (onKeyDown) {
onKeyDown(event);
}
});
_defineProperty(_assertThisInitialized(_this), "saveRef", function (index) {
return function (node) {
_this.stars[index] = node;
};
});
_defineProperty(_assertThisInitialized(_this), "saveRate", function (node) {
_this.rate = node;
});
var _value = props.value;
if (_value === undefined) {
_value = props.defaultValue;
}
_this.stars = {};
_this.state = {
value: _value,
focused: false,
cleanedValue: null
};
return _this;
}
_createClass(Rate, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.autoFocus && !this.props.disabled) {
this.focus();
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
var value = nextProps.value;
if (value === undefined) {
value = nextProps.defaultValue;
}
this.setState({
value: value
});
}
}
}, {
key: "getStarDOM",
value: function getStarDOM(index) {
return ReactDOM.findDOMNode(this.stars[index]);
}
}, {
key: "getStarValue",
value: function getStarValue(index, x) {
var value = index + 1;
if (this.props.allowHalf) {
var starEle = this.getStarDOM(index);
var leftDis = getOffsetLeft(starEle);
var width = starEle.clientWidth;
if (x - leftDis < width / 2) {
value -= 0.5;
}
}
return value;
}
}, {
key: "focus",
value: function focus() {
if (!this.props.disabled) {
this.rate.focus();
}
}
}, {
key: "blur",
value: function blur() {
if (!this.props.disabled) {
this.rate.focus();
}
}
}, {
key: "changeValue",
value: function changeValue(value) {
if (!('value' in this.props)) {
this.setState({
value: value
});
}
this.props.onChange(value);
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
count = _this$props2.count,
allowHalf = _this$props2.allowHalf,
style = _this$props2.style,
prefixCls = _this$props2.prefixCls,
disabled = _this$props2.disabled,
className = _this$props2.className,
character = _this$props2.character,
tabIndex = _this$props2.tabIndex;
var _this$state = this.state,
value = _this$state.value,
hoverValue = _this$state.hoverValue,
focused = _this$state.focused;
var stars = [];
var disabledClass = disabled ? "".concat(prefixCls, "-disabled") : '';
for (var index = 0; index < count; index++) {
stars.push( /*#__PURE__*/React.createElement(Star, {
ref: this.saveRef(index),
index: index,
count: count,
disabled: disabled,
prefixCls: "".concat(prefixCls, "-star"),
allowHalf: allowHalf,
value: hoverValue === undefined ? value : hoverValue,
onClick: this.onClick,
onHover: this.onHover,
key: index,
character: character,
focused: focused
}));
}
return /*#__PURE__*/React.createElement("ul", {
className: classNames(prefixCls, disabledClass, className),
style: style,
onMouseLeave: disabled ? null : this.onMouseLeave,
tabIndex: disabled ? -1 : tabIndex,
onFocus: disabled ? null : this.onFocus,
onBlur: disabled ? null : this.onBlur,
onKeyDown: disabled ? null : this.onKeyDown,
ref: this.saveRate,
role: "radiogroup"
}, stars);
}
}]);
return Rate;
}(Component);
_defineProperty(Rate, "defaultProps", {
defaultValue: 0,
count: 5,
allowHalf: false,
allowClear: true,
style: {},
prefixCls: 'rc-rate',
onChange: noop,
character: '★',
onHoverChange: noop,
tabIndex: 0
});
export { Rate as default };
//# sourceMappingURL=Rate.js.map