UNPKG

@antmjs/vantui

Version:

一套适用于Taro3及React的vantui组件库

161 lines (159 loc) 7.45 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; 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 { get } from '../default-props'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; export function Rate(props) { var _useState = useState(get().Rate), _useState2 = _slicedToArray(_useState, 1), d = _useState2[0]; var _d$props = _objectSpread(_objectSpread({}, d), props), _d$props$count = _d$props.count, count = _d$props$count === void 0 ? 5 : _d$props$count, gutter = _d$props.gutter, _d$props$icon = _d$props.icon, icon = _d$props$icon === void 0 ? 'star' : _d$props$icon, _d$props$voidIcon = _d$props.voidIcon, voidIcon = _d$props$voidIcon === void 0 ? 'star-o' : _d$props$voidIcon, _d$props$disabled = _d$props.disabled, disabled = _d$props$disabled === void 0 ? false : _d$props$disabled, size = _d$props.size, disabledColor = _d$props.disabledColor, color = _d$props.color, voidColor = _d$props.voidColor, allowHalf = _d$props.allowHalf, _d$props$readonly = _d$props.readonly, readonly = _d$props$readonly === void 0 ? false : _d$props$readonly, _d$props$touchable = _d$props.touchable, touchable = _d$props$touchable === void 0 ? true : _d$props$touchable, value = _d$props.value, onChange = _d$props.onChange, style = _d$props.style, className = _d$props.className, _d$props$defaultValue = _d$props.defaultValue, defaultValue = _d$props$defaultValue === void 0 ? 5 : _d$props$defaultValue, _d$props$iconClassPre = _d$props.iconClassPrefix, iconClassPrefix = _d$props$iconClassPre === void 0 ? 'van-icon' : _d$props$iconClassPre, others = _objectWithoutProperties(_d$props, _excluded); var indexRef = useRef("".concat(+new Date()).concat(Math.ceil(Math.random() * 10000))); var _useState3 = useState(Array.from({ length: count })), _useState4 = _slicedToArray(_useState3, 2), countArray = _useState4[0], setCountArray = _useState4[1]; var noControlled = useMemo(function () { return typeof value === 'undefined'; }, [value]); var _useState5 = useState(noControlled ? defaultValue : value), _useState6 = _slicedToArray(_useState5, 2), innerValue = _useState6[0], setInnerValue = _useState6[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; event.preventDefault(); event.stopPropagation(); 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;