Blame view

src/components/collapse/collapse.vue 2.98 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
                  type: [Array, String]
3f611f57   梁灏   Collapse add simp...
18
19
20
21
              },
              simple: {
                  type: Boolean,
                  default: false
49306c7a   梁灏   add Collapse comp...
22
23
              }
          },
a190ce84   梁灏   support Collapse
24
25
26
27
28
          data () {
              return {
                  currentValue: this.value
              };
          },
49306c7a   梁灏   add Collapse comp...
29
30
          computed: {
              classes () {
3f611f57   梁灏   Collapse add simp...
31
32
33
34
35
36
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-simple`]: this.simple
                      }
                  ];
49306c7a   梁灏   add Collapse comp...
37
38
              }
          },
a190ce84   梁灏   support Collapse
39
          mounted () {
49306c7a   梁灏   add Collapse comp...
40
41
42
43
44
45
46
              this.setActive();
          },
          methods: {
              setActive () {
                  const activeKey = this.getActiveKey();
  
                  this.$children.forEach((child, index) => {
a190ce84   梁灏   support Collapse
47
                      const name = child.name || index.toString();
49306c7a   梁灏   add Collapse comp...
48
  
f60d2298   cky   delete: collapse组...
49
                      child.isActive = activeKey.indexOf(name) > -1;
49306c7a   梁灏   add Collapse comp...
50
                      child.index = index;
b0893113   jingsam   :art: add eslint
51
                  });
49306c7a   梁灏   add Collapse comp...
52
53
              },
              getActiveKey () {
a190ce84   梁灏   support Collapse
54
                  let activeKey = this.currentValue || [];
49306c7a   梁灏   add Collapse comp...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                  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
72
                  const name = data.name.toString();
49306c7a   梁灏   add Collapse comp...
73
74
75
76
                  let newActiveKey = [];
  
                  if (this.accordion) {
                      if (!data.isActive) {
a190ce84   梁灏   support Collapse
77
                          newActiveKey.push(name);
49306c7a   梁灏   add Collapse comp...
78
79
80
                      }
                  } else {
                      let activeKey = this.getActiveKey();
a190ce84   梁灏   support Collapse
81
                      const nameIndex = activeKey.indexOf(name);
49306c7a   梁灏   add Collapse comp...
82
83
  
                      if (data.isActive) {
a190ce84   梁灏   support Collapse
84
85
                          if (nameIndex > -1) {
                              activeKey.splice(nameIndex, 1);
49306c7a   梁灏   add Collapse comp...
86
87
                          }
                      } else {
a190ce84   梁灏   support Collapse
88
89
                          if (nameIndex < 0) {
                              activeKey.push(name);
49306c7a   梁灏   add Collapse comp...
90
91
92
93
94
95
                          }
                      }
  
                      newActiveKey = activeKey;
                  }
  
a190ce84   梁灏   support Collapse
96
97
                  this.currentValue = newActiveKey;
                  this.$emit('input', newActiveKey);
49306c7a   梁灏   add Collapse comp...
98
99
100
101
                  this.$emit('on-change', newActiveKey);
              }
          },
          watch: {
a190ce84   梁灏   support Collapse
102
103
104
105
              value (val) {
                  this.currentValue = val;
              },
              currentValue () {
49306c7a   梁灏   add Collapse comp...
106
107
108
                  this.setActive();
              }
          }
b0893113   jingsam   :art: add eslint
109
110
      };
  </script>