3
0

u-collapse-item.vue 7.3 KB

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