react-antd-xform 1.0.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.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/_virtual/_commonjsHelpers.js +8 -0
- package/dist/_virtual/index.js +4 -0
- package/dist/_virtual/index2.js +4 -0
- package/dist/_virtual/jsx-runtime.js +4 -0
- package/dist/_virtual/react-jsx-runtime.development.js +4 -0
- package/dist/_virtual/react-jsx-runtime.production.min.js +4 -0
- package/dist/_virtual/use-sync-external-store-shim.development.js +4 -0
- package/dist/_virtual/use-sync-external-store-shim.production.js +4 -0
- package/dist/form/array-helper.d.ts +12 -0
- package/dist/form/array-helper.js +115 -0
- package/dist/form/common-utils.d.ts +12 -0
- package/dist/form/common-utils.js +90 -0
- package/dist/form/context/formEnvContext.d.ts +6 -0
- package/dist/form/context/formEnvContext.js +23 -0
- package/dist/form/context/modelContext.d.ts +4 -0
- package/dist/form/context/modelContext.js +11 -0
- package/dist/form/enum.d.ts +9 -0
- package/dist/form/enum.js +15 -0
- package/dist/form/form-item.d.ts +15 -0
- package/dist/form/form-item.js +286 -0
- package/dist/form/index.d.ts +22 -0
- package/dist/form/index.js +73 -0
- package/dist/form/layout.d.ts +4 -0
- package/dist/form/layout.js +180 -0
- package/dist/form/model-utils.d.ts +23 -0
- package/dist/form/model-utils.js +171 -0
- package/dist/form/model.d.ts +103 -0
- package/dist/form/model.js +504 -0
- package/dist/form/type.d.ts +289 -0
- package/dist/form-ui/common-utils.d.ts +9 -0
- package/dist/form-ui/common-utils.js +17 -0
- package/dist/form-ui/default-button.d.ts +3 -0
- package/dist/form-ui/default-button.js +39 -0
- package/dist/form-ui/default-component.d.ts +2 -0
- package/dist/form-ui/default-component.js +63 -0
- package/dist/form-ui/type.d.ts +2 -0
- package/dist/helpers/AsyncValue.d.ts +38 -0
- package/dist/helpers/AsyncValue.js +154 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +20 -0
- package/dist/node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.js +63 -0
- package/dist/node_modules/.pnpm/get-own-enumerable-keys@1.0.0/node_modules/get-own-enumerable-keys/index.js +10 -0
- package/dist/node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/browser.js +32 -0
- package/dist/node_modules/.pnpm/is-obj@3.0.0/node_modules/is-obj/index.js +7 -0
- package/dist/node_modules/.pnpm/is-regexp@3.1.0/node_modules/is-regexp/index.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Hash.js +23 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_ListCache.js +23 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Map.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_MapCache.js +23 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Symbol.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_apply.js +17 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_arrayMap.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_arrayPush.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assignValue.js +15 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assocIndexOf.js +15 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseAssignValue.js +18 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseFlatten.js +25 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGet.js +15 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.js +16 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseHasIn.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsArguments.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsNative.js +24 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_basePick.js +12 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_basePickBy.js +18 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseSet.js +33 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseSetToString.js +16 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseToString.js +23 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_castPath.js +15 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_coreJsData.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_defineProperty.js +14 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_flatRest.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_freeGlobal.js +6 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getMapData.js +10 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getNative.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getRawTag.js +27 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getValue.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hasPath.js +27 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashClear.js +10 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashDelete.js +9 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashGet.js +17 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashHas.js +12 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashSet.js +13 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isFlattenable.js +12 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isIndex.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isKey.js +18 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isKeyable.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isMasked.js +13 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheClear.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheDelete.js +22 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheGet.js +10 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheHas.js +9 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheSet.js +16 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheClear.js +16 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheDelete.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheGet.js +9 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheHas.js +9 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheSet.js +12 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_memoizeCapped.js +17 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_nativeCreate.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_objectToString.js +9 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_overRest.js +23 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_setToString.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_shortOut.js +21 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stringToPath.js +18 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_toKey.js +13 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_toSource.js +19 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/constant.js +9 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/eq.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/flatten.js +10 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/hasIn.js +10 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/identity.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArguments.js +15 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArray.js +5 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isFunction.js +15 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isLength.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isObject.js +8 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isObjectLike.js +7 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isSymbol.js +11 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/memoize.js +24 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/pick.js +12 -0
- package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/toString.js +9 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/ObserverComponent.js +5 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/index.js +18 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/observer.js +84 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/useObserver.js +72 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/UniversalFinalizationRegistry.js +96 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/assertEnvironment.js +8 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/observerBatching.js +17 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/observerFinalizationRegistry.js +9 -0
- package/dist/node_modules/.pnpm/mobx-react-lite@4.1.0_mobx@6.13.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/mobx-react-lite/es/utils/printDebugValue.js +7 -0
- package/dist/node_modules/.pnpm/react@18.3.1/node_modules/react/cjs/react-jsx-runtime.development.js +890 -0
- package/dist/node_modules/.pnpm/react@18.3.1/node_modules/react/cjs/react-jsx-runtime.production.min.js +33 -0
- package/dist/node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js +12 -0
- package/dist/node_modules/.pnpm/stringify-object@5.0.0/node_modules/stringify-object/index.js +94 -0
- package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@18.3.1/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js +77 -0
- package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@18.3.1/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js +62 -0
- package/dist/node_modules/.pnpm/use-sync-external-store@1.5.0_react@18.3.1/node_modules/use-sync-external-store/shim/index.js +12 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +58 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import invariant from "../node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/browser.js";
|
|
2
|
+
import { makeObservable, action, computed, observable, toJS } from "mobx";
|
|
3
|
+
import { ModelType, FieldType } from "./enum.js";
|
|
4
|
+
import { composeValue, observableGetIn, keyToValueShape, splitToPath, observableSetIn } from "./common-utils.js";
|
|
5
|
+
const EMPTY_PATH = [];
|
|
6
|
+
const ROOT_MODEL_CREATE_OPTIONS = {
|
|
7
|
+
modelType: ModelType.rootModel
|
|
8
|
+
};
|
|
9
|
+
class IdGenerator {
|
|
10
|
+
constructor(prefix) {
|
|
11
|
+
this._nextId = 1;
|
|
12
|
+
this.prefix = prefix;
|
|
13
|
+
}
|
|
14
|
+
getNextId() {
|
|
15
|
+
return `${this.prefix}_${this._nextId++}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class FormModel {
|
|
19
|
+
constructor(initValues, options = ROOT_MODEL_CREATE_OPTIONS) {
|
|
20
|
+
this.state = {};
|
|
21
|
+
this._fieldMap = /* @__PURE__ */ new Map();
|
|
22
|
+
this._valueShape = "auto";
|
|
23
|
+
this._checkMap = /* @__PURE__ */ new Map();
|
|
24
|
+
this._selfDeleted = false;
|
|
25
|
+
if ((options == null ? void 0 : options.modelType) === ModelType.subModel) {
|
|
26
|
+
this._modelType = ModelType.subModel;
|
|
27
|
+
this.parent = options.parent;
|
|
28
|
+
this.root = this.parent.root;
|
|
29
|
+
this.name = options.name;
|
|
30
|
+
this.id = this.root._modelIdGenerator.getNextId();
|
|
31
|
+
} else {
|
|
32
|
+
this._modelType = ModelType.rootModel;
|
|
33
|
+
this.parent = this;
|
|
34
|
+
this.root = this;
|
|
35
|
+
this.name = "";
|
|
36
|
+
this._modelIdGenerator = new IdGenerator("Model");
|
|
37
|
+
this._fieldIdGenerator = new IdGenerator("Field");
|
|
38
|
+
this.id = this._modelIdGenerator.getNextId();
|
|
39
|
+
this._values = composeValue(initValues, {});
|
|
40
|
+
if (this._values == null) {
|
|
41
|
+
console.warn("[xform] FormModel 根节点的初始 values 不能为 null");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
makeObservable(
|
|
45
|
+
this,
|
|
46
|
+
{
|
|
47
|
+
// root model 才会使用 this._values
|
|
48
|
+
_values: this._modelType === ModelType.rootModel ? observable : false,
|
|
49
|
+
values: computed,
|
|
50
|
+
state: observable,
|
|
51
|
+
setValue: action,
|
|
52
|
+
// 注意 name 是可以变化的;在数组元素调换位置的情况下 name 会进行变更
|
|
53
|
+
name: observable.ref,
|
|
54
|
+
path: computed,
|
|
55
|
+
_selfDeleted: observable.ref,
|
|
56
|
+
isDeleted: computed,
|
|
57
|
+
_markDeleted: action
|
|
58
|
+
},
|
|
59
|
+
{ name: `${this.id}(${this.name})` }
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
get values() {
|
|
63
|
+
if (this._modelType === ModelType.rootModel) {
|
|
64
|
+
return this._values;
|
|
65
|
+
} else {
|
|
66
|
+
return this.parent.getValue(this.name);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
set values(nextValues) {
|
|
70
|
+
if (this.isDeleted) {
|
|
71
|
+
console.warn(
|
|
72
|
+
"[xform] 对已删除的 Model 进行赋值将被忽略。请不要对已删除的 Model/Field 进行操作。"
|
|
73
|
+
);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (this._modelType === ModelType.rootModel) {
|
|
77
|
+
if (nextValues == null) {
|
|
78
|
+
console.warn("[xform] FormModel 根节点的 values 不能设置为 null/undefined");
|
|
79
|
+
}
|
|
80
|
+
this._values = nextValues;
|
|
81
|
+
} else {
|
|
82
|
+
this.parent.setValue(this.name, nextValues);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
get path() {
|
|
86
|
+
if (this._modelType == ModelType.rootModel) {
|
|
87
|
+
return EMPTY_PATH;
|
|
88
|
+
} else {
|
|
89
|
+
return [...this.parent.path, this.name];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
getValue(name, defaultValue) {
|
|
93
|
+
return observableGetIn(this.values, String(name), defaultValue);
|
|
94
|
+
}
|
|
95
|
+
setValue(name, value) {
|
|
96
|
+
if (this.isDeleted) {
|
|
97
|
+
console.warn(
|
|
98
|
+
"[xform] 对已删除的 Model 进行赋值将被忽略。请不要对已删除的 Model/Field 进行操作。"
|
|
99
|
+
);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (this._modelType === ModelType.subModel && this.values == null) {
|
|
103
|
+
this._updateValueShape(keyToValueShape(splitToPath(String(name))[0]));
|
|
104
|
+
this.values = this._valueShape === "array" ? [] : {};
|
|
105
|
+
}
|
|
106
|
+
observableSetIn(this.values, name, value);
|
|
107
|
+
}
|
|
108
|
+
getSubModel(name) {
|
|
109
|
+
const path = Array.isArray(name) ? name : splitToPath(name);
|
|
110
|
+
let mod = this;
|
|
111
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
112
|
+
mod = mod._getSubModelByShortName(path[i]);
|
|
113
|
+
}
|
|
114
|
+
return mod._getSubModelByShortName(path[path.length - 1]);
|
|
115
|
+
}
|
|
116
|
+
getField(name) {
|
|
117
|
+
const path = Array.isArray(name) ? name : splitToPath(name);
|
|
118
|
+
if (path.length > 1) {
|
|
119
|
+
const lastName = path[path.length - 1];
|
|
120
|
+
const subModel = this.getSubModel(path.slice(0, -1));
|
|
121
|
+
return subModel.getField([lastName]);
|
|
122
|
+
}
|
|
123
|
+
const shortName = path[0];
|
|
124
|
+
this._updateValueShape(keyToValueShape(shortName));
|
|
125
|
+
let field = this._fieldMap.get(shortName);
|
|
126
|
+
if (field == null) {
|
|
127
|
+
field = new Field({
|
|
128
|
+
fieldType: FieldType.normal,
|
|
129
|
+
parent: this,
|
|
130
|
+
name: shortName
|
|
131
|
+
});
|
|
132
|
+
this._fieldMap.set(shortName, field);
|
|
133
|
+
}
|
|
134
|
+
return field;
|
|
135
|
+
}
|
|
136
|
+
getTupleField(...tupleParts) {
|
|
137
|
+
this._updateValueShape("object");
|
|
138
|
+
const name = `tuple(${tupleParts.join(",")})`;
|
|
139
|
+
let field = this._fieldMap.get(name);
|
|
140
|
+
if (field == null) {
|
|
141
|
+
field = new Field({
|
|
142
|
+
fieldType: FieldType.tuple,
|
|
143
|
+
parent: this,
|
|
144
|
+
name,
|
|
145
|
+
tupleParts
|
|
146
|
+
});
|
|
147
|
+
this._fieldMap.set(name, field);
|
|
148
|
+
}
|
|
149
|
+
return field;
|
|
150
|
+
}
|
|
151
|
+
_asField() {
|
|
152
|
+
if (this._modelType === ModelType.rootModel) {
|
|
153
|
+
throw new Error("[xform] 根节点下不支持使用 name=&。根节点的数据结构只能为普通对象。");
|
|
154
|
+
}
|
|
155
|
+
return this.parent.getField(this.name);
|
|
156
|
+
}
|
|
157
|
+
_updateValueShape(valueShape) {
|
|
158
|
+
if (this._valueShape === "auto") {
|
|
159
|
+
this._valueShape = valueShape;
|
|
160
|
+
this._subModels = valueShape === "object" ? {} : [];
|
|
161
|
+
} else {
|
|
162
|
+
invariant(
|
|
163
|
+
this._valueShape === valueShape,
|
|
164
|
+
"[xform] FormModel 的结构需要在使用过程中保持一致,一个数据索引对应的结构不能从数组变为对象,也不能从对象变为数组"
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** 递归前序遍历该 model 下所有的 model 对象(包含 model 本身) */
|
|
169
|
+
iterateModels(iteratee) {
|
|
170
|
+
iteratee(this);
|
|
171
|
+
if (this._subModels != null) {
|
|
172
|
+
for (const subModel of Object.values(this._subModels)) {
|
|
173
|
+
subModel.iterateModels(iteratee);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** 递归遍历该 model 下(包括 model 本身)所有存在的 field 对象(包括 normal field 和 tuple field,也包括所有 fork) */
|
|
178
|
+
iterateFields(iteratee) {
|
|
179
|
+
this.iterateModels((model) => {
|
|
180
|
+
model._fieldMap.forEach((field) => {
|
|
181
|
+
field._forkMap.forEach(iteratee);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/** 递归遍历该 model 下(包括 model 本身)所有存在的 check 对象) */
|
|
186
|
+
iterateChecks(iteratee) {
|
|
187
|
+
this.iterateModels((model) => {
|
|
188
|
+
model._checkMap.forEach(iteratee);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
_getSubModelByShortName(name) {
|
|
192
|
+
this._updateValueShape(keyToValueShape(name));
|
|
193
|
+
let subModel = this._subModels[name];
|
|
194
|
+
if (subModel == null) {
|
|
195
|
+
subModel = new FormModel(null, {
|
|
196
|
+
modelType: ModelType.subModel,
|
|
197
|
+
parent: this,
|
|
198
|
+
name
|
|
199
|
+
});
|
|
200
|
+
this._subModels[name] = subModel;
|
|
201
|
+
}
|
|
202
|
+
return subModel;
|
|
203
|
+
}
|
|
204
|
+
getCheck(name) {
|
|
205
|
+
let check = this._checkMap.get(name);
|
|
206
|
+
if (check == null) {
|
|
207
|
+
check = new Check(this, name);
|
|
208
|
+
this._checkMap.set(name, check);
|
|
209
|
+
}
|
|
210
|
+
return check;
|
|
211
|
+
}
|
|
212
|
+
/** 判断当前 model 是否已经删除 */
|
|
213
|
+
get isDeleted() {
|
|
214
|
+
if (this._selfDeleted) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
return this._modelType === ModelType.subModel && this.parent.isDeleted;
|
|
218
|
+
}
|
|
219
|
+
_markDeleted() {
|
|
220
|
+
invariant(this._modelType === ModelType.subModel, "只有 subModels 才允许被删除.");
|
|
221
|
+
this.name = "(deleted)";
|
|
222
|
+
this._selfDeleted = true;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
class Check {
|
|
226
|
+
constructor(parent, name) {
|
|
227
|
+
this.isMounted = false;
|
|
228
|
+
this.config = null;
|
|
229
|
+
this.error = void 0;
|
|
230
|
+
this.cancelValidation = null;
|
|
231
|
+
this.validating = false;
|
|
232
|
+
this.parent = parent;
|
|
233
|
+
this.name = name;
|
|
234
|
+
makeObservable(this, {
|
|
235
|
+
path: computed,
|
|
236
|
+
validate: action,
|
|
237
|
+
error: observable,
|
|
238
|
+
cancelValidation: observable.ref,
|
|
239
|
+
validating: observable.ref
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
get path() {
|
|
243
|
+
return this.parent.path.concat([this.name]);
|
|
244
|
+
}
|
|
245
|
+
_track(config) {
|
|
246
|
+
if (this.isMounted) {
|
|
247
|
+
console.warn(`[xform] check \`${this.path.join(".")}\` 已在视图中被加载。`);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
this.config = config;
|
|
251
|
+
this.isMounted = true;
|
|
252
|
+
return () => {
|
|
253
|
+
this.config = null;
|
|
254
|
+
this.isMounted = false;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
async validate() {
|
|
258
|
+
var _a;
|
|
259
|
+
if (!this.isMounted) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const { validate } = this.config;
|
|
263
|
+
let cancelled = false;
|
|
264
|
+
(_a = this.cancelValidation) == null ? void 0 : _a.call(this);
|
|
265
|
+
this.validating = true;
|
|
266
|
+
this.cancelValidation = action(() => {
|
|
267
|
+
cancelled = true;
|
|
268
|
+
this.cancelValidation = null;
|
|
269
|
+
this.validating = false;
|
|
270
|
+
});
|
|
271
|
+
const handleValidateResult = action((error) => {
|
|
272
|
+
if (cancelled) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
this.cancelValidation = null;
|
|
276
|
+
this.validating = false;
|
|
277
|
+
this.error = error;
|
|
278
|
+
return error;
|
|
279
|
+
});
|
|
280
|
+
const model = this.parent;
|
|
281
|
+
const result = validate(toJS(model.values), model);
|
|
282
|
+
if (typeof (result == null ? void 0 : result.then) === "function") {
|
|
283
|
+
return Promise.resolve(result).then(handleValidateResult);
|
|
284
|
+
} else {
|
|
285
|
+
return handleValidateResult(result);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const _Field = class _Field {
|
|
290
|
+
constructor(opts) {
|
|
291
|
+
this.config = null;
|
|
292
|
+
this.isMounted = false;
|
|
293
|
+
this.state = {};
|
|
294
|
+
this.handleFocus = () => {
|
|
295
|
+
};
|
|
296
|
+
this.handleBlur = () => {
|
|
297
|
+
return this.validate("blur");
|
|
298
|
+
};
|
|
299
|
+
this.handleChange = (nextValue, ...rest) => {
|
|
300
|
+
var _a, _b, _c;
|
|
301
|
+
if (nextValue === void 0 && ((_a = this.config) == null ? void 0 : _a.defaultValue) !== void 0) {
|
|
302
|
+
console.warn(
|
|
303
|
+
"[xform] xform 中所有组件均为受控用法,不支持 onChange(undefined),该调用将自动变为 onChange(null)"
|
|
304
|
+
);
|
|
305
|
+
nextValue = null;
|
|
306
|
+
}
|
|
307
|
+
this.value = nextValue;
|
|
308
|
+
(_c = (_b = this.config) == null ? void 0 : _b.afterChange) == null ? void 0 : _c.call(_b, nextValue, ...rest);
|
|
309
|
+
return this.validate("change");
|
|
310
|
+
};
|
|
311
|
+
this.clear = () => {
|
|
312
|
+
var _a, _b;
|
|
313
|
+
if (this.isDeleted) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const parent = this.parent;
|
|
317
|
+
(_b = (_a = this.state).cancelValidation) == null ? void 0 : _b.call(_a);
|
|
318
|
+
this.state.error = null;
|
|
319
|
+
if (parent.values == null) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (this.fieldType === FieldType.normal) {
|
|
323
|
+
delete parent.values[this.name];
|
|
324
|
+
} else if (this.fieldType === FieldType.tuple) {
|
|
325
|
+
for (const part of this._tupleParts) {
|
|
326
|
+
delete parent.values[part];
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
this.fieldType = opts.fieldType;
|
|
331
|
+
this.parent = opts.parent;
|
|
332
|
+
this.name = opts.name;
|
|
333
|
+
this.id = this.parent.root._fieldIdGenerator.getNextId();
|
|
334
|
+
this._forkName = opts.forkName ?? _Field.ORIGINAL;
|
|
335
|
+
if (opts.fieldType === FieldType.tuple) {
|
|
336
|
+
this._tupleParts = opts.tupleParts;
|
|
337
|
+
} else if (opts.fieldType === FieldType.readonly) {
|
|
338
|
+
this._readonlyValue = opts.value;
|
|
339
|
+
}
|
|
340
|
+
const name = this.name;
|
|
341
|
+
const forkName = this._forkName;
|
|
342
|
+
makeObservable(
|
|
343
|
+
this,
|
|
344
|
+
{
|
|
345
|
+
state: observable,
|
|
346
|
+
value: computed,
|
|
347
|
+
path: computed,
|
|
348
|
+
validate: action,
|
|
349
|
+
handleBlur: action,
|
|
350
|
+
handleChange: action,
|
|
351
|
+
clear: action,
|
|
352
|
+
isDeleted: computed
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
name: `${this.id}(${name}${forkName === _Field.ORIGINAL ? "" : "#" + forkName})`
|
|
356
|
+
}
|
|
357
|
+
);
|
|
358
|
+
if (forkName === _Field.ORIGINAL) {
|
|
359
|
+
this._forkMap = /* @__PURE__ */ new Map();
|
|
360
|
+
} else {
|
|
361
|
+
const original = this.parent.getField(name);
|
|
362
|
+
this._forkMap = original._forkMap;
|
|
363
|
+
}
|
|
364
|
+
this._forkMap.set(forkName, this);
|
|
365
|
+
}
|
|
366
|
+
static getHtmlId(prefix, field) {
|
|
367
|
+
if (prefix == null || typeof prefix !== "string") {
|
|
368
|
+
return void 0;
|
|
369
|
+
}
|
|
370
|
+
const path = field.path.join(".");
|
|
371
|
+
const fork = field._forkName !== _Field.ORIGINAL ? `#${field._forkName}` : "";
|
|
372
|
+
return `${prefix}${path}${fork}`;
|
|
373
|
+
}
|
|
374
|
+
get value() {
|
|
375
|
+
if (this.fieldType === FieldType.normal) {
|
|
376
|
+
return this.parent.getValue(this.name);
|
|
377
|
+
} else if (this.fieldType === FieldType.tuple) {
|
|
378
|
+
return this._tupleParts.map((part) => this.parent.getValue(part));
|
|
379
|
+
} else if (this.fieldType === FieldType.readonly) {
|
|
380
|
+
return this._readonlyValue;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
set value(value) {
|
|
384
|
+
if (this.isDeleted) {
|
|
385
|
+
console.warn(
|
|
386
|
+
"[xform] 对已删除的 Field 进行赋值将被忽略。请不要对已删除的 Model/Field 进行操作。"
|
|
387
|
+
);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (this.fieldType === FieldType.normal) {
|
|
391
|
+
this.parent.setValue(this.name, value);
|
|
392
|
+
} else if (this.fieldType === FieldType.tuple) {
|
|
393
|
+
this._tupleParts.forEach((part, index) => {
|
|
394
|
+
this.parent.setValue(part, value == null ? value : value[index]);
|
|
395
|
+
});
|
|
396
|
+
} else if (this.fieldType === FieldType.readonly) {
|
|
397
|
+
console.warn(
|
|
398
|
+
"[xform] 对只读 Field 进行赋值将被忽略,请检查是否为 FormItem 设置了 props.name 或 props.field."
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
get path() {
|
|
403
|
+
return this.parent.path.concat([this.name]);
|
|
404
|
+
}
|
|
405
|
+
_track(config) {
|
|
406
|
+
if (this.isMounted) {
|
|
407
|
+
console.warn(
|
|
408
|
+
`[xform] field \`${this.path.join(
|
|
409
|
+
"."
|
|
410
|
+
)}\` 已在视图中被加载,你需要 fork 该字段来进行多次加载.`
|
|
411
|
+
);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
this.config = config;
|
|
415
|
+
this.isMounted = true;
|
|
416
|
+
return () => {
|
|
417
|
+
this.config = null;
|
|
418
|
+
this.isMounted = false;
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
getFork(forkName) {
|
|
422
|
+
if (this._forkMap.has(forkName)) {
|
|
423
|
+
return this._forkMap.get(forkName);
|
|
424
|
+
} else {
|
|
425
|
+
const common = { parent: this.parent, name: this.name, forkName };
|
|
426
|
+
if (this.fieldType === FieldType.normal) {
|
|
427
|
+
return new _Field({ fieldType: FieldType.normal, ...common });
|
|
428
|
+
} else if (this.fieldType === FieldType.tuple) {
|
|
429
|
+
return new _Field({
|
|
430
|
+
fieldType: FieldType.tuple,
|
|
431
|
+
tupleParts: this._tupleParts,
|
|
432
|
+
...common
|
|
433
|
+
});
|
|
434
|
+
} else if (this.fieldType === FieldType.readonly) {
|
|
435
|
+
return new _Field({
|
|
436
|
+
fieldType: FieldType.readonly,
|
|
437
|
+
value: this._readonlyValue,
|
|
438
|
+
...common
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
async validate(trigger = "*") {
|
|
444
|
+
var _a, _b;
|
|
445
|
+
if (!this.isMounted) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const {
|
|
449
|
+
validate,
|
|
450
|
+
defaultValue,
|
|
451
|
+
isEmpty,
|
|
452
|
+
required,
|
|
453
|
+
requiredMessage = "该字段为必填项",
|
|
454
|
+
validateOnMount,
|
|
455
|
+
validateOnBlur,
|
|
456
|
+
validateOnChange
|
|
457
|
+
} = this.config;
|
|
458
|
+
const needValidate = trigger === "*" || validateOnBlur && trigger === "blur" || validateOnMount && trigger === "mount" || validateOnChange && trigger === "change";
|
|
459
|
+
if (!needValidate) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
let cancelled = false;
|
|
463
|
+
(_b = (_a = this.state).cancelValidation) == null ? void 0 : _b.call(_a);
|
|
464
|
+
this.state.validating = true;
|
|
465
|
+
this.state.cancelValidation = action(() => {
|
|
466
|
+
cancelled = true;
|
|
467
|
+
this.state.cancelValidation = null;
|
|
468
|
+
this.state.validating = false;
|
|
469
|
+
});
|
|
470
|
+
const handleValidateResult = action((error) => {
|
|
471
|
+
if (cancelled) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
this.state.cancelValidation = null;
|
|
475
|
+
this.state.validating = false;
|
|
476
|
+
this.state.error = error;
|
|
477
|
+
return error;
|
|
478
|
+
});
|
|
479
|
+
const value = toJS(composeValue(this.value, defaultValue));
|
|
480
|
+
if (required && isEmpty(value)) {
|
|
481
|
+
return handleValidateResult(requiredMessage);
|
|
482
|
+
}
|
|
483
|
+
if (validate) {
|
|
484
|
+
const result = validate(value, this, trigger);
|
|
485
|
+
if (typeof (result == null ? void 0 : result.then) === "function") {
|
|
486
|
+
return Promise.resolve(result).then(handleValidateResult);
|
|
487
|
+
} else {
|
|
488
|
+
return handleValidateResult(result);
|
|
489
|
+
}
|
|
490
|
+
} else {
|
|
491
|
+
return handleValidateResult(null);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
get isDeleted() {
|
|
495
|
+
return this.parent.isDeleted;
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
_Field.ORIGINAL = "original";
|
|
499
|
+
let Field = _Field;
|
|
500
|
+
export {
|
|
501
|
+
Check,
|
|
502
|
+
Field,
|
|
503
|
+
FormModel
|
|
504
|
+
};
|