index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view :class="'van-collapse-item custom-class ' + (index !== 0 ? 'van-hairline--top' : '')">
  3. <van-cell
  4. :title="title"
  5. title-class="title-class"
  6. :icon="icon"
  7. :value="value"
  8. :label="label"
  9. :is-link="isLink"
  10. :clickable="clickable"
  11. :border="border && expanded"
  12. :class="utils.bem('collapse-item__title', { disabled, expanded })"
  13. right-icon-class="van-cell__right-icon"
  14. custom-class="van-cell"
  15. hover-class="van-cell--hover"
  16. @click="onClick"
  17. >
  18. <slot name="title" slot="title" />
  19. <slot name="icon" slot="icon" />
  20. <slot name="value" />
  21. <slot name="right-icon" slot="right-icon" />
  22. </van-cell>
  23. <view :class="utils.bem('collapse-item__wrapper', { transition })" :style="'height: ' + contentHeight + ';'" @transitionend="onTransitionEnd">
  24. <view class="van-collapse-item__content content-class">
  25. <slot />
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script module="utils" lang="wxs" src="@/miniprogram_npm/@vant/weapp/wxs/utils.wxs"></script>
  31. <script>
  32. 'use strict';
  33. Object.defineProperty(exports, '__esModule', {
  34. value: true
  35. });
  36. var component_1 = require('../common/component');
  37. var nextTick = function () {
  38. return new Promise(function (resolve) {
  39. return setTimeout(resolve, 20);
  40. });
  41. };
  42. component_1.VantComponent({
  43. classes: ['title-class', 'content-class'],
  44. relation: {
  45. name: 'collapse',
  46. type: 'ancestor',
  47. current: 'collapse-item'
  48. },
  49. props: {
  50. name: null,
  51. title: null,
  52. value: null,
  53. icon: String,
  54. label: String,
  55. disabled: Boolean,
  56. clickable: Boolean,
  57. border: {
  58. type: Boolean,
  59. value: true
  60. },
  61. isLink: {
  62. type: Boolean,
  63. value: true
  64. }
  65. },
  66. data: {
  67. contentHeight: 0,
  68. expanded: false,
  69. transition: false
  70. },
  71. mounted: function () {
  72. var that = this;
  73. this.updateExpanded()
  74. .then(nextTick)
  75. .then(function () {
  76. var data = {
  77. transition: true
  78. };
  79. if (that.expanded) {
  80. data.contentHeight = 'auto';
  81. }
  82. that.setData(data);
  83. });
  84. },
  85. methods: {
  86. updateExpanded: function () {
  87. if (!this.parent) {
  88. return Promise.resolve();
  89. }
  90. var _a = this.parent.data;
  91. var value = _a.value;
  92. var accordion = _a.accordion;
  93. var _b = this.parent.children;
  94. var children = _b === void 0 ? [] : _b;
  95. var name = this.name;
  96. var index = children.indexOf(this);
  97. var currentName = name == null ? index : name;
  98. var expanded = accordion
  99. ? value === currentName
  100. : (value || []).some(function (name) {
  101. return name === currentName;
  102. });
  103. var stack = [];
  104. if (expanded !== this.expanded) {
  105. stack.push(this.updateStyle(expanded));
  106. }
  107. stack.push(
  108. this.set({
  109. index: index,
  110. expanded: expanded
  111. })
  112. );
  113. return Promise.all(stack);
  114. },
  115. updateStyle: function (expanded) {
  116. var that = this;
  117. return this.getRect('.van-collapse-item__content')
  118. .then(function (rect) {
  119. return rect.height;
  120. })
  121. .then(function (height) {
  122. if (expanded) {
  123. return that.set({
  124. contentHeight: height ? height + 'px' : 'auto'
  125. });
  126. }
  127. return that
  128. .set({
  129. contentHeight: height + 'px'
  130. })
  131. .then(nextTick)
  132. .then(function () {
  133. return that.set({
  134. contentHeight: 0
  135. });
  136. });
  137. });
  138. },
  139. onClick: function () {
  140. if (this.disabled) {
  141. return;
  142. }
  143. var _a = this;
  144. var name = _a.name;
  145. var expanded = _a.expanded;
  146. var index = this.parent.children.indexOf(this);
  147. var currentName = name == null ? index : name;
  148. this.parent.switch(currentName, !expanded);
  149. },
  150. onTransitionEnd: function () {
  151. if (this.expanded) {
  152. this.setData({
  153. contentHeight: 'auto'
  154. });
  155. }
  156. }
  157. }
  158. });
  159. </script>
  160. <style>
  161. @import './index.css';
  162. </style>