form-driver 0.3.14 → 0.4.0

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.
@@ -1,46 +1,63 @@
1
1
  import React, { useState } from "react";
2
- import { AFTER_CHANGE_CALLBACK, CHANGE_SCHEMA_CALLBACK, MFieldSchema, M3UISpec } from "../framework/Schema";
2
+ import {
3
+ AFTER_CHANGE_CALLBACK,
4
+ CHANGE_SCHEMA_CALLBACK,
5
+ MFieldSchema,
6
+ M3UISpec,
7
+ } from "../framework/Schema";
3
8
  import { Button, message, Modal } from "antd";
4
9
  import "./MViewer.less";
5
10
  import { MFieldViewer } from "./MFieldViewer";
6
- import { MUtil } from './MUtil';
7
- import { assembly, MORPH } from './Assembly';
11
+ import { MUtil } from "./MUtil";
12
+ import { assembly, MORPH } from "./Assembly";
8
13
  import _ from "lodash";
9
- import { ensureM3 } from './Init';
10
- import { MContext } from './MContext';
14
+ import { ensureM3 } from "./Init";
15
+ import { MContext } from "./MContext";
11
16
  import { PersistantTool, PersistantConf } from "./Persistant";
12
17
 
13
18
  export interface MViewerProp {
14
- schema: MFieldSchema,
15
- database: any,
16
- layout?: M3UISpec,
17
- style?: React.CSSProperties,
18
- morph: MORPH,
19
+ schema: MFieldSchema;
20
+ database: any;
21
+ layout?: M3UISpec;
22
+ style?: React.CSSProperties;
23
+ morph: MORPH;
19
24
  onSubmit?: (finalData: any) => Promise<any>;
20
- afterChange?: AFTER_CHANGE_CALLBACK,
21
- changeSchema?: CHANGE_SCHEMA_CALLBACK,
22
- changeDatabase?: CHANGE_SCHEMA_CALLBACK,
23
- wrapper?: (elem: React.ReactElement, schema: Partial<MFieldSchema>) => React.ReactElement,
24
- formItemWrapper?: (elem: React.ReactElement, schema: Partial<MFieldSchema>) => React.ReactElement,
25
+ afterChange?: AFTER_CHANGE_CALLBACK;
26
+ changeSchema?: CHANGE_SCHEMA_CALLBACK;
27
+ changeDatabase?: CHANGE_SCHEMA_CALLBACK;
28
+ wrapper?: (
29
+ elem: React.ReactElement,
30
+ schema: Partial<MFieldSchema>
31
+ ) => React.ReactElement;
32
+ formItemWrapper?: (
33
+ elem: React.ReactElement,
34
+ schema: Partial<MFieldSchema>
35
+ ) => React.ReactElement;
25
36
  /** 持久存储选项,nil表示不持久存储 */
26
- persistant?: PersistantConf
37
+ persistant?: PersistantConf;
27
38
  }
28
39
 
29
40
  export interface M3Prop {
30
- schema: MFieldSchema | MFieldSchema[],
31
- database: any,
32
- form?: any,
33
- layout?: M3UISpec,
34
- style?: React.CSSProperties,
35
- morph: MORPH,
41
+ schema: MFieldSchema | MFieldSchema[];
42
+ database: any;
43
+ form?: any;
44
+ layout?: M3UISpec;
45
+ style?: React.CSSProperties;
46
+ morph: MORPH;
36
47
  onSubmit?: (finalData: any) => Promise<any>;
37
- afterChange?: AFTER_CHANGE_CALLBACK,
38
- changeSchema?: CHANGE_SCHEMA_CALLBACK,
39
- changeDatabase?: CHANGE_SCHEMA_CALLBACK,
40
- wrapper?: (elem: React.ReactElement, schema: Partial<MFieldSchema>) => React.ReactElement,
41
- formItemWrapper?: (elem: React.ReactElement, schema: Partial<MFieldSchema>) => React.ReactElement,
48
+ afterChange?: AFTER_CHANGE_CALLBACK;
49
+ changeSchema?: CHANGE_SCHEMA_CALLBACK;
50
+ changeDatabase?: CHANGE_SCHEMA_CALLBACK;
51
+ wrapper?: (
52
+ elem: React.ReactElement,
53
+ schema: Partial<MFieldSchema>
54
+ ) => React.ReactElement;
55
+ formItemWrapper?: (
56
+ elem: React.ReactElement,
57
+ schema: Partial<MFieldSchema>
58
+ ) => React.ReactElement;
42
59
  /** 持久存储选项,nil表示不持久存储 */
43
- persistant?: PersistantConf
60
+ persistant?: PersistantConf;
44
61
  }
45
62
 
46
63
  interface State {
@@ -58,53 +75,72 @@ export class MViewer extends React.Component<MViewerProp, State> {
58
75
  this.state = {
59
76
  forceValid: false,
60
77
  ctrlVersion: 1,
61
- }
78
+ };
62
79
 
63
80
  ensureM3();
64
81
 
65
82
  const props = this.props;
66
83
 
67
84
  // 值类型兼容预处理
68
- this.database = assembly.types[props.schema.type]?.standardValue(assembly, props.schema, props.database, false);
85
+ this.database = assembly.types[props.schema.type]?.standardValue(
86
+ assembly,
87
+ props.schema,
88
+ props.database,
89
+ false
90
+ );
69
91
 
70
92
  // 填入默认值
71
93
  MUtil.applyDefaultValue(props.schema, props.database, "");
72
94
  // 填入本地缓存值
73
- this.recover()
95
+ this.recover();
74
96
  }
75
97
 
76
98
  recover() {
77
- const { ctrlVersion } = this.state
78
- const { persistant } = this.props
99
+ const { ctrlVersion } = this.state;
100
+ const { persistant } = this.props;
79
101
  PersistantTool.load(this.database, persistant, () => {
80
102
  this.setState({
81
- ctrlVersion: ctrlVersion + 1
82
- })
103
+ ctrlVersion: ctrlVersion + 1,
104
+ });
83
105
  });
84
106
  }
85
107
 
86
108
  render() {
87
109
  const props = this.props;
88
110
  const database = this.database;
89
- const { ctrlVersion, forceValid } = this.state
90
-
91
- return <MContext.Provider value={{
92
- rootProps: props,
93
- forceValid, setForceValid: (b) => { this.setState({ forceValid: true }) }
94
- }}>
95
- <div key={ctrlVersion} className={MUtil.phoneLike() ? "MEditor_p" : "MEditor"} style={props.style}>
96
- <MFieldViewer
97
- schema={props.schema}
98
- database={database}
99
- path=""
100
- morph={props.morph}
101
- afterChange={PersistantTool.patchAfterChange(props.afterChange, props.persistant)}
102
- changeSchema={props.changeSchema}
103
- changeDatabase={props.changeDatabase}
104
- />
105
- {props.children}
106
- </div>
107
- </MContext.Provider>
111
+ const { ctrlVersion, forceValid } = this.state;
112
+
113
+ return (
114
+ <MContext.Provider
115
+ value={{
116
+ rootProps: props,
117
+ forceValid,
118
+ setForceValid: (b) => {
119
+ this.setState({ forceValid: true });
120
+ },
121
+ }}
122
+ >
123
+ <div
124
+ key={ctrlVersion}
125
+ className={MUtil.phoneLike() ? "MEditor_p" : "MEditor"}
126
+ style={props.style}
127
+ >
128
+ <MFieldViewer
129
+ schema={props.schema}
130
+ database={database}
131
+ path=""
132
+ morph={props.morph}
133
+ afterChange={PersistantTool.patchAfterChange(
134
+ props.afterChange,
135
+ props.persistant
136
+ )}
137
+ changeSchema={props.changeSchema}
138
+ changeDatabase={props.changeDatabase}
139
+ />
140
+ {props.children}
141
+ </div>
142
+ </MContext.Provider>
143
+ );
108
144
  }
109
145
  }
110
146
 
@@ -133,63 +169,119 @@ export class MViewer extends React.Component<MViewerProp, State> {
133
169
  * @param props
134
170
  */
135
171
  export function SubmitBar(props: {
136
- style?: React.CSSProperties,
137
- onSubmit?: (finalData: any) => Promise<any>,
138
- onCancel?: () => void,
139
- children?: React.ReactNode | ((loading: boolean) => React.ReactNode)
172
+ style?: React.CSSProperties;
173
+ onSubmit?: (finalData: any) => Promise<any>;
174
+ onCancel?: () => void;
175
+ children?: React.ReactNode | ((loading: boolean) => React.ReactNode);
140
176
  }): JSX.Element {
141
-
142
177
  const [loading, setLoading] = useState(false);
143
- let style: React.CSSProperties = props.children ? undefined : { textAlign: "center", ...props.style }
144
-
145
- return <MContext.Consumer>{
146
- ctx => {
147
- const onClick = () => {
148
- if (loading) {
149
- message.warn("正在提交,请稍候");
150
- return;
151
- }
152
- const r = assembly.validate(ctx.rootProps.schema, ctx.rootProps.database);
153
- const submit = props.onSubmit ?? ctx.rootProps.onSubmit;
154
- ctx.setForceValid(true);
155
- if (r) {
156
- Modal.warning({
157
- content: '还没填完呢',
158
- onOk: () => {
159
- const viewer = document.getElementById(r.path);
160
- if (viewer) {
161
- viewer.scrollIntoView({ behavior: 'smooth', block: 'start' });
162
- } else {
163
- console.error("viewer not found", r);
164
- }
165
- }
166
- });
167
- } else {
168
- if (submit) {
169
- setLoading(true);
170
- const finalData = MUtil.filterHide(ctx.rootProps.schema, ctx.rootProps.database)
171
- submit(finalData).then(() => {
172
- PersistantTool.clear(ctx.rootProps.persistant)
173
- }).finally(() => {
174
- setLoading(false);
178
+ let style: React.CSSProperties = props.children
179
+ ? undefined
180
+ : { textAlign: "center", ...props.style };
181
+
182
+ return (
183
+ <MContext.Consumer>
184
+ {(ctx) => {
185
+ const onClick = () => {
186
+ if (loading) {
187
+ message.warn("正在提交,请稍候");
188
+ return;
189
+ }
190
+ const r = assembly.validate(
191
+ ctx.rootProps.schema,
192
+ ctx.rootProps.database
193
+ );
194
+ // console.log("当前数据格式", {
195
+ // schema: ctx.rootProps.schema,
196
+ // database: ctx.rootProps.database,
197
+ // });
198
+ const submit = props.onSubmit ?? ctx.rootProps.onSubmit;
199
+ ctx.setForceValid(true);
200
+ if (r) {
201
+ Modal.warning({
202
+ content: "还没填完呢",
203
+ onOk: () => {
204
+ const viewer = document.getElementById(r.path);
205
+ if (viewer) {
206
+ viewer.scrollIntoView({ behavior: "smooth", block: "start" });
207
+ } else {
208
+ console.error("viewer not found", r);
209
+ }
210
+ },
175
211
  });
176
212
  } else {
177
- message.success("填是填完了,但提交功能还在开发中,请联系程序员解决");
213
+ if (submit) {
214
+ // const sortQuestion = ctx.rootProps.schema.objectFields
215
+ // .filter((q) => q.editor === "AACheckDrag")
216
+ // ?.map((q) => q.name);
217
+ setLoading(true);
218
+ const finalData = MUtil.filterHide(
219
+ ctx.rootProps.schema,
220
+ ctx.rootProps.database
221
+ );
222
+ // const beforeHandleData = [...finalData];
223
+ // Object.keys(finalData).map((n) => {
224
+ // if (sortQuestion?.includes(n)) {
225
+ // finalData[n] = finalData[n].map((item) => {
226
+ // if (item.includes("open")) {
227
+ // return item.split("open-")?.[1];
228
+ // }
229
+ // return item;
230
+ // });
231
+ // }
232
+ // });
233
+ // console.log("最终提交的数据", {
234
+ // a: sortQuestion,
235
+ // b: ctx.rootProps.schema,
236
+ // c: ctx.rootProps.database,
237
+ // beforeHandleData: ctx.rootProps.database,
238
+ // finalData,
239
+ // });
240
+ submit(finalData)
241
+ .then(() => {
242
+ PersistantTool.clear(ctx.rootProps.persistant);
243
+ })
244
+ .finally(() => {
245
+ setLoading(false);
246
+ });
247
+ } else {
248
+ message.success(
249
+ "填是填完了,但提交功能还在开发中,请联系程序员解决"
250
+ );
251
+ }
178
252
  }
179
- }
180
- };
181
- return <div style={style} onClick={props.children ? onClick : undefined}>
182
- {
183
- props.onCancel ? <Button style={{ width: "40%", marginRight: '20px' }} onClick={props.onCancel}>返回</Button> : null
184
- }
185
- {
186
- props.children
187
- ? (_.isFunction(props.children) ? props.children(loading) : props.children)
188
- : <Button style={{ width: "40%" }} type="primary" loading={loading} onClick={props.children ? undefined : onClick}>提交</Button>
189
- }
190
- </div>
191
- }
192
- }</MContext.Consumer>
253
+ };
254
+ return (
255
+ <div style={style} onClick={props.children ? onClick : undefined}>
256
+ {props.onCancel ? (
257
+ <Button
258
+ style={{ width: "40%", marginRight: "20px" }}
259
+ onClick={props.onCancel}
260
+ >
261
+ 返回
262
+ </Button>
263
+ ) : null}
264
+ {props.children ? (
265
+ _.isFunction(props.children) ? (
266
+ props.children(loading)
267
+ ) : (
268
+ props.children
269
+ )
270
+ ) : (
271
+ <Button
272
+ style={{ width: "40%" }}
273
+ type="primary"
274
+ loading={loading}
275
+ onClick={props.children ? undefined : onClick}
276
+ >
277
+ 提交
278
+ </Button>
279
+ )}
280
+ </div>
281
+ );
282
+ }}
283
+ </MContext.Consumer>
284
+ );
193
285
  }
194
286
 
195
287
  export function useM3Database(initValue) {