Skip to content

ModalForm

Functionally similar to DrawerForm, but with a different position.
  • The form component is encapsulated based on FormPro
  • The modal component uses NModal

Basic Usage ​

vue
vue
<template>
  <!-- Add/Edit -->
  <ModalForm
    ref="modalForm"
    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";

/** Form configuration */
const editFormConfig: DialogForm.Form = {
  config: [
    { name: "username", label: "Username" },
    { name: "nickname", label: "Nickname" },
    {
      name: "status",
      label: "Status",
      component: "radio",
      props: {
        options: [
          { label: "Active", value: 1 },
          { label: "Disabled", value: 0 },
        ],
      },
    },
  ],
  props: {
    rules: {
      username: [
        { required: true, message: "Username cannot be empty", trigger: "blur" },
      ],
      nickname: [
        { required: true, message: "Nickname cannot be empty", trigger: "blur" },
      ],
    },
    // Other form property configurations
  },
};

/** Initialize form */
const modelValue = ref<User.Form>({
  status: 1,
});

/** Open modal */
const modalFormRef = useTemplateRef("modalForm");

const openModal = (row?: User.VO) => {
  modalFormRef.value?.open(row ? "Edit User" : "Add User", modelValue.value);

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

No Validation Needed ​

If you don't need validation or other form configurations, just pass the form item configuration form-config without passing form.

vue
vue
<template>
  <!-- Add/Edit -->
  <ModalForm
    ref="modalForm"
    v-model="modelValue"
    :form-config="editFormConfig.config"
    :loading="spin"
    @submit="submitForm"
  />
</template>

Props ​

NameTypeRequiredDefaultDescription
propsModal PropsNo{}Modal properties except show and segmented
widthnumberNo700Width
formSame as TablePro's formNo{}Form configuration
form-configFormItemConfig[]No[]Form item configuration
loadingbooleanNofalseLoading state
use-typesubmit, viewNosubmitUsage type

💡 Note

  • loading will also affect the loading state of the Submit and Cancel buttons
  • use-type determines the usage type. When set to view, only the Cancel button is displayed at the bottom

Slots ​

NameParamsDescription
header()Content above the form
footer()Content below the form

Expose ​

Function NameParamsDescription
open(title: string, data: object) => voidMethod to open the modal.
title is the modal title
data is the form data
close() => voidClose the modal. Will reset the form

Events ​

NameTypeDescription
submit(val) => voidTrigger submit
cancel() => voidTrigger cancel

Contributors

Changelog

Released under the MIT License