rate.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { unitConvert, getRect } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-rate`;
  13. let Rate = class Rate extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`, `${prefix}-class-icon`, `${prefix}-class-text`];
  17. this.properties = props;
  18. this.controlledProps = [
  19. {
  20. key: 'value',
  21. event: 'change',
  22. },
  23. ];
  24. this.data = {
  25. prefix,
  26. classPrefix: name,
  27. defaultTexts: ['极差', '失望', '一般', '满意', '惊喜'],
  28. tipsVisible: false,
  29. tipsLeft: 0,
  30. actionType: '',
  31. scaleIndex: -1,
  32. isVisibleToScreenReader: false,
  33. };
  34. this.methods = {
  35. onTouch(e, eventType) {
  36. const { count, allowHalf, gap, value: currentValue, size } = this.properties;
  37. const [touch] = e.changedTouches;
  38. const margin = unitConvert(gap);
  39. getRect(this, `.${name}__wrapper`).then((rect) => {
  40. const { width, left } = rect;
  41. const starWidth = (width - (count - 1) * margin) / count;
  42. const offsetX = touch.pageX - left;
  43. const num = (offsetX + margin) / (starWidth + margin);
  44. const remainder = num % 1;
  45. const integral = num - remainder;
  46. let value = remainder <= 0.5 && allowHalf ? integral + 0.5 : integral + 1;
  47. if (value > count) {
  48. value = count;
  49. }
  50. else if (value < 0) {
  51. value = 0;
  52. }
  53. if (eventType === 'move' || (eventType === 'tap' && allowHalf)) {
  54. const left = Math.ceil(value - 1) * (unitConvert(gap) + unitConvert(size)) + unitConvert(size) * 0.5;
  55. this.setData({
  56. tipsVisible: true,
  57. actionType: eventType,
  58. scaleIndex: Math.ceil(value),
  59. tipsLeft: Math.max(left, 0),
  60. });
  61. }
  62. if (value !== currentValue) {
  63. this._trigger('change', { value });
  64. }
  65. if (this.touchEnd) {
  66. this.hideTips();
  67. }
  68. });
  69. },
  70. onTap(e) {
  71. const { disabled } = this.properties;
  72. if (disabled)
  73. return;
  74. this.onTouch(e, 'tap');
  75. },
  76. onTouchStart() {
  77. this.touchEnd = false;
  78. },
  79. onTouchMove(e) {
  80. this.onTouch(e, 'move');
  81. this.showAlertText();
  82. },
  83. onTouchEnd() {
  84. this.touchEnd = true;
  85. this.hideTips();
  86. },
  87. hideTips() {
  88. if (this.data.actionType === 'move') {
  89. this.setData({ tipsVisible: false, scaleIndex: -1 });
  90. }
  91. },
  92. onSelect(e) {
  93. const { value } = e.currentTarget.dataset;
  94. const { actionType } = this.data;
  95. if (actionType === 'move')
  96. return;
  97. this._trigger('change', { value });
  98. setTimeout(() => this.setData({ tipsVisible: false, scaleIndex: -1 }), 300);
  99. },
  100. showAlertText() {
  101. if (this.data.isVisibleToScreenReader === true)
  102. return;
  103. this.setData({
  104. isVisibleToScreenReader: true,
  105. });
  106. setTimeout(() => {
  107. this.setData({
  108. isVisibleToScreenReader: false,
  109. });
  110. }, 2e3);
  111. },
  112. };
  113. }
  114. };
  115. Rate = __decorate([
  116. wxComponent()
  117. ], Rate);
  118. export default Rate;