fuchengshen
2022-06-24 0924fcc06ead4d10ea5d0d579c8224850a0f37a4
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
<template>
    <div class="cl-switch">
        <el-switch
            :ref="setRefs('switch')"
            :model-value="status"
            :disabled="disabled"
            :loading="loading"
            :width="width"
            :inline-prompt="inlinePrompt"
            :active-icon="activeIcon"
            :inactive-icon="inactiveIcon"
            :active-text="activeText"
            :inactive-text="inactiveText"
            :active-value="activeValue"
            :inactive-value="inactiveValue"
            :active-color="activeColor"
            :inactive-color="inactiveColor"
            :border-color="borderColor"
            :string="string"
            :validate-event="validateEvent"
            :before-change="beforeChange"
            @change="onChange"
        />
    </div>
</template>
 
<script lang="ts">
import { ElMessage } from "element-plus";
import { defineComponent, inject, ref, watch } from "vue";
import { useCool } from "/@/cool";
 
export default defineComponent({
    name: "cl-switch",
 
    props: {
        scope: null,
        column: null,
        modelValue: [Boolean, String, Number],
        disabled: Boolean,
        loading: Boolean,
        width: Number,
        inlinePrompt: Boolean,
        activeIcon: String,
        inactiveIcon: String,
        activeText: String,
        inactiveText: String,
        activeValue: {
            type: [Boolean, String, Number],
            default: 1
        },
        inactiveValue: {
            type: [Boolean, String, Number],
            default: 0
        },
        activeColor: String,
        inactiveColor: String,
        borderColor: String,
        string: String,
        validateEvent: {
            type: Boolean,
            default: true
        },
        beforeChange: Function
    },
 
    emits: ["update:modelValue", "change"],
 
    setup(props, { emit }) {
        const { refs, setRefs } = useCool();
        const crud = inject<any>("crud");
 
        // 状态
        const status = ref<any>(props.modelValue);
 
        watch(
            () => props.modelValue,
            (val: any) => {
                status.value = val;
            }
        );
 
        function focus() {
            refs.value.switch.focus();
        }
 
        function onChange(val: boolean | string | number) {
            crud.service
                .update({
                    ...props.scope,
                    [props.column.property]: val
                })
                .then(() => {
                    emit("update:modelValue", val);
                    emit("change", val);
                    status.value = val;
                    ElMessage.success("更新成功");
                })
                .catch((err: string) => {
                    ElMessage.error(err);
                });
        }
 
        return {
            refs,
            setRefs,
            focus,
            onChange,
            status
        };
    }
});
</script>