rate.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. };
  33. this.methods = {
  34. onTouch(e, eventType) {
  35. const { count, allowHalf, gap, value: currentValue, size } = this.properties;
  36. const [touch] = e.touches;
  37. const margin = unitConvert(gap);
  38. getRect(this, `.${name}__wrapper`).then((rect) => {
  39. const { width, left } = rect;
  40. const starWidth = (width - (count - 1) * margin) / count;
  41. const offsetX = touch.pageX - left;
  42. const num = (offsetX + margin) / (starWidth + margin);
  43. const remainder = num % 1;
  44. const integral = num - remainder;
  45. let value = remainder <= 0.5 && allowHalf ? integral + 0.5 : integral + 1;
  46. if (value > count) {
  47. value = count;
  48. }
  49. else if (value < 0) {
  50. value = 0;
  51. }
  52. if (eventType === 'move' || (eventType === 'tap' && allowHalf)) {
  53. const left = Math.ceil(value - 1) * (unitConvert(gap) + unitConvert(size)) + unitConvert(size) * 0.5;
  54. this.setData({
  55. tipsVisible: true,
  56. actionType: eventType,
  57. scaleIndex: Math.ceil(value),
  58. tipsLeft: Math.max(left, 0),
  59. });
  60. }
  61. if (value !== currentValue) {
  62. this._trigger('change', { value });
  63. }
  64. if (this.touchEnd) {
  65. this.hideTips();
  66. }
  67. });
  68. },
  69. onTap(e) {
  70. this.onTouch(e, 'tap');
  71. },
  72. onTouchStart() {
  73. this.touchEnd = false;
  74. },
  75. onTouchMove(e) {
  76. this.onTouch(e, 'move');
  77. },
  78. onTouchEnd() {
  79. this.touchEnd = true;
  80. this.hideTips();
  81. },
  82. hideTips() {
  83. if (this.data.actionType === 'move') {
  84. this.setData({ tipsVisible: false, scaleIndex: -1 });
  85. }
  86. },
  87. onSelect(e) {
  88. const { value } = e.currentTarget.dataset;
  89. const { actionType } = this.data;
  90. if (actionType === 'move')
  91. return;
  92. this._trigger('change', { value });
  93. setTimeout(() => this.setData({ tipsVisible: false, scaleIndex: -1 }), 300);
  94. },
  95. };
  96. }
  97. };
  98. Rate = __decorate([
  99. wxComponent()
  100. ], Rate);
  101. export default Rate;