index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { __awaiter, __generator, __spreadArray } from "tslib";
  2. import { Component, triggerEvent, getValueFromProps } from '../_util/simply';
  3. import { CollapseDefaultProps } from './props';
  4. import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
  5. import createValue from '../mixins/value';
  6. Component(CollapseDefaultProps, {
  7. getInstance: function () {
  8. if (this.$id) {
  9. return my;
  10. }
  11. return this;
  12. },
  13. getBoundingClientRectWithBuilder: function (builder) {
  14. return __awaiter(this, void 0, void 0, function () {
  15. return __generator(this, function (_a) {
  16. switch (_a.label) {
  17. case 0: return [4 /*yield*/, getInstanceBoundingClientRect(this.getInstance(), builder(this.$id ? "-".concat(this.$id) : ''))];
  18. case 1: return [2 /*return*/, _a.sent()];
  19. }
  20. });
  21. });
  22. },
  23. formatCurrent: function (val, props) {
  24. var current = __spreadArray([], (val || []), true);
  25. var items = props.items;
  26. current = current.filter(function (item) {
  27. if (!items[item] || items[item].disabled) {
  28. return false;
  29. }
  30. return true;
  31. });
  32. if (props.accordion) {
  33. current = current.length > 0 ? [current[0]] : [];
  34. }
  35. return __spreadArray([], current, true);
  36. },
  37. onChange: function (e) {
  38. var itemIndex = parseInt(e.currentTarget.dataset.index, 10);
  39. var _a = getValueFromProps(this, [
  40. 'items',
  41. 'accordion',
  42. ]), items = _a[0], accordion = _a[1];
  43. if (items[itemIndex] && items[itemIndex].disabled) {
  44. return;
  45. }
  46. var arr = this.getValue();
  47. var current = __spreadArray([], arr, true);
  48. var index = current.indexOf(itemIndex);
  49. if (index >= 0) {
  50. current.splice(index, 1);
  51. }
  52. else {
  53. if (accordion) {
  54. current = [itemIndex];
  55. }
  56. else {
  57. current.push(itemIndex);
  58. current.sort();
  59. }
  60. }
  61. if (!this.isControlled()) {
  62. this.update(current);
  63. }
  64. triggerEvent(this, 'change', current, e);
  65. },
  66. updateContentHeight: function (prevCurrent, nextCurrent) {
  67. return __awaiter(this, void 0, void 0, function () {
  68. var prevCurrentArray, nextCurrentArray, expandArray, closeArray, items, contentHeight;
  69. var _this = this;
  70. return __generator(this, function (_a) {
  71. switch (_a.label) {
  72. case 0:
  73. prevCurrentArray = prevCurrent;
  74. nextCurrentArray = nextCurrent;
  75. expandArray = [];
  76. closeArray = [];
  77. nextCurrentArray.forEach(function (item) {
  78. if (prevCurrentArray.indexOf(item) < 0) {
  79. expandArray.push(item);
  80. }
  81. });
  82. prevCurrentArray.forEach(function (item) {
  83. if (nextCurrentArray.indexOf(item) < 0) {
  84. closeArray.push(item);
  85. }
  86. });
  87. items = getValueFromProps(this, 'items');
  88. return [4 /*yield*/, Promise.all(items.map(function (item, index) { return __awaiter(_this, void 0, void 0, function () {
  89. var height;
  90. return __generator(this, function (_a) {
  91. switch (_a.label) {
  92. case 0:
  93. if (!(expandArray.indexOf(index) >= 0 ||
  94. closeArray.indexOf(index) >= 0)) return [3 /*break*/, 2];
  95. return [4 /*yield*/, this.getBoundingClientRectWithBuilder(function (id) { return ".ant-collapse-item-content".concat(id, "-").concat(index); })];
  96. case 1:
  97. height = (_a.sent()).height;
  98. return [2 /*return*/, "".concat(height, "px")];
  99. case 2: return [2 /*return*/, this.data.contentHeight[index]];
  100. }
  101. });
  102. }); }))];
  103. case 1:
  104. contentHeight = _a.sent();
  105. if (closeArray.length === 0) {
  106. this.setData({
  107. contentHeight: contentHeight,
  108. });
  109. }
  110. else {
  111. this.setData({
  112. contentHeight: contentHeight,
  113. });
  114. setTimeout(function () {
  115. contentHeight = contentHeight.map(function (item, index) {
  116. if (closeArray.indexOf(index) >= 0) {
  117. return '0px';
  118. }
  119. return item;
  120. });
  121. _this.setData({
  122. contentHeight: contentHeight,
  123. });
  124. }, 10);
  125. }
  126. return [2 /*return*/];
  127. }
  128. });
  129. });
  130. },
  131. resetContentHeight: function (e) {
  132. var index = parseInt(e.currentTarget.dataset.index, 10);
  133. if (this.getValue().indexOf(index) < 0) {
  134. return;
  135. }
  136. var contentHeight = __spreadArray([], this.data.contentHeight, true);
  137. contentHeight[index] = '';
  138. this.setData({
  139. contentHeight: contentHeight,
  140. });
  141. },
  142. }, {
  143. contentHeight: [],
  144. hasChange: false,
  145. }, [
  146. createValue({
  147. valueKey: 'current',
  148. defaultValueKey: 'defaultCurrent',
  149. transformValue: function (current, extra) {
  150. var value = this.formatCurrent(current, extra ? extra.nextProps : getValueFromProps(this));
  151. return {
  152. needUpdate: true,
  153. value: value,
  154. };
  155. },
  156. }),
  157. ], {
  158. didUpdate: function (prevProps, prevData) {
  159. console.log(prevProps.items !== this.props.items, !this.isEqualValue(prevData));
  160. if (prevProps.items !== this.props.items ||
  161. !this.isEqualValue(prevData)) {
  162. this.updateContentHeight(this.getValue(prevData), this.getValue());
  163. }
  164. },
  165. didMount: function () {
  166. var current = this.getValue();
  167. var contentHeight = this.props.items.map(function (item, index) {
  168. if (current.indexOf(index) >= 0) {
  169. return '';
  170. }
  171. return '0px';
  172. });
  173. this.setData({
  174. hasChange: true,
  175. contentHeight: contentHeight,
  176. });
  177. },
  178. });