index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { __assign, __awaiter, __generator } from "tslib";
  2. import { useComponent, useEvent, useRef, useState, } from 'functional-mini/component';
  3. import '../_util/assert-component2';
  4. import { mountComponent } from '../_util/component';
  5. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  6. import { useMixState } from '../_util/hooks/useMixState';
  7. import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
  8. import { RateFunctionalProps } from './props';
  9. var Rate = function (props) {
  10. var _a = useMixState(props.defaultValue, {
  11. value: props.value,
  12. postState: function (value) {
  13. if (props.allowHalf) {
  14. return {
  15. valid: true,
  16. value: value % 0.5 !== 0 ? Math.round(value) : value,
  17. };
  18. }
  19. return {
  20. valid: true,
  21. value: Math.ceil(value),
  22. };
  23. },
  24. }), rateValue = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  25. var triggerEvent = useComponentEvent(props).triggerEvent;
  26. var _c = useState(null), displayValue = _c[0], setDisplayValue = _c[1];
  27. var ref = useRef(null);
  28. var instance = useComponent();
  29. function getInstance() {
  30. if (instance.$id) {
  31. return my;
  32. }
  33. return instance;
  34. }
  35. function getRate(clientX) {
  36. return __awaiter(this, void 0, void 0, function () {
  37. var gutter, count, _a, left, width, halfRateWidth, num, halfRateCount, val, rate;
  38. return __generator(this, function (_b) {
  39. switch (_b.label) {
  40. case 0:
  41. gutter = props.gutter, count = props.count;
  42. return [4 /*yield*/, getInstanceBoundingClientRect(getInstance(), "#ant-rate-container".concat(instance.$id ? "-".concat(instance.$id) : ''))];
  43. case 1:
  44. _a = _b.sent(), left = _a.left, width = _a.width;
  45. halfRateWidth = (width - (count - 1) * gutter) / count / 2;
  46. num = clientX - left;
  47. halfRateCount = 0;
  48. /* eslint-disable no-constant-condition */
  49. while (true) {
  50. val = halfRateWidth * halfRateCount + gutter * Math.floor(halfRateCount / 2);
  51. if (halfRateCount >= count * 2 || num <= val) {
  52. break;
  53. }
  54. halfRateCount++;
  55. }
  56. rate = props.allowHalf
  57. ? halfRateCount * 0.5
  58. : Math.ceil(halfRateCount * 0.5);
  59. return [2 /*return*/, rate];
  60. }
  61. });
  62. });
  63. }
  64. useEvent('handleStarTap', function (e) { return __awaiter(void 0, void 0, void 0, function () {
  65. var _a, clientX, x, clickX, rate;
  66. return __generator(this, function (_b) {
  67. switch (_b.label) {
  68. case 0:
  69. if (props.readonly) {
  70. return [2 /*return*/];
  71. }
  72. _a = e.detail, clientX = _a.clientX, x = _a.x;
  73. clickX = typeof x === 'number' ? x : clientX;
  74. return [4 /*yield*/, getRate(clickX)];
  75. case 1:
  76. rate = _b.sent();
  77. if (rateValue === rate && props.allowClear) {
  78. rate = 0;
  79. }
  80. if (!isControlled) {
  81. update(rate);
  82. }
  83. if (rateValue !== rate) {
  84. triggerEvent('change', rate);
  85. }
  86. return [2 /*return*/];
  87. }
  88. });
  89. }); });
  90. useEvent('handleStarMove', function (e) { return __awaiter(void 0, void 0, void 0, function () {
  91. var touches, clientX, rate;
  92. return __generator(this, function (_a) {
  93. switch (_a.label) {
  94. case 0:
  95. if (props.readonly) {
  96. return [2 /*return*/];
  97. }
  98. touches = e.touches;
  99. clientX = touches[0].clientX;
  100. if (!ref.current) {
  101. ref.current = {
  102. originalRate: rateValue,
  103. };
  104. }
  105. return [4 /*yield*/, getRate(clientX)];
  106. case 1:
  107. rate = _a.sent();
  108. if (ref.current) {
  109. ref.current = __assign(__assign({}, ref.current), { currentRate: rate });
  110. if (isControlled) {
  111. setDisplayValue(rate);
  112. }
  113. else {
  114. update(rate);
  115. }
  116. }
  117. return [2 /*return*/];
  118. }
  119. });
  120. }); });
  121. useEvent('handleStarMoveEnd', function () { return __awaiter(void 0, void 0, void 0, function () {
  122. var _a, currentRate, originalRate;
  123. return __generator(this, function (_b) {
  124. if (props.readonly) {
  125. return [2 /*return*/];
  126. }
  127. if (!ref.current) {
  128. return [2 /*return*/];
  129. }
  130. _a = ref.current, currentRate = _a.currentRate, originalRate = _a.originalRate;
  131. ref.current = null;
  132. if (isControlled) {
  133. setDisplayValue(null);
  134. }
  135. if (currentRate !== originalRate) {
  136. triggerEvent('change', currentRate);
  137. }
  138. return [2 /*return*/];
  139. });
  140. }); });
  141. return {
  142. mixin: {
  143. value: displayValue !== null ? displayValue : rateValue,
  144. },
  145. };
  146. };
  147. mountComponent(Rate, RateFunctionalProps);