Radio
Install
import { createApp } from 'vue';
import { RadioGroup, Radio } from 'vant';
const app = createApp();
app.use(Radio);
app.use(RadioGroup);
Usage
Basic Usage
Use v-model
to bind the name of checked radio.
<van-radio-group v-model="checked">
<van-radio name="1">Radio 1</van-radio>
<van-radio name="2">Radio 2</van-radio>
</van-radio-group>
import { ref } from 'vue';
export default {
setup() {
const checked = ref('1');
return { checked };
},
};
Horizontal
<van-radio-group v-model="checked" direction="horizontal">
<van-radio name="1">Radio 1</van-radio>
<van-radio name="2">Radio 2</van-radio>
</van-radio-group>
Disabled
<van-radio-group v-model="checked" disabled>
<van-radio name="1">Radio 1</van-radio>
<van-radio name="2">Radio 2</van-radio>
</van-radio-group>
Custom Shape
<van-radio-group v-model="checked">
<van-radio name="1" shape="square">Radio 1</van-radio>
<van-radio name="2" shape="square">Radio 2</van-radio>
</van-radio-group>
Custom Color
<van-radio-group v-model="checked">
<van-radio name="1" checked-color="#ee0a24">Radio 1</van-radio>
<van-radio name="2" checked-color="#ee0a24">Radio 2</van-radio>
</van-radio-group>
Custom Icon Size
<van-radio-group v-model="checked">
<van-radio name="1" icon-size="24px">Radio 1</van-radio>
<van-radio name="2" icon-size="24px">Radio 2</van-radio>
</van-radio-group>
Custom Icon
Use icon slot to custom icon
<van-radio-group v-model="checked">
<van-radio name="1">
Radio 1
<template #icon="props">
<img class="img-icon" :src="props.checked ? activeIcon : inactiveIcon" />
</template>
</van-radio>
<van-radio name="2">
Radio 2
<template #icon="props">
<img class="img-icon" :src="props.checked ? activeIcon : inactiveIcon" />
</template>
</van-radio>
</van-radio-group>
<style>
.img-icon {
height: 20px;
}
</style>
import { ref } from 'vue';
export default {
setup() {
const checked = ref('1');
return {
checked,
activeIcon: 'https://img01.yzcdn.cn/vant/user-active.png',
inactiveIcon: 'https://img01.yzcdn.cn/vant/user-inactive.png',
};
},
};
Disable Label Click
<van-radio-group v-model="checked">
<van-radio name="1" label-disabled>Radio 1</van-radio>
<van-radio name="2" label-disabled>Radio 2</van-radio>
</van-radio-group>
Inside a Cell
<van-radio-group v-model="checked">
<van-cell-group>
<van-cell title="Radio 1" clickable @click="checked = '1'">
<template #right-icon>
<van-radio name="1" />
</template>
</van-cell>
<van-cell title="Radio 2" clickable @click="checked = '2'">
<template #right-icon>
<van-radio name="2" />
</template>
</van-cell>
</van-cell-group>
</van-radio-group>
API
Radio Props
Attribute |
Description |
Type |
Default |
name |
Radio name |
any |
- |
shape |
Can be set to square |
string |
round |
disabled |
Whether to disable radio |
boolean |
false |
label-disabled |
Whether to disable label click |
boolean |
false |
label-position |
Can be set to left |
string |
right |
icon-size |
Icon size |
number | string |
20px |
checked-color |
Checked color |
string |
#1989fa |
RadioGroup Props
Attribute |
Description |
Type |
Default |
v-model |
Name of checked radio |
any |
- |
disabled |
Disable all radios |
boolean |
false |
direction |
Direction, can be set to horizontal |
string |
vertical |
icon-size |
Icon size of all radios |
number | string |
20px |
checked-color |
Checked color of all radios |
string |
#1989fa |
Radio Events
Event |
Description |
Parameters |
click |
Emitted when radio is clicked |
event: MouseEvent |
RadioGroup Events
Event |
Description |
Parameters |
change |
Emitted when value changed |
name: string |
Radio Slots
Name |
Description |
SlotProps |
default |
Custom label |
- |
icon |
Custom icon |
checked: boolean |
Less Variables
How to use: Custom Theme.
Name |
Default Value |
Description |
@radio-size |
20px |
- |
@radio-border-color |
@gray-5 |
- |
@radio-transition-duration |
@animation-duration-fast |
- |
@radio-label-margin |
@padding-xs |
- |
@radio-label-color |
@text-color |
- |
@radio-checked-icon-color |
@blue |
- |
@radio-disabled-icon-color |
@gray-5 |
- |
@radio-disabled-label-color |
@gray-5 |
- |
@radio-disabled-background-color |
@border-color |
- |