In business scenarios, a radio button component similar to the one shown below is needed, so you can encapsulate one by yourself and reuse it at any time
Component: cp-radio-btn
<script setup lang="ts">
defineProps<{
sexList: { label: string; value: number | string }[]
modelValue?: number | string
}>()
defineEmits<{
(e: 'update:modelValue', value: number | string): void
}>()
</script>
<template>
<div class="cp-radio-btn">
<a
class="item"
href="javascript:;"
v-for="item in sexList"
:key="item.value"
:class="{ active: modelValue == item.value }"
@click="$emit('update:modelValue', item.value)"
>{
{ item.label }}</a
>
</div>
</template>
<style lang="scss" scoped>
.cp-radio-btn {
display: flex;
flex-wrap: wrap;
.item {
height: 32px;
min-width: 60px;
line-height: 30px;
padding: 0 14px;
text-align: center;
border: 1px solid var(--cp-bg);
background-color: var(--cp-bg);
margin-right: 10px;
box-sizing: border-box;
color: var(--cp-text2);
margin-bottom: 10px;
border-radius: 4px;
transition: all 0.3s;
&.active {
border-color: var(--cp-primary);
background-color: var(--cp-plain);
}
}
}
</style>
Use: When using this component, just pass in the corresponding array data, as follows:
const sexList = [
{
label: '男',
value: 1
},
{
label: '女',
value: 0
}
]
<cp-radio-btn :sexList="sexList" v-model="PatientObj.gender"></cp-radio-btn>
Like the vant component, the bound data of v-model here is (number | string), and the bound data type can be modified according to business needs.