Skip to content

抽屉表单 DrawerForm

以抽屉形式展示表单,收集数据。

基本使用

vue
vue
<template>
  <!-- 新增、编辑 -->
  <DrawerForm
    ref="drawerForm"
    v-model="modelValue"
    :form="editFormConfig"
    :loading="spin"
    @submit="submitForm"
  />
</template>

<script setup lang="ts">
import { spin, startSpin, endSpin, executeAsync } from "@/utils";
import UserAPI from "@/api/system/user";

/** 表单配置项 */
const editFormConfig: DialogForm.Form = {
  config: [
    { name: "username", label: "用户名" },
    { name: "nickname", label: "用户昵称" },
    {
      name: "status",
      label: "状态",
      component: "radio",
      props: {
        options: [
          { label: "正常", value: 1 },
          { label: "禁用", value: 0 },
        ],
      },
    },
  ],
  props: {
    rules: {
      username: [
        { required: true, message: "用户名不能为空", trigger: "blur" },
      ],
      nickname: [
        { required: true, message: "用户昵称不能为空", trigger: "blur" },
      ],
    },
    // 其他的表单属性配置
  },
};

/** 初始化表单 */
const modelValue = ref<User.Form>({
  status: 1,
});

/** 打开抽屉 */
const drawerFormRef = useTemplateRef("drawerForm");

const openDrawer = (row?: User.VO) => {
  drawerFormRef.value?.open(row ? "编辑用户" : "新增用户", modelValue.value);

  if (row) {
    startSpin();
    UserAPI.getFormData(row.id)
      .then((data) => {
        modelValue.value = { ...data };
      })
      .finally(() => endSpin());
  }
};
/** 表单提交 */
const submitForm = (val: User.Form) =>
  executeAsync(
    () => (val.id ? UserAPI.update(val.id, val) : UserAPI.create(val)),
    () => {
      drawerFormRef.value?.close();
      handleQuery();
    }
  );
</script>

无需校验

不需要校验和其他的表单配置时,只需要传递表单项的配置 form-config 即可,无需传递 form

vue
vue
<template>
  <!-- 新增、编辑 -->
  <DrawerForm
    ref="drawerForm"
    v-model="modelValue"
    :form-config="editFormConfig.config"
    :loading="spin"
    @submit="submitForm"
  />
</template>

Props

名称类型必传默认值说明
propsDrawer Props{}Drawer属性 show 除外
form同 TablePro 的 form{}表单配置项
form-configFormItemConfig[][]表单项配置
loadingbooleanfasle加载状态
use-typesubmit viewsubmit使用类型

💡 提示

  • loading 会连同 提交取消 按钮的 loading 状态
  • use-type 为使用的类型。为 view 时仅显示底部的 取消 按钮

Slots

属性参数说明
header()头部内容,展示在表单上方
footer()尾部内容,展示在表单下方

Expose

函数名参数说明
open(title: string, data: object) => void打开抽屉方法
title 为抽屉标题
data 为表单数据
close() => void关闭抽屉。会重置表单

Events

名称类型说明
submit(val) => void触发提交
cancel() => void触发取消

贡献者

页面历史

基于 MIT 许可发布