diff --git a/frontend/src/components/FormDesigner/Designer.tsx b/frontend/src/components/FormDesigner/Designer.tsx index 5fab1882..0a86accd 100644 --- a/frontend/src/components/FormDesigner/Designer.tsx +++ b/frontend/src/components/FormDesigner/Designer.tsx @@ -43,7 +43,7 @@ import { arrayMove } from '@dnd-kit/sortable'; import ComponentPanel from './components/ComponentPanel'; import DesignCanvas from './components/DesignCanvas'; import PropertyPanel from './components/PropertyPanel'; -import FormPreview, { type FormPreviewRef } from './components/FormPreview'; +import FormPreview, { type FormPreviewRef } from './Preview'; import { COMPONENT_LIST } from './config'; import type { FieldConfig, FormConfig, FormSchema } from './types'; import type { ComponentMeta } from './config'; diff --git a/frontend/src/components/FormDesigner/components/FormPreview.tsx b/frontend/src/components/FormDesigner/Preview.tsx similarity index 90% rename from frontend/src/components/FormDesigner/components/FormPreview.tsx rename to frontend/src/components/FormDesigner/Preview.tsx index f3ac64cc..5959f14b 100644 --- a/frontend/src/components/FormDesigner/components/FormPreview.tsx +++ b/frontend/src/components/FormDesigner/Preview.tsx @@ -1,24 +1,62 @@ /** * 表单预览组件 - * 预览设计好的表单效果 + * 独立的表单预览组件,支持完整的表单交互和验证 + * + * 特点: + * - ✅ 完全独立,可在任何地方使用 + * - ✅ 支持所有表单字段类型 + * - ✅ 支持验证规则和联动规则 + * - ✅ 支持动态数据源(API、预定义) + * - ✅ 非受控组件,内部管理状态 + * + * @example + * ```tsx + * // 使用示例 + * import { FormPreview, FormPreviewRef } from '@/components/FormDesigner'; + * + * const MyComponent = () => { + * const previewRef = useRef(null); + * + * // 从后端获取表单定义 + * const formSchema = await getFormDefinitionById(id); + * + * return ( + * + * ); + * }; + * ``` */ import React, { useState, useEffect, useImperativeHandle, forwardRef } from 'react'; import { Form, message } from 'antd'; import type { Rule } from 'antd/es/form'; import dayjs from 'dayjs'; -import type { FieldConfig, FormConfig, ValidationRule, LinkageRule } from '../types'; -import FieldRenderer from './FieldRenderer'; -import GridFieldPreview from './GridFieldPreview'; -import '../styles.css'; +import type { FieldConfig, FormConfig, ValidationRule, LinkageRule } from './types'; +import FieldRenderer from './components/FieldRenderer'; +import GridFieldPreview from './components/GridFieldPreview'; +import './styles.css'; -interface FormPreviewProps { +/** + * 表单预览组件的 Props + */ +export interface FormPreviewProps { + /** 字段列表 */ fields: FieldConfig[]; + /** 表单配置 */ formConfig: FormConfig; } +/** + * 表单预览组件暴露的方法 + */ export interface FormPreviewRef { + /** 提交表单(触发验证) */ submit: () => Promise; + /** 重置表单 */ reset: () => void; } diff --git a/frontend/src/components/FormDesigner/index.tsx b/frontend/src/components/FormDesigner/index.tsx index bcae44be..bde1db9c 100644 --- a/frontend/src/components/FormDesigner/index.tsx +++ b/frontend/src/components/FormDesigner/index.tsx @@ -1,14 +1,18 @@ /** * 表单设计器组件库入口 * - * 提供两个核心组件: + * 提供三个核心组件: * 1. FormDesigner - 设计时组件,用于可视化设计表单 - * 2. FormRenderer - 运行时组件,用于渲染和提交表单 + * 2. FormPreview - 预览组件,用于预览和交互表单(推荐) + * 3. FormRenderer - 运行时组件,用于渲染和提交表单(已废弃,建议使用 FormPreview) */ export { default as FormDesigner } from './Designer'; export type { FormDesignerProps } from './Designer'; +export { default as FormPreview } from './Preview'; +export type { FormPreviewProps, FormPreviewRef } from './Preview'; + export { default as FormRenderer } from './Renderer'; export type { FormRendererProps } from './Renderer'; diff --git a/frontend/src/pages/Workflow/Design/components/FormPreviewModal.tsx b/frontend/src/pages/Workflow/Design/components/FormPreviewModal.tsx index 2b2b4173..bc9e154c 100644 --- a/frontend/src/pages/Workflow/Design/components/FormPreviewModal.tsx +++ b/frontend/src/pages/Workflow/Design/components/FormPreviewModal.tsx @@ -4,13 +4,8 @@ */ import React from 'react'; -import { - Dialog, - DialogContent, - DialogHeader, - DialogTitle, -} from '@/components/ui/dialog'; -import { FormRenderer } from '@/components/FormDesigner'; +import { Modal } from 'antd'; +import { FormPreview } from '@/components/FormDesigner'; import type { FormDefinitionResponse } from '@/pages/Form/Definition/types'; interface FormPreviewModalProps { @@ -28,48 +23,29 @@ const FormPreviewModal: React.FC = ({ return null; } + const { schema } = formDefinition; + return ( - - - - - 预览表单:{formDefinition.name} - {formDefinition.description && ( - - {formDefinition.description} - - )} - - - -
- -
- -
-
表单标识:{formDefinition.key}
-
表单版本:v{formDefinition.formVersion}
-
状态: - - {formDefinition.status === 'PUBLISHED' ? '已发布' : - formDefinition.status === 'DRAFT' ? '草稿' : - formDefinition.status} - -
-
-
-
+ + + ); }; export default FormPreviewModal; -