CollapseItem.vue
2.8 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
108
109
<template>
<div class="collapse-item-com" :style="{ marginBottom: marginBottom + 'px' }">
<div class="title-view" @click="toggle">
<div class="title-str">
<slot v-if="$slots.leftIcon" name="leftIcon"></slot>
<span v-else class="iconfont left-icon" :class="leftIcon"></span>
<slot v-if="$slots.title" name="title"></slot>
<span v-else class="title-text">{{ title }}</span>
</div>
<slot v-if="$slots.rightIcon" name="rightIcon"></slot>
<span v-else class="iconfont right-icon" :class="_rightIcon"></span>
</div>
<div class="collapse-extra" :style="{ height: isActive ? height + 'px' : '0' }" ref="collapseExtra">
<slot v-if="$slots.collapseExtra" name="collapseExtra"></slot>
</div>
</div>
</template>
<script>
export default {
name: 'CollapseItem',
props: {
name: {
type: String
},
title: {
type: String,
default: null
},
leftIcon: {
type: String,
default: null
},
rightIcon: {
type: String,
default: null
},
marginBottom: {
type: [String, Number]
}
},
data() {
return {
index: 0,
height: 0,
isActive: false
}
},
computed: {
_rightIcon() {
if (this.rightIcon) {
return this.rightIcon
} else {
if (this.isActive) {
return 'icon-xiangshang2'
} else {
return 'icon-xiangxia2'
}
}
}
},
methods: {
toggle() {
this.$parent.toggle({
name: this.name || this.index,
isActive: this.isActive
})
}
},
mounted() {
this.$nextTick(() => {
this.height = this.$refs.collapseExtra.childNodes[0].offsetHeight
})
}
}
</script>
<style lang="scss" scoped>
.collapse-item-com {
background-color: #fff;
.title-view {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid #f5f5f5;
.title-str {
.left-icon {
color: #666;
font-size: 12px;
}
.title-text {
font-size: 16px;
color: #333;
}
}
.right-icon {
color: #666;
font-size: 12px;
}
}
.collapse-extra {
height: 0;
will-change: 'height';
overflow: hidden;
transition: all 0.2s ease 0s;
}
}
</style>