rate.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var lodashUnified = require('lodash-unified');
  5. var index$1 = require('../../icon/index.js');
  6. var rate = require('./rate2.js');
  7. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  8. var constants = require('../../form/src/constants.js');
  9. var useFormCommonProps = require('../../form/src/hooks/use-form-common-props.js');
  10. var index = require('../../../hooks/use-namespace/index.js');
  11. var useFormItem = require('../../form/src/hooks/use-form-item.js');
  12. var shared = require('@vue/shared');
  13. var event = require('../../../constants/event.js');
  14. var event$1 = require('../../../utils/dom/event.js');
  15. var aria = require('../../../constants/aria.js');
  16. const __default__ = vue.defineComponent({
  17. name: "ElRate"
  18. });
  19. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  20. ...__default__,
  21. props: rate.rateProps,
  22. emits: rate.rateEmits,
  23. setup(__props, { expose, emit }) {
  24. const props = __props;
  25. function getValueFromMap(value, map) {
  26. const isExcludedObject = (val) => shared.isObject(val);
  27. const matchedKeys = Object.keys(map).map((key) => +key).filter((key) => {
  28. const val = map[key];
  29. const excluded = isExcludedObject(val) ? val.excluded : false;
  30. return excluded ? value < key : value <= key;
  31. }).sort((a, b) => a - b);
  32. const matchedValue = map[matchedKeys[0]];
  33. return isExcludedObject(matchedValue) && matchedValue.value || matchedValue;
  34. }
  35. const formContext = vue.inject(constants.formContextKey, void 0);
  36. const formItemContext = vue.inject(constants.formItemContextKey, void 0);
  37. const rateSize = useFormCommonProps.useFormSize();
  38. const ns = index.useNamespace("rate");
  39. const { inputId, isLabeledByFormItem } = useFormItem.useFormItemInputId(props, {
  40. formItemContext
  41. });
  42. const currentValue = vue.ref(props.modelValue);
  43. const hoverIndex = vue.ref(-1);
  44. const pointerAtLeftHalf = vue.ref(true);
  45. const iconRefs = vue.ref([]);
  46. const iconClientWidths = vue.computed(() => iconRefs.value.map((icon) => icon.$el.clientWidth));
  47. const rateClasses = vue.computed(() => [ns.b(), ns.m(rateSize.value)]);
  48. const rateDisabled = vue.computed(() => props.disabled || (formContext == null ? void 0 : formContext.disabled));
  49. const rateStyles = vue.computed(() => {
  50. return ns.cssVarBlock({
  51. "void-color": props.voidColor,
  52. "disabled-void-color": props.disabledVoidColor,
  53. "fill-color": activeColor.value
  54. });
  55. });
  56. const text = vue.computed(() => {
  57. let result = "";
  58. if (props.showScore) {
  59. result = props.scoreTemplate.replace(/\{\s*value\s*\}/, rateDisabled.value ? `${props.modelValue}` : `${currentValue.value}`);
  60. } else if (props.showText) {
  61. result = props.texts[Math.ceil(currentValue.value) - 1];
  62. }
  63. return result;
  64. });
  65. const valueDecimal = vue.computed(() => props.modelValue * 100 - Math.floor(props.modelValue) * 100);
  66. const colorMap = vue.computed(() => shared.isArray(props.colors) ? {
  67. [props.lowThreshold]: props.colors[0],
  68. [props.highThreshold]: { value: props.colors[1], excluded: true },
  69. [props.max]: props.colors[2]
  70. } : props.colors);
  71. const activeColor = vue.computed(() => {
  72. const color = getValueFromMap(currentValue.value, colorMap.value);
  73. return shared.isObject(color) ? "" : color;
  74. });
  75. const decimalStyle = vue.computed(() => {
  76. let width = "";
  77. if (rateDisabled.value) {
  78. width = `${valueDecimal.value}%`;
  79. } else if (props.allowHalf) {
  80. width = "50%";
  81. }
  82. return {
  83. color: activeColor.value,
  84. width
  85. };
  86. });
  87. const componentMap = vue.computed(() => {
  88. let icons = shared.isArray(props.icons) ? [...props.icons] : { ...props.icons };
  89. icons = vue.markRaw(icons);
  90. return shared.isArray(icons) ? {
  91. [props.lowThreshold]: icons[0],
  92. [props.highThreshold]: {
  93. value: icons[1],
  94. excluded: true
  95. },
  96. [props.max]: icons[2]
  97. } : icons;
  98. });
  99. const decimalIconComponent = vue.computed(() => getValueFromMap(props.modelValue, componentMap.value));
  100. const voidComponent = vue.computed(() => rateDisabled.value ? shared.isString(props.disabledVoidIcon) ? props.disabledVoidIcon : vue.markRaw(props.disabledVoidIcon) : shared.isString(props.voidIcon) ? props.voidIcon : vue.markRaw(props.voidIcon));
  101. const activeComponent = vue.computed(() => getValueFromMap(currentValue.value, componentMap.value));
  102. function showDecimalIcon(item) {
  103. const showWhenDisabled = rateDisabled.value && valueDecimal.value > 0 && item - 1 < props.modelValue && item > props.modelValue;
  104. const showWhenAllowHalf = props.allowHalf && pointerAtLeftHalf.value && item - 0.5 <= currentValue.value && item > currentValue.value;
  105. return showWhenDisabled || showWhenAllowHalf;
  106. }
  107. function emitValue(value) {
  108. if (props.clearable && value === props.modelValue) {
  109. value = 0;
  110. }
  111. emit(event.UPDATE_MODEL_EVENT, value);
  112. if (props.modelValue !== value) {
  113. emit(event.CHANGE_EVENT, value);
  114. }
  115. }
  116. function selectValue(value) {
  117. if (rateDisabled.value) {
  118. return;
  119. }
  120. if (props.allowHalf && pointerAtLeftHalf.value) {
  121. emitValue(currentValue.value);
  122. } else {
  123. emitValue(value);
  124. }
  125. }
  126. function handleKey(e) {
  127. if (rateDisabled.value) {
  128. return;
  129. }
  130. const code = event$1.getEventCode(e);
  131. const step = props.allowHalf ? 0.5 : 1;
  132. let _currentValue = currentValue.value;
  133. switch (code) {
  134. case aria.EVENT_CODE.up:
  135. case aria.EVENT_CODE.right:
  136. _currentValue += step;
  137. break;
  138. case aria.EVENT_CODE.left:
  139. case aria.EVENT_CODE.down:
  140. _currentValue -= step;
  141. break;
  142. }
  143. _currentValue = lodashUnified.clamp(_currentValue, 0, props.max);
  144. if (_currentValue === currentValue.value) {
  145. return;
  146. }
  147. e.stopPropagation();
  148. e.preventDefault();
  149. emit(event.UPDATE_MODEL_EVENT, _currentValue);
  150. emit(event.CHANGE_EVENT, _currentValue);
  151. return _currentValue;
  152. }
  153. function setCurrentValue(value, event) {
  154. if (rateDisabled.value) {
  155. return;
  156. }
  157. if (props.allowHalf && event) {
  158. pointerAtLeftHalf.value = event.offsetX * 2 <= iconClientWidths.value[value - 1];
  159. currentValue.value = pointerAtLeftHalf.value ? value - 0.5 : value;
  160. } else {
  161. currentValue.value = value;
  162. }
  163. hoverIndex.value = value;
  164. }
  165. function resetCurrentValue() {
  166. if (rateDisabled.value) {
  167. return;
  168. }
  169. if (props.allowHalf) {
  170. pointerAtLeftHalf.value = props.modelValue !== Math.floor(props.modelValue);
  171. }
  172. currentValue.value = props.modelValue;
  173. hoverIndex.value = -1;
  174. }
  175. vue.watch(() => props.modelValue, (val) => {
  176. currentValue.value = val;
  177. pointerAtLeftHalf.value = props.modelValue !== Math.floor(props.modelValue);
  178. });
  179. if (!props.modelValue) {
  180. emit(event.UPDATE_MODEL_EVENT, 0);
  181. }
  182. expose({
  183. setCurrentValue,
  184. resetCurrentValue
  185. });
  186. return (_ctx, _cache) => {
  187. var _a;
  188. return vue.openBlock(), vue.createElementBlock("div", {
  189. id: vue.unref(inputId),
  190. class: vue.normalizeClass([vue.unref(rateClasses), vue.unref(ns).is("disabled", vue.unref(rateDisabled))]),
  191. role: "slider",
  192. "aria-label": !vue.unref(isLabeledByFormItem) ? _ctx.ariaLabel || "rating" : void 0,
  193. "aria-labelledby": vue.unref(isLabeledByFormItem) ? (_a = vue.unref(formItemContext)) == null ? void 0 : _a.labelId : void 0,
  194. "aria-valuenow": currentValue.value,
  195. "aria-valuetext": vue.unref(text) || void 0,
  196. "aria-valuemin": "0",
  197. "aria-valuemax": _ctx.max,
  198. tabindex: "0",
  199. style: vue.normalizeStyle(vue.unref(rateStyles)),
  200. onKeydown: handleKey
  201. }, [
  202. (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(_ctx.max, (item, key) => {
  203. return vue.openBlock(), vue.createElementBlock("span", {
  204. key,
  205. class: vue.normalizeClass(vue.unref(ns).e("item")),
  206. onMousemove: ($event) => setCurrentValue(item, $event),
  207. onMouseleave: resetCurrentValue,
  208. onClick: ($event) => selectValue(item)
  209. }, [
  210. vue.createVNode(vue.unref(index$1.ElIcon), {
  211. ref_for: true,
  212. ref_key: "iconRefs",
  213. ref: iconRefs,
  214. class: vue.normalizeClass([
  215. vue.unref(ns).e("icon"),
  216. { hover: hoverIndex.value === item },
  217. vue.unref(ns).is("active", item <= currentValue.value),
  218. vue.unref(ns).is("focus-visible", item === Math.ceil(currentValue.value || 1))
  219. ])
  220. }, {
  221. default: vue.withCtx(() => [
  222. vue.withDirectives((vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(activeComponent)), null, null, 512)), [
  223. [vue.vShow, !showDecimalIcon(item) && item <= currentValue.value]
  224. ]),
  225. vue.withDirectives((vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(voidComponent)), null, null, 512)), [
  226. [vue.vShow, !showDecimalIcon(item) && item > currentValue.value]
  227. ]),
  228. vue.withDirectives((vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(voidComponent)), {
  229. class: vue.normalizeClass([vue.unref(ns).em("decimal", "box")])
  230. }, null, 8, ["class"])), [
  231. [vue.vShow, showDecimalIcon(item)]
  232. ]),
  233. vue.withDirectives(vue.createVNode(vue.unref(index$1.ElIcon), {
  234. style: vue.normalizeStyle(vue.unref(decimalStyle)),
  235. class: vue.normalizeClass([vue.unref(ns).e("icon"), vue.unref(ns).e("decimal")])
  236. }, {
  237. default: vue.withCtx(() => [
  238. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(decimalIconComponent))))
  239. ]),
  240. _: 2
  241. }, 1032, ["style", "class"]), [
  242. [vue.vShow, showDecimalIcon(item)]
  243. ])
  244. ]),
  245. _: 2
  246. }, 1032, ["class"])
  247. ], 42, ["onMousemove", "onClick"]);
  248. }), 128)),
  249. _ctx.showText || _ctx.showScore ? (vue.openBlock(), vue.createElementBlock("span", {
  250. key: 0,
  251. class: vue.normalizeClass(vue.unref(ns).e("text")),
  252. style: vue.normalizeStyle({ color: _ctx.textColor })
  253. }, vue.toDisplayString(vue.unref(text)), 7)) : vue.createCommentVNode("v-if", true)
  254. ], 46, ["id", "aria-label", "aria-labelledby", "aria-valuenow", "aria-valuetext", "aria-valuemax"]);
  255. };
  256. }
  257. });
  258. var Rate = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "rate.vue"]]);
  259. exports["default"] = Rate;
  260. //# sourceMappingURL=rate.js.map