闫增涛
2024-02-23 fdfb3ca757ecd6c396632ed276ff354671d3a7e5
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Component({
  externalClasses: ['custom-class'],
 
  properties: {
    activeKey: {
      type: Number,
      value: 0,
    },
    tabList: {
      type: Array,
      value: [],
    },
    showMore: Boolean, // 是否需要下拉功能
  },
  observers: {
    activeKey(newVal) {
      if (this.properties.tabList && newVal) {
        this.setActive(newVal).catch((e) => {
          console.error(e);
        });
      }
    },
  },
 
  data: {
    currentActive: -1,
  },
  attached() {
    this.setActive(this.properties.activeKey).catch((e) => {
      console.error(e);
    });
  },
 
  methods: {
    setActive(activeKey) {
      if (
        !this.properties.tabList[activeKey] ||
        this.properties.tabList[activeKey].disabled
      ) {
        return Promise.reject('数据异常或不可操作');
      }
      return new Promise((resolve) => {
        this.setData(
          {
            currentActive: activeKey,
          },
          () => resolve(),
        );
      });
    },
    onClick(event) {
      let activeKey;
      if (event.type === 'select') {
        activeKey = event.detail;
      } else {
        activeKey = event.currentTarget.dataset.index;
      }
      this.setActive(activeKey)
        .then(() => {
          const { currentActive } = this.data;
          this.triggerEvent('change', { index: currentActive });
        })
        .catch((e) => {
          console.error(e);
        });
    },
  },
});