index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <uni-shadow-root class="vant-collapse-item-index"><view :class="'van-collapse-item custom-class '+(index !== 0 ? 'van-hairline--top' : '')">
  3. <van-cell :title="title" title-class="title-class" :icon="icon" :value="value" :label="label" :is-link="isLink" :clickable="clickable" :border="border && expanded" :class="utils.bem('collapse-item__title', { disabled, expanded })" right-icon-class="van-cell__right-icon" custom-class="van-cell" hover-class="van-cell--hover" @click="onClick">
  4. <slot name="title" slot="title"></slot>
  5. <slot name="icon" slot="icon"></slot>
  6. <slot name="value"></slot>
  7. <slot name="right-icon" slot="right-icon"></slot>
  8. </van-cell>
  9. <view :class="utils.bem('collapse-item__wrapper', { transition })" :style="'height: '+(contentHeight)+';'" @transitionend="onTransitionEnd">
  10. <view class="van-collapse-item__content content-class">
  11. <slot></slot>
  12. </view>
  13. </view>
  14. </view></uni-shadow-root>
  15. </template>
  16. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  17. <script>
  18. import VanCell from '../cell/index.vue'
  19. global['__wxVueOptions'] = {components:{'van-cell': VanCell}}
  20. global['__wxRoute'] = 'vant/collapse-item/index'
  21. import { VantComponent } from '../common/component';
  22. const nextTick = () => new Promise(resolve => setTimeout(resolve, 20));
  23. VantComponent({
  24. classes: ['title-class', 'content-class'],
  25. relation: {
  26. name: 'collapse',
  27. type: 'ancestor',
  28. linked(parent) {
  29. this.parent = parent;
  30. }
  31. },
  32. props: {
  33. name: null,
  34. title: null,
  35. value: null,
  36. icon: String,
  37. label: String,
  38. disabled: Boolean,
  39. clickable: Boolean,
  40. border: {
  41. type: Boolean,
  42. value: true
  43. },
  44. isLink: {
  45. type: Boolean,
  46. value: true
  47. }
  48. },
  49. data: {
  50. contentHeight: 0,
  51. expanded: false,
  52. transition: false
  53. },
  54. mounted() {
  55. this.updateExpanded()
  56. .then(nextTick)
  57. .then(() => {
  58. const data = { transition: true };
  59. if (this.data.expanded) {
  60. data.contentHeight = 'auto';
  61. }
  62. this.setData(data);
  63. });
  64. },
  65. methods: {
  66. updateExpanded() {
  67. if (!this.parent) {
  68. return Promise.resolve();
  69. }
  70. const { value, accordion } = this.parent.data;
  71. const { children = [] } = this.parent;
  72. const { name } = this.data;
  73. const index = children.indexOf(this);
  74. const currentName = name == null ? index : name;
  75. const expanded = accordion
  76. ? value === currentName
  77. : (value || []).some((name) => name === currentName);
  78. const stack = [];
  79. if (expanded !== this.data.expanded) {
  80. stack.push(this.updateStyle(expanded));
  81. }
  82. stack.push(this.set({ index, expanded }));
  83. return Promise.all(stack);
  84. },
  85. updateStyle(expanded) {
  86. return this.getRect('.van-collapse-item__content')
  87. .then((rect) => rect.height)
  88. .then((height) => {
  89. if (expanded) {
  90. return this.set({
  91. contentHeight: height ? `${height}px` : 'auto'
  92. });
  93. }
  94. return this.set({ contentHeight: `${height}px` })
  95. .then(nextTick)
  96. .then(() => this.set({ contentHeight: 0 }));
  97. });
  98. },
  99. onClick() {
  100. if (this.data.disabled) {
  101. return;
  102. }
  103. const { name, expanded } = this.data;
  104. const index = this.parent.children.indexOf(this);
  105. const currentName = name == null ? index : name;
  106. this.parent.switch(currentName, !expanded);
  107. },
  108. onTransitionEnd() {
  109. if (this.data.expanded) {
  110. this.setData({
  111. contentHeight: 'auto'
  112. });
  113. }
  114. }
  115. }
  116. });
  117. export default global['__wxComponents']['vant/collapse-item/index']
  118. </script>
  119. <style platform="mp-weixin">
  120. @import '../common/index.css';.van-collapse-item__title .van-cell__right-icon{-webkit-transform:rotate(90deg);transform:rotate(90deg);transition:-webkit-transform .3s;transition:transform .3s;transition:transform .3s,-webkit-transform .3s;transition:-webkit-transform var(--collapse-item-transition-duration,.3s);transition:transform var(--collapse-item-transition-duration,.3s);transition:transform var(--collapse-item-transition-duration,.3s),-webkit-transform var(--collapse-item-transition-duration,.3s)}.van-collapse-item__title--expanded .van-cell__right-icon{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}.van-collapse-item__title--disabled .van-cell,.van-collapse-item__title--disabled .van-cell__right-icon{color:#c8c9cc!important;color:var(--collapse-item-title-disabled-color,#c8c9cc)!important}.van-collapse-item__title--disabled .van-cell--hover{background-color:#fff!important;background-color:var(--white,#fff)!important}.van-collapse-item__wrapper{overflow:hidden}.van-collapse-item__wrapper--transition{transition:height .3s ease-in-out}.van-collapse-item__content{padding:15px;padding:var(--collapse-item-content-padding,15px);color:#969799;color:var(--collapse-item-content-text-color,#969799);font-size:13px;font-size:var(--collapse-item-content-font-size,13px);line-height:1.5;line-height:var(--collapse-item-content-line-height,1.5);background-color:#fff;background-color:var(--collapse-item-content-background-color,#fff)}
  121. </style>