/** * FormDesigner 组件使用示例页面 * * 展示如何使用 @/components/FormDesigner 中的组件: * - FormDesigner: 表单设计器(设计时) * - FormRenderer: 表单渲染器(运行时) */ import React, { useState } from 'react'; import { message, Tabs, Card, Button, Modal, Space, Divider, Alert } from 'antd'; import { FormDesigner, FormRenderer, type FormSchema } from '@/components/FormDesigner'; import { WORKFLOW_COMPONENTS } from '@/components/FormDesigner/extensions/workflow'; const FormDesignerExamplesPage: React.FC = () => { // ==================== 示例 1: 普通表单设计器 ==================== const [designerSchema, setDesignerSchema] = useState(); const handleDesignerSave = async (schema: FormSchema) => { console.log('💾 保存的表单 Schema:', JSON.stringify(schema, null, 2)); message.success('表单设计已保存!请查看控制台'); setPreviewSchema(schema); // 保存后更新预览 // 实际项目中这里会调用后端 API 保存 // await formSchemaService.save(schema); }; // ==================== 示例 1.5: 工作流表单设计器(带审批字段) ==================== const [workflowDesignerSchema, setWorkflowDesignerSchema] = useState(); const handleWorkflowDesignerSave = async (schema: FormSchema) => { console.log('🔄 保存的工作流表单 Schema:', JSON.stringify(schema, null, 2)); message.success('工作流表单设计已保存!请查看控制台'); setWorkflowPreviewSchema(schema); // 实际项目中这里会调用后端 API 保存 // await workflowFormService.save(schema); }; const [workflowPreviewSchema, setWorkflowPreviewSchema] = useState(); // ==================== 示例 2: 表单渲染器(静态) ==================== const [previewSchema, setPreviewSchema] = useState(); const handleStaticFormSubmit = async (data: Record) => { console.log('📤 静态表单提交数据:', data); message.success('表单提交成功!请查看控制台'); }; // ==================== 示例 3: 工作流表单(弹窗) ==================== const [workflowModalVisible, setWorkflowModalVisible] = useState(false); const workflowFormSchema: FormSchema = { version: '1.0', formConfig: { labelAlign: 'right', size: 'middle', formWidth: 600 }, fields: [ { id: 'wf_field_1', type: 'input', label: '工作流名称', name: 'workflowName', placeholder: '请输入工作流名称', required: true }, { id: 'wf_field_2', type: 'select', label: '目标环境', name: 'environment', placeholder: '请选择环境', required: true, dataSourceType: 'static', options: [ { label: '开发环境', value: 'dev' }, { label: '测试环境', value: 'test' }, { label: '生产环境', value: 'prod' } ] }, { id: 'wf_field_3', type: 'textarea', label: '执行说明', name: 'description', placeholder: '请输入执行说明', rows: 4 }, ], }; const handleWorkflowSubmit = async (data: Record) => { console.log('🚀 工作流启动参数:', data); message.success('工作流已启动!'); setWorkflowModalVisible(false); }; // ==================== 示例 4: 只读模式 ==================== const readonlySchema: FormSchema = { version: '1.0', formConfig: { labelAlign: 'right', size: 'middle', formWidth: 600 }, fields: [ { id: 'ro_field_1', type: 'input', label: '申请人', name: 'applicant' }, { id: 'ro_field_2', type: 'date', label: '申请日期', name: 'applyDate' }, { id: 'ro_field_3', type: 'select', label: '审批状态', name: 'approvalStatus', dataSourceType: 'static', options: [ { label: '待审批', value: 'pending' }, { label: '已通过', value: 'approved' }, { label: '已拒绝', value: 'rejected' } ] }, { id: 'ro_field_4', type: 'textarea', label: '审批意见', name: 'approvalComment' }, ], }; const readonlyData = { applicant: '张三', applyDate: '2024-07-20', approvalStatus: 'approved', approvalComment: '同意申请,准予执行。', }; // ==================== 示例 5: 复杂表单(带栅格布局) ==================== const complexFormSchema: FormSchema = { version: '1.0', formConfig: { labelAlign: 'right', size: 'middle', formWidth: 800, title: '用户信息登记表' }, fields: [ { id: 'grid_1', type: 'grid', label: '基本信息', name: 'grid_basic', columns: 2, columnSpans: [12, 12], gutter: 16, children: [ [ { id: 'f1', type: 'input', label: '姓名', name: 'name', placeholder: '请输入姓名', required: true }, { id: 'f2', type: 'number', label: '年龄', name: 'age', placeholder: '请输入年龄', min: 1, max: 150 }, ], [ { id: 'f3', type: 'radio', label: '性别', name: 'gender', dataSourceType: 'static', options: [{ label: '男', value: 'male' }, { label: '女', value: 'female' }] }, { id: 'f4', type: 'date', label: '出生日期', name: 'birthday', placeholder: '请选择日期' }, ], ], }, { id: 'grid_2', type: 'grid', label: '联系方式', name: 'grid_contact', columns: 2, columnSpans: [12, 12], gutter: 16, children: [ [ { id: 'f5', type: 'input', label: '手机号', name: 'phone', placeholder: '请输入手机号' }, { id: 'f6', type: 'input', label: '邮箱', name: 'email', placeholder: '请输入邮箱' }, ], [ { id: 'f7', type: 'input', label: '微信', name: 'wechat', placeholder: '请输入微信号' }, { id: 'f8', type: 'input', label: 'QQ', name: 'qq', placeholder: '请输入QQ号' }, ], ], }, { id: 'f9', type: 'textarea', label: '个人简介', name: 'bio', placeholder: '请输入个人简介', rows: 4, }, ], }; const handleComplexFormSubmit = async (data: Record) => { console.log('📋 复杂表单提交数据:', data); message.success('表单提交成功!'); }; // ==================== 示例 6: 带生命周期钩子的表单 ==================== const lifecycleFormSchema: FormSchema = { version: '1.0', formConfig: { labelAlign: 'right', size: 'middle', formWidth: 600 }, fields: [ { id: 'lc_field_1', type: 'input', label: '用户名', name: 'username', placeholder: '请输入用户名', required: true }, { id: 'lc_field_2', type: 'input', label: '邮箱', name: 'email', placeholder: '请输入邮箱', required: true, validationRules: [ { type: 'email', message: '请输入有效的邮箱地址' } ] }, { id: 'lc_field_3', type: 'select', label: '用户角色', name: 'role', placeholder: '请选择角色', required: true, dataSourceType: 'static', options: [ { label: '管理员', value: 'admin' }, { label: '普通用户', value: 'user' }, { label: '访客', value: 'guest' } ] }, ], }; const handleLifecycleBeforeSubmit = async (values: Record) => { console.log('🎣 beforeSubmit 钩子被调用:', values); // 模拟数据处理 const processedValues = { ...values, email: values.email?.toLowerCase(), // 邮箱转小写 submitTime: new Date().toISOString(), // 添加提交时间 }; message.info('正在处理表单数据...'); await new Promise(resolve => setTimeout(resolve, 500)); console.log('✅ beforeSubmit 处理完成,返回修改后的数据:', processedValues); return processedValues; }; const handleLifecycleSubmit = async (data: Record) => { console.log('📤 提交到服务器:', data); // 模拟 API 调用 await new Promise(resolve => setTimeout(resolve, 1000)); const response = { id: Math.random(), success: true, data }; console.log('✅ 服务器响应:', response); message.success('用户创建成功!'); return response; }; const handleLifecycleAfterSubmit = async (response: any) => { console.log('🎣 afterSubmit 钩子被调用,服务器响应:', response); // 模拟后续操作(如跳转、刷新列表等) await new Promise(resolve => setTimeout(resolve, 300)); message.success(`用户 ID: ${response.id.toFixed(0)} 已成功创建`); console.log('✅ afterSubmit 完成,可以执行跳转或刷新操作'); }; const handleLifecycleError = async (error: any) => { console.error('🎣 onError 钩子被调用:', error); // 可以在这里做错误上报、记录等 message.error(`操作失败: ${error.message || '未知错误'}`); }; // ==================== Tab 配置 ==================== const tabItems = [ { key: '1', label: '📝 示例 1: 普通表单设计器', children: (

普通表单设计器(FormDesigner)

可视化拖拽设计表单,支持导入/导出 JSON Schema,支持字段属性配置、验证规则、联动规则等。

), }, { key: '1.5', label: '🔄 示例 1.5: 工作流表单设计器', children: (

工作流表单设计器(带审批字段)

通过 extraComponents 参数注入工作流扩展字段(审批人选择器等),实现插件式扩展。

✅ 核心组件 + 工作流扩展组件(审批人选择器)

✅ 左侧"高级字段"分类中可以看到"审批人"组件

✅ 拖拽到画布后,右侧显示完整的审批配置(9项)

✅ 支持审批模式、审批人类型、超时设置等

} type="success" showIcon style={{ marginBottom: 16 }} />
使用方式:
{` {` value={schema}`}
{` onChange={setSchema}`}
{` extraComponents={WORKFLOW_COMPONENTS}`}
{`/>`}
), }, { key: '2', label: '🎨 示例 2: 普通表单渲染', children: (

表单渲染器(普通表单)

根据设计器保存的 Schema 渲染表单,支持数据收集、验证和提交。

{previewSchema ? ( ) : (
请先在"示例 1"中设计表单并保存
)}
), }, { key: '2.5', label: '🔄 示例 2.5: 工作流表单渲染', children: (

工作流表单渲染(带审批字段)

渲染包含审批字段的表单,必须传递 extraComponents 参数以支持扩展组件。

{workflowPreviewSchema ? ( { console.log('🔄 工作流表单提交数据:', data); message.success('工作流表单提交成功!'); }} showSubmit showCancel /> ) : (
请先在"示例 1.5"中设计工作流表单并保存
)}
), }, { key: '3', label: '🚀 示例 3: 工作流表单', children: (

工作流表单(弹窗模式)

模拟工作流启动场景,在 Modal 中使用表单渲染器收集参数。

使用场景

  • 工作流启动参数收集
  • 任务审批表单
  • 快捷操作弹窗
setWorkflowModalVisible(false)} footer={null} width={workflowFormSchema.formConfig.formWidth || 600} > setWorkflowModalVisible(false)} showCancel submitText="启动" cancelText="取消" />
), }, { key: '4', label: '👁️ 示例 4: 只读模式', children: (

只读模式

查看已提交的表单数据,所有字段禁用编辑,不显示提交按钮。

使用场景

  • 审批流程详情查看
  • 历史数据展示
  • 表单归档查看
), }, { key: '5', label: '📋 示例 5: 复杂表单', children: (

复杂表单(带栅格布局)

展示包含栅格布局的复杂表单,支持多列排版。

), }, { key: '6', label: '🎣 示例 6: 生命周期钩子', children: (

生命周期钩子(beforeSubmit / afterSubmit / onError)

演示表单提交的完整生命周期钩子,支持数据预处理、提交后处理和错误处理。

beforeSubmit: 提交前处理,可修改数据或取消提交(返回 false)

onSubmit: 执行实际提交操作

afterSubmit: 提交成功后的处理(跳转、刷新等)

onError: 错误处理(验证失败、提交失败等)

} type="info" showIcon style={{ marginBottom: 16 }} />
使用方式:
{` {` schema={schema}`}
{` beforeSubmit={async (values) => {`}
{` // 数据预处理`}
{` return modifiedValues;`}
{` }}`}
{` onSubmit={async (data) => {`}
{` // 提交到服务器`}
{` return response;`}
{` }}`}
{` afterSubmit={async (response) => {`}
{` // 提交后处理`}
{` }}`}
{` onError={async (error) => {`}
{` // 错误处理`}
{` }}`}
{`/>`}
), }, ]; return (

FormDesigner 组件使用示例

本页面展示如何使用 @/components/FormDesigner 中的 FormDesignerFormRenderer 组件。

); }; export default FormDesignerExamplesPage;