@alifd/meet-react
Version:
Fusion Mobile React UI System Component
103 lines • 6.71 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import { rpx2vw4style as __rpx2vw__ } from "@alifd/babel-runtime-jsx-style-transform";
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import classNames from 'classnames';
import React, { createElement, forwardRef, useContext } from "react";
import View from '../view';
import Form from '../form';
import { useValue, useGuid } from '../utils/hooks';
import { getDefaultItemRender, useTouchChangeEventProps } from './utils';
var Rating = function Rating(props, ref) {
var _props$prefix = props.prefix,
prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
sizeProp = props.size,
_props$disabled = props.disabled,
disabled = _props$disabled === void 0 ? false : _props$disabled,
_props$readOnly = props.readOnly,
readOnly = _props$readOnly === void 0 ? false : _props$readOnly,
_props$count = props.count,
count = _props$count === void 0 ? 5 : _props$count,
_props$iconType = props.iconType,
iconType = _props$iconType === void 0 ? 'favorites-filling' : _props$iconType,
onChange = props.onChange,
className = props.className,
style = props.style,
_props$allowHalf = props.allowHalf,
allowHalf = _props$allowHalf === void 0 ? false : _props$allowHalf,
propsItemRender = props.itemRender;
var _useValue = useValue(props, 0),
_useValue2 = _slicedToArray(_useValue, 3),
value = _useValue2[0],
setValue = _useValue2[1],
isControlled = _useValue2[2];
var _useContext = useContext(Form.ItemContext),
contentAlign = _useContext.contentAlign,
contextSize = _useContext.size;
var size = sizeProp || contextSize;
var id = useGuid('rating-');
var gradeType = value > count * 0.4 ? 'high' : 'low';
var iconClassName = classNames("".concat(prefix, "rating--").concat(size, "-icon"), "".concat(prefix, "rating-icon"));
var classnames = {
rating: classNames("".concat(prefix, "rating"), "".concat(prefix, "rating--").concat(size), "".concat(prefix, "rating--").concat(contentAlign), "".concat(prefix, "rating--grade-").concat(gradeType), _defineProperty(_defineProperty({}, "".concat(prefix, "rating--readonly"), readOnly), "".concat(prefix, "rating--disabled"), disabled), className),
iconWrapper: classNames("".concat(prefix, "rating--").concat(size, "-icon-wrapper"), "".concat(prefix, "rating-icon-wrapper")),
icon: iconClassName,
activeIcon: classNames(iconClassName, "".concat(prefix, "rating-icon--active"), "".concat(prefix, "rating-icon--active-").concat(gradeType))
};
var handleChange = function handleChange(v) {
if (disabled || readOnly || v < 0) {
return;
}
if (!isControlled) {
setValue(v);
}
if (onChange) {
onChange(v);
}
};
var touchProps = useTouchChangeEventProps({
id: id,
disabled: disabled || readOnly,
count: count,
allowHalf: allowHalf,
value: value,
onChange: handleChange
});
var itemRender = propsItemRender || getDefaultItemRender({
value: value,
iconType: iconType,
size: size,
classnames: classnames
});
var stars = [];
for (var i = 0; i < count; i++) {
var isActive = Math.floor(value - 1) >= i;
var isHalfActive = value - i < 1 && value - i > 0;
stars.push( /*#__PURE__*/React.createElement(View, {
key: i,
className: classNames(classnames.iconWrapper, _defineProperty(_defineProperty({}, "".concat(classnames.iconWrapper, "--first"), i === 0), "".concat(classnames.iconWrapper, "--last"), i === count - 1))
}, itemRender({
key: i,
value: value,
count: count,
isActive: isActive,
isHalfActive: isHalfActive
})));
}
return /*#__PURE__*/React.createElement(View, _extends({
ref: ref,
className: classnames.rating,
id: id,
style: __rpx2vw__(style)
}, touchProps), stars);
};
Rating.displayName = 'Rating';
export default /*#__PURE__*/forwardRef(Rating);