vantui-edit
Version:
一套适用于Taro3及React的vantui组件库
166 lines (148 loc) • 7 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["count", "gutter", "icon", "voidIcon", "disabled", "size", "disabledColor", "color", "voidColor", "allowHalf", "readonly", "touchable", "value", "onChange", "style", "className", "defaultValue", "iconClassPrefix"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { useState, useEffect, useRef, useMemo } from 'react';
import { View } from '@tarojs/components';
import * as utils from '../wxs/utils';
import { Icon } from '../icon/index';
import { getAllRect } from '../common/utils';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export function Rate(props) {
var _props$count = props.count,
count = _props$count === void 0 ? 5 : _props$count,
gutter = props.gutter,
_props$icon = props.icon,
icon = _props$icon === void 0 ? 'star' : _props$icon,
_props$voidIcon = props.voidIcon,
voidIcon = _props$voidIcon === void 0 ? 'star-o' : _props$voidIcon,
disabled = props.disabled,
size = props.size,
disabledColor = props.disabledColor,
color = props.color,
voidColor = props.voidColor,
allowHalf = props.allowHalf,
readonly = props.readonly,
_props$touchable = props.touchable,
touchable = _props$touchable === void 0 ? true : _props$touchable,
value = props.value,
onChange = props.onChange,
style = props.style,
className = props.className,
_props$defaultValue = props.defaultValue,
defaultValue = _props$defaultValue === void 0 ? 5 : _props$defaultValue,
_props$iconClassPrefi = props.iconClassPrefix,
iconClassPrefix = _props$iconClassPrefi === void 0 ? 'van-icon' : _props$iconClassPrefi,
others = _objectWithoutProperties(props, _excluded);
var indexRef = useRef("".concat(+new Date()).concat(Math.ceil(Math.random() * 10000)));
var _useState = useState(Array.from({
length: count
})),
_useState2 = _slicedToArray(_useState, 2),
countArray = _useState2[0],
setCountArray = _useState2[1];
var noControlled = useMemo(function () {
return typeof value === 'undefined';
}, [value]);
var _useState3 = useState(noControlled ? defaultValue : value),
_useState4 = _slicedToArray(_useState3, 2),
innerValue = _useState4[0],
setInnerValue = _useState4[1];
var onSelect = function onSelect(event) {
var score = event.currentTarget.dataset.score;
Object.defineProperty(event, 'detail', {
value: +score + 1
});
if (!disabled && !readonly) {
if (noControlled) {
setInnerValue(event.detail);
}
onChange === null || onChange === void 0 ? void 0 : onChange(event);
}
}; // touchmove匹配到的节点找不到data-score,先注释掉
var onTouchMove = function onTouchMove(event) {
var _event$touches$, _event$touches;
if (!touchable) return;
var _ref = (_event$touches$ = event === null || event === void 0 ? void 0 : (_event$touches = event.touches) === null || _event$touches === void 0 ? void 0 : _event$touches[0]) !== null && _event$touches$ !== void 0 ? _event$touches$ : {},
clientX = _ref.clientX;
if (clientX) {
getAllRect(null, ".rate-com-index".concat(indexRef.current, " .van-rate__icon")).then(function (list) {
var targetIndex = list.sort(function (a, b) {
return a.right - b.right;
}).findIndex(function (item) {
return clientX >= item.left && clientX <= item.right;
});
if (targetIndex !== -1) {
onSelect(Object.assign(Object.assign({}, event), {
currentTarget: {
dataset: {
score: allowHalf ? targetIndex / 2 - 0.5 : targetIndex
}
}
}));
}
});
}
}; // useEffect(
// function () {
// if (value !== innerValue) {
// setInnerValue(value as number)
// }
// },
// // eslint-disable-next-line react-hooks/exhaustive-deps
// [value],
// )
useEffect(function () {
setCountArray(Array.from({
length: count
}));
}, [count]);
var rateValue = noControlled ? innerValue : value;
return /*#__PURE__*/_jsx(View, _objectSpread(_objectSpread({
className: "rate-com-index".concat(indexRef.current, " ") + utils.bem('rate') + ' ' + className,
style: style,
onTouchMove: onTouchMove
}, others), {}, {
children: countArray.map(function (_item, index) {
return /*#__PURE__*/_jsxs(View, {
className: utils.bem('rate__item'),
style: utils.style({
paddingRight: index !== count - 1 ? utils.addUnit(gutter) : null
}),
children: [/*#__PURE__*/_jsx(Icon, {
classPrefix: iconClassPrefix,
name: index + 1 <= rateValue ? icon : voidIcon,
className: utils.bem('rate__icon', [{
disabled: disabled,
full: index + 1 <= rateValue
}]) + " icon-class",
style: utils.style({
fontSize: utils.addUnit(size)
}),
id: "rate-com-index".concat(indexRef.current, "-rate__").concat(index),
"data-score": index,
color: disabled ? disabledColor : index + 1 <= rateValue ? color : voidColor,
onClick: onSelect
}), allowHalf && /*#__PURE__*/_jsx(Icon, {
classPrefix: iconClassPrefix,
name: index + 0.5 <= rateValue ? icon : voidIcon,
className: utils.bem('rate__icon', ['half', {
disabled: disabled,
full: index + 0.5 <= rateValue
}]) + " icon-class",
style: utils.style({
fontSize: utils.addUnit(size)
}),
id: "rate-com-index".concat(indexRef.current, "-rate__").concat(index - 0.5),
"data-score": index - 0.5,
color: disabled ? disabledColor : index + 0.5 <= rateValue ? color : voidColor,
onClick: onSelect
})]
}, index);
})
}));
}
export default Rate;