tn-collapse-item.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view class="tn-collapse-item-class tn-collapse-item" :style="[itemStyle]">
  3. <!-- 头部 -->
  4. <view
  5. class="tn-collapse-item__head"
  6. :style="[headStyle]"
  7. :hover-stay-time="200"
  8. :hover-class="hoverClass"
  9. @tap.stop="headClick"
  10. >
  11. <block v-if="!$slots['title-all'] || !$slots['$title-all']">
  12. <view
  13. v-if="!$slots.title || !$slots.$title"
  14. class="tn-collapse-item__head__title tn-text-ellipsis"
  15. :style="[
  16. { textAlign: align ? align : 'left'},
  17. isShow && activeStyle && !arrow ? activeStyle : ''
  18. ]"
  19. >{{ title }}</view>
  20. <view v-else>
  21. <slot name="title"></slot>
  22. </view>
  23. <view class="tn-collapse-item__head__icon__wrap">
  24. <view
  25. v-if="arrow"
  26. class="tn-icon-down tn-collapse-item__head__icon__arrow"
  27. :class="{'tn-collapse-item__head__icon__arrow--active': isShow}"
  28. :style="[arrowIconStyle]"
  29. ></view>
  30. </view>
  31. </block>
  32. <view v-else>
  33. <slot name="title-all"></slot>
  34. </view>
  35. </view>
  36. <!-- 内容 -->
  37. <view
  38. class="tn-collapse-item__body"
  39. :style="[{
  40. height: isShow ? height + 'px' : '0'
  41. }]"
  42. >
  43. <view class="tn-collapse-item__body__content" :id="elId" :style="[bodyStyle]">
  44. <slot></slot>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. export default {
  51. name: 'tn-collapse-item',
  52. props: {
  53. // 展开
  54. open: {
  55. type: Boolean,
  56. default: false
  57. },
  58. // 唯一标识
  59. name: {
  60. type: String,
  61. default: ''
  62. },
  63. // 标题
  64. title: {
  65. type: String,
  66. default: ''
  67. },
  68. // 标题对齐方式
  69. align: {
  70. type: String,
  71. default: 'left'
  72. },
  73. // 点击不收起
  74. disabled: {
  75. type: Boolean,
  76. default: false
  77. },
  78. // 活动时样式
  79. activeStyle: {
  80. type: Object,
  81. default() {
  82. return {}
  83. }
  84. },
  85. // 标识
  86. index: {
  87. type: [Number, String],
  88. default: ''
  89. }
  90. },
  91. computed: {
  92. arrowIconStyle() {
  93. let style = {}
  94. if (this.arrowColor) {
  95. style.color = this.arrowColor
  96. }
  97. return style
  98. }
  99. },
  100. data() {
  101. return {
  102. isShow: false,
  103. elId: this.$t.uuid(),
  104. // body高度
  105. height: 0,
  106. // 头部样式
  107. headStyle: {},
  108. // 主体样式
  109. bodyStyle: {},
  110. // item样式
  111. itemStyle: {},
  112. // 显示右边箭头
  113. arrow: true,
  114. // 箭头颜色
  115. arrowColor: '',
  116. // 点击头部时的效果样式
  117. hoverClass: ''
  118. }
  119. },
  120. watch: {
  121. open(value) {
  122. this.isShow = value
  123. }
  124. },
  125. created() {
  126. this.parent = false
  127. this.isShow = this.open
  128. },
  129. mounted() {
  130. this.init()
  131. },
  132. methods: {
  133. // 异步获取内容或者修改了内容时重新获取内容的信息
  134. init() {
  135. this.parent = this.$t.$parent.call(this, 'tn-collapse')
  136. if (this.parent) {
  137. this.nameSync = this.name ? this.name : this.parent.childrens.length
  138. // 不存在才添加对应实例
  139. !this.parent.childrens.includes(this) && this.parent.childrens.push(this)
  140. this.headStyle = this.parent.headStyle
  141. this.bodyStyle = this.parent.bodyStyle
  142. this.itemStyle = this.parent.itemStyle
  143. this.arrow = this.parent.arrow
  144. this.arrowColor = this.parent.arrowColor
  145. this.hoverClass = this.parent.hoverClass
  146. }
  147. this.$nextTick(() => {
  148. this.queryRect()
  149. })
  150. },
  151. // 点击头部
  152. headClick() {
  153. if (this.disabled) return
  154. if (this.parent && this.parent.accordion) {
  155. this.parent.childrens.map(child => {
  156. // 如果是手风琴模式,将其他的item关闭
  157. if (this !== child) {
  158. child.isShow = false
  159. }
  160. })
  161. }
  162. this.isShow = !this.isShow
  163. // 触发修改事件
  164. this.$emit('change', {
  165. index: this.index,
  166. show: this.isShow
  167. })
  168. // 只有在打开时才触发父元素的change
  169. if (this.isShow) this.parent && this.parent.onChange()
  170. this.$forceUpdate()
  171. },
  172. // 查询内容高度
  173. queryRect() {
  174. this._tGetRect('#'+this.elId).then(res => {
  175. this.height = res.height
  176. })
  177. }
  178. }
  179. }
  180. </script>
  181. <style lang="scss" scoped>
  182. .tn-collapse-item {
  183. &__head {
  184. position: relative;
  185. display: flex;
  186. flex-direction: row;
  187. justify-content: space-around;
  188. align-items: center;
  189. color: $tn-font-color;
  190. font-size: 30rpx;
  191. line-height: 1;
  192. padding: 24rpx 0;
  193. padding-left: 24rpx;
  194. text-align: left;
  195. background-color: #FFFFFF;
  196. &__title {
  197. flex: 1;
  198. overflow: hidden;
  199. }
  200. &__icon {
  201. &__arrow {
  202. transition: all 0.3s;
  203. margin-right: 20rpx;
  204. margin-left: 14rpx;
  205. font-size: inherit;
  206. &--active {
  207. transform: rotate(180deg);
  208. transform-origin: center center;
  209. }
  210. }
  211. }
  212. }
  213. &__body {
  214. transition: all 0.3s;
  215. overflow: hidden;
  216. &__content {
  217. overflow: hidden;
  218. font-size: 28rpx;
  219. color: $tn-font-color;
  220. text-align: left;
  221. background-color: #FFFFFF;
  222. padding-left: 24rpx;
  223. }
  224. }
  225. }
  226. </style>