u-collapse-item.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <view class="u-collapse-item">
  3. <u-cell
  4. :title="$slots.title ? '' : title"
  5. :value="value"
  6. :label="label"
  7. :icon="icon"
  8. :isLink="isLink"
  9. :clickable="clickable"
  10. :border="parentData.border && showBorder"
  11. @click="clickHandler"
  12. :arrowDirection="expanded ? 'up' : 'down'"
  13. :disabled="disabled"
  14. >
  15. <!-- 微信小程序不支持,因为微信中不支持 <slot name="title" slot="title" />的写法 -->
  16. <template #title>
  17. <slot name="title">
  18. <text v-if="!$slots.title && title">
  19. {{title}}
  20. </text>
  21. </slot>
  22. </template>
  23. <template #icon>
  24. <slot name="icon">
  25. <u-icon v-if="!$slots.icon && icon" :size="22" :name="icon"></u-icon>
  26. </slot>
  27. </template>
  28. <template #value>
  29. <slot name="value">
  30. <text v-if="!$slots.value && value">
  31. {{value}}
  32. </text>
  33. </slot>
  34. </template>
  35. <template #right-icon>
  36. <slot name="right-icon">
  37. </slot>
  38. </template>
  39. </u-cell>
  40. <view
  41. class="u-collapse-item__content"
  42. :animation="animationData"
  43. ref="animation"
  44. >
  45. <view
  46. class="u-collapse-item__content__text content-class"
  47. :id="elId"
  48. :ref="elId"
  49. ><slot /></view>
  50. </view>
  51. <u-line v-if="parentData.border"></u-line>
  52. </view>
  53. </template>
  54. <script>
  55. import props from './props.js';
  56. import mpMixin from '../../libs/mixin/mpMixin';
  57. import mixin from '../../libs/mixin/mixin';
  58. import { nextTick } from 'vue';
  59. import { guid, sleep, error } from '../../libs/function/index';
  60. import test from '../../libs/function/test';
  61. // #ifdef APP-NVUE
  62. const animation = uni.requireNativePlugin('animation')
  63. const dom = uni.requireNativePlugin('dom')
  64. // #endif
  65. /**
  66. * collapseItem 折叠面板Item
  67. * @description 通过折叠面板收纳内容区域(搭配u-collapse使用)
  68. * @tutorial https://ijry.github.io/uview-plus/components/collapse.html
  69. * @property {String} title 标题
  70. * @property {String} value 标题右侧内容
  71. * @property {String} label 标题下方的描述信息
  72. * @property {Boolean} disbled 是否禁用折叠面板 ( 默认 false )
  73. * @property {Boolean} isLink 是否展示右侧箭头并开启点击反馈 ( 默认 true )
  74. * @property {Boolean} clickable 是否开启点击反馈 ( 默认 true )
  75. * @property {Boolean} border 是否显示内边框 ( 默认 true )
  76. * @property {String} align 标题的对齐方式 ( 默认 'left' )
  77. * @property {String | Number} name 唯一标识符
  78. * @property {String} icon 标题左侧图片,可为绝对路径的图片或内置图标
  79. * @event {Function} change 某个item被打开或者收起时触发
  80. * @example <u-collapse-item :title="item.head" v-for="(item, index) in itemList" :key="index">{{item.body}}</u-collapse-item>
  81. */
  82. export default {
  83. name: "u-collapse-item",
  84. mixins: [mpMixin, mixin, props],
  85. data() {
  86. return {
  87. elId: guid(),
  88. // uni.createAnimation的导出数据
  89. animationData: {},
  90. // 是否展开状态
  91. expanded: false,
  92. // 根据expanded确定是否显示border,为了控制展开时,cell的下划线更好的显示效果,进行一定时间的延时
  93. showBorder: false,
  94. // 是否动画中,如果是则不允许继续触发点击
  95. animating: false,
  96. // 父组件u-collapse的参数
  97. parentData: {
  98. accordion: false,
  99. border: false
  100. }
  101. };
  102. },
  103. watch: {
  104. expanded(n) {
  105. clearTimeout(this.timer)
  106. this.timer = null
  107. // 这里根据expanded的值来进行一定的延时,是为了cell的下划线更好的显示效果
  108. this.timer = setTimeout(() => {
  109. this.showBorder = n
  110. }, n ? 10 : 290)
  111. }
  112. },
  113. mounted() {
  114. this.init()
  115. console.log('$slots', this.$slots)
  116. },
  117. methods: {
  118. // 异步获取内容,或者动态修改了内容时,需要重新初始化
  119. async init() {
  120. // 初始化数据
  121. this.updateParentData()
  122. if (!this.parent) {
  123. return error('u-collapse-item必须要搭配u-collapse组件使用')
  124. }
  125. const {
  126. value,
  127. accordion,
  128. children = []
  129. } = this.parent
  130. if (accordion) {
  131. if (test.array(value)) {
  132. return error('手风琴模式下,u-collapse组件的value参数不能为数组')
  133. }
  134. this.expanded = this.name == value
  135. } else {
  136. if (!test.array(value) && value !== null) {
  137. return error('非手风琴模式下,u-collapse组件的value参数必须为数组')
  138. }
  139. this.expanded = (value || []).some(item => item == this.name)
  140. }
  141. // 设置组件的展开或收起状态
  142. await nextTick()
  143. this.setContentAnimate()
  144. },
  145. updateParentData() {
  146. // 此方法在mixin中
  147. this.getParentData('u-collapse')
  148. },
  149. async setContentAnimate() {
  150. // 每次面板打开或者收起时,都查询元素尺寸
  151. // 好处是,父组件从服务端获取内容后,变更折叠面板后可以获得最新的高度
  152. const rect = await this.queryRect()
  153. const height = this.expanded ? rect.height : 0
  154. this.animating = true
  155. // #ifdef APP-NVUE
  156. const ref = this.$refs['animation'].ref
  157. animation.transition(ref, {
  158. styles: {
  159. height: height + 'px'
  160. },
  161. duration: this.duration,
  162. // 必须设置为true,否则会到面板收起或展开时,页面其他元素不会随之调整它们的布局
  163. needLayout: true,
  164. timingFunction: 'ease-in-out',
  165. }, () => {
  166. this.animating = false
  167. })
  168. // #endif
  169. // #ifndef APP-NVUE
  170. const animation = uni.createAnimation({
  171. timingFunction: 'ease-in-out',
  172. });
  173. animation
  174. .height(height)
  175. .step({
  176. duration: this.duration,
  177. })
  178. .step()
  179. // 导出动画数据给面板的animationData值
  180. this.animationData = animation.export()
  181. // 标识动画结束
  182. sleep(this.duration).then(() => {
  183. this.animating = false
  184. })
  185. // #endif
  186. },
  187. // 点击collapsehead头部
  188. clickHandler() {
  189. if (this.disabled && this.animating) return
  190. // 设置本组件为相反的状态
  191. this.parent && this.parent.onChange(this)
  192. },
  193. // 查询内容高度
  194. queryRect() {
  195. // #ifndef APP-NVUE
  196. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://ijry.github.io/uview-plus/js/getRect.html
  197. // 组件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
  198. return new Promise(resolve => {
  199. this.$uGetRect(`#${this.elId}`).then(size => {
  200. resolve(size)
  201. })
  202. })
  203. // #endif
  204. // #ifdef APP-NVUE
  205. // nvue下,使用dom模块查询元素高度
  206. // 返回一个promise,让调用此方法的主体能使用then回调
  207. return new Promise(resolve => {
  208. dom.getComponentRect(this.$refs[this.elId], res => {
  209. resolve(res.size)
  210. })
  211. })
  212. // #endif
  213. }
  214. },
  215. };
  216. </script>
  217. <style lang="scss" scoped>
  218. @import "../../libs/css/components.scss";
  219. .u-collapse-item {
  220. &__content {
  221. overflow: hidden;
  222. height: 0;
  223. &__text {
  224. padding: 12px 15px;
  225. color: $u-content-color;
  226. font-size: 14px;
  227. line-height: 18px;
  228. }
  229. }
  230. }
  231. </style>