Radio 单选框
Radio 单选框
介绍
在一组备选项中进行单选。
引入
在app.json或index.json中引入组件,详细介绍见快速上手。
"usingComponents": { "van-radio": "@vant/weapp/radio/index", "van-radio-group": "@vant/weapp/radio-group/index"}
代码演示
基础用法
通过value绑定值当前选中项的 name 。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-radio name="1">单选框 1</van-radio> <van-radio name="2">单选框 2</van-radio></van-radio-group>
Page({ data: { radio: '1', }, onChange(event) { this.setData({ radio: event.detail, }); },});
水平排列
将direction属性设置为horizontal后,单选框组会变成水平排列。
<van-radio-group value="{{ radio }}" bind:change="onChange" direction="horizontal"> <van-radio name="1">单选框 1</van-radio> <van-radio name="2">单选框 2</van-radio></van-radio-group>
禁用状态
通过disabled属性禁止选项切换,在Radio上设置diabled可以禁用单个选项。
<van-radio-group value="{{ radio }}" disabled bind:change="onChange"> <van-radio name="1">单选框 1</van-radio> <van-radio name="2">单选框 2</van-radio></van-radio-group>
自定义形状
将shape属性设置为square,单选框的形状会变成方形。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-radio name="1" shape="square">单选框 1</van-radio> <van-radio name="2" shape="square">单选框 2</van-radio></van-radio-group>
自定义颜色
通过checked-color属性设置选中状态的图标颜色。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-radio name="1" checked-color="#07c160">单选框 1</van-radio> <van-radio name="2" checked-color="#07c160">单选框 2</van-radio></van-radio-group>
自定义大小
通过icon-size属性可以自定义图标的大小。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-radio name="1" icon-size="24px">单选框 1</van-radio> <van-radio name="2" icon-size="24px">单选框 2</van-radio></van-radio-group>
自定义图标
通过icon插槽自定义图标,需要设置use-icon-slot属性。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-radio use-icon-slot value="{{ radio }}" name="1"> 自定义图标 <image slot="icon" src="{{ radio === '1' ? icon.active : icon.normal }}" /> </van-radio> <van-radio use-icon-slot value="{{ radio }}" name="2"> 自定义图标 <image slot="icon" src="{{ radio === '2' ? icon.active : icon.normal }}" /> </van-radio></van-radio-group>
Page({ data: { radio: true, icon: { normal: '//img.yzcdn.cn/icon-normal.png', active: '//img.yzcdn.cn/icon-active.png', }, }, onChange(event) { this.setData({ radio: event.detail, }); },});
禁用文本点击
通过设置label-disabled属性可以禁用单选框文本点击。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-radio name="1" label-disabled>单选框 1</van-radio> <van-radio name="2" label-disabled>单选框 2</van-radio></van-radio-group>
与 Cell 组件一起使用
此时你需要再引入Cell和CellGroup组件。
<van-radio-group value="{{ radio }}" bind:change="onChange"> <van-cell-group> <van-cell title="单选框 1" clickable data-name="1" bind:click="onClick"> <van-radio slot="right-icon" name="1" /> </van-cell> <van-cell title="单选框 2" clickable data-name="2" bind:click="onClick"> <van-radio slot="right-icon" name="2" /> </van-cell> </van-cell-group></van-radio-group>
Page({ data: { radio: '1', }, onChange(event) { this.setData({ radio: event.detail, }); }, onClick(event) { const { name } = event.currentTarget.dataset; this.setData({ radio: name, }); },});
API
RadioGroup Props
| name |
在表单内提交时的标识符 |
string |
- |
|
|
|
|
| disabled |
是否禁用所有单选框 |
boolean |
false |
|
|
|
|
| direction v1.6.7 |
排列方向,可选值为 horizontal |
string |
vertical |
|
|
|
|
Radio Props
| shape |
形状,可选值为 square |
string |
round |
|
|
|
|
| disabled |
是否为禁用状态 |
boolean |
false |
|
|
|
|
| label-disabled |
是否禁用文本内容点击 |
boolean |
false |
|
|
|
|
| label-position |
文本位置,可选值为 left |
string |
right |
|
|
|
|
| icon-size | 图标大小,默认单位为px | string | number | 20px |
|---|---|---|---|
| checked-color |
选中状态颜色 |
string |
#1989fa |
|
|
|
|
| use-icon-slot |
是否使用 icon 插槽 |
boolean |
false |
|
|
|
|
Radio Event
| bind:change |
当绑定值变化时触发的事件 |
当前选中项的 name |
|
|
|
Radio 外部样式类
RadioGroup Event
| bind:change |
当绑定值变化时触发的事件 |
当前选中项的 name |
|
|
|