collapse.vue
2.94 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<template>
<div :class="classes">
<slot></slot>
</div>
</template>
<script>
const prefixCls = 'ivu-collapse';
export default {
props: {
accordion: {
type: Boolean,
default: false
},
value: {
type: [Array, String]
}
},
data () {
return {
currentValue: this.value
};
},
computed: {
classes () {
return `${prefixCls}`;
}
},
mounted () {
this.setActive();
},
methods: {
setActive () {
const activeKey = this.getActiveKey();
this.$children.forEach((child, index) => {
const name = child.name || index.toString();
let isActive = false;
if (self.accordion) {
isActive = activeKey === name;
} else {
isActive = activeKey.indexOf(name) > -1;
}
child.isActive = isActive;
child.index = index;
});
},
getActiveKey () {
let activeKey = this.currentValue || [];
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) {
const name = data.name.toString();
let newActiveKey = [];
if (this.accordion) {
if (!data.isActive) {
newActiveKey.push(name);
}
} else {
let activeKey = this.getActiveKey();
const nameIndex = activeKey.indexOf(name);
if (data.isActive) {
if (nameIndex > -1) {
activeKey.splice(nameIndex, 1);
}
} else {
if (nameIndex < 0) {
activeKey.push(name);
}
}
newActiveKey = activeKey;
}
this.currentValue = newActiveKey;
this.$emit('input', newActiveKey);
this.$emit('on-change', newActiveKey);
}
},
watch: {
value (val) {
this.currentValue = val;
},
currentValue () {
this.setActive();
}
}
};
</script>