Skip to content

Radio 单选框

单选框封装,和z-form组件配合食用,风味更佳。

基础用法

传入options自动生成选项

<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const radioVal = ref(1)

function handleRadioChange(val: number) {
  console.log('radio change:', val)
}
</script>

<template>
  <z-radio v-model="radioVal" :options="options" @change="handleRadioChange" />
</template>

禁用

option中的某项设置disabledtrue

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

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2, disabled: true },
  { label: '选项三', value: 3 },
])

const radioVal = ref(1)
</script>

<template>
  <z-radio v-model="radioVal" :options="options" />
</template>

全部禁用,组件传入disabledtrue

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

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const radioVal = ref(1)
</script>

<template>
  <z-radio v-model="radioVal" :disabled="true" :options="options" />
</template>

键值对配置

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

const options = ref([
  { data: { title: '选项一' }, key: 1 },
  { data: { title: '选项二' }, key: 2 },
  { data: { title: '选项三' }, key: 3 },
])

const alias = {
  label: 'data.title',
  value: 'key',
}

const radioVal = ref(1)
</script>

<template>
  <z-radio v-model="radioVal" :alias="alias" :options="options" />
</template>

取消选中

配置isCanceltrue,可以取消选中

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

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const radioVal = ref(1)
</script>

<template>
  <z-radio v-model="radioVal" is-cancel :options="options" />
</template>

按钮样式

option某项设置type或者直接组件传入type

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

const options = ref([
  { label: '选项一', value: 1 },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3, type: 'radio-button' },
])

const radioVal = ref(1)
</script>

<template>
  <div class="flex flex-col">
    <z-radio v-model="radioVal" type="radio-button" :options="options" />
    <z-radio v-model="radioVal" :options="options" class="mt-4" />
  </div>
</template>

带有边框

border属性或者字段可以渲染为带有边框的多选框。

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

const options = ref([
  { label: '选项一', value: 1, border: true },
  { label: '选项二', value: 2 },
  { label: '选项三', value: 3 },
])

const radioVal = ref(1)
</script>

<template>
  <div class="flex flex-col">
    <z-radio v-model="radioVal" :options="options" />
    <z-radio v-model="radioVal" :border="true" :options="options" class="mt-4" />
  </div>
</template>

z-radio 属性

属性名说明类型默认值
model-value / v-model绑定值string / number / boolean
options可配置项array
typeRadio 形式stringradio
alias键值对配置object{ label: 'label', value: 'value', disabled: 'disabled' }
isCancel是否取消选中booleanfalse
border是否显示边框booleanfalse
size单选框按钮或边框按钮的大小stringdefault
disabled是否禁用booleanfalse
text-color按钮形式的 Radio 激活时的文本颜色string#ffffff
fill按钮形式的 Radio 激活时的填充色和边框色string#409EFF
validate-event输入时是否触发表单的校验booleantrue
name原生 name 属性string
id原生 id 属性string

z-radio 事件

事件名说明类型
change当绑定值变化时触发的事件Function

Options 项可配置属性

属性名说明类型默认值
value单选框的绑定值string / number / boolean
label单选框的文案string
disabled是否禁用单选框booleanfalse
border是否显示边框booleanfalse
size单选框的尺寸enum
name原始 name 属性string
typeRadio 形式stringel-radio

Released under the MIT License.