Number 数字输入框

通过鼠标或键盘,输入范围内的数值。

代码演示

基础用法
禁用状态
步进
严格步进
精度
不同的输入框尺寸
不同的输入框尺寸
不同的输入框尺寸
按钮位置
<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: {
    number1: {
      type: "number",
      title: "基础用法",
      minimum: 1,
      maximum: 10,
      default: 1,
      ui: {
        showRequired: true,
        change: (curVal: any, oldVal: any) => console.log(curVal, oldVal),
      },
    },
    number2: {
      type: "number",
      title: "禁用状态",
      default: 1,
      readOnly: true,
    },
    number3: {
      type: "number",
      title: "步进",
      default: 5,
      ui: {
        step: 2,
      },
    },
    number4: {
      type: "number",
      title: "严格步进",
      default: 2,
      ui: {
        step: 2,
        stepStrictly: true,
      },
    },
    number5: {
      type: "number",
      title: "精度",
      default: 1,
      ui: {
        step: 0.1,
        maximum: 10,
        precision: 2,
      },
    },
    number6_1: {
      type: "number",
      title: "不同的输入框尺寸",
      default: 1,
      ui: {
        size: "large",
        grid: {
          span: 8,
        },
      },
    },
    number6_2: {
      type: "number",
      title: "不同的输入框尺寸",
      default: 2,
      ui: {
        grid: {
          span: 8,
        },
      },
    },
    number6_3: {
      type: "number",
      title: "不同的输入框尺寸",
      default: 3,
      ui: {
        size: "small",
        grid: {
          span: 8,
        },
      },
    },
    number7: {
      type: "number",
      title: "按钮位置",
      default: 1,
      minimum: 1,
      maximum: 10,
      ui: {
        controlsPosition: "right",
      },
    },
  },
  required: ["number1"],
};
let formData: any = ref({});

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

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

<style scoped></style>
复制

API

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

meta 属性

成员说明类型默认值
[minimum]最小值number-
[exclusiveMinimum]约束是否包括 minimum 值,true 表示排除 minimumboolean-
[maximum]最大值number-
[exclusiveMaximum]约束是否包括 maximum 值,true 表示排除 maximumboolean-
[multipleOf]倍数number1

meta.ui 属性(antdv)

成员说明类型默认值
[slotNameOfAddonAfter]带标签的 input,设置后置标签,slot 名称string1
[slotNameOfAddonBefore]带标签的 input,设置前置标签,slot 名称string1
[slotNameOfPrefix]带有前缀图标的 input,slot 名称string1
@change变化回调Function(value: number | string)-

meta.ui 属性(element)

成员说明类型默认值
@change绑定值被改变时触发(currentValue: number, oldValue: number)-