index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 { useState } from 'functional-mini/compat';
  49. import { useEvent, useReady, useRef, } from 'functional-mini/component';
  50. import '../_util/assert-component2';
  51. import { mountComponent } from '../_util/component';
  52. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  53. import { useEvent as useStableCallback } from '../_util/hooks/useEvent';
  54. import { useInstanceBoundingClientRect } from '../_util/hooks/useInstanceBoundingClientRect';
  55. import { useComponentUpdateEffect } from '../_util/hooks/useLayoutEffect';
  56. import { useMixState } from '../_util/hooks/useMixState';
  57. import { TabsFunctionalProps } from './props';
  58. var Tabs = function (props) {
  59. var _a = useMixState(props.defaultCurrent, {
  60. value: props.current,
  61. }), currentValue = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
  62. var _c = useState({
  63. scrollLeft: 0,
  64. scrollTop: 0,
  65. leftFade: false,
  66. rightFade: false,
  67. }), state = _c[0], updateState = _c[1];
  68. var scrollRef = useRef({ scrollLeft: 0, scrollTop: 0 });
  69. var triggerEvent = useComponentEvent(props).triggerEvent;
  70. var updatePartState = function (part) {
  71. updateState(function (old) {
  72. return __assign(__assign({}, old), part);
  73. });
  74. };
  75. var _d = useInstanceBoundingClientRect(), getBoundingClientRectWithBuilder = _d.getBoundingClientRectWithBuilder, getBoundingClientRect = _d.getBoundingClientRect;
  76. var updateFade = useStableCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
  77. var _a, view, item;
  78. return __generator(this, function (_b) {
  79. switch (_b.label) {
  80. case 0:
  81. updatePartState({
  82. leftFade: !!scrollRef.current.scrollLeft,
  83. });
  84. return [4 /*yield*/, Promise.all([
  85. getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-scroll-view".concat(id); }),
  86. getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-item".concat(id, "-").concat(props.items.length - 1); }),
  87. ])];
  88. case 1:
  89. _a = _b.sent(), view = _a[0], item = _a[1];
  90. if (!item || !view) {
  91. return [2 /*return*/];
  92. }
  93. updatePartState({
  94. rightFade: item.left + item.width / 2 > view.width,
  95. });
  96. return [2 /*return*/];
  97. }
  98. });
  99. }); });
  100. var updateScroll = useStableCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
  101. var current, _a, view, item, scrollTop, needScroll_1, distance, scrollLeft, needScroll, distance;
  102. return __generator(this, function (_b) {
  103. switch (_b.label) {
  104. case 0:
  105. current = currentValue;
  106. return [4 /*yield*/, Promise.all([
  107. getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-scroll-view".concat(id); }),
  108. getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-item".concat(id, "-").concat(current); }),
  109. ])];
  110. case 1:
  111. _a = _b.sent(), view = _a[0], item = _a[1];
  112. if (!view || !item) {
  113. return [2 /*return*/];
  114. }
  115. if (props.direction === 'vertical') {
  116. scrollTop = scrollRef.current.scrollTop || 0;
  117. needScroll_1 = false;
  118. if (props.scrollMode === 'center') {
  119. needScroll_1 = true;
  120. scrollTop +=
  121. item.top - view.top - Math.max((view.height - item.height) / 2, 0);
  122. }
  123. else {
  124. distance = item.top - view.top;
  125. if (distance < 0) {
  126. scrollTop += distance;
  127. needScroll_1 = true;
  128. }
  129. else if (distance + item.height > view.height) {
  130. scrollTop += Math.min(distance + item.height - view.height, distance);
  131. needScroll_1 = true;
  132. }
  133. }
  134. if (needScroll_1) {
  135. if (scrollTop === state.scrollTop) {
  136. scrollTop += Math.random();
  137. }
  138. updatePartState({
  139. scrollTop: scrollTop,
  140. });
  141. }
  142. return [2 /*return*/];
  143. }
  144. scrollLeft = scrollRef.current.scrollLeft || 0;
  145. needScroll = false;
  146. if (props.scrollMode === 'center') {
  147. needScroll = true;
  148. scrollLeft +=
  149. item.left - view.left - Math.max((view.width - item.width) / 2, 0);
  150. }
  151. else {
  152. distance = item.left - view.left;
  153. if (distance < 0) {
  154. scrollLeft += distance;
  155. needScroll = true;
  156. }
  157. else if (distance + item.width > view.width) {
  158. scrollLeft += Math.min(distance + item.width - view.width, distance);
  159. needScroll = true;
  160. }
  161. }
  162. if (needScroll) {
  163. if (scrollLeft === state.scrollLeft) {
  164. scrollLeft += Math.random();
  165. }
  166. updatePartState({
  167. scrollLeft: scrollLeft,
  168. });
  169. updateFade();
  170. }
  171. return [2 /*return*/];
  172. }
  173. });
  174. }); });
  175. useEvent('onScroll', function (e) { return __awaiter(void 0, void 0, void 0, function () {
  176. return __generator(this, function (_a) {
  177. if (props.direction === 'vertical') {
  178. scrollRef.current.scrollTop = e.detail.scrollTop;
  179. return [2 /*return*/];
  180. }
  181. scrollRef.current.scrollLeft = e.detail.scrollLeft;
  182. updateFade();
  183. return [2 /*return*/];
  184. });
  185. }); });
  186. useEvent('onChange', function (e) { return __awaiter(void 0, void 0, void 0, function () {
  187. var index;
  188. return __generator(this, function (_a) {
  189. index = parseInt(e.currentTarget.dataset.index, 10);
  190. if (props.items[index].disabled) {
  191. return [2 /*return*/];
  192. }
  193. if (currentValue === index) {
  194. return [2 /*return*/];
  195. }
  196. if (!isControlled) {
  197. update(index);
  198. }
  199. triggerEvent('change', index, e);
  200. return [2 /*return*/];
  201. });
  202. }); });
  203. useComponentUpdateEffect(function () {
  204. updateScroll();
  205. }, [props.items, currentValue]);
  206. var _e = useState(
  207. // vertical 模式下,不需要设置高度
  208. props.direction === 'vertical' ? 0 : 40), scrollHeight = _e[0], setScrollHeight = _e[1];
  209. useReady(function () {
  210. updateScroll();
  211. getBoundingClientRect('.ant-tabs-bar-item').then(function (res) {
  212. if (res && res.height > 0 && props.direction !== 'vertical') {
  213. setScrollHeight(res.height);
  214. }
  215. });
  216. }, []);
  217. return __assign({ scrollHeight: scrollHeight, mixin: {
  218. value: currentValue,
  219. } }, state);
  220. };
  221. mountComponent(Tabs, TabsFunctionalProps);