common.taro.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { computed, Ref, ref } from 'vue';
  2. import { StarN } from '@nutui/icons-vue-taro';
  3. import { createComponent, renderIcon } from '@/packages/utils/create';
  4. import { pxCheck } from '@/packages/utils/pxCheck';
  5. import { useTouch } from '@/packages/utils/useTouch';
  6. const { componentName } = createComponent('rate');
  7. const useComponent = (touchable: Boolean = true) => {
  8. return {
  9. props: {
  10. count: {
  11. type: [String, Number],
  12. default: 5
  13. },
  14. modelValue: {
  15. type: [String, Number],
  16. default: 0
  17. },
  18. icon: {
  19. type: Object,
  20. default: () => StarN
  21. },
  22. activeColor: {
  23. type: String,
  24. default: ''
  25. },
  26. voidColor: {
  27. type: String,
  28. default: ''
  29. },
  30. readonly: {
  31. type: Boolean,
  32. default: false
  33. },
  34. disabled: {
  35. type: Boolean,
  36. default: false
  37. },
  38. allowHalf: {
  39. type: Boolean,
  40. default: false
  41. },
  42. touchable: {
  43. type: Boolean,
  44. default: true
  45. },
  46. spacing: {
  47. type: [String, Number],
  48. default: 14
  49. }
  50. },
  51. components: { StarN },
  52. emits: ['update:modelValue', 'change'],
  53. setup(props: any, { emit, slots }: any) {
  54. const rateRefs = ref<HTMLElement[]>([]);
  55. const classes = computed(() => {
  56. const prefixCls = componentName;
  57. return {
  58. [prefixCls]: true
  59. };
  60. });
  61. const updateVal = (value: number) => {
  62. emit('update:modelValue', value);
  63. emit('change', value);
  64. };
  65. const onClick = (e: number, index: number) => {
  66. if (props.disabled || props.readonly) return;
  67. let value = 0;
  68. if (index === 1 && props.modelValue === index) {
  69. } else {
  70. value = index;
  71. if (props.allowHalf && e == 2) {
  72. value -= 0.5;
  73. }
  74. }
  75. updateVal(value);
  76. };
  77. const getScoreByPosition = (x: number, rateRefs: Ref<HTMLElement[]>, allowHalf: boolean) => {
  78. let v = 0;
  79. for (let index = rateRefs.value.length - 1; index >= 0; index--) {
  80. const item = rateRefs.value[index];
  81. if (x > item.offsetLeft) {
  82. if (allowHalf) {
  83. v = index + (x > item.offsetLeft + item.clientWidth / 2 ? 1 : 0.5);
  84. } else {
  85. v = index + 1;
  86. }
  87. break;
  88. }
  89. }
  90. return v;
  91. };
  92. const touch = useTouch();
  93. const touchMethods = {
  94. onTouchStart(event: Event) {
  95. if (!props.touchable) return;
  96. touch.start(event);
  97. },
  98. onTouchMove(event: Event) {
  99. if (!props.touchable || !touchable) return;
  100. touch.move(event);
  101. if (touch.isHorizontal()) {
  102. if (rateRefs.value) {
  103. event.preventDefault();
  104. updateVal(getScoreByPosition(touch.moveX.value, rateRefs, props.allowHalf));
  105. }
  106. }
  107. }
  108. };
  109. const refRandomId = Math.random().toString(36).slice(-8);
  110. return {
  111. classes,
  112. ...touchMethods,
  113. onClick,
  114. pxCheck,
  115. rateRefs,
  116. refRandomId,
  117. renderIcon,
  118. slots
  119. };
  120. }
  121. };
  122. };
  123. // import { useTaroRect } from '@/packages/utils/useTaroRect';
  124. // const getScoreByPositionTaro = async (x: number, rateRefs: Ref<HTMLElement[]>, allowHalf: boolean) => {
  125. // let v = 0;
  126. // for (let index = rateRefs.value.length - 1; index >= 0; index--) {
  127. // const _item = rateRefs.value[index];
  128. // let item = await useTaroRect(_item, Taro);
  129. // if (x > (item.left)) {
  130. // if (allowHalf) {
  131. // v = index + (x > item.left + item.width / 2 ? 1 : 0.5);
  132. // } else {
  133. // v = index + 1;
  134. // }
  135. // break;
  136. // }
  137. // }
  138. // return v;
  139. // };
  140. export const taroComponent = useComponent(false);