Novecane
2025-02-15 a12e5f63a90446525b2f322fb2bbd21d5285cd6d
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<template>
    <view class="uni-collapse-item">
        <!-- onClick(!isOpen) -->
        <view @click="onClick(!isOpen)" class="uni-collapse-item__title"
            :class="{'is-open':isOpen &&titleBorder === 'auto' ,'uni-collapse-item-border':titleBorder !== 'none'}">
            <view class="uni-collapse-item__title-wrap">
                <slot name="title">
                    <view class="uni-collapse-item__title-box" :class="{'is-disabled':disabled}">
                        <image v-if="thumb" :src="thumb" class="uni-collapse-item__title-img" />
                        <text class="uni-collapse-item__title-text">{{ title }}</text>
                    </view>
                </slot>
            </view>
            <view v-if="showArrow"
                :class="{ 'uni-collapse-item__title-arrow-active': isOpen, 'uni-collapse-item--animation': showAnimation === true }"
                class="uni-collapse-item__title-arrow">
                <uni-icons :color="disabled?'#ddd':'#bbb'" size="14" type="bottom" />
            </view>
        </view>
        <view class="uni-collapse-item__wrap" :class="{'is--transition':showAnimation}"
            :style="{height: (isOpen?height:0) +'px'}">
            <view :id="elId" ref="collapse--hook" class="uni-collapse-item__wrap-content"
                :class="{open:isheight,'uni-collapse-item--border':border&&isOpen}">
                <slot></slot>
            </view>
        </view>
 
    </view>
</template>
 
<script>
    // #ifdef APP-NVUE
    const dom = weex.requireModule('dom')
    // #endif
    /**
     * CollapseItem 折叠面板子组件
     * @description 折叠面板子组件
     * @property {String} title 标题文字
     * @property {String} thumb 标题左侧缩略图
     * @property {String} name 唯一标志符
     * @property {Boolean} open = [true|false] 是否展开组件
     * @property {Boolean} titleBorder = [true|false] 是否显示标题分隔线
     * @property {String} border = ['auto'|'show'|'none'] 是否显示分隔线
     * @property {Boolean} disabled = [true|false] 是否展开面板
     * @property {Boolean} showAnimation = [true|false] 开启动画
     * @property {Boolean} showArrow = [true|false] 是否显示右侧箭头
     */
    export default {
        name: 'uniCollapseItem',
        props: {
            // 列表标题
            title: {
                type: String,
                default: ''
            },
            name: {
                type: [Number, String],
                default: ''
            },
            // 是否禁用
            disabled: {
                type: Boolean,
                default: false
            },
            // #ifdef APP-PLUS
            // 是否显示动画,app 端默认不开启动画,卡顿严重
            showAnimation: {
                type: Boolean,
                default: false
            },
            // #endif
            // #ifndef APP-PLUS
            // 是否显示动画
            showAnimation: {
                type: Boolean,
                default: true
            },
            // #endif
            // 是否展开
            open: {
                type: Boolean,
                default: false
            },
            // 缩略图
            thumb: {
                type: String,
                default: ''
            },
            // 标题分隔线显示类型
            titleBorder: {
                type: String,
                default: 'auto'
            },
            border: {
                type: Boolean,
                default: true
            },
            showArrow: {
                type: Boolean,
                default: true
            }
        },
        data() {
            // TODO 随机生生元素ID,解决百度小程序获取同一个元素位置信息的bug
            const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
            return {
                isOpen: false,
                isheight: null,
                height: 0,
                elId,
                nameSync: 0
            }
        },
        watch: {
            open(val) {
                this.isOpen = val
                this.onClick(val, 'init')
            }
        },
        updated(e) {
            this.$nextTick(() => {
                this.init(true)
            })
        },
        created() {
            this.collapse = this.getCollapse()
            this.oldHeight = 0
            this.onClick(this.open, 'init')
        },
        // #ifndef VUE3
        // TODO vue2
        destroyed() {
            if (this.__isUnmounted) return
            this.uninstall()
        },
        // #endif
        // #ifdef VUE3
        // TODO vue3
        unmounted() {
            this.__isUnmounted = true
            this.uninstall()
        },
        // #endif
        mounted() {
            if (!this.collapse) return
            if (this.name !== '') {
                this.nameSync = this.name
            } else {
                this.nameSync = this.collapse.childrens.length + ''
            }
            if (this.collapse.names.indexOf(this.nameSync) === -1) {
                this.collapse.names.push(this.nameSync)
            } else {
                console.warn(`name 值 ${this.nameSync} 重复`);
            }
            if (this.collapse.childrens.indexOf(this) === -1) {
                this.collapse.childrens.push(this)
            }
            this.init()
        },
        methods: {
            init(type) {
                // #ifndef APP-NVUE
                this.getCollapseHeight(type)
                // #endif
                // #ifdef APP-NVUE
                this.getNvueHwight(type)
                // #endif
            },
            uninstall() {
                if (this.collapse) {
                    this.collapse.childrens.forEach((item, index) => {
                        if (item === this) {
                            this.collapse.childrens.splice(index, 1)
                        }
                    })
                    this.collapse.names.forEach((item, index) => {
                        if (item === this.nameSync) {
                            this.collapse.names.splice(index, 1)
                        }
                    })
                }
            },
            onClick(isOpen, type) {
                if (this.disabled) return
                this.isOpen = isOpen
                if (this.isOpen && this.collapse) {
                    this.collapse.setAccordion(this)
                }
                if (type !== 'init') {
                    this.collapse.onChange(isOpen, this)
                }
            },
            getCollapseHeight(type, index = 0) {
                const views = uni.createSelectorQuery().in(this)
                views
                    .select(`#${this.elId}`)
                    .fields({
                        size: true
                    }, data => {
                        // TODO 百度中可能获取不到节点信息 ,需要循环获取
                        if (index >= 10) return
                        if (!data) {
                            index++
                            this.getCollapseHeight(false, index)
                            return
                        }
                        // #ifdef APP-NVUE
                        this.height = data.height + 1
                        // #endif
                        // #ifndef APP-NVUE
                        this.height = data.height
                        // #endif
                        this.isheight = true
                        if (type) return
                        this.onClick(this.isOpen, 'init')
                    })
                    .exec()
            },
            getNvueHwight(type) {
                const result = dom.getComponentRect(this.$refs['collapse--hook'], option => {
                    if (option && option.result && option.size) {
                        // #ifdef APP-NVUE
                        this.height = option.size.height + 1
                        // #endif
                        // #ifndef APP-NVUE
                        this.height = option.size.height
                        // #endif
                        this.isheight = true
                        if (type) return
                        this.onClick(this.open, 'init')
                    }
                })
            },
            /**
             * 获取父元素实例
             */
            getCollapse(name = 'uniCollapse') {
                let parent = this.$parent;
                let parentName = parent.$options.name;
                while (parentName !== name) {
                    parent = parent.$parent;
                    if (!parent) return false;
                    parentName = parent.$options.name;
                }
                return parent;
            }
        }
    }
</script>
 
<style lang="scss">
    .uni-collapse-item {
        /* #ifndef APP-NVUE */
        box-sizing: border-box;
 
        /* #endif */
        &__title {
            /* #ifndef APP-NVUE */
            display: flex;
            width: 100%;
            box-sizing: border-box;
            /* #endif */
            flex-direction: row;
            align-items: center;
            transition: border-bottom-color .3s;
 
            // transition-property: border-bottom-color;
            // transition-duration: 5s;
            &-wrap {
                width: 100%;
                flex: 1;
 
            }
 
            &-box {
                padding: 0 15px;
                /* #ifndef APP-NVUE */
                display: flex;
                width: 100%;
                box-sizing: border-box;
                /* #endif */
                flex-direction: row;
                justify-content: space-between;
                align-items: center;
                height: 48px;
                line-height: 48px;
                background-color: #fff;
                color: #303133;
                font-size: 13px;
                font-weight: 500;
                /* #ifdef H5 */
                cursor: pointer;
                outline: none;
 
                /* #endif */
                &.is-disabled {
                    .uni-collapse-item__title-text {
                        color: #999;
                    }
                }
 
            }
 
            &.uni-collapse-item-border {
                border-bottom: 1px solid #ebeef5;
            }
 
            &.is-open {
                border-bottom-color: transparent;
            }
 
            &-img {
                height: 22px;
                width: 22px;
                margin-right: 10px;
            }
 
            &-text {
                flex: 1;
                font-size: 14px;
                /* #ifndef APP-NVUE */
                white-space: nowrap;
                color: inherit;
                /* #endif */
                /* #ifdef APP-NVUE */
                lines: 1;
                /* #endif */
                overflow: hidden;
                text-overflow: ellipsis;
            }
 
            &-arrow {
                /* #ifndef APP-NVUE */
                display: flex;
                box-sizing: border-box;
                /* #endif */
                align-items: center;
                justify-content: center;
                width: 20px;
                height: 20px;
                margin-right: 10px;
                transform: rotate(0deg);
 
                &-active {
                    transform: rotate(-180deg);
                }
            }
 
 
        }
 
        &__wrap {
            /* #ifndef APP-NVUE */
            will-change: height;
            box-sizing: border-box;
            /* #endif */
            background-color: #fff;
            overflow: hidden;
            position: relative;
            height: 0;
 
            &.is--transition {
                // transition: all 0.3s;
                transition-property: height, border-bottom-width;
                transition-duration: 0.3s;
                /* #ifndef APP-NVUE */
                will-change: height;
                /* #endif */
            }
 
 
 
            &-content {
                position: absolute;
                font-size: 13px;
                color: #303133;
                // transition: height 0.3s;
                border-bottom-color: transparent;
                border-bottom-style: solid;
                border-bottom-width: 0;
 
                &.uni-collapse-item--border {
                    border-bottom-width: 1px;
                    border-bottom-color: red;
                    border-bottom-color: #ebeef5;
                }
 
                &.open {
                    position: relative;
                }
            }
        }
 
        &--animation {
            transition-property: transform;
            transition-duration: 0.3s;
            transition-timing-function: ease;
        }
 
    }
</style>