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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
| <template>
| <view class="uni-collapse">
| <slot />
| </view>
| </template>
| <script>
| /**
| * Collapse 折叠面板
| * @description 展示可以折叠 / 展开的内容区域
| * @tutorial https://ext.dcloud.net.cn/plugin?id=23
| * @property {String|Array} value 当前激活面板改变时触发(如果是手风琴模式,参数类型为string,否则为array)
| * @property {Boolean} accordion = [true|false] 是否开启手风琴效果是否开启手风琴效果
| * @event {Function} change 切换面板时触发,如果是手风琴模式,返回类型为string,否则为array
| */
| export default {
| name: 'uniCollapse',
| emits:['change','activeItem','input','update:modelValue'],
| props: {
| value: {
| type: [String, Array],
| default: ''
| },
| modelValue: {
| type: [String, Array],
| default: ''
| },
| accordion: {
| // 是否开启手风琴效果
| type: [Boolean, String],
| default: false
| },
| },
| data() {
| return {}
| },
| computed: {
| // TODO 兼容 vue2 和 vue3
| dataValue() {
| let value = (typeof this.value === 'string' && this.value === '') ||
| (Array.isArray(this.value) && this.value.length === 0)
| let modelValue = (typeof this.modelValue === 'string' && this.modelValue === '') ||
| (Array.isArray(this.modelValue) && this.modelValue.length === 0)
| if (value) {
| return this.modelValue
| }
| if (modelValue) {
| return this.value
| }
|
| return this.value
| }
| },
| watch: {
| dataValue(val) {
| this.setOpen(val)
| }
| },
| created() {
| this.childrens = []
| this.names = []
| },
| mounted() {
| this.$nextTick(()=>{
| this.setOpen(this.dataValue)
| })
| },
| methods: {
| setOpen(val) {
| let str = typeof val === 'string'
| let arr = Array.isArray(val)
| this.childrens.forEach((vm, index) => {
| if (str) {
| if (val === vm.nameSync) {
| if (!this.accordion) {
| console.warn('accordion 属性为 false ,v-model 类型应该为 array')
| return
| }
| vm.isOpen = true
| }
| }
| if (arr) {
| val.forEach(v => {
| if (v === vm.nameSync) {
| if (this.accordion) {
| console.warn('accordion 属性为 true ,v-model 类型应该为 string')
| return
| }
| vm.isOpen = true
| }
| })
| }
| })
| this.emit(val)
| },
| setAccordion(self) {
| if (!this.accordion) return
| this.childrens.forEach((vm, index) => {
| if (self !== vm) {
| vm.isOpen = false
| }
| })
| },
| resize() {
| this.childrens.forEach((vm, index) => {
| // #ifndef APP-NVUE
| vm.getCollapseHeight()
| // #endif
| // #ifdef APP-NVUE
| vm.getNvueHwight()
| // #endif
| })
| },
| onChange(isOpen, self) {
| let activeItem = []
|
| if (this.accordion) {
| activeItem = isOpen ? self.nameSync : ''
| } else {
| this.childrens.forEach((vm, index) => {
| if (vm.isOpen) {
| activeItem.push(vm.nameSync)
| }
| })
| }
| this.$emit('change', activeItem)
| this.emit(activeItem)
| },
| emit(val){
| this.$emit('input', val)
| this.$emit('update:modelValue', val)
| }
| }
| }
| </script>
| <style lang="scss" >
| .uni-collapse {
| /* #ifndef APP-NVUE */
| width: 100%;
| display: flex;
| /* #endif */
| /* #ifdef APP-NVUE */
| flex: 1;
| /* #endif */
| flex-direction: column;
| background-color: #fff;
| }
| </style>
|
|