rate.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. const { prefix } = config;
  11. const name = `${prefix}-rate`;
  12. const rpx2px = (() => {
  13. let systemInfo = null;
  14. return (rpx) => {
  15. if (!systemInfo) {
  16. systemInfo = wx.getSystemInfoSync();
  17. }
  18. return (rpx * (systemInfo ? systemInfo.screenWidth : 750)) / 750;
  19. };
  20. })();
  21. let Rate = class Rate extends SuperComponent {
  22. constructor() {
  23. super(...arguments);
  24. this.externalClasses = [`${prefix}-class`, `${prefix}-class-icon`, `${prefix}-class-text`];
  25. this.properties = props;
  26. this.controlledProps = [
  27. {
  28. key: 'value',
  29. event: 'change',
  30. },
  31. ];
  32. this.data = {
  33. prefix,
  34. classPrefix: name,
  35. defaultTexts: ['极差', '失望', '一般', '满意', '惊喜'],
  36. disabledColor: '#999999',
  37. };
  38. }
  39. onTouch(e) {
  40. const { count, allowHalf, gap, value: currentValue } = this.properties;
  41. const [touch] = e.touches;
  42. const margin = rpx2px(gap);
  43. const selQuery = this.createSelectorQuery();
  44. selQuery
  45. .select(`.${name}__wrapper`)
  46. .boundingClientRect((rect) => {
  47. const { width, left } = rect;
  48. const starWidth = (width - (count - 1) * margin) / count;
  49. const offsetX = touch.pageX - left;
  50. const num = (offsetX + margin) / (starWidth + margin);
  51. const remainder = num % 1;
  52. const integral = num - remainder;
  53. let value = remainder <= 0.5 && allowHalf ? integral + 0.5 : integral + 1;
  54. if (value > count) {
  55. value = count;
  56. }
  57. else if (value < 0) {
  58. value = 0;
  59. }
  60. if (value !== currentValue) {
  61. this._trigger('change', { value });
  62. }
  63. })
  64. .exec();
  65. }
  66. };
  67. Rate = __decorate([
  68. wxComponent()
  69. ], Rate);
  70. export default Rate;