u-collapse-item.vue 7.1 KB

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