form-driver 0.4.1 → 0.4.2

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.
@@ -0,0 +1,113 @@
1
+ import { MUtil } from "../framework/MUtil";
2
+ import { MFieldSchemaAnonymity, MValidationResult } from "../framework/Schema";
3
+ import { MType } from "./MType";
4
+
5
+ import { validateRequired } from "../framework/Validator";
6
+ import { assembly, Assembly } from "../framework/Assembly";
7
+ import _ from "lodash";
8
+
9
+ // 验证比重值的合法性
10
+ function validateWeightValue(
11
+ a: Assembly,
12
+ schema: any,
13
+ value: any,
14
+ path: string
15
+ ): MValidationResult {
16
+ if (_.isNil(value)) {
17
+ return undefined;
18
+ }
19
+
20
+ if (!_.isObject(value)) {
21
+ return { message: "比重值格式错误", path };
22
+ }
23
+
24
+ const fs = MUtil.option(schema) as any;
25
+ const totalWeight = schema.weight ?? 100; // 默认总比重为100
26
+ let currentTotal = 0;
27
+
28
+ // 计算当前已分配比重
29
+ if (Array.isArray(value)) {
30
+ currentTotal = value?.reduce?.((acc, val) => acc + val.value, 0);
31
+ }
32
+
33
+ // 比重题的校验文案都是固定显示在表单上,所以返回的 message 都是空的
34
+ // 验证总比重是否小于目标值
35
+ if (schema.weight && currentTotal < schema.weight && currentTotal !== 0) {
36
+ return {
37
+ message: `已分配比重不得小于总比重`,
38
+ path,
39
+ };
40
+ }
41
+
42
+ // 验证总比重是否超过最大值
43
+ if (schema.weight !== undefined && currentTotal > schema.weight) {
44
+ return {
45
+ message: ``,
46
+ path,
47
+ };
48
+ }
49
+ console.log("验证参数", value, currentTotal, schema.weight);
50
+
51
+ return undefined;
52
+ }
53
+
54
+ export const MWeightType: MType = {
55
+ validators: [validateRequired, validateWeightValue],
56
+
57
+ toReadable: (
58
+ assembly: Assembly,
59
+ s: MFieldSchemaAnonymity,
60
+ vs: any
61
+ ): string => {
62
+ const fs = MUtil.option(s) as any;
63
+ if (_.isNil(vs)) {
64
+ return assembly.theme.READABLE_BLANK;
65
+ } else if (!_.isObject(vs)) {
66
+ return assembly.theme.READABLE_ERROR;
67
+ }
68
+
69
+ const parts: string[] = [];
70
+ for (let f of fs) {
71
+ const weight = vs[f.value];
72
+ if (!_.isNil(weight) && weight !== 0) {
73
+ parts.push(`${f.label}: ${weight}`);
74
+ }
75
+ }
76
+
77
+ return parts.length > 0 ? parts.join(", ") : assembly.theme.READABLE_BLANK;
78
+ },
79
+
80
+ standardValue: (a: Assembly, s: any, vs: any, strict: boolean): any => {
81
+ if (!_.isObject(vs)) {
82
+ return s.defaultValue;
83
+ }
84
+ if (!strict) {
85
+ return vs;
86
+ }
87
+
88
+ const fs = MUtil.option(s) as any;
89
+ const result: any = {};
90
+
91
+ for (let f of fs) {
92
+ const weight = vs[f.value];
93
+ if (!_.isNil(weight) && _.isNumber(weight) && weight >= 0) {
94
+ // 如果要求整数,则转换为整数
95
+ if (s.requireInteger) {
96
+ result[f.value] = Math.round(weight);
97
+ } else {
98
+ result[f.value] = weight;
99
+ }
100
+ }
101
+ }
102
+
103
+ return result;
104
+ },
105
+
106
+ createDefaultValue: (assembly: Assembly, s: MFieldSchemaAnonymity): any => {
107
+ if (s.defaultValue) {
108
+ return _.clone(s.defaultValue);
109
+ } else {
110
+ return {}; // 默认为空对象
111
+ }
112
+ },
113
+ };
@@ -1,8 +1,8 @@
1
1
  import React, { ReactNode } from "react";
2
2
  import { MProp } from "../framework/Schema";
3
3
  import _ from "lodash";
4
- import { assembly } from '../framework/Assembly';
5
- import { MContext } from '../framework/MContext';
4
+ import { assembly } from "../framework/Assembly";
5
+ import { MContext } from "../framework/MContext";
6
6
  import { MUtil } from "../framework/MUtil";
7
7
 
8
8
  export interface ViewerState {
@@ -16,24 +16,27 @@ export interface ViewerState {
16
16
  * 每一个Viewer,都用来表达(展示readable/编辑editor)json上的一个值 / 或者不会改变数据的装饰物
17
17
  * 这个类负责展示校验错误,提供数据存取api
18
18
  */
19
- export abstract class Viewer<S extends ViewerState> extends React.Component<MProp, S> {
19
+ export abstract class Viewer<S extends ViewerState> extends React.Component<
20
+ MProp,
21
+ S
22
+ > {
20
23
  /**
21
24
  * 获取此字段在database中的值
22
25
  */
23
- getValue(){
26
+ getValue() {
24
27
  return MUtil.get(this.props.database, this.props.path);
25
28
  }
26
29
 
27
30
  /**
28
31
  * 修复全局 Scheme
29
32
  */
30
- changeSchema(v){
33
+ changeSchema(v) {
31
34
  if (this.props.changeSchema) {
32
- this.props.changeSchema(v)
35
+ this.props.changeSchema(v);
33
36
  }
34
37
  }
35
38
 
36
- getParentValue(){
39
+ getParentValue() {
37
40
  return MUtil.get(this.props.database, MUtil.parentPath(this.props.path));
38
41
  }
39
42
 
@@ -44,76 +47,95 @@ export abstract class Viewer<S extends ViewerState> extends React.Component<MPro
44
47
  * @param updateCtrlVersion - 子类可以用state.ctrlVersion作为key的一部分,在数据变化时用changeValue(value, true)改变输入框的内容
45
48
  * @param final - 参考AFTER_CHANGE_CALLBACK
46
49
  */
47
- changeValueEx(v:any, updateCtrlVersion:boolean, final:boolean) {
50
+ changeValueEx(v: any, updateCtrlVersion: boolean, final: boolean) {
48
51
  // 字段校验状态是否变化
49
52
  MUtil.set(this.props.database, this.props.path, v);
50
53
  this.props.afterChange?.(this.props.path, v, final);
51
- this.setState((p) =>{
52
- return {ctrlVersion: updateCtrlVersion? (p.ctrlVersion ?? 0 )+1 : (p.ctrlVersion ?? 0)}
54
+ this.setState((p) => {
55
+ return {
56
+ ctrlVersion: updateCtrlVersion
57
+ ? (p.ctrlVersion ?? 0) + 1
58
+ : p.ctrlVersion ?? 0,
59
+ };
53
60
  });
54
61
  }
55
62
 
56
63
  /**
57
64
  * changeValueEx(v, false, true)的快捷方式
58
- * @param v
65
+ * @param v
59
66
  */
60
- changeValue(v:any) {
67
+ changeValue(v: any) {
61
68
  this.changeValueEx(v, false, true);
62
69
  }
63
70
 
64
71
  /**
65
72
  * 子类实现编辑器元素的渲染,render根据这个元素再加上校验
66
73
  */
67
- protected abstract element(ctx?: any):ReactNode;
74
+ protected abstract element(ctx?: any): ReactNode;
68
75
 
69
76
  render() {
70
- return <MContext.Consumer>{
71
- ctx =>{
72
- // const isShow = MUtil.isShow(this.props.database, ctx.rootProps.schema?.objectFields, this.props.schema.showIf)
73
- // // 隐藏的字段不展示
74
- // if (!isShow) return null
75
- const childElement = this.element(ctx);
76
- if(this.props.morph === "readable" || this.state?.noValidate){
77
- return childElement;
78
- } else {
79
- // 加上校验错误提示
80
- if(!ctx?.forceValid) {
77
+ return (
78
+ <MContext.Consumer>
79
+ {(ctx) => {
80
+ // const isShow = MUtil.isShow(this.props.database, ctx.rootProps.schema?.objectFields, this.props.schema.showIf)
81
+ // // 隐藏的字段不展示
82
+ // if (!isShow) return null
83
+ const childElement = this.element(ctx);
84
+ if (this.props.morph === "readable" || this.state?.noValidate) {
81
85
  return childElement;
86
+ } else {
87
+ // 加上校验错误提示
88
+ if (!ctx?.forceValid) {
89
+ return childElement;
90
+ }
91
+ const v = assembly.validate(
92
+ this.props.schema,
93
+ this.getValue(),
94
+ this.props.path
95
+ );
96
+ return [
97
+ childElement,
98
+ v?.message ? (
99
+ <div
100
+ key="错误提示"
101
+ className="ant-form-item-explain ant-form-item-explain-error"
102
+ >
103
+ {v.message}
104
+ </div>
105
+ ) : undefined,
106
+ ];
82
107
  }
83
- const v = assembly.validate(this.props.schema, this.getValue(), this.props.path);
84
- return [
85
- childElement,
86
- v?.message ? <div key="错误提示" className="ant-form-item-explain ant-form-item-explain-error">{v.message}</div> : undefined
87
- ];
88
- }
89
- }
90
- }</MContext.Consumer>
108
+ }}
109
+ </MContext.Consumer>
110
+ );
91
111
  }
92
112
 
93
- getPlaceholder(index:number = 0): string|undefined {
113
+ getPlaceholder(index: number = 0): string | undefined {
94
114
  let p = this.props.schema.placeholder;
95
- if(_.isString(p)){
115
+ if (_.isString(p)) {
96
116
  return p;
97
- } else if(p) {
117
+ } else if (p) {
98
118
  return p[index];
99
119
  } else {
100
120
  return undefined;
101
121
  }
102
122
  }
103
123
 
104
- antProp(useVersionCtrl:boolean = false){
124
+ antProp(useVersionCtrl: boolean = false) {
105
125
  return {
106
- key: useVersionCtrl ? this.props.path + "/" + this.state.ctrlVersion : this.props.path,
126
+ key: useVersionCtrl
127
+ ? this.props.path + "/" + this.state.ctrlVersion
128
+ : this.props.path,
107
129
  placeholder: this.getPlaceholder(),
108
130
  bordered: !this.props.hideBorder,
109
- disabled: this.props.disable
110
- }
131
+ disabled: this.props.disable,
132
+ };
111
133
  }
112
134
  }
113
135
 
114
136
  export abstract class BaseViewer extends Viewer<ViewerState> {
115
- constructor(p:MProp){
137
+ constructor(p: MProp) {
116
138
  super(p);
117
- this.state = {ctrlVersion: 1, noValidate: false};
139
+ this.state = { ctrlVersion: 1, noValidate: false };
118
140
  }
119
- };
141
+ }
@@ -13,27 +13,30 @@ export class AIntBox extends BaseViewer {
13
13
  const props = {
14
14
  ...super.antProp(),
15
15
  bordered: undefined,
16
- style: {border: this.props.hideBorder ? "none" : undefined, ...this.props.style},
16
+ style: {
17
+ border: this.props.hideBorder ? "none" : undefined,
18
+ ...this.props.style,
19
+ },
17
20
  min: this.props.schema.min,
18
21
  max: this.props.schema.max,
19
22
  // type: "number",
20
23
  pattern: "\\d*",
21
24
  defaultValue: super.getValue(),
22
- onChange: (v)=>{
23
- let n: number | undefined = Number(v)
24
- if(_.isNaN(n)){
25
+ onChange: (v) => {
26
+ let n: number | undefined = Number(v);
27
+ if (_.isNaN(n)) {
25
28
  n = undefined;
26
29
  }
27
- if (p.onChange) p.onChange(n)
28
- console.log(n)
30
+ if (p.onChange) p.onChange(n);
31
+ console.log(n);
29
32
  this.changeValueEx(n, false, false);
30
33
  },
31
- onBlur: () =>{
32
- if (p.onBlur) p.onBlur()
34
+ onBlur: () => {
35
+ if (p.onBlur) p.onBlur();
33
36
  this.changeValueEx(super.getValue(), false, true);
34
37
  },
35
38
  ...p,
36
- }
37
- return <InputNumber {...props}></InputNumber>
39
+ };
40
+ return <InputNumber {...props}></InputNumber>;
38
41
  }
39
42
  }