index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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')" style="height: 0;" :animation="animation">
  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. VantComponent({
  23. classes: ['title-class', 'content-class'],
  24. relation: {
  25. name: 'collapse',
  26. type: 'ancestor',
  27. current: 'collapse-item',
  28. },
  29. props: {
  30. name: null,
  31. title: null,
  32. value: null,
  33. icon: String,
  34. label: String,
  35. disabled: Boolean,
  36. clickable: Boolean,
  37. border: {
  38. type: Boolean,
  39. value: true,
  40. },
  41. isLink: {
  42. type: Boolean,
  43. value: true,
  44. },
  45. },
  46. data: {
  47. expanded: false,
  48. },
  49. created() {
  50. this.animation = wx.createAnimation({
  51. duration: 0,
  52. timingFunction: 'ease-in-out',
  53. });
  54. },
  55. mounted() {
  56. this.updateExpanded();
  57. this.inited = true;
  58. },
  59. methods: {
  60. updateExpanded() {
  61. if (!this.parent) {
  62. return Promise.resolve();
  63. }
  64. const { value, accordion } = this.parent.data;
  65. const { children = [] } = this.parent;
  66. const { name } = this.data;
  67. const index = children.indexOf(this);
  68. const currentName = name == null ? index : name;
  69. const expanded = accordion
  70. ? value === currentName
  71. : (value || []).some((name) => name === currentName);
  72. if (expanded !== this.data.expanded) {
  73. this.updateStyle(expanded);
  74. }
  75. this.setData({ index, expanded });
  76. },
  77. updateStyle(expanded) {
  78. const { inited } = this;
  79. this.getRect('.van-collapse-item__content')
  80. .then((rect) => rect.height)
  81. .then((height) => {
  82. const { animation } = this;
  83. if (expanded) {
  84. animation
  85. .height(height)
  86. .top(1)
  87. .step({
  88. duration: inited ? 300 : 1,
  89. })
  90. .height('auto')
  91. .step();
  92. this.setData({
  93. animation: animation.export(),
  94. });
  95. return;
  96. }
  97. animation.height(height).top(0).step({ duration: 1 }).height(0).step({
  98. duration: 300,
  99. });
  100. this.setData({
  101. animation: animation.export(),
  102. });
  103. });
  104. },
  105. onClick() {
  106. if (this.data.disabled) {
  107. return;
  108. }
  109. const { name, expanded } = this.data;
  110. const index = this.parent.children.indexOf(this);
  111. const currentName = name == null ? index : name;
  112. this.parent.switch(currentName, !expanded);
  113. },
  114. },
  115. });
  116. export default global['__wxComponents']['vant/collapse-item/index']
  117. </script>
  118. <style platform="mp-weixin">
  119. @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__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)}
  120. </style>