index.js 7.1 KB

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