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
<template>
<div class="check-btn">
<div v-for="item in options" :key="item[fieldNames.value]"
@click="onClick(item)"
:class="['py-2 px-3 shrink-0 check-btn-item',
{ 'check-btn-item-active': isSelect(item) },
{ 'check-btn-item-disabled': isDisabled(item) },
`text-align-${textAlign}`]"
>
{{ item[fieldNames.text] }}
</div>
</div>
</template>
<script>
export default {
name: 'CheckBtn',
props:{
value: [String, Number, Array],
// 选项
options: { default: () =>[] },
fieldNames: {
type: Object,
default: () => {
return {text: 'name', value: 'value'}
}
},
// 点击取消选中 单选
clearable: { default: true },
// 是否多选
multiple: Boolean,
textAlign: { default: 'center' },
// 关闭选中样式
activeStyleNone: Boolean
},
emits: ['update:value', 'change'],
data() {
return {
innerValue: undefined
}
},
created() {
this.init()
},
methods: {
init() {
this.innerValue = this.value
this.multipleCheck()
},
multipleCheck() {
if (this.multiple) {
if (this.value != null && !Array.isArray(this.value)) {
console.warn('CheckBtn: multiple must be Array type')
}
this.innerValue = this.innerValue || []
}
},
onClick(item){
if (this.isDisabled(item)) {
return
}
const val = item[this.fieldNames.value]
if (this.multiple) {
if (this.innerValue.includes(val)) {
this.innerValue = this.innerValue.filter(e => e !== val)
} else {
this.innerValue.push(val)
}
} else {
if (this.clearable && this.innerValue === val) {
this.innerValue = undefined
} else {
this.innerValue = val
}
}
this.$emit('update:value', this.innerValue)
this.$emit('change', this.innerValue, item)
},
// 是否选中
isSelect(item){
if (this.activeStyleNone) {
return false
}
if (this.multiple) {
return this.innerValue.includes(item[this.fieldNames.value])
} else {
return item[this.fieldNames.value] === this.innerValue
}
},
isDisabled(item){
return item.disabled
}
},
watch: {
value: {
handler(value) {
this.innerValue = value
this.multipleCheck()
},
immediate: true
},
}
}
</script>
<style lang="less" scoped>
.check-btn {
display: flex;
flex-wrap: wrap;
row-gap: 10px;
column-gap: 10px;
.check-btn-item {
border: 1px solid transparent;
background-color: #FAFAFA;
border-radius: 8px;
text-align: center;
transition: all .2s;
}
.check-btn-item-active {
border: 1px solid var(--van-primary-color);
background-color: #F0F6FF;
color: var(--van-primary-color);
}
.check-btn-item-disabled {
background-color: #F0F6FF;
color: var(--van-primary-color);
// opacity: 0.5;
filter: grayscale(1);
}
.text-align-left {
text-align: left;
}
.text-align-center {
text-align: center;
}
.text-align-right {
text-align: right;
}
}
.check-btn.check-btn-workbench {
.check-btn-item {
border-radius: 30px;
padding-top: 4px;
padding-bottom: 4px;
color: #595959;
}
.check-btn-item-active {
color: var(--van-primary-color);
}
}
// 圆角
.check-btn[round] {
.check-btn-item {
border-radius: 30px;
}
}
// 每行排列个数
.check-btn[column-3] {
.check-btn-item {
width: calc(33.3% - 6.6px);
}
}
.check-btn[column-2] {
.check-btn-item {
width: calc(50% - 5px);
}
}
.check-btn[column-1] {
.check-btn-item {
width: 100%;
}
}
</style>