@coding-form/form-engine 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,233 +1,23 @@
1
- [![npm](https://img.shields.io/npm/v/@coding-form/form-engine.svg)](https://www.npmjs.com/package/@coding-form/form-engine)
2
- # Form Engine
1
+ # Rslib project
3
2
 
4
- 一个基于 React 的动态表单引擎,支持表单动态渲染、字段验证、事件处理和布局控制。
3
+ ## Setup
5
4
 
6
- ## 项目结构
7
-
8
- ```
9
- coding-form/
10
- ├── packages/
11
- │ └── form-engine/ # 核心表单视图库
12
- │ ├── src/
13
- │ │ ├── form/ # 表单组件
14
- │ │ ├── factory/ # 表单项工厂
15
- │ │ ├── register/ # 组件注册中心
16
- │ │ ├── store/ # Redux 状态管理
17
- │ │ ├── context/ # React Context
18
- │ │ ├── instance/ # 表单实例控制
19
- │ │ ├── presenters/ # 表单展示层
20
- │ │ ├── validate/ # 表单验证
21
- │ │ ├── event/ # 事件处理
22
- │ │ ├── layout/ # 布局系统
23
- │ │ ├── hooks/ # 自定义 Hooks
24
- │ │ └── types/ # TypeScript 类型定义
25
- │ └── dist/ # 构建输出
26
- ├── apps/ # 示例应用
27
- │ ├── app-pc/ # PC 端示例
28
- │ └── app-mobile/ # 移动端示例
29
- └── package.json # 工作区配置
30
- ```
31
-
32
- ## 核心功能
33
-
34
- ### 1. 动态表单渲染
35
-
36
- 通过元数据(Meta)动态生成表单,支持:
37
- - 多种数据类型:`STRING`, `INTEGER`, `LONG`, `DOUBLE`, `BOOLEAN`
38
- - 字段属性:名称、编码、类型、默认值、提示等
39
- - 嵌套子表单(subForms)
40
-
41
- ### 2. 表单验证
42
-
43
- ```typescript
44
- interface FormFieldValidator {
45
- target: FieldKey; // 校验目标字段
46
- validator: (instance: FormInstance, value: any) => boolean | string;
47
- }
48
- ```
49
-
50
- - 支持自定义验证规则
51
- - 异步验证支持
52
- - 隐藏字段自动跳过验证
53
-
54
- ### 3. 事件系统
55
-
56
- 支持表单事件绑定和处理:
57
-
58
- ```typescript
59
- interface FormEvent {
60
- type: 'load' | 'change' | 'blur'; // 事件类型
61
- target: FieldKey; // 事件目标
62
- event: (formInstance: FormInstance, value: any) => void;
63
- }
64
- ```
65
-
66
- - `load`: 表单加载时触发
67
- - `change`: 字段值变化时触发
68
- - `blur`: 字段失去焦点时触发
69
-
70
- ### 4. 布局控制
71
-
72
- 支持卡片布局和默认布局:
73
-
74
- ```typescript
75
- interface FormLayout {
76
- formCode: string;
77
- type: 'card';
78
- props: CardLayout
79
- }
80
-
81
- interface CardLayout {
82
- title: string;
83
- layout: 'horizontal' | 'vertical';
84
- fields: FieldLayout[];
85
- mainFields: string[];
86
- }
87
- ```
88
-
89
- ### 5. 刷新组件
90
-
91
- 通过增加字段 `version` 版本号来触发组件的重新渲染:
92
-
93
- ```typescript
94
- interface StateField {
95
- version?: number; // 版本号,每次刷新时递增
96
- }
97
- ```
98
-
99
- - `refreshFields`: 刷新指定字段,触发组件重新渲染
100
- - 支持单个字段或字段数组
101
- - 支持主表单和子表单
102
- - 通过递增 `version` 值实现强制刷新
103
-
104
- ```typescript
105
- // 刷新单个字段
106
- form.refreshFields('field_code');
107
-
108
- // 刷新多个字段
109
- form.refreshFields(['field1', 'field2']);
110
-
111
- // 刷转子表单字段
112
- form.refreshFields(['field1'], 'sub_form_code');
113
- ```
114
-
115
- ### 6. 表单实例控制
116
-
117
- 提供丰富的表单操作方法:
118
-
119
- ```typescript
120
- // 获取/设置字段值
121
- getFieldValue(name: string, formCode?: string)
122
- getFieldsValue(formCode?: string)
123
- setFieldValue(name: string, value: any, formCode?: string)
124
- setFieldsValue(values: any, formCode?: string)
125
-
126
- // 表单操作
127
- resetFields(nameList?: string[], formCode?: string)
128
- validateFields(nameList?: string[], formCode?: string)
129
- submit(formCode?: string)
130
-
131
- // 动态控制
132
- hiddenFields(hidden: boolean, nameList: string[]|string, formCode?: string)
133
- requiredFields(required: boolean, nameList: string[]|string, formCode?: string)
134
- refreshFields(nameList: string[]|string, formCode?: string) // 刷新字段,触发重新渲染
135
- ```
136
-
137
- ## 快速开始
138
-
139
- ### 1. 安装依赖
5
+ Install the dependencies:
140
6
 
141
7
  ```bash
142
8
  pnpm install
143
9
  ```
144
10
 
145
- ### 2. 注册表单组件
146
-
147
- ```tsx
148
- import { FormRegistry } from '@coding-form/form-engine';
149
- import YourFormComponent from './YourFormComponent';
150
- import formInstanceFactory from './formInstanceFactory';
151
-
152
- FormRegistry.getInstance().register(YourFormComponent, formInstanceFactory);
153
- ```
154
-
155
- ### 3. 使用表单视图
156
-
157
- ```tsx
158
- import { FormView } from '@coding-form/form-engine';
159
-
160
- const meta = {
161
- name: '用户信息',
162
- code: 'user_form',
163
- fields: [
164
- {
165
- id: '1',
166
- name: '姓名',
167
- code: 'name',
168
- type: 'text_input',
169
- dataType: 'STRING',
170
- hidden: false,
171
- required: true,
172
- placeholder: '请输入姓名'
173
- }
174
- ]
175
- };
11
+ ## Get started
176
12
 
177
- <FormView meta={meta} />;
178
- ```
179
-
180
- ### 4. 开发模式
13
+ Build the library:
181
14
 
182
15
  ```bash
183
- # 监听构建 form-view
184
- pnpm watch:form-view
185
-
186
- # 运行 PC 示例应用
187
- pnpm dev:app-pc
188
-
189
- # 运行移动端示例应用
190
- pnpm dev:app-mobile
16
+ pnpm run build
191
17
  ```
192
18
 
193
- ### 5. 构建
19
+ Build the library in watch mode:
194
20
 
195
21
  ```bash
196
- pnpm build
197
- ```
198
-
199
- ## 技术栈
200
-
201
- - **React 18** - UI 框架
202
- - **TypeScript** - 类型系统
203
- - **Redux Toolkit** - 状态管理
204
- - **React Redux** - React 绑定
205
- - **Rslib** - 构建工具
206
-
207
- ## API 参考
208
-
209
- ### FormView 组件属性
210
-
211
- | 属性 | 类型 | 说明 |
212
- |------|------|------|
213
- | meta | FormMeta | 表单元数据定义 |
214
- | form | FormInstance | 表单实例 |
215
- | onValuesChange | (values: any) => void | 值变化回调 |
216
- | review | boolean | 是否预览模式 |
217
- | validators | FormFieldValidator[] | 验证规则数组 |
218
- | events | FormEvent[] | 事件定义数组 |
219
- | layouts | FormLayout[] | 布局定义数组 |
220
-
221
- ### 数据类型
222
-
223
- | 类型 | 说明 |
224
- |------|------|
225
- | STRING | 字符串 |
226
- | INTEGER | 整数 |
227
- | LONG | 长整数 |
228
- | DOUBLE | 小数 |
229
- | BOOLEAN | 布尔值 |
230
-
231
- ## 许可证
232
-
233
- [LICENSE](./LICENSE)
22
+ pnpm run dev
23
+ ```
@@ -12,17 +12,22 @@ const FormViewContent = (props)=>{
12
12
  const meta = props.meta;
13
13
  const subFormList = meta.subForms || [];
14
14
  const review = props.review || false;
15
+ const handleOnSubmit = (values, formCode)=>{
16
+ props.onFinish?.(values, formCode);
17
+ };
15
18
  return /*#__PURE__*/ react.createElement(FormContext.Provider, {
16
19
  value: context
17
- }, /*#__PURE__*/ react.createElement(FormSubView, {
20
+ }, props.header, /*#__PURE__*/ react.createElement(FormSubView, {
18
21
  Form: Form,
19
22
  formCode: meta.code,
20
- review: review
23
+ review: review,
24
+ onFinish: handleOnSubmit
21
25
  }), subFormList && subFormList.map((item)=>/*#__PURE__*/ react.createElement(FormSubView, {
22
26
  Form: Form,
23
27
  formCode: item.code,
24
- review: review
25
- })));
28
+ review: review,
29
+ onFinish: handleOnSubmit
30
+ })), props.footer);
26
31
  };
27
32
  const FormView = (props)=>{
28
33
  const Form = FormRegistry.getInstance().getFormComponent();
@@ -3,6 +3,7 @@ interface FormSubViewProps {
3
3
  formCode: string;
4
4
  Form: React.ComponentType<any>;
5
5
  review: boolean;
6
+ onFinish: (values: any, formCode: string) => void;
6
7
  }
7
8
  export declare const FormSubView: React.FC<FormSubViewProps>;
8
9
  export {};
@@ -23,7 +23,10 @@ const FormSubView = (props)=>{
23
23
  if (events && events.length > 0) for (const event of events)event.event(formInstance);
24
24
  }, []);
25
25
  return /*#__PURE__*/ react.createElement(Form, {
26
- form: formTarget
26
+ form: formTarget,
27
+ onFinish: (values)=>{
28
+ props.onFinish(values, props.formCode);
29
+ }
27
30
  }, layoutContext.render(props.formCode, fields, review, context));
28
31
  };
29
32
  export { FormSubView };
@@ -1,14 +1,15 @@
1
+ import { NamePath } from "../types";
1
2
  export declare class FormControl {
2
3
  private readonly formCode;
3
4
  private readonly proxyTarget;
4
5
  constructor(formCode: string, target: any);
5
6
  getFormCode(): string;
6
7
  getProxyTarget(): any;
7
- getFieldValue(name: string): any;
8
+ getFieldValue(name: NamePath): any;
8
9
  getFieldsValue(): any;
9
- resetFields(nameList?: string[]): void;
10
+ resetFields(nameList?: NamePath[] | NamePath): void;
10
11
  setFieldsValue(values: any): void;
11
- setFieldValue(name: string, value: any): void;
12
+ setFieldValue(name: NamePath, value: any): void;
12
13
  submit(): void;
13
- validateFields(nameList?: string[]): Promise<any>;
14
+ validateFields(nameList?: NamePath[] | NamePath): Promise<any>;
14
15
  }
@@ -1,7 +1,7 @@
1
- import { FormMeta } from "../types";
1
+ import { FormInstanceInterface, FormMeta, NamePath } from "../types";
2
2
  import { FormControl } from "./control";
3
3
  import { FormPresenter } from "../presenters";
4
- export declare class FormInstance {
4
+ export declare class FormInstance implements FormInstanceInterface {
5
5
  private readonly instanceList;
6
6
  private readonly meta;
7
7
  private presenter;
@@ -11,14 +11,14 @@ export declare class FormInstance {
11
11
  private initInstanceList;
12
12
  getFormControl(formCode?: string): FormControl | undefined;
13
13
  getProxyTarget(formCode?: string): any;
14
- getFieldValue(name: string, formCode?: string): void;
14
+ getFieldValue(name: NamePath, formCode?: string): void;
15
15
  getFieldsValue(formCode?: string): any;
16
- resetFields(nameList?: string[], formCode?: string): void;
16
+ resetFields(nameList?: NamePath[] | NamePath, formCode?: string): void;
17
17
  setFieldsValue(values: any, formCode?: string): void;
18
- setFieldValue(name: string, value: any, formCode?: string): void;
18
+ setFieldValue(name: NamePath, value: any, formCode?: string): void;
19
19
  submit(formCode?: string): void;
20
- validateFields(nameList?: string[], formCode?: string): Promise<any>;
21
- hiddenFields(hidden: boolean, nameList: string[] | string, formCode?: string): void;
22
- requiredFields(required: boolean, nameList: string[] | string, formCode?: string): void;
23
- refreshFields(nameList: string[] | string, formCode?: string): void;
20
+ validateFields(nameList?: NamePath[] | NamePath, formCode?: string): Promise<any>;
21
+ hiddenFields(hidden: boolean, nameList: NamePath[] | NamePath, formCode?: string): void;
22
+ requiredFields(required: boolean, nameList: NamePath[] | NamePath, formCode?: string): void;
23
+ refreshFields(nameList: NamePath[] | NamePath, formCode?: string): void;
24
24
  }
@@ -1,13 +1,13 @@
1
- import { Dispatch, FormMeta, FormState } from "../types";
1
+ import { Dispatch, FormMeta, FormState, NamePath } from "../types";
2
2
  export declare class FormPresenter {
3
3
  private state;
4
4
  private readonly dispatch;
5
5
  constructor(state: FormState, dispatch: Dispatch<FormState>);
6
6
  syncState(state: FormState): void;
7
7
  initialState(meta: FormMeta): void;
8
- hiddenFields(hidden: boolean, nameList: string[] | string, formCode?: string): void;
9
- refreshFields(nameList: string[] | string, formCode?: string): void;
10
- requiredFields(required: boolean, nameList: string[] | string, formCode?: string): void;
8
+ hiddenFields(hidden: boolean, nameList: NamePath[] | NamePath, formCode?: string): void;
9
+ refreshFields(nameList: NamePath[] | NamePath, formCode?: string): void;
10
+ requiredFields(required: boolean, nameList: NamePath[] | NamePath, formCode?: string): void;
11
11
  private hiddenMapFields;
12
12
  private refreshMapFields;
13
13
  private requiredMapFields;
@@ -6,3 +6,4 @@ export * from "./validate";
6
6
  export * from "./event";
7
7
  export * from "./dispatch";
8
8
  export * from "./layout";
9
+ export * from "./instance";
@@ -6,3 +6,4 @@ export * from "./validate.js";
6
6
  export * from "./event.js";
7
7
  export * from "./dispatch.js";
8
8
  export * from "./layout.js";
9
+ export * from "./instance.js";
@@ -0,0 +1,26 @@
1
+ export type NamePath = string | string[];
2
+ /**
3
+ * 表单实例对象能力
4
+ */
5
+ export interface FormInstanceInterface {
6
+ /** 获取表某个单值 **/
7
+ getFieldValue: (name: NamePath, formCode?: string) => any;
8
+ /** 获取表所有值 **/
9
+ getFieldsValue: (formCode?: string) => any;
10
+ /** 重置表单值 **/
11
+ resetFields: (fields?: NamePath[] | NamePath, formCode?: string) => void;
12
+ /** 设置表单所有值 **/
13
+ setFieldsValue: (values: any, formCode?: string) => void;
14
+ /** 设置表单值 **/
15
+ setFieldValue: (name: NamePath, value: any, formCode?: string) => void;
16
+ /** 表单提交 **/
17
+ submit: (formCode?: string) => void;
18
+ /** 校验字段 **/
19
+ validateFields: (nameList?: NamePath[] | NamePath, formCode?: string) => Promise<any>;
20
+ /** 隐藏字段控制 **/
21
+ hiddenFields: (hidden: boolean, nameList: NamePath[] | NamePath, formCode?: string) => void;
22
+ /** 必填字段控制 **/
23
+ requiredFields: (required: boolean, nameList: NamePath[] | NamePath, formCode?: string) => void;
24
+ /** 刷新字段控制 **/
25
+ refreshFields: (nameList: NamePath[] | NamePath, formCode?: string) => void;
26
+ }
File without changes
@@ -6,11 +6,3 @@ export declare const dataTypeOptions: {
6
6
  label: string;
7
7
  value: string;
8
8
  }[];
9
- /**
10
- * 表单类型
11
- */
12
- export interface FormType {
13
- name: string;
14
- type: string;
15
- dataType: DataType;
16
- }
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  import { DataType } from "./types";
2
3
  import { FormInstance } from "../instance";
3
4
  import { FormEvent, FormFieldValidator } from "./";
@@ -69,6 +70,12 @@ export interface FormViewProps {
69
70
  form?: FormInstance;
70
71
  /** 表单数据更新事件 */
71
72
  onValuesChange?: (values: any) => void;
73
+ /** 表单提交数据 **/
74
+ onFinish?: (values: any, formCode?: string) => void;
75
+ /** 表单头内容 **/
76
+ header?: React.ReactNode;
77
+ /** 表单底部内容 **/
78
+ footer?: React.ReactNode;
72
79
  /** 是否预览模式 */
73
80
  review?: boolean;
74
81
  /** 字段校验逻辑 */
@@ -0,0 +1 @@
1
+ import "react";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coding-form/form-engine",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "form-engine components",
5
5
  "keywords": [
6
6
  "coding-flow",