index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. import { useComponent, useEvent, useRef, useState, } from 'functional-mini/component';
  49. import '../_util/assert-component2';
  50. import { mountComponent } from '../_util/component';
  51. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  52. import { useMixState } from '../_util/hooks/useMixState';
  53. import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
  54. import { RateFunctionalProps } from './props';
  55. var Rate = function (props) {
  56. var _a = useMixState(props.defaultValue, {
  57. value: props.value,
  58. postState: function (value) {
  59. if (props.allowHalf) {
  60. return {
  61. valid: true,
  62. value: value % 0.5 !== 0 ? Math.round(value) : value,
  63. };
  64. }
  65. return {
  66. valid: true,
  67. value: Math.ceil(value),
  68. };
  69. },
  70. }), rateValue = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  71. var triggerEvent = useComponentEvent(props).triggerEvent;
  72. var _c = useState(null), displayValue = _c[0], setDisplayValue = _c[1];
  73. var ref = useRef(null);
  74. var instance = useComponent();
  75. function getInstance() {
  76. if (instance.$id) {
  77. return my;
  78. }
  79. return instance;
  80. }
  81. function getRate(clientX) {
  82. return __awaiter(this, void 0, void 0, function () {
  83. var gutter, count, _a, left, width, halfRateWidth, num, halfRateCount, val, rate;
  84. return __generator(this, function (_b) {
  85. switch (_b.label) {
  86. case 0:
  87. gutter = props.gutter, count = props.count;
  88. return [4 /*yield*/, getInstanceBoundingClientRect(getInstance(), "#ant-rate-container".concat(instance.$id ? "-".concat(instance.$id) : ''))];
  89. case 1:
  90. _a = _b.sent(), left = _a.left, width = _a.width;
  91. halfRateWidth = (width - (count - 1) * gutter) / count / 2;
  92. num = clientX - left;
  93. halfRateCount = 0;
  94. /* eslint-disable no-constant-condition */
  95. while (true) {
  96. val = halfRateWidth * halfRateCount + gutter * Math.floor(halfRateCount / 2);
  97. if (halfRateCount >= count * 2 || num <= val) {
  98. break;
  99. }
  100. halfRateCount++;
  101. }
  102. rate = props.allowHalf
  103. ? halfRateCount * 0.5
  104. : Math.ceil(halfRateCount * 0.5);
  105. return [2 /*return*/, rate];
  106. }
  107. });
  108. });
  109. }
  110. useEvent('handleStarTap', function (e) { return __awaiter(void 0, void 0, void 0, function () {
  111. var _a, clientX, x, clickX, rate;
  112. return __generator(this, function (_b) {
  113. switch (_b.label) {
  114. case 0:
  115. if (props.readonly) {
  116. return [2 /*return*/];
  117. }
  118. _a = e.detail, clientX = _a.clientX, x = _a.x;
  119. clickX = typeof x === 'number' ? x : clientX;
  120. return [4 /*yield*/, getRate(clickX)];
  121. case 1:
  122. rate = _b.sent();
  123. if (rateValue === rate && props.allowClear) {
  124. rate = 0;
  125. }
  126. if (!isControlled) {
  127. update(rate);
  128. }
  129. if (rateValue !== rate) {
  130. triggerEvent('change', rate);
  131. }
  132. return [2 /*return*/];
  133. }
  134. });
  135. }); });
  136. useEvent('handleStarMove', function (e) { return __awaiter(void 0, void 0, void 0, function () {
  137. var touches, clientX, rate;
  138. return __generator(this, function (_a) {
  139. switch (_a.label) {
  140. case 0:
  141. if (props.readonly) {
  142. return [2 /*return*/];
  143. }
  144. touches = e.touches;
  145. clientX = touches[0].clientX;
  146. if (!ref.current) {
  147. ref.current = {
  148. originalRate: rateValue,
  149. };
  150. }
  151. return [4 /*yield*/, getRate(clientX)];
  152. case 1:
  153. rate = _a.sent();
  154. if (ref.current) {
  155. ref.current = __assign(__assign({}, ref.current), { currentRate: rate });
  156. if (isControlled) {
  157. setDisplayValue(rate);
  158. }
  159. else {
  160. update(rate);
  161. }
  162. }
  163. return [2 /*return*/];
  164. }
  165. });
  166. }); });
  167. useEvent('handleStarMoveEnd', function () { return __awaiter(void 0, void 0, void 0, function () {
  168. var _a, currentRate, originalRate;
  169. return __generator(this, function (_b) {
  170. if (props.readonly) {
  171. return [2 /*return*/];
  172. }
  173. if (!ref.current) {
  174. return [2 /*return*/];
  175. }
  176. _a = ref.current, currentRate = _a.currentRate, originalRate = _a.originalRate;
  177. ref.current = null;
  178. if (isControlled) {
  179. setDisplayValue(null);
  180. }
  181. if (currentRate !== originalRate) {
  182. triggerEvent('change', currentRate);
  183. }
  184. return [2 /*return*/];
  185. });
  186. }); });
  187. return {
  188. mixin: {
  189. value: displayValue !== null ? displayValue : rateValue,
  190. },
  191. };
  192. };
  193. mountComponent(Rate, RateFunctionalProps);