jiangping
2023-10-26 627c3e0a6920131d75eafa4646db373ccc936546
minipro_standard/uni_modules/uview-ui/components/u-collapse-item/u-collapse-item.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,225 @@
<template>
   <view class="u-collapse-item">
      <u-cell
         :title="title"
         :value="value"
         :label="label"
         :icon="icon"
         :isLink="isLink"
         :clickable="clickable"
         :border="parentData.border && showBorder"
         @click="clickHandler"
         :arrowDirection="expanded ? 'up' : 'down'"
         :disabled="disabled"
      >
         <!-- #ifndef MP-WEIXIN -->
         <!-- å¾®ä¿¡å°ç¨‹åºä¸æ”¯æŒï¼Œå› ä¸ºå¾®ä¿¡ä¸­ä¸æ”¯æŒ <slot name="title" slot="title" />的写法 -->
         <template slot="title">
            <slot name="title"></slot>
         </template>
         <template slot="icon">
            <slot name="icon"></slot>
         </template>
         <template slot="value">
            <slot name="value"></slot>
         </template>
         <template slot="right-icon">
            <slot name="right-icon"></slot>
         </template>
         <!-- #endif -->
      </u-cell>
      <view
         class="u-collapse-item__content"
         :animation="animationData"
         ref="animation"
      >
         <view
            class="u-collapse-item__content__text content-class"
            :id="elId"
            :ref="elId"
         ><slot /></view>
      </view>
      <u-line v-if="parentData.border"></u-line>
   </view>
</template>
<script>
   import props from './props.js';
   // #ifdef APP-NVUE
   const animation = uni.requireNativePlugin('animation')
   const dom = uni.requireNativePlugin('dom')
   // #endif
   /**
    * collapseItem æŠ˜å é¢æ¿Item
    * @description é€šè¿‡æŠ˜å é¢æ¿æ”¶çº³å†…容区域(搭配u-collapse使用)
    * @tutorial https://www.uviewui.com/components/collapse.html
    * @property {String}         title       æ ‡é¢˜
    * @property {String}         value       æ ‡é¢˜å³ä¾§å†…容
    * @property {String}         label       æ ‡é¢˜ä¸‹æ–¹çš„æè¿°ä¿¡æ¯
    * @property {Boolean}         disbled    æ˜¯å¦ç¦ç”¨æŠ˜å é¢æ¿ ( é»˜è®¤ false )
    * @property {Boolean}         isLink       æ˜¯å¦å±•示右侧箭头并开启点击反馈 ( é»˜è®¤ true )
    * @property {Boolean}         clickable   æ˜¯å¦å¼€å¯ç‚¹å‡»åé¦ˆ ( é»˜è®¤ true )
    * @property {Boolean}         border      æ˜¯å¦æ˜¾ç¤ºå†…边框 ( é»˜è®¤ true )
    * @property {String}         align      æ ‡é¢˜çš„对齐方式 ( é»˜è®¤ 'left' )
    * @property {String | Number}   name      å”¯ä¸€æ ‡è¯†ç¬¦
    * @property {String}         icon      æ ‡é¢˜å·¦ä¾§å›¾ç‰‡ï¼Œå¯ä¸ºç»å¯¹è·¯å¾„的图片或内置图标
    * @event {Function}         change          æŸä¸ªitem被打开或者收起时触发
    * @example <u-collapse-item :title="item.head" v-for="(item, index) in itemList" :key="index">{{item.body}}</u-collapse-item>
    */
   export default {
      name: "u-collapse-item",
      mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
      data() {
         return {
            elId: uni.$u.guid(),
            // uni.createAnimation的导出数据
            animationData: {},
            // æ˜¯å¦å±•开状态
            expanded: false,
            // æ ¹æ®expanded确定是否显示border,为了控制展开时,cell的下划线更好的显示效果,进行一定时间的延时
            showBorder: false,
            // æ˜¯å¦åŠ¨ç”»ä¸­ï¼Œå¦‚æžœæ˜¯åˆ™ä¸å…è®¸ç»§ç»­è§¦å‘ç‚¹å‡»
            animating: false,
            // çˆ¶ç»„ä»¶u-collapse的参数
            parentData: {
               accordion: false,
               border: false
            }
         };
      },
      watch: {
         expanded(n) {
            clearTimeout(this.timer)
            this.timer = null
            // è¿™é‡Œæ ¹æ®expanded的值来进行一定的延时,是为了cell的下划线更好的显示效果
            this.timer = setTimeout(() => {
               this.showBorder = n
            }, n ? 10 : 290)
         }
      },
      mounted() {
         this.init()
      },
      methods: {
         // å¼‚步获取内容,或者动态修改了内容时,需要重新初始化
         init() {
            // åˆå§‹åŒ–数据
            this.updateParentData()
            if (!this.parent) {
               return uni.$u.error('u-collapse-item必须要搭配u-collapse组件使用')
            }
            const {
               value,
               accordion,
               children = []
            } = this.parent
            if (accordion) {
               if (uni.$u.test.array(value)) {
                  return uni.$u.error('手风琴模式下,u-collapse组件的value参数不能为数组')
               }
               this.expanded = this.name == value
            } else {
               if (!uni.$u.test.array(value) && value !== null) {
                  return uni.$u.error('非手风琴模式下,u-collapse组件的value参数必须为数组')
               }
               this.expanded = (value || []).some(item => item == this.name)
            }
            // è®¾ç½®ç»„件的展开或收起状态
            this.$nextTick(function() {
               this.setContentAnimate()
            })
         },
         updateParentData() {
            // æ­¤æ–¹æ³•在mixin中
            this.getParentData('u-collapse')
         },
         async setContentAnimate() {
            // æ¯æ¬¡é¢æ¿æ‰“开或者收起时,都查询元素尺寸
            // å¥½å¤„是,父组件从服务端获取内容后,变更折叠面板后可以获得最新的高度
            const rect = await this.queryRect()
            const height = this.expanded ? rect.height : 0
            this.animating = true
            // #ifdef APP-NVUE
            const ref = this.$refs['animation'].ref
            animation.transition(ref, {
               styles: {
                  height: height + 'px'
               },
               duration: this.duration,
               // å¿…须设置为true,否则会到面板收起或展开时,页面其他元素不会随之调整它们的布局
               needLayout: true,
               timingFunction: 'ease-in-out',
            }, () => {
               this.animating = false
            })
            // #endif
            // #ifndef APP-NVUE
            const animation = uni.createAnimation({
               timingFunction: 'ease-in-out',
            });
            animation
               .height(height)
               .step({
                  duration: this.duration,
               })
               .step()
            // å¯¼å‡ºåŠ¨ç”»æ•°æ®ç»™é¢æ¿çš„animationData值
            this.animationData = animation.export()
            // æ ‡è¯†åŠ¨ç”»ç»“æŸ
            uni.$u.sleep(this.duration).then(() => {
               this.animating = false
            })
            // #endif
         },
         // ç‚¹å‡»collapsehead头部
         clickHandler() {
            if (this.disabled && this.animating) return
            // è®¾ç½®æœ¬ç»„件为相反的状态
            this.parent && this.parent.onChange(this)
         },
         // æŸ¥è¯¢å†…容高度
         queryRect() {
            // #ifndef APP-NVUE
            // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://www.uviewui.com/js/getRect.html
            // ç»„件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
            return new Promise(resolve => {
               this.$uGetRect(`#${this.elId}`).then(size => {
                  resolve(size)
               })
            })
            // #endif
            // #ifdef APP-NVUE
            // nvue下,使用dom模块查询元素高度
            // è¿”回一个promise,让调用此方法的主体能使用then回调
            return new Promise(resolve => {
               dom.getComponentRect(this.$refs[this.elId], res => {
                  resolve(res.size)
               })
            })
            // #endif
         }
      },
   };
</script>
<style lang="scss" scoped>
   @import "../../libs/css/components.scss";
   .u-collapse-item {
      &__content {
         overflow: hidden;
         height: 0;
         &__text {
            padding: 12px 15px;
            color: $u-content-color;
            font-size: 14px;
            line-height: 18px;
         }
      }
   }
</style>