index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  4. import { createVNode as _createVNode } from "vue";
  5. import { watch, defineComponent, ref, reactive, onMounted } from 'vue';
  6. import { initDefaultProps, getPropsSlot, findDOMNode } from '../_util/props-util';
  7. import { withInstall } from '../_util/type';
  8. import { getOffsetLeft } from './util';
  9. import classNames from '../_util/classNames';
  10. import PropTypes from '../_util/vue-types';
  11. import KeyCode from '../_util/KeyCode';
  12. import StarFilled from "@ant-design/icons-vue/es/icons/StarFilled";
  13. import Tooltip from '../tooltip';
  14. import useConfigInject from '../_util/hooks/useConfigInject';
  15. import Star from './Star';
  16. import useRefs from '../_util/hooks/useRefs';
  17. import { useInjectFormItemContext } from '../form/FormItemContext';
  18. export var rateProps = function rateProps() {
  19. return {
  20. prefixCls: String,
  21. count: Number,
  22. value: Number,
  23. allowHalf: {
  24. type: Boolean,
  25. default: undefined
  26. },
  27. allowClear: {
  28. type: Boolean,
  29. default: undefined
  30. },
  31. tooltips: Array,
  32. disabled: {
  33. type: Boolean,
  34. default: undefined
  35. },
  36. character: PropTypes.any,
  37. autofocus: {
  38. type: Boolean,
  39. default: undefined
  40. },
  41. tabindex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  42. direction: String,
  43. id: String,
  44. onChange: Function,
  45. onHoverChange: Function,
  46. 'onUpdate:value': Function,
  47. onFocus: Function,
  48. onBlur: Function,
  49. onKeydown: Function
  50. };
  51. };
  52. var Rate = defineComponent({
  53. compatConfig: {
  54. MODE: 3
  55. },
  56. name: 'ARate',
  57. inheritAttrs: false,
  58. props: initDefaultProps(rateProps(), {
  59. value: 0,
  60. count: 5,
  61. allowHalf: false,
  62. allowClear: true,
  63. tabindex: 0,
  64. direction: 'ltr'
  65. }),
  66. // emits: ['hoverChange', 'update:value', 'change', 'focus', 'blur', 'keydown'],
  67. setup: function setup(props, _ref) {
  68. var slots = _ref.slots,
  69. attrs = _ref.attrs,
  70. emit = _ref.emit,
  71. expose = _ref.expose;
  72. var _useConfigInject = useConfigInject('rate', props),
  73. prefixCls = _useConfigInject.prefixCls,
  74. direction = _useConfigInject.direction;
  75. var formItemContext = useInjectFormItemContext();
  76. var rateRef = ref();
  77. var _useRefs = useRefs(),
  78. _useRefs2 = _slicedToArray(_useRefs, 2),
  79. setRef = _useRefs2[0],
  80. starRefs = _useRefs2[1];
  81. var state = reactive({
  82. value: props.value,
  83. focused: false,
  84. cleanedValue: null,
  85. hoverValue: undefined
  86. });
  87. watch(function () {
  88. return props.value;
  89. }, function () {
  90. state.value = props.value;
  91. });
  92. var getStarDOM = function getStarDOM(index) {
  93. return findDOMNode(starRefs.value.get(index));
  94. };
  95. var getStarValue = function getStarValue(index, x) {
  96. var reverse = direction.value === 'rtl';
  97. var value = index + 1;
  98. if (props.allowHalf) {
  99. var starEle = getStarDOM(index);
  100. var leftDis = getOffsetLeft(starEle);
  101. var width = starEle.clientWidth;
  102. if (reverse && x - leftDis > width / 2) {
  103. value -= 0.5;
  104. } else if (!reverse && x - leftDis < width / 2) {
  105. value -= 0.5;
  106. }
  107. }
  108. return value;
  109. };
  110. var changeValue = function changeValue(value) {
  111. if (props.value === undefined) {
  112. state.value = value;
  113. }
  114. emit('update:value', value);
  115. emit('change', value);
  116. formItemContext.onFieldChange();
  117. };
  118. var onHover = function onHover(e, index) {
  119. var hoverValue = getStarValue(index, e.pageX);
  120. if (hoverValue !== state.cleanedValue) {
  121. state.hoverValue = hoverValue;
  122. state.cleanedValue = null;
  123. }
  124. emit('hoverChange', hoverValue);
  125. };
  126. var onMouseLeave = function onMouseLeave() {
  127. state.hoverValue = undefined;
  128. state.cleanedValue = null;
  129. emit('hoverChange', undefined);
  130. };
  131. var onClick = function onClick(event, index) {
  132. var allowClear = props.allowClear;
  133. var newValue = getStarValue(index, event.pageX);
  134. var isReset = false;
  135. if (allowClear) {
  136. isReset = newValue === state.value;
  137. }
  138. onMouseLeave();
  139. changeValue(isReset ? 0 : newValue);
  140. state.cleanedValue = isReset ? newValue : null;
  141. };
  142. var onFocus = function onFocus(e) {
  143. state.focused = true;
  144. emit('focus', e);
  145. };
  146. var onBlur = function onBlur(e) {
  147. state.focused = false;
  148. emit('blur', e);
  149. formItemContext.onFieldBlur();
  150. };
  151. var onKeyDown = function onKeyDown(event) {
  152. var keyCode = event.keyCode;
  153. var count = props.count,
  154. allowHalf = props.allowHalf;
  155. var reverse = direction.value === 'rtl';
  156. if (keyCode === KeyCode.RIGHT && state.value < count && !reverse) {
  157. if (allowHalf) {
  158. state.value += 0.5;
  159. } else {
  160. state.value += 1;
  161. }
  162. changeValue(state.value);
  163. event.preventDefault();
  164. } else if (keyCode === KeyCode.LEFT && state.value > 0 && !reverse) {
  165. if (allowHalf) {
  166. state.value -= 0.5;
  167. } else {
  168. state.value -= 1;
  169. }
  170. changeValue(state.value);
  171. event.preventDefault();
  172. } else if (keyCode === KeyCode.RIGHT && state.value > 0 && reverse) {
  173. if (allowHalf) {
  174. state.value -= 0.5;
  175. } else {
  176. state.value -= 1;
  177. }
  178. changeValue(state.value);
  179. event.preventDefault();
  180. } else if (keyCode === KeyCode.LEFT && state.value < count && reverse) {
  181. if (allowHalf) {
  182. state.value += 0.5;
  183. } else {
  184. state.value += 1;
  185. }
  186. changeValue(state.value);
  187. event.preventDefault();
  188. }
  189. emit('keydown', event);
  190. };
  191. var focus = function focus() {
  192. if (!props.disabled) {
  193. rateRef.value.focus();
  194. }
  195. };
  196. var blur = function blur() {
  197. if (!props.disabled) {
  198. rateRef.value.blur();
  199. }
  200. };
  201. expose({
  202. focus: focus,
  203. blur: blur
  204. });
  205. onMounted(function () {
  206. var autofocus = props.autofocus,
  207. disabled = props.disabled;
  208. if (autofocus && !disabled) {
  209. focus();
  210. }
  211. });
  212. var characterRender = function characterRender(node, _ref2) {
  213. var index = _ref2.index;
  214. var tooltips = props.tooltips;
  215. if (!tooltips) return node;
  216. return _createVNode(Tooltip, {
  217. "title": tooltips[index]
  218. }, {
  219. default: function _default() {
  220. return [node];
  221. }
  222. });
  223. };
  224. var character = getPropsSlot(slots, props, 'character') || _createVNode(StarFilled, null, null);
  225. return function () {
  226. var count = props.count,
  227. allowHalf = props.allowHalf,
  228. disabled = props.disabled,
  229. tabindex = props.tabindex,
  230. _props$id = props.id,
  231. id = _props$id === void 0 ? formItemContext.id.value : _props$id;
  232. var className = attrs.class,
  233. style = attrs.style;
  234. var stars = [];
  235. var disabledClass = disabled ? "".concat(prefixCls.value, "-disabled") : '';
  236. for (var index = 0; index < count; index++) {
  237. stars.push(_createVNode(Star, {
  238. "ref": setRef(index),
  239. "key": index,
  240. "index": index,
  241. "count": count,
  242. "disabled": disabled,
  243. "prefixCls": "".concat(prefixCls.value, "-star"),
  244. "allowHalf": allowHalf,
  245. "value": state.hoverValue === undefined ? state.value : state.hoverValue,
  246. "onClick": onClick,
  247. "onHover": onHover,
  248. "character": character,
  249. "characterRender": characterRender,
  250. "focused": state.focused
  251. }, null));
  252. }
  253. var rateClassName = classNames(prefixCls.value, disabledClass, className, _defineProperty({}, "".concat(prefixCls.value, "-rtl"), direction.value === 'rtl'));
  254. return _createVNode("ul", _objectSpread(_objectSpread({}, attrs), {}, {
  255. "id": id,
  256. "class": rateClassName,
  257. "style": style,
  258. "onMouseleave": disabled ? null : onMouseLeave,
  259. "tabindex": disabled ? -1 : tabindex,
  260. "onFocus": disabled ? null : onFocus,
  261. "onBlur": disabled ? null : onBlur,
  262. "onKeydown": disabled ? null : onKeyDown,
  263. "ref": rateRef,
  264. "role": "radiogroup"
  265. }), [stars]);
  266. };
  267. }
  268. });
  269. export default withInstall(Rate);