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
| <template>
| <switch
| class="cl-switch"
| :checked="checked"
| :disabled="isDisabled"
| :color="color"
| @change="onChange"
| />
| </template>
|
| <script lang="ts">
| import { computed, defineComponent } from "vue";
| import { useForm } from "../../hooks";
|
| export default defineComponent({
| props: {
| // 绑定值
| modelValue: [Boolean, String, Number],
| // 是否禁用
| disabled: {
| type: Boolean,
| default: null,
| },
| // 打开时的值
| activeValue: {
| type: [Boolean, String, Number],
| default: true,
| },
| // 关闭时的值
| inactiveValue: {
| type: [Boolean, String, Number],
| default: false,
| },
| // 打开时的背景色
| color: String,
| },
|
| emits: ["update:modelValue", "change"],
|
| setup(props, { emit }) {
| const { disabled } = useForm();
|
| // 是否选中
| const checked = computed(() => props.modelValue === props.activeValue);
|
| // 是否禁用
| const isDisabled = computed(() => disabled.value || props.disabled);
|
| // 监听变化
| function onChange(e: any) {
| const v = e.detail.value ? props.activeValue : props.inactiveValue;
|
| emit("update:modelValue", v);
| emit("change", v);
| }
|
| return {
| checked,
| isDisabled,
| onChange,
| };
| },
| });
| </script>
|
|