Skip to content

Radio 单选框

在一组选项中进行单项选择,通常配合 RadioGroup 使用。

交互式调试

🔧调试模式Playground

基础用法

vue
<script setup lang="ts">
import { ref } from 'vue'
const checked = ref('A')
</script>

<template>
  <lk-radio-group v-model="checked">
    <lk-radio name="A">选项 A</lk-radio>
    <lk-radio name="B">选项 B</lk-radio>
    <lk-radio name="C">选项 C</lk-radio>
  </lk-radio-group>
  <view>当前选中:{{ checked }}</view>
</template>

单独使用

单独使用时,通过 modelValue 判断当前项是否选中。

vue
<script setup lang="ts">
import { ref } from 'vue'

const answer = ref('yes')
</script>

<template>
  <lk-radio v-model="answer" name="yes">同意</lk-radio>
</template>

横向排列

vue
<script setup lang="ts">
import { ref } from 'vue'
const plan = ref('lite')
</script>

<template>
  <lk-radio-group v-model="plan" direction="row">
    <lk-radio name="lite">基础版</lk-radio>
    <lk-radio name="pro">专业版</lk-radio>
    <lk-radio name="enterprise">企业版</lk-radio>
  </lk-radio-group>
</template>

图标类型

vue
<template>
  <!-- dot(默认):选中显示中心圆点 -->
  <lk-radio-group v-model="val" icon-type="dot">
    <lk-radio name="1">选项 1</lk-radio>
    <lk-radio name="2">选项 2</lk-radio>
  </lk-radio-group>

  <!-- check:选中显示勾选图标 -->
  <lk-radio-group v-model="val" icon-type="check">
    <lk-radio name="1">选项 1</lk-radio>
    <lk-radio name="2">选项 2</lk-radio>
  </lk-radio-group>
</template>

方形外观

vue
<template>
  <lk-radio-group v-model="val" shape="square">
    <lk-radio name="yes">同意</lk-radio>
    <lk-radio name="no">不同意</lk-radio>
  </lk-radio-group>
</template>

禁用状态

vue
<template>
  <!-- 整组禁用 -->
  <lk-radio-group v-model="val" disabled>
    <lk-radio name="A">选项 A(禁用)</lk-radio>
    <lk-radio name="B">选项 B(禁用)</lk-radio>
  </lk-radio-group>

  <!-- 单项禁用 -->
  <lk-radio-group v-model="val2">
    <lk-radio name="X">可选</lk-radio>
    <lk-radio name="Y" disabled>不可选</lk-radio>
    <lk-radio name="Z">可选</lk-radio>
  </lk-radio-group>
</template>

自定义选中颜色

vue
<template>
  <lk-radio-group v-model="val" active-color="#f59e0b">
    <lk-radio name="gold">黄金选项</lk-radio>
    <lk-radio name="silver">白银选项</lk-radio>
  </lk-radio-group>
</template>

与表单结合

vue
<script setup lang="ts">
import { ref } from 'vue'
const gender = ref('male')
const agree = ref(false)
</script>

<template>
  <lk-form>
    <lk-form-item label="性别" required>
      <lk-radio-group v-model="gender" direction="row">
        <lk-radio name="male">男</lk-radio>
        <lk-radio name="female">女</lk-radio>
      </lk-radio-group>
    </lk-form-item>
  </lk-form>
</template>

API

RadioGroup Props

参数说明类型可选值默认值
modelValue绑定值,支持 v-modelstring / number / boolean''
direction排列方向stringrow / columnrow
shape外观形状stringcircle / squarecircle
iconType图标样式stringdot / checkdot
size尺寸stringsm / md / lgmd
disabled是否全部禁用booleanfalse
activeColor选中颜色string''
prop表单字段名,配合 lk-form 联动校验string''
validateEvent值变更时是否触发表单校验booleantrue
id根节点 idstring''
customClass自定义类名string / object / array''
customStyle自定义样式string / object''

RadioGroup Events

事件名说明回调参数
update:modelValue绑定值变化(value: string | number | boolean)
change选项改变(value: string | number | boolean)
item-change单个选项选中时触发(value: string | number | boolean)

Radio Props

参数说明类型可选值默认值
modelValue单独使用时的绑定值string / number / boolean''
name唯一标识符,作为选中值string / number / boolean''
label标签文字,也可作为值回退string''
disabled是否禁用该项booleanfalse
labelDisabled点击文字是否触发选择booleanfalse
shape覆盖 Group 的形状string'' / circle / square''
iconType覆盖 Group 的图标类型string'' / dot / check''
activeColor覆盖 Group 的选中色string''
iconSize图标尺寸string / number''
id根节点 idstring''
customClass自定义类名string / object / array''
customStyle自定义样式string / object''

Radio Events

事件名说明回调参数
update:modelValue单独使用时的绑定值变化(value: string | number | boolean)
change单独使用时的值变化回调(value: string | number | boolean)
click点击单选框时触发,禁用态不触发(event: Event, checked: boolean, value: string | number | boolean)

Radio Slots

插槽名说明
default标签内容
icon自定义选中图标(参数:{ checked, disabled }

基于 MIT 协议开源 · 粤ICP备2022114240号-3

Radio 单选框

正在连接预览服务...

请先运行 pnpm run dev:h5