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
| <template>
| <view class="cl-checkbox" :class="[classList]" :style="[baseStyle]" @tap.stop="change">
| <slot name="icon" :checked="checked">
| <view
| class="cl-checkbox__input"
| :style="{
| height: size,
| width: size,
| }"
| >
| <text class="cl-icon-toast-success" v-if="checked"></text>
| </view>
| </slot>
|
| <view class="cl-checkbox__label">
| <slot></slot>
| </view>
| </view>
| </template>
|
| <script lang="ts">
| import { computed, defineComponent, ref, watch } from "vue";
| import { isArray, isBoolean } from "lodash-es";
| import { getParent, parseRpx } from "/@/cool/utils";
| import { useForm, useStyle } from "../../hooks";
|
| export default defineComponent({
| name: "cl-checkbox",
|
| props: {
| // 绑定值
| modelValue: [String, Number, Boolean],
| // 标识
| label: [String, Number, Boolean],
| // 是否禁用
| disabled: {
| type: Boolean,
| default: null,
| },
| // 是否边框样式
| border: {
| type: Boolean,
| default: null,
| },
| // 是否带背景色
| bg: {
| type: Boolean,
| default: null,
| },
| // 是否圆角
| round: {
| type: Boolean,
| default: null,
| },
| // 是否只显示文字
| text: {
| type: Boolean,
| default: null,
| },
| // 图标大小
| size: [String, Number],
| },
|
| emits: ["update:modelValue", "change"],
|
| setup(props, { emit }) {
| const { disabled } = useForm();
|
| // <cl-checkbox-group />
| const parent = getParent("cl-checkbox-group", [
| "modelValue",
| "round",
| "border",
| "fill",
| "disabled",
| "size",
| "text",
| "bg",
| "onChange",
| ]);
|
| // 是否选中
| const checked = ref(false);
|
| // 是否禁用
| const isDisabled = computed(() => {
| return (
| disabled.value ||
| (isBoolean(props.disabled) ? props.disabled : parent.value?.disabled)
| );
| });
|
| // 大小
| const size = computed(() => {
| return parseRpx(props.size || parent.value?.size);
| });
|
| // 样式
| const classList = computed(() => {
| const d = {
| "is-disabled": isDisabled.value,
| "is-checked": checked.value,
| };
|
| ["border", "fill", "text", "round", "bg"].forEach((k) => {
| // @ts-ignore
| d[`is-${k}`] = isBoolean(props[k]) ? props[k] : parent.value?.[k];
| });
|
| return d;
| });
|
| // 绑定值改变
| function change() {
| if (isDisabled.value) {
| return false;
| }
|
| checked.value = !checked.value;
|
| emit("update:modelValue", checked.value);
| emit("change", checked.value);
|
| // 传递给父组件
| if (parent.value) {
| parent.value.onChange(props.label);
| }
| }
|
| // 监听绑定值变化
| watch(
| () => (parent.value ? parent.value.modelValue : props.modelValue),
| (val: any) => {
| if (isArray(val)) {
| checked.value = val.includes(props.label);
| } else {
| checked.value = val;
| }
| },
| {
| immediate: true,
| },
| );
|
| return {
| size,
| checked,
| classList,
| change,
| ...useStyle(),
| };
| },
| });
| </script>
|
|