username@email.com
2024-03-25 99e2324eea7af7dd8da898277abd6f2cbb32e3f2
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
<template>
    <view :class="['fy-dropdown-item', 'fy_dropdown-item']" @touchmove.stop.prevent="() => {}" :style="{'background-color': backgroundColor, 'height': activeItemHeight+'rpx'}" v-if="active">
        <template v-if="!custom">
            <template v-if="type == 'column'">
                <Fy-dropdown-column :options="options" @success="onCateSuccess" />
            </template>
            <template v-else>
                <scroll-view :scroll-y="true" class="fy_dropdown-item_scroll" :style="getScrollStyle">
                    <view v-for="(item, index) in options" :key="index" @click.stop.prevent="cellClick(item.value)" class="fy_dropdown-item_scroll_cell"
                          :style="{'height': `${itemHeight}rpx`, 'line-height': `${itemHeight}rpx`}">
                        <text class="fy_cell_title" :style="{ color: value == item.value ? activeColor : inactiveColor }">{{ item.label }}</text>
                        <u-icon name="arrow-down"  :color="activeColor"  v-if="value == item.value" size="16"></u-icon>
                    </view>
                </scroll-view>
            </template>
        </template>
        <slot v-else></slot>
    </view>
</template>
 
<script>
import FyDropdownColumn from './fy-dropdown-column.vue';
 
/**
 * dropdown-item 下拉菜单
 * @description 该组件一般用于向下展开菜单,同时可切换多个选项卡的场景
 * @property {String | Number} v-model 双向绑定选项卡选择值
 * @property {Array[Object]} options 选项数据,如果传入了默认slot,此参数无效
 * @property {Boolean} disabled 是否禁用此选项卡(默认false)
 * @property {String | Number} duration 选项卡展开和收起的过渡时间,单位ms(默认300)
 * @property {String | Number} height 弹窗下拉内容的高度(内容超出将会滚动)(默认auto)
 * @example <fy-dropdown-item></fy-dropdown-item>
 */
export default {
    name: 'fy-dropdown-item',
    components: { FyDropdownColumn },
    props: {
        // 当前选中项的value值
        dropdownKey: {
            type: String, default: ''
        },
        // 当前选中项的value值
        value: {
            type: [Number, String, Array, Object], default: ''
        },
        // 选项数据,如果传入了默认slot,此参数无效
        options: {
            type: Array, default () { return [] }
        },
        // 是否禁用此菜单项
        disabled: {
            type: Boolean, default: false
        },
        backgroundColor: {
            type: String, default: 'transparent'
        },
        maxHeight: {
            type: Number, default: 800
        },
        itemHeight: {
            type: Number, default: 90
        },
        type: {
            type: String, default: ''
        },
        custom: {
            type: Boolean, default: false
        }
    },
    data() {
        return {
            initComponent: false,
            activeItemHeight: 300,
            active: false, // 当前项是否处于展开状态
            // #ifndef MP
            parent: null,
            // #endif
            activeColor: '#00bcd4', // 激活时左边文字和右边对勾图标的颜色
            inactiveColor: '#606266', // 未激活时左边文字和右边对勾图标的颜色
        }
    },
    computed: {
        getScrollHeight() {
            let height = this.options.length * this.itemHeight;
            if (height === 0) {
                return 300;
            } else if(height > this.maxHeight) {
                return this.maxHeight;
            } else {
                return height;
            }
        },
        getScrollStyle() {
            return { 'height': `${this.getScrollHeight}rpx` }
        }
    },
    created() {
        this.activeItemHeight = 300;
        // #ifdef MP
        this.parent = null;
        // #endif
    },
    mounted() {
        this.init();
    },
    methods: {
        // 获取父组件的参数,因为支付宝小程序不支持provide/inject的写法
        // this.$parent在非H5中,可以准确获取到父组件,但是在H5中,需要多次this.$parent.$parent.xxx
        // 这里默认值等于undefined有它的含义,因为最顶层元素(组件)的$parent就是undefined,意味着不传name
        // 值(默认为undefined),就是查找最顶层的$parent
        getParent(name = undefined) {
            try {
                let parent = this.$parent;
                // 通过while历遍,这里主要是为了H5需要多层解析的问题
                while (parent) {
                    // 父组件
                    if (parent.$options && parent.$options.name !== name) {
                        // 如果组件的name不相等,继续上一级寻找
                        parent = parent.$parent;
                    } else {
                        return parent;
                    }
                }
                return false;
            } catch(err) {
                console.log(err);
                return false;
            }
        },
        // 将当前px转换为rpx
        getScaleToRpx(px) {
            try {
                const screenWidth = uni.getSystemInfoSync().screenWidth;
                const unit = 750 / screenWidth;
                return px * unit;
            } catch(err) {
                return px;
            }
        },
        init() {
            try {
                let parent = this.getParent.call(this, 'fy-dropdown');
                if (!parent) return;
                this.parent = parent;
                this.active = parent.currentKey == this.dropdownKey;
 
                if (this.initComponent === false) {
                    parent.childList.push(this); // 供父组件依次更新子组件显示内容
                }
                if (this.active === false)  {
                    this.initComponent = true;
                    return;
                }
 
                if (this.type === 'cate' || this.type === 'address') {
                    this.activeItemHeight = this.getScaleToRpx(320) + 100;
                } else if(this.type === 'column') {
                    this.activeItemHeight = this.getScaleToRpx(220) + 100;
                } else {
                    this.activeItemHeight = this.getScrollHeight;
                }
 
                this.initComponent = true;
            } catch(err) {
                console.log(err)
            }
        },
        // cell被点击
        cellClick(value) {
            // 修改通过v-model绑定的值
            this.$emit('input', value);
            // 通知父组件(fy-dropdown)收起菜单
            this.parent.close();
            // 发出事件,抛出当前勾选项的value
            this.$emit('change', value);
        },
        onCateSuccess(data) {
            this.parent.close();
            this.$emit('change', data);
        }
    }
}
</script>
 
<style lang="scss">
@import './fy-dropdown-item.scss';
</style>