UNPKG

tdesign-vue

Version:
204 lines (200 loc) 8.54 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, toRefs, ref, computed } from '@vue/composition-api'; import { StarFilledIcon } from 'tdesign-icons-vue'; import { useVModel } from '../hooks/useVModel.js'; import props from './props.js'; import { useConfig } from '../config-provider/useConfig.js'; import { Tooltip } from '../tooltip/index.js'; import { renderTNodeJSXDefault } from '../utils/render-tnode.js'; import 'lodash-es'; import '../config-provider/context.js'; import '../_common/js/global-config/default-config.js'; import '../_common/js/global-config/locale/zh_CN.js'; import '../_chunks/dep-ba613a02.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-fdb1b253.js'; import 'dayjs'; import '../_common/js/global-config/t.js'; import '../tooltip/tooltip.js'; import '../_chunks/dep-6a4dc7bb.js'; import '../tooltip/props.js'; import '../popup/props.js'; import '../popup/popup.js'; import '@popperjs/core'; import '../utils/dom.js'; import 'vue'; import 'raf'; import '../utils/easing.js'; import '../utils/helper.js'; import '@babel/runtime/helpers/objectWithoutProperties'; import '../_common/js/utils/setStyle.js'; import '../popup/container.js'; import '../config-provider/config-receiver.js'; import '../utils/mixins.js'; import '../utils/event.js'; import '../popup/utils.js'; import '@babel/runtime/helpers/readOnlyError'; import '../utils/map-props.js'; import '../utils/withInstall.js'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var _Rate = defineComponent({ name: "TRate", props: _objectSpread({}, props), setup: function setup(props2) { var activeColor = Array.isArray(props2.color) ? props2.color[0] : props2.color; var defaultColor = Array.isArray(props2.color) ? props2.color[1] : "var(--td-bg-color-component)"; var _toRefs = toRefs(props2), inputValue = _toRefs.value; var _useVModel = useVModel(inputValue, props2.defaultValue, props2.onChange, "change"), _useVModel2 = _slicedToArray(_useVModel, 2), starValue = _useVModel2[0], setStarValue = _useVModel2[1]; var hoverValue = ref(void 0); var rootRef = ref(); var _useConfig = useConfig("classPrefix"), classPrefix = _useConfig.classPrefix; var _useConfig2 = useConfig("rate"), global = _useConfig2.global; var displayValue = computed(function () { return Number(hoverValue.value || starValue.value); }); var displayText = computed(function () { return props2.texts.length === 0 ? global.value.rateText : props2.texts; }); var getStarValue = function getStarValue(event, index) { if (props2.allowHalf) { var _rootRef$value, _rootRef$value$getBou2, _firstStar$getBoundin2; var _rootRef$value$getBou = (_rootRef$value = rootRef.value) === null || _rootRef$value === void 0 || (_rootRef$value$getBou2 = _rootRef$value.getBoundingClientRect) === null || _rootRef$value$getBou2 === void 0 ? void 0 : _rootRef$value$getBou2.call(_rootRef$value), left = _rootRef$value$getBou.left; var firstStar = rootRef.value.firstChild.nextSibling; var _firstStar$getBoundin = firstStar === null || firstStar === void 0 || (_firstStar$getBoundin2 = firstStar.getBoundingClientRect) === null || _firstStar$getBoundin2 === void 0 ? void 0 : _firstStar$getBoundin2.call(firstStar), width = _firstStar$getBoundin.width; var clientX = event.clientX; var starMiddle = width * (index - 0.5) + props2.gap * (index - 1); if (clientX - left >= starMiddle) return index; if (clientX - left < starMiddle) return index - 0.5; } return index; }; var mouseEnterHandler = function mouseEnterHandler(event, index) { if (props2.disabled) return; hoverValue.value = getStarValue(event, index); }; var mouseLeaveHandler = function mouseLeaveHandler() { if (props2.disabled) return; hoverValue.value = void 0; }; var clickHandler = function clickHandler(event, index) { if (props2.disabled) return; var value = getStarValue(event, index); if (props2.clearable && value === starValue.value) { hoverValue.value = void 0; setStarValue(0); } else { setStarValue(value); } }; var getStarCls = function getStarCls(index) { if (props2.allowHalf && index + 0.5 === displayValue.value) return "".concat(classPrefix.value, "-rate__item--half"); if (index >= displayValue.value) return ""; if (index < displayValue.value) return "".concat(classPrefix.value, "-rate__item--full"); }; var activeIconStyle = computed(function () { return { fontSize: props2.size, color: activeColor }; }); var inactiveIconStyle = computed(function () { return { fontSize: props2.size, color: defaultColor }; }); return { classPrefix: classPrefix, mouseLeaveHandler: mouseLeaveHandler, getStarCls: getStarCls, clickHandler: clickHandler, mouseEnterHandler: mouseEnterHandler, activeColor: activeColor, defaultColor: defaultColor, displayText: displayText, displayValue: displayValue, rootRef: rootRef, activeIconStyle: activeIconStyle, inactiveIconStyle: inactiveIconStyle }; }, methods: { renderRateIcon: function renderRateIcon() { var h = this.$createElement; return renderTNodeJSXDefault(this, "icon", h(StarFilledIcon)); } }, render: function render() { var _this = this; var h = arguments[0]; var classPrefix = this.classPrefix, mouseLeaveHandler = this.mouseLeaveHandler, getStarCls = this.getStarCls, clickHandler = this.clickHandler, mouseEnterHandler = this.mouseEnterHandler, displayText = this.displayText, displayValue = this.displayValue; return h("div", { "class": "".concat(classPrefix, "-rate"), "on": { "mouseleave": mouseLeaveHandler } }, [h("ul", { "class": "".concat(classPrefix, "-rate__list"), "style": { gap: "".concat(this.gap, "px") }, "ref": "rootRef" }, [_toConsumableArray(Array(Number(this.count))).map(function (_, index) { return h("li", { "key": index, "class": ["".concat(classPrefix, "-rate__item"), getStarCls(index)], "on": { "click": function click(event) { return clickHandler(event, index + 1); }, "mousemove": function mousemove(event) { return mouseEnterHandler(event, index + 1); } } }, [_this.showText ? h(Tooltip, { "key": index, "attrs": { "content": displayText[displayValue - 1] } }, [h("div", { "class": "".concat(classPrefix, "-rate__star-top"), "style": _objectSpread({}, _this.activeIconStyle) }, [_this.renderRateIcon()]), h("div", { "class": "".concat(classPrefix, "-rate__star-bottom"), "style": _objectSpread({}, _this.inactiveIconStyle) }, [_this.renderRateIcon()])]) : h("div", [h("div", { "class": "".concat(classPrefix, "-rate__star-top"), "style": _objectSpread({}, _this.activeIconStyle) }, [_this.renderRateIcon()]), h("div", { "class": "".concat(classPrefix, "-rate__star-bottom"), "style": _objectSpread({}, _this.inactiveIconStyle) }, [_this.renderRateIcon()])])]); })]), this.showText && h("div", { "class": "".concat(classPrefix, "-rate__text") }, [displayText[displayValue - 1]])]); } }); export { _Rate as default }; //# sourceMappingURL=rate.js.map