u-collapse-item.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <view class="u-collapse-item" :style="[itemStyle]">
  3. <view :hover-stay-time="200" class="u-collapse-head" @tap.stop="headClick" :hover-class="hoverClass" :style="[headStyle]">
  4. <view class="u-collapse-title u-line-1" :style="[{ textAlign: align ? align : 'left' },
  5. isShow && activeStyle && !arrow ? activeStyle : '']">
  6. {{ title }}
  7. </view>
  8. <view class="u-icon-wrap">
  9. <u-icon v-if="arrow" :color="arrowColor ? arrowColor : $u.color.tipsColor" :class="{ 'u-arrow-down-icon-active': isShow }"
  10. class="u-arrow-down-icon" name="arrow-down"></u-icon>
  11. </view>
  12. </view>
  13. <view class="u-collapse-body" :style="[{
  14. height: isShow ? height + 'px' : '0'
  15. }]">
  16. <view class="u-collapse-content" :id="elId" :style="[bodyStyle]">
  17. <slot></slot>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * collapseItem 手风琴Item
  25. * @description 通过折叠面板收纳内容区域(搭配u-collapse使用)
  26. * @tutorial https://www.uviewui.com/components/collapse.html
  27. * @property {String} title 面板标题
  28. * @property {String Number} index 主要用于事件的回调,标识那个Item被点击
  29. * @property {Boolean} disabled 面板是否可以打开或收起(默认false)
  30. * @property {Boolean} open 设置某个面板的初始状态是否打开(默认false)
  31. * @property {String Number} name 唯一标识符,如不设置,默认用当前collapse-item的索引值
  32. * @property {String} align 标题的对齐方式(默认left)
  33. * @property {Object} active-style 不显示箭头时,可以添加当前选择的collapse-item活动样式,对象形式
  34. * @event {Function} change 某个item被打开或者收起时触发
  35. * @example <u-collapse-item :title="item.head" v-for="(item, index) in itemList" :key="index">{{item.body}}</u-collapse-item>
  36. */
  37. export default {
  38. name: "u-collapse-item",
  39. props: {
  40. // 标题
  41. title: {
  42. type: String,
  43. default: ''
  44. },
  45. // 标题的对齐方式
  46. align: {
  47. type: String,
  48. default: 'left'
  49. },
  50. // 是否可以点击收起
  51. disabled: {
  52. type: Boolean,
  53. default: false
  54. },
  55. // collapse显示与否
  56. open: {
  57. type: Boolean,
  58. default: false
  59. },
  60. // 唯一标识符
  61. name: {
  62. type: [Number, String],
  63. default: ''
  64. },
  65. //活动样式
  66. activeStyle: {
  67. type: Object,
  68. default () {
  69. return {}
  70. }
  71. },
  72. // 标识当前为第几个
  73. index: {
  74. type: [String, Number],
  75. default: ''
  76. }
  77. },
  78. inject: ['uCollapse'],
  79. data() {
  80. return {
  81. isShow: false,
  82. elId: this.$u.guid(),
  83. height: 0, // body内容的高度
  84. headStyle: {}, // 头部样式,对象形式
  85. bodyStyle: {}, // 主体部分样式
  86. //itemStyle: {}, // 每个item的整体样式
  87. arrowColor: '', // 箭头的颜色
  88. hoverClass: '', // 头部按下时的效果样式类
  89. };
  90. },
  91. mounted() {
  92. this.$nextTick(() => {
  93. this.queryRect();
  94. });
  95. },
  96. watch: {
  97. open(val) {
  98. this.isShow = val;
  99. }
  100. },
  101. computed: {
  102. arrow() {
  103. return this.uCollapse.arrow;
  104. },
  105. itemStyle() {
  106. return this.uCollapse.itemStyle;
  107. }
  108. },
  109. created() {
  110. this.isShow = this.open;
  111. this.nameSync = this.name ? this.name : this.uCollapse.childrens.length;
  112. this.uCollapse.childrens.push(this);
  113. //this.itemStyle = this.uCollapse.itemStyle;
  114. this.headStyle = this.uCollapse.headStyle;
  115. this.bodyStyle = this.uCollapse.bodyStyle;
  116. this.arrowColor = this.uCollapse.arrowColor;
  117. this.hoverClass = this.uCollapse.hoverClass;
  118. },
  119. methods: {
  120. // 点击collapsehead头部
  121. headClick() {
  122. if (this.disabled) return;
  123. if (this.uCollapse.accordion == true) {
  124. this.uCollapse.childrens.map(val => {
  125. // 自身不设置为false,因为后面有this.isShow = !this.isShow;处理了
  126. if (this != val) {
  127. val.isShow = false;
  128. }
  129. });
  130. }
  131. this.isShow = !this.isShow;
  132. // 触发本组件的事件
  133. uni.$emit('change', {
  134. index: this.index,
  135. show: this.isShow
  136. })
  137. // 只有在打开时才发出事件
  138. if (this.isShow) this.uCollapse.onChange();
  139. this.$forceUpdate();
  140. },
  141. // 查询内容高度
  142. queryRect() {
  143. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://www.uviewui.com/js/getRect.html
  144. // 组件内部一般用this.$uGetRect,对外的为this.$u.getRect,二者功能一致,名称不同
  145. this.$uGetRect('#' + this.elId).then(res => {
  146. this.height = res.height;
  147. })
  148. }
  149. }
  150. };
  151. </script>
  152. <style lang="scss" scoped>
  153. .u-collapse-head {
  154. position: relative;
  155. display: flex;
  156. justify-content: space-between;
  157. align-items: center;
  158. color: $u-main-color;
  159. font-size: 30rpx;
  160. line-height: 1;
  161. padding: 24rpx 0;
  162. text-align: left;
  163. }
  164. .u-collapse-title {
  165. flex: 1;
  166. overflow: hidden;
  167. margin-right: 14rpx;
  168. }
  169. .u-arrow-down-icon {
  170. transition: all 0.3s;
  171. margin-right: 24rpx;
  172. }
  173. .u-arrow-down-icon-active {
  174. transform: rotate(180deg);
  175. transform-origin: center center;
  176. }
  177. .u-collapse-body {
  178. overflow: hidden;
  179. transition: all 0.3s;
  180. }
  181. .u-collapse-content {
  182. overflow: hidden;
  183. font-size: 28rpx;
  184. color: $u-tips-color;
  185. text-align: left;
  186. }
  187. </style>