-
zhangwei
2024-08-22 d5eb1ecff5d0236359c744b334cf30fcfdad1132
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
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号:9 15 1 01    31  33 2 0  061   9 3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-switch__input" :style="{zoom:isNvue?1:scaleRatio,transform:`scale(${isNvue?scaleRatio:1})`}">
        <!-- #ifdef APP-NVUE -->
        <switch v-if="type==='switch'" @change="change" :name="name" :checked="val" :disabled="disabled"
            :color="getColor">
        </switch>
        <!-- #endif -->
        <!-- #ifndef APP-NVUE -->
        <view class="fui-switch__input-def"
            :style="{background:val?color:'#dfdfdf',borderColor:val?color:borderColor}"
            :class="{'fui-checkbox__disabled':disabled,'fui-switch__color':val && !color}" v-if="type==='switch'">
            <view class="fui-switch__input-before" :class="{'fui-switch__input--checked':val}"
                :style="{background:background}"></view>
            <view class="fui-switch__input-after" :style="{background:val?(btnColor?btnColor:btnBgColor):btnBgColor}"
                :class="{'fui-switch__input--after':val}">
                <slot></slot>
            </view>
            <switch class="fui-switch__hidden" :class="{'fui-pointer__events':isLabel}" @change="change" :name="name"
                :checked="val" :disabled="disabled" :color="color">
            </switch>
        </view>
        <!-- #endif -->
        <view class="fui-checkbox__self"
            :class="{'fui-checkbox__disabled':disabled,'fui-switch__color':!getColor && val}"
            :style="{background:val?getColor:background,border:val?`1px solid ${getColor}`:`1px solid ${borderColor}`}"
            v-else>
            <view class="fui-check__mark" :style="{borderBottomColor:checkMarkColor,borderRightColor:checkMarkColor}"
                v-if="val"></view>
            <switch class="fui-switch__hidden" :class="{'fui-pointer__events':isLabel}"
                style="opacity: 0;position: absolute;" @change="change" :name="name" type="checkbox" :checked="val"
                :disabled="disabled"></switch>
        </view>
    </view>
</template>
 
<script>
    export default {
        name: "fui-switch",
        emits: ['change'],
        // #ifdef MP-WEIXIN
        behaviors: ['wx://form-field-group'],
        // #endif
        // #ifdef MP-BAIDU
        behaviors: ['swan://form-field'],
        // #endif
        // #ifdef MP-QQ
        behaviors: ['qq://form-field'],
        // #endif
        // #ifdef H5
        behaviors: ['uni://form-field'],
        // #endif
        props: {
            //开关选择器名称
            name: {
                type: String,
                default: ''
            },
            checked: {
                type: Boolean,
                default: false
            },
            disabled: {
                type: Boolean,
                default: false
            },
            //样式,有效值:switch, checkbox
            type: {
                type: String,
                default: 'switch'
            },
            //switch选中颜色
            color: {
                type: String,
                default: ''
            },
            background: {
                type: String,
                default: '#fdfdfd'
            },
            //switch选中时按钮颜色 V2.3.0+(nvue不支持)
            btnColor: {
                type: String,
                default: ''
            },
            btnBgColor: {
                type: String,
                default: '#fff'
            },
            //边框颜色,type=checkbox时生效
            borderColor: {
                type: String,
                default: '#ccc'
            },
            //对号颜色,type=checkbox时生效
            checkMarkColor: {
                type: String,
                default: '#fff'
            },
            scaleRatio: {
                type: [Number, String],
                default: 1
            }
        },
        computed: {
            getColor() {
                let color = this.color;
                // #ifdef APP-NVUE
                if (!color || color === true) {
                    const app = uni && uni.$fui && uni.$fui.color;
                    color = (app && app.primary) || '#465CFF';
                }
                // #endif
                return color;
            }
        },
        data() {
            let isNvue = false;
            // #ifdef APP-NVUE
            isNvue = true;
            // #endif
            return {
                val: false,
                isNvue: isNvue,
                isLabel: false
            };
        },
        watch: {
            checked(val) {
                this.val = val;
            }
        },
        created() {
            this.val = this.checked;
            this.label = this.getParent();
            if (this.label) {
                this.isLabel = true
                this.label.childrens.push(this)
            }
        },
        methods: {
            change(e, label) {
                if (this.label && !label) return;
                this.val = e.detail.value;
                this.$emit('change', e)
            },
            labelClick() {
                if (this.disabled) return;
                let e = {
                    detail: {
                        value: !this.val
                    }
                }
                this.change(e, true)
            },
            getParent(name = 'fui-label') {
                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 scoped>
    /* #ifndef APP-NVUE */
    .fui-switch__input {
        display: inline;
        margin: 0;
    }
 
    /* #endif */
 
    /* #ifdef APP-NVUE */
    .fui-switch__input {
        border: 1px solid transparent;
    }
 
    /* #endif */
 
    .fui-checkbox__self {
        font-size: 0;
        width: 40rpx;
        height: 40rpx;
        /* #ifdef APP-NVUE */
        border-radius: 40rpx;
        /* #endif */
        /* #ifndef APP-NVUE */
        border-radius: 50%;
        display: inline-flex;
        box-sizing: border-box;
        vertical-align: top;
        /* #endif */
        flex-direction: row;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        position: relative;
    }
 
    /* #ifndef APP-NVUE */
    .fui-switch__input-def {
        position: relative;
        width: 52px;
        height: 32px;
        border: 1px solid #CCCCCC;
        outline: 0;
        border-radius: 16px;
        box-sizing: border-box;
        transition: background-color .1s, border .1s;
    }
 
    .fui-switch__input-before {
        position: absolute;
        top: 0;
        left: 0;
        width: 50px;
        height: 30px;
        border-radius: 15px;
        transition: transform .3s
    }
 
    .fui-switch__input-after {
        position: absolute;
        top: 0;
        left: 0;
        width: 30px;
        height: 30px;
        border-radius: 15px;
        background-color: #fff;
        box-shadow: 0 0 3px rgba(0, 0, 0, .4);
        transition: transform .3s;
        display: flex;
        align-items: center;
        justify-content: center;
    }
 
    .fui-switch__input--checked {
        transform: scale(0)
    }
 
    .fui-switch__input--after {
        transform: translateX(20px)
    }
 
    .fui-switch__color {
        background: var(--fui-color-primary, #465CFF) !important;
        border-color: var(--fui-color-primary, #465CFF) !important;
    }
 
    /* #endif */
 
    /* #ifdef H5 || APP-VUE */
    ::v-deep .uni-switch-input {
        margin-right: 0 !important;
    }
 
    /* #endif */
 
    /* #ifdef APP-NVUE */
    .uni-switch-input {
        margin-right: 0 !important;
    }
 
    /* #endif */
 
    /* #ifdef MP-WEIXIN */
    .wx-switch-input {
        margin-right: 0 !important;
    }
 
    /* #endif */
 
    .fui-check__mark {
        width: 20rpx;
        height: 40rpx;
        border-bottom-style: solid;
        border-bottom-width: 3px;
        border-bottom-color: #FFFFFF;
        border-right-style: solid;
        border-right-width: 3px;
        border-right-color: #FFFFFF;
        transform: rotate(45deg) scale(0.5);
        transform-origin: 54% 48%;
        /* #ifndef APP-NVUE */
        box-sizing: border-box;
        /* #endif */
    }
 
    .fui-switch__hidden {
        position: absolute;
        top: -1px;
        left: -1px;
        opacity: 0;
        /* #ifndef APP-NVUE */
        width: 100%;
        height: 100%;
        border: 0 none;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        z-index: 2;
        /* #endif */
 
        /* #ifdef APP-NVUE */
        width: 100wx;
        height: 100wx;
        right: 0;
        bottom: 0;
        border-width: 0;
        /* #endif */
    }
 
    /* #ifdef H5 */
    .fui-pointer__events {
        pointer-events: none;
    }
 
    /* #endif */
 
    .fui-checkbox__disabled {
        opacity: 0.6;
    }
</style>