Textarea 多行文本框

一般用于多行字符串。

代码演示

<template>
  <div>
    <v-formly-v3 ref="form" v-model="formData" :meta="meta">
      <template v-slot:testSlot>
        <div style="color: green; border: 1px dashed green">
          slot 类型的 text
        </div>
      </template>
    </v-formly-v3>
    <div class="btns">
      <a-button type="primary" @click="submit"> 提交 </a-button>
    </div>
  </div>
</template>

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

const form = ref<null | InstanceType<typeof VFormly>>(null);
const formData = ref({});
const meta = {
  type: "object",
  properties: {
    remark: {
      type: "string",
      title: "描述",
      ui: {
        component: "textarea",
        showRequired: true,
        change: (val: any) => console.log("change", val),
        focus: (e: any) => console.log("focus", e),
        blur: (e: any) => console.log("blur", e),
        pressEnter: (e: any) => console.log("pressEnter", e),
      },
    },
    remark1: {
      type: "string",
      title: "指定 autoSize",
      ui: {
        component: "textarea",
        placeholder: "{ minRows: 2, maxRows: 6 }",
        autoSize: { minRows: 2, maxRows: 6 },
      },
    },
    remark2: {
      type: "string",
      title: "自适应内容高度",
      default:
        "content content content content content content content content content content content content content content content content content content content content content content content content",
      ui: {
        component: "textarea",
      },
    },
    remark3: {
      type: "string",
      title: "不自适应内容高度",
      default:
        "content content content content content content content content content content content content content content content content content content content content content content content content",
      ui: {
        component: "textarea",
        autoSize: false,
      },
    },
    remark4: {
      type: "string",
      title: "禁用状态",
      default: "content content content",
      ui: {
        component: "textarea",
      },
      readOnly: true,
    },
    remark5: {
      type: "string",
      title: "显示清除按钮",
      default: "content content content",
      ui: {
        component: "textarea",
        allowClear: true,
      },
    },
    remark6: {
      type: "string",
      title: "最大长度",
      maxLength: 10,
      ui: {
        component: "textarea",
        placeholder: "maxLength = 10",
        showCount: true,
        autoSize: false,
      },
    },
  },
  required: ["remark"],
};

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

API

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

meta 属性

成员说明类型默认值
:maxLength最大长度number-
:readOnly禁用状态boolean-

meta.ui 属性(antdv)

成员说明类型默认值
:autosize自适应内容高度,可设置为true | false或对象:{ minRows: 2, maxRows: 6 }boolean | objecttrue
@change内容变更事件function(value)-
@focus焦点事件function(e)-
@blur失焦事件function(e)-
@pressEnter按下回车事件function(e)-

meta.ui 属性(element)

成员说明类型默认值
:autosize自适应内容高度,可设置为true | false或对象:{ minRows: 2, maxRows: 6 }boolean | objecttrue
@change内容变更事件function(value)-
@input在 Input 值改变时触发function(value)-
@focus焦点事件function(e)-
@blur失焦事件function(e)-