You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.0 KiB

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