Skip to content

createDynamicFormPop

createDynamicFormPop 是基于 pop.createModal 的动态表单快捷 API,用于快速打开“带校验的表单弹窗”,适合新建/编辑这类标准 CRUD 场景。

引入方式

typescript
import { createDynamicFormPop } from '@erp/biz';

API 说明

typescript
type DynamicFormPopProps<T = Record<string, any>> = {
  vm: T;
  config: Partial<DynamicFormI>;
  modalConfig?: Partial<ModalConfig>;
};

function createDynamicFormPop<T = Record<string, any>>(
  params: DynamicFormPopProps<T>,
): Promise<T>;

参数说明

参数名描述类型是否必传默认值
vm弹窗表单数据模型(作为 DynamicFormmodelValueT-
config动态表单配置(字段、按钮、校验规则等)Partial<DynamicFormI>-
modalConfig外层 Modal 配置,如 titlewidthidPartial<ModalConfig>-

返回值

  • 成功提交:返回最新表单模型 T
  • 用户关闭/取消:Promise reject(可通过 try/catch 忽略)

基础用法

通过配置直接弹窗

typescript
import { createDynamicFormPop, pop } from '@erp/biz';

type Status = '启用' | '禁用';
type FormVm = { name: string; code: string; status: Status; remark?: string };

async function openCreateForm() {
  try {
    const data = await createDynamicFormPop<FormVm>({
      vm: { name: '', code: '', status: '启用', remark: '' },
      config: {
        title: '新建',
        showSide: false,
        cols: 2,
        buttons: [
          { code: 'submit', label: '提交', type: 'primary', validator: true },
          { code: 'cancel', label: '取消', type: 'secondary' },
        ],
        groups: [
          {
            label: '基础信息',
            fields: [
              { key: 'name', label: '名称', type: 'text', required: true },
              { key: 'code', label: '编码', type: 'text', required: true },
              {
                key: 'status',
                label: '状态',
                type: 'select',
                required: true,
                options: [
                  { label: '启用', value: '启用' },
                  { label: '禁用', value: '禁用' },
                ],
              },
              { key: 'remark', label: '备注', type: 'textarea' },
            ],
          },
        ],
      },
      modalConfig: {
        title: '新建数据',
        width: 620,
        id: 'create-demo-form',
      },
    });

    // TODO: 调用新增接口
    // await createApi(data);
    pop.success('创建成功');
  } catch {
    // 用户取消
  }
}

CRUD 示例模板

完整模板见:

  • /template/crud-page/crud-page.md

基于 MIT 许可发布