uni-recycle-item.uvue 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<template>
  <view class="uni-recycle-view-item" :style="this.itemHeight != 0 ? {height: this.itemHeight + 'px'} : {}">
    <slot></slot>
  </view>
</template>

<script>
  /**
   * recycle-item 长列表子项组件
   * @description 用于展示超长列表数据每一项
   * @property {any[]} item 当前组件渲染的列表项
   */
  export default {
    name: "uni-recycle-item",
    props: {
      item: {
        type: Object as PropType<any>,
        required: true
      }
    },
    inject: {
      itemHeight: {
        type: Number as PropType<number>
      },
      setCachedSize: {
        type: Function as PropType<(item : any, size : number) => void>
      },
      getCachedSize: {
        type: Function as PropType<(item : any) => number | null>
      },
    },
    mounted() {
      if (this.itemHeight == 0) {
        const cachedSize = this.getCachedSize(this.item)
        if(cachedSize == null) {
          uni.createSelectorQuery().in(this).select('.uni-recycle-view-item').boundingClientRect().exec((ret) => {
            this.setCachedSize(this.item, (ret[0] as NodeInfo).height!)
          })
        }
      }
    }
  }
</script>

<style>
  .uni-recycle-view-item {
    box-sizing: border-box;
    overflow: hidden;
  }
50
</style>