Blame view

src/components/collapse/collapse.vue 2.74 KB
49306c7a   梁灏   add Collapse comp...
1
2
3
4
5
6
7
8
9
  <template>
      <div :class="classes">
          <slot></slot>
      </div>
  </template>
  <script>
      const prefixCls = 'ivu-collapse';
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
10
          name: 'Collapse',
49306c7a   梁灏   add Collapse comp...
11
12
13
14
15
          props: {
              accordion: {
                  type: Boolean,
                  default: false
              },
a190ce84   梁灏   support Collapse
16
              value: {
49306c7a   梁灏   add Collapse comp...
17
18
19
                  type: [Array, String]
              }
          },
a190ce84   梁灏   support Collapse
20
21
22
23
24
          data () {
              return {
                  currentValue: this.value
              };
          },
49306c7a   梁灏   add Collapse comp...
25
26
27
28
29
          computed: {
              classes () {
                  return `${prefixCls}`;
              }
          },
a190ce84   梁灏   support Collapse
30
          mounted () {
49306c7a   梁灏   add Collapse comp...
31
32
33
34
35
36
37
              this.setActive();
          },
          methods: {
              setActive () {
                  const activeKey = this.getActiveKey();
  
                  this.$children.forEach((child, index) => {
a190ce84   梁灏   support Collapse
38
                      const name = child.name || index.toString();
49306c7a   梁灏   add Collapse comp...
39
  
f60d2298   cky   delete: collapse组...
40
                      child.isActive = activeKey.indexOf(name) > -1;
49306c7a   梁灏   add Collapse comp...
41
                      child.index = index;
b0893113   jingsam   :art: add eslint
42
                  });
49306c7a   梁灏   add Collapse comp...
43
44
              },
              getActiveKey () {
a190ce84   梁灏   support Collapse
45
                  let activeKey = this.currentValue || [];
49306c7a   梁灏   add Collapse comp...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
                  const accordion = this.accordion;
  
                  if (!Array.isArray(activeKey)) {
                      activeKey = [activeKey];
                  }
  
                  if (accordion && activeKey.length > 1) {
                      activeKey = [activeKey[0]];
                  }
  
                  for (let i = 0; i < activeKey.length; i++) {
                      activeKey[i] = activeKey[i].toString();
                  }
  
                  return activeKey;
              },
              toggle (data) {
a190ce84   梁灏   support Collapse
63
                  const name = data.name.toString();
49306c7a   梁灏   add Collapse comp...
64
65
66
67
                  let newActiveKey = [];
  
                  if (this.accordion) {
                      if (!data.isActive) {
a190ce84   梁灏   support Collapse
68
                          newActiveKey.push(name);
49306c7a   梁灏   add Collapse comp...
69
70
71
                      }
                  } else {
                      let activeKey = this.getActiveKey();
a190ce84   梁灏   support Collapse
72
                      const nameIndex = activeKey.indexOf(name);
49306c7a   梁灏   add Collapse comp...
73
74
  
                      if (data.isActive) {
a190ce84   梁灏   support Collapse
75
76
                          if (nameIndex > -1) {
                              activeKey.splice(nameIndex, 1);
49306c7a   梁灏   add Collapse comp...
77
78
                          }
                      } else {
a190ce84   梁灏   support Collapse
79
80
                          if (nameIndex < 0) {
                              activeKey.push(name);
49306c7a   梁灏   add Collapse comp...
81
82
83
84
85
86
                          }
                      }
  
                      newActiveKey = activeKey;
                  }
  
a190ce84   梁灏   support Collapse
87
88
                  this.currentValue = newActiveKey;
                  this.$emit('input', newActiveKey);
49306c7a   梁灏   add Collapse comp...
89
90
91
92
                  this.$emit('on-change', newActiveKey);
              }
          },
          watch: {
a190ce84   梁灏   support Collapse
93
94
95
96
              value (val) {
                  this.currentValue = val;
              },
              currentValue () {
49306c7a   梁灏   add Collapse comp...
97
98
99
                  this.setActive();
              }
          }
b0893113   jingsam   :art: add eslint
100
101
      };
  </script>