Radio 单选框

单选框。

代码演示

基础用法
禁用状态
单选框组
按钮样式
带有边框
<template>
  <div>
    <v-formly-v3 ref="form" v-model="formData" :meta="meta"> </v-formly-v3>
    <div class="btns">
      <el-button type="danger" @click="clear"> 重置 </el-button>
      <el-button type="primary" @click="submit"> 提交 </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, toRaw, unref } from "vue";
import type VFormly from "@/element-plus/ElFormly.vue";

const form = ref<null | InstanceType<typeof VFormly>>(null);
const meta = {
  type: "object",
  properties: {
    radio1: {
      title: "基础用法",
      type: "string",
      ui: {
        component: "radio",
        options: [{ label: "Option 1" }, { label: "Option 2" }],
      },
    },
    radio2: {
      title: "禁用状态",
      type: "string",
      readOnly: true,
      default: "Option 2",
      ui: {
        component: "radio",
        options: [{ label: "Option 1" }, { label: "Option 2" }],
      },
    },
    radio3: {
      title: "单选框组",
      type: "string",
      default: 3,
      ui: {
        component: "radio",
        options: [
          { text: "Option 1", label: 3 },
          { text: "Option 2", label: 6 },
          { text: "Option 3", label: 9 },
        ],
      },
    },
    radio4: {
      title: "按钮样式",
      type: "string",
      default: "New York",
      ui: {
        component: "radio",
        button: true,
        options: [
          { label: "New York" },
          { label: "Washington" },
          { label: "Los Angeles" },
          { label: "Chicago" },
        ],
      },
    },
    radio5: {
      title: "带有边框",
      type: "string",
      default: "Option 1",
      ui: {
        component: "radio",
        border: true,
        options: [{ label: "Option 1" }, { label: "Option 2" }],
      },
    },
  },
  required: [""],
};
let formData: any = ref({});

function clear() {
  formData.value = null;
}

async function submit() {
  let valid = await form.value!.validate();
  if (valid) {
    console.log(toRaw(unref(formData)));
  }
}
</script>

<style scoped></style>
复制

API

我们只列出属性中不一致的或新添加的,一致的地方请参考 具体组件库 文档

meta 属性

成员说明类型默认值
:readOnly禁用状态boolean-

meta.ui 属性(antdv)

参考 RadioGroup 组件属性

成员说明类型默认值
:component指定组件为'radio'stringradio
:direction单选框展示方向,默认横向展示,vertical则竖向展示string-
:showRequired是否显示标签前的红色*号booleanfalse
@change选项变化时的回调函数Function(value: any)-

meta.ui 属性(element)

参考 RadioGroup 组件属性

成员说明类型默认值
:component指定组件为'radio'stringradio
:button是否为按钮类型单选框boolean-
:showRequired是否显示标签前的红色*号booleanfalse
@change绑定值变化时触发的事件选中的 Radio label 值-