Blame view

src/Tabs/Tabs.js 2.05 KB
3a3ecabe   Imshann   init
1
2
3
4
5
6
  import template from "./Tabs.html";
  import style from "antd/lib/tabs/style/index.css";
  
  class Tabs {
      useModules = ["esNgAntd"];
  
6674355b   Imshann   优化选项阿凯
7
      context = true;
3a3ecabe   Imshann   init
8
9
10
11
12
13
14
15
16
  
      state = {
          childrens: [],
          labels: [],
          tabWidth: 0,
          offsetLeft: 0,
          activeKey: this.props.defaultActiveKey,
      };
  
6674355b   Imshann   优化选项阿凯
17
      constructor($element) {
3a3ecabe   Imshann   init
18
19
20
          esNgAntd.createStyle("ant-tabs", style);
          if (this.props.defaultActiveKey) {
              setTimeout(() => {
6674355b   Imshann   优化选项阿凯
21
                  if ($element[0].querySelector(".ant-tabs-tab-active")) {
3a3ecabe   Imshann   init
22
23
24
25
26
27
28
29
30
31
32
33
                      this.state.tabWidth = $element[0].querySelector(
                          ".ant-tabs-tab-active"
                      ).clientWidth;
                      this.$apply();
                  }
              }, 0);
          }
      }
  
      handleClick(labelKey, tabKey, event) {
          let target = event.target;
          this.state.tabWidth = target.clientWidth;
6674355b   Imshann   优化选项阿凯
34
35
36
          let currentMarginLeft = parseFloat(
              getComputedStyle(target, null).marginLeft
          );
3a3ecabe   Imshann   init
37
38
39
40
          let beforeTarget = Array.prototype.slice
              .call(target.parentNode.querySelectorAll("div"))
              .splice(0, tabKey);
          if (beforeTarget.length > 0) {
6674355b   Imshann   优化选项阿凯
41
42
43
44
45
46
47
48
49
50
51
              this.state.offsetLeft =
                  beforeTarget
                      .map(function (item) {
                          return (
                              item.clientWidth +
                              parseFloat(getComputedStyle(item, null).marginLeft)
                          );
                      })
                      .reduce(function (prev, curr) {
                          return prev + curr;
                      }) + currentMarginLeft;
3a3ecabe   Imshann   init
52
53
54
55
56
57
58
59
60
61
62
          } else {
              this.state.offsetLeft = 0;
          }
          this.state.activeKey = labelKey;
          this.state.childrens.forEach(function (element) {
              element.state.activeKey = labelKey;
          });
          this.props.onChange({
              key: labelKey,
          });
      }
6674355b   Imshann   优化选项阿凯
63
64
65
66
  
      render() {
          return template;
      }
3a3ecabe   Imshann   init
67
  }
6674355b   Imshann   优化选项阿凯
68
69
70
71
72
  
  Tabs.propTypes = {
      defaultActiveKey: PropTypes.string,
      onChange: PropTypes.function,
  };