TabBar.js 3.44 KB
// components/TabBar/TabBar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabList: {
      type: Array,
      observer: function (list) {
        // 规范化 tab 列表为对象数组,支持字符串与多种字段名
        this.normalizeTabList(list)
      }
    },
    activeIndex: {
      type: Number,
      observer: function (n) {
        console.log(this.data.tabIndex, 'activeIndex', n)
        console.log(this.data.tabIndex != n)
        if (this.data.tabIndex != n) {
          this.setData({
            tabIndex: this.data.activeIndex
          })
          this.setTab(this.data.tabIndex)
        }
      }
    },
    scroll: Boolean
  },
  /**
   * 组件的初始数据
   */
  data: {
    tabIndex: 0,
    tabItems: []
  },
  lifetimes: {
    ready() {
      // console.log('lifetimes',this.data.tabIndex);
      // console.log('lifetimes',this.data.activeIndex);
      // 确保首屏渲染有规范化后的 tabItems
      this.normalizeTabList(this.data.tabList)
      this.setTab(this.data.tabIndex)
      let that = this
      wx.$eventbus.on('page_show', n => {
        !that.data.width && that.setTab(that.data.tabIndex)
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    normalizeTabList(list = []) {
      const tabItems = (list || []).map(item => {
        let label = ''
        let count
        if (typeof item === 'string') {
          label = item
        } else if (item && typeof item === 'object') {


          label =
            item.text ??
            item.label ??
            item.name ??
            item.title ??
            item.brandName ??
            ''
          if (item.firstBrandId == -1) {
            label = item.firstBrandName
          }
          count = item.count
        } else {
          label = ''
        }
        return { label, count, _raw: item }
      })
      this.setData({ tabItems })
    },

    change(e) {
      let { index } = e.currentTarget.dataset
      this.setTab(index)
    },
    setTab(index = 0) {
      let { tabIndex } = this.data
      wx.$util.getViewInfo(
        `#tab-${tabIndex}`,
        old => {
          let oldLeft = old.left
          let oldWidth = old.width
          wx.$util.getViewInfo(
            `#tab-${index}`,
            res => {
              let { left, width } = res
              if (tabIndex != index) {
                let readyLeft = oldLeft > left ? left : oldLeft
                let readyWidth =
                  oldLeft > left ? oldLeft - left : left - oldLeft
                this.setData({
                  activeStyle: `width: var(--fontSize64);background-color:#347BFF;border-radius:3rpx;left:${readyLeft + readyWidth / 2
                    }px;transition: all .1s;`
                })
              }
              setTimeout(() => {
                this.setData({
                  activeStyle: `width: var(--fontSize64);left:${left + (width - 32) / 2
                    }px;background-color:#347BFF;`,
                  tabIndex: index,
                  to: `to-${index}`,
                  width
                })
                const itemObj =
                  this.data.tabItems && this.data.tabItems[index]
                    ? this.data.tabItems[index]._raw ??
                    this.data.tabItems[index]
                    : undefined
                this.triggerEvent('click', { index, item: itemObj })
              }, 100)
            },
            this
          )
        },
        this
      )
    }
  }
})