index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  17. 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;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  38. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  39. if (ar || !(i in from)) {
  40. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  41. ar[i] = from[i];
  42. }
  43. }
  44. return to.concat(ar || Array.prototype.slice.call(from));
  45. };
  46. import { useRef, useState } from 'functional-mini/compat';
  47. import { useEffect, useEvent } from 'functional-mini/component';
  48. import '../_util/assert-component2';
  49. import { mountComponent } from '../_util/component';
  50. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  51. import { useInstanceBoundingClientRect } from '../_util/hooks/useInstanceBoundingClientRect';
  52. import { useComponentUpdateEffect } from '../_util/hooks/useLayoutEffect';
  53. import { useMixState } from '../_util/hooks/useMixState';
  54. import { CollapseFunctionalProps } from './props';
  55. import { useEvent as useStableCallback } from '../_util/hooks/useEvent';
  56. var Collapse = function (props) {
  57. var _a = useState([]), contentHeight = _a[0], setContentHeight = _a[1];
  58. var _b = useState(false), hasChange = _b[0], setHasChange = _b[1];
  59. var taskQueueRef = useRef();
  60. var previousValueRef = useRef([]);
  61. var _c = useMixState(props.defaultCurrent, {
  62. value: props.current,
  63. postState: function (val) {
  64. var current = __spreadArray([], (val || []), true);
  65. var items = props.items;
  66. current = current.filter(function (item) {
  67. if (!items[item] || items[item].disabled) {
  68. return false;
  69. }
  70. return true;
  71. });
  72. if (props.accordion) {
  73. current = current.length > 0 ? [current[0]] : [];
  74. }
  75. return {
  76. valid: true,
  77. value: __spreadArray([], current, true),
  78. };
  79. },
  80. }), value = _c[0], _d = _c[1], isControlled = _d.isControlled, update = _d.update;
  81. var triggerEvent = useComponentEvent(props).triggerEvent;
  82. useEvent('onChange', function (e) {
  83. var itemIndex = parseInt(e.currentTarget.dataset.index, 10);
  84. if (props.items[itemIndex] && props.items[itemIndex].disabled) {
  85. return;
  86. }
  87. var arr = value;
  88. var current = __spreadArray([], arr, true);
  89. var index = current.indexOf(itemIndex);
  90. if (index >= 0) {
  91. current.splice(index, 1);
  92. }
  93. else {
  94. if (props.accordion) {
  95. current = [itemIndex];
  96. }
  97. else {
  98. current.push(itemIndex);
  99. current.sort();
  100. }
  101. }
  102. if (!isControlled) {
  103. update(current);
  104. }
  105. triggerEvent('change', current);
  106. });
  107. var getBoundingClientRectWithBuilder = useInstanceBoundingClientRect().getBoundingClientRectWithBuilder;
  108. var updateContentHeight = useStableCallback(function (prevCurrent, nextCurrent) { return __awaiter(void 0, void 0, void 0, function () {
  109. var prevCurrentArray, nextCurrentArray, expandArray, closeArray, newContentHeight;
  110. return __generator(this, function (_a) {
  111. switch (_a.label) {
  112. case 0:
  113. prevCurrentArray = prevCurrent;
  114. nextCurrentArray = nextCurrent;
  115. expandArray = [];
  116. closeArray = [];
  117. nextCurrentArray.forEach(function (item) {
  118. if (prevCurrentArray.indexOf(item) < 0) {
  119. expandArray.push(item);
  120. }
  121. });
  122. prevCurrentArray.forEach(function (item) {
  123. if (nextCurrentArray.indexOf(item) < 0) {
  124. closeArray.push(item);
  125. }
  126. });
  127. return [4 /*yield*/, Promise.all(props.items.map(function (item, index) { return __awaiter(void 0, void 0, void 0, function () {
  128. var height;
  129. return __generator(this, function (_a) {
  130. switch (_a.label) {
  131. case 0:
  132. if (!(expandArray.indexOf(index) >= 0 ||
  133. closeArray.indexOf(index) >= 0)) return [3 /*break*/, 2];
  134. return [4 /*yield*/, getBoundingClientRectWithBuilder(function (id) { return ".ant-collapse-item-content".concat(id, "-").concat(index); })];
  135. case 1:
  136. height = (_a.sent()).height;
  137. return [2 /*return*/, "".concat(height, "px")];
  138. case 2: return [2 /*return*/, contentHeight[index]];
  139. }
  140. });
  141. }); }))];
  142. case 1:
  143. newContentHeight = _a.sent();
  144. if (closeArray.length === 0) {
  145. setContentHeight(newContentHeight);
  146. }
  147. else {
  148. setContentHeight(newContentHeight);
  149. taskQueueRef.current = function () {
  150. setTimeout(function () {
  151. setContentHeight(function (contentHeight) {
  152. return contentHeight.map(function (item, index) {
  153. if (closeArray.indexOf(index) >= 0) {
  154. return '0px';
  155. }
  156. return item;
  157. });
  158. });
  159. }, 10);
  160. };
  161. }
  162. return [2 /*return*/];
  163. }
  164. });
  165. }); });
  166. useEffect(function () {
  167. if (taskQueueRef.current) {
  168. var task = taskQueueRef.current;
  169. taskQueueRef.current = null;
  170. if (typeof task === 'function') {
  171. task();
  172. }
  173. }
  174. });
  175. useEffect(function () {
  176. var current = value;
  177. var contentHeight = props.items.map(function (item, index) {
  178. if (current.indexOf(index) >= 0) {
  179. return '';
  180. }
  181. return '0px';
  182. });
  183. setContentHeight(contentHeight);
  184. setHasChange(true);
  185. previousValueRef.current = value;
  186. }, []);
  187. useComponentUpdateEffect(function () {
  188. var previous = previousValueRef.current;
  189. previousValueRef.current = value;
  190. updateContentHeight(previous, value);
  191. }, [props.items, JSON.stringify(value)]);
  192. useEvent('resetContentHeight', function (e) {
  193. var index = parseInt(e.currentTarget.dataset.index, 10);
  194. if (value.indexOf(index) < 0) {
  195. return;
  196. }
  197. setContentHeight(function (oldContentHeight) {
  198. var newContentHeight = __spreadArray([], oldContentHeight, true);
  199. newContentHeight[index] = '';
  200. return newContentHeight;
  201. });
  202. });
  203. return {
  204. contentHeight: contentHeight,
  205. hasChange: hasChange,
  206. mixin: {
  207. value: value,
  208. },
  209. };
  210. };
  211. mountComponent(Collapse, CollapseFunctionalProps);