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
| <template>
| <view class="cl-radio" :class="[classList]" :style="[baseStyle]" @tap.stop="change">
| <slot name="icon" :checked="checked">
| <view
| class="cl-radio__input"
| :style="{
| height: size,
| width: size,
| }"
| >
| <text v-if="checked"></text>
| </view>
| </slot>
|
| <view class="cl-radio__label">
| <slot></slot>
| </view>
| </view>
| </template>
|
| <script lang="ts">
| import { computed, defineComponent, ref, watch } from "vue";
| import { useForm, useStyle } from "../../hooks";
| import { isBoolean } from "lodash-es";
| import { getParent, parseRpx } from "/@/cool/utils";
|
| export default defineComponent({
| name: "cl-radio",
|
| props: {
| // 绑定值
| modelValue: [String, Number],
| // 标识
| label: [String, Number, Boolean],
| // 是否禁用
| disabled: {
| type: Boolean,
| default: null,
| },
| // 是否边框样式
| border: {
| type: Boolean,
| default: null,
| },
| // 是否带背景色
| bg: {
| type: Boolean,
| default: null,
| },
| // 是否宽度填充
| fill: {
| type: Boolean,
| default: null,
| },
| // 是否只显示文字
| text: {
| type: Boolean,
| default: null,
| },
| // 是否圆角
| round: {
| type: Boolean,
| default: null,
| },
| // 图标大小
| size: [String, Number],
| },
|
| emits: ["update:modelValue", "change"],
|
| setup(props, { emit }) {
| const { disabled } = useForm();
|
| // cl-radio-group
| const parent = getParent("cl-radio-group", [
| "modelValue",
| "disabled",
| "border",
| "fill",
| "size",
| "text",
| "bg",
| "round",
| "change",
| ]);
|
| // 是否选中
| const checked = ref(false);
|
| // 监听绑定值变化
| watch(
| () => (parent.value ? parent.value.modelValue : props.modelValue),
| (val: any) => {
| checked.value = val === props.label;
| },
| {
| immediate: true,
| },
| );
|
| // 是否禁用
| 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 = true;
|
| // 更新 <cl-checkbox-group />
| if (parent.value) {
| parent.value.change(props.label);
| } else {
| emit("update:modelValue", props.label);
| emit("change", props.label);
| }
| }
|
| return {
| size,
| checked,
| classList,
| change,
| ...useStyle(),
| };
| },
| });
| </script>
|
|