Skip to content

PageModal 弹窗表单

配置化生成新增/编辑弹窗,支持 Dialog 和 Drawer 两种展示方式。

Props

参数说明类型默认值
modal-config弹窗配置IModalConfig

IModalConfig

属性说明类型默认值
formItems表单项配置(必填)IFormItem[]
formAction提交接口(data) => Promise<any>
component弹窗类型"dialog" | "drawer""dialog"
dialogElDialog 属性Partial<DialogProps>
drawerElDrawer 属性(component: "drawer" 时使用)Partial<DrawerProps>
formElForm 属性Partial<FormProps>
pk主键字段名string'id'
permPrefix权限前缀string
colon标签后是否显示冒号booleanfalse
beforeSubmit提交前钩子(data) => void

配置示例

新增

typescript
const addModalConfig: IModalConfig<UserForm> = reactive({
  permPrefix: "sys:user",
  dialog: { title: "新增用户", width: 800, draggable: true },
  form: { labelWidth: 100 },
  formAction: UserAPI.create,
  formItems: [
    {
      label: "用户名",
      prop: "username",
      type: "input",
      rules: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
      attrs: { placeholder: "请输入用户名" },
      col: { xs: 24, sm: 12 },
    },
    {
      label: "所属部门",
      prop: "deptId",
      type: "tree-select",
      rules: [{ required: true, message: "所属部门不能为空", trigger: "change" }],
      attrs: {
        data: deptArr,
        filterable: true,
        "check-strictly": true,
        "render-after-expand": false,
      },
    },
    {
      label: "角色",
      prop: "roleIds",
      type: "select",
      attrs: { multiple: true },
      options: roleArr,
      initialValue: [],
    },
    {
      label: "状态",
      prop: "status",
      type: "radio",
      options: [
        { label: "正常", value: 1 },
        { label: "禁用", value: 0 },
      ],
      initialValue: 1,
    },
  ],
});

编辑

编辑配置可切换为抽屉模式,formAction 根据主键调用更新接口:

typescript
const editModalConfig: IModalConfig<UserForm> = reactive({
  permPrefix: "sys:user",
  component: "drawer",
  drawer: { title: "修改用户", size: 500 },
  pk: "id",
  formAction: (data) => UserAPI.update(data.id, data),
  formItems: [
    {
      label: "用户名",
      prop: "username",
      type: "input",
      attrs: { readonly: true },
    },
    {
      label: "状态",
      prop: "status",
      type: "switch",
      attrs: {
        inlinePrompt: true,
        activeText: "正常",
        inactiveText: "禁用",
        activeValue: 1,
        inactiveValue: 0,
      },
    },
  ],
});

自定义表单项

type: "custom" 配合插槽实现自定义组件:

vue
<page-modal ref="addModalRef" :modal-config="addModalConfig" @submit-click="handleSubmitClick">
  <template #gender="scope">
    <DictSelect v-model="scope.formData[scope.prop]" code="gender" v-bind="scope.attrs" />
  </template>
</page-modal>

Events

事件名说明回调参数
submit-click表单提交成功(formData: any)

实例方法

方法说明
setFormData(data)设置表单数据(编辑/查看时使用)
setModalVisible(visible?)打开/关闭弹窗
getFormData(key?)获取表单数据
setFormItemData(key, value)设置某一字段值
handleDisabled(disable)设置表单禁用状态(查看模式)

基于 MIT 许可发布 · 如需部署协助或二开定制,请查看 支持与合作