uv-collapse-item.vue 6.7 KB

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