mobx-formkit 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/AGENTS.md +761 -0
- package/LICENSE +21 -0
- package/README.md +214 -0
- package/lib/ArrayMap.d.ts +50 -0
- package/lib/ArrayMap.d.ts.map +1 -0
- package/lib/ArrayMap.js +151 -0
- package/lib/ArrayMap.js.map +1 -0
- package/lib/Base.d.ts +237 -0
- package/lib/Base.d.ts.map +1 -0
- package/lib/Base.js +872 -0
- package/lib/Base.js.map +1 -0
- package/lib/Bindings.d.ts +28 -0
- package/lib/Bindings.d.ts.map +1 -0
- package/lib/Bindings.js +73 -0
- package/lib/Bindings.js.map +1 -0
- package/lib/Field.d.ts +195 -0
- package/lib/Field.d.ts.map +1 -0
- package/lib/Field.js +734 -0
- package/lib/Field.js.map +1 -0
- package/lib/Form.d.ts +75 -0
- package/lib/Form.d.ts.map +1 -0
- package/lib/Form.js +199 -0
- package/lib/Form.js.map +1 -0
- package/lib/Options.d.ts +9 -0
- package/lib/Options.d.ts.map +1 -0
- package/lib/Options.js +85 -0
- package/lib/Options.js.map +1 -0
- package/lib/State.d.ts +43 -0
- package/lib/State.d.ts.map +1 -0
- package/lib/State.js +147 -0
- package/lib/State.js.map +1 -0
- package/lib/Validator.d.ts +25 -0
- package/lib/Validator.d.ts.map +1 -0
- package/lib/Validator.js +143 -0
- package/lib/Validator.js.map +1 -0
- package/lib/compat.d.ts +29 -0
- package/lib/compat.d.ts.map +1 -0
- package/lib/compat.js +117 -0
- package/lib/compat.js.map +1 -0
- package/lib/composer.d.ts +26 -0
- package/lib/composer.d.ts.map +1 -0
- package/lib/composer.js +65 -0
- package/lib/composer.js.map +1 -0
- package/lib/esm/ArrayMap.js +146 -0
- package/lib/esm/ArrayMap.js.map +1 -0
- package/lib/esm/Base.js +868 -0
- package/lib/esm/Base.js.map +1 -0
- package/lib/esm/Bindings.js +69 -0
- package/lib/esm/Bindings.js.map +1 -0
- package/lib/esm/Field.js +730 -0
- package/lib/esm/Field.js.map +1 -0
- package/lib/esm/Form.js +195 -0
- package/lib/esm/Form.js.map +1 -0
- package/lib/esm/Options.js +81 -0
- package/lib/esm/Options.js.map +1 -0
- package/lib/esm/State.js +143 -0
- package/lib/esm/State.js.map +1 -0
- package/lib/esm/Validator.js +139 -0
- package/lib/esm/Validator.js.map +1 -0
- package/lib/esm/compat.js +96 -0
- package/lib/esm/compat.js.map +1 -0
- package/lib/esm/composer.js +63 -0
- package/lib/esm/composer.js.map +1 -0
- package/lib/esm/index.js +8 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/models/FieldProps.js +116 -0
- package/lib/esm/models/FieldProps.js.map +1 -0
- package/lib/esm/models/OptionsModel.js +51 -0
- package/lib/esm/models/OptionsModel.js.map +1 -0
- package/lib/esm/models/StateInterface.js +9 -0
- package/lib/esm/models/StateInterface.js.map +1 -0
- package/lib/esm/models/ValidatorInterface.js +8 -0
- package/lib/esm/models/ValidatorInterface.js.map +1 -0
- package/lib/esm/parser.js +235 -0
- package/lib/esm/parser.js.map +1 -0
- package/lib/esm/props.js +118 -0
- package/lib/esm/props.js.map +1 -0
- package/lib/esm/utils.js +156 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/esm/validators/AJV.js +105 -0
- package/lib/esm/validators/AJV.js.map +1 -0
- package/lib/esm/validators/DVR.js +104 -0
- package/lib/esm/validators/DVR.js.map +1 -0
- package/lib/esm/validators/JOI.js +54 -0
- package/lib/esm/validators/JOI.js.map +1 -0
- package/lib/esm/validators/VALIBOT.js +50 -0
- package/lib/esm/validators/VALIBOT.js.map +1 -0
- package/lib/esm/validators/VINEJS.js +60 -0
- package/lib/esm/validators/VINEJS.js.map +1 -0
- package/lib/esm/validators/VJF.js +88 -0
- package/lib/esm/validators/VJF.js.map +1 -0
- package/lib/esm/validators/YUP.js +69 -0
- package/lib/esm/validators/YUP.js.map +1 -0
- package/lib/esm/validators/ZOD.js +43 -0
- package/lib/esm/validators/ZOD.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +15 -0
- package/lib/index.js.map +1 -0
- package/lib/models/BaseInterface.d.ts +40 -0
- package/lib/models/BaseInterface.d.ts.map +1 -0
- package/lib/models/BindingsInterface.d.ts +18 -0
- package/lib/models/BindingsInterface.d.ts.map +1 -0
- package/lib/models/FieldInterface.d.ts +88 -0
- package/lib/models/FieldInterface.d.ts.map +1 -0
- package/lib/models/FieldProps.d.ts +111 -0
- package/lib/models/FieldProps.d.ts.map +1 -0
- package/lib/models/FieldProps.js +116 -0
- package/lib/models/FieldProps.js.map +1 -0
- package/lib/models/FormInterface.d.ts +184 -0
- package/lib/models/FormInterface.d.ts.map +1 -0
- package/lib/models/OptionsInterface.d.ts +8 -0
- package/lib/models/OptionsInterface.d.ts.map +1 -0
- package/lib/models/OptionsModel.d.ts +99 -0
- package/lib/models/OptionsModel.d.ts.map +1 -0
- package/lib/models/OptionsModel.js +51 -0
- package/lib/models/OptionsModel.js.map +1 -0
- package/lib/models/SharedActionsInterface.d.ts +27 -0
- package/lib/models/SharedActionsInterface.d.ts.map +1 -0
- package/lib/models/SharedEventsInterface.d.ts +8 -0
- package/lib/models/SharedEventsInterface.d.ts.map +1 -0
- package/lib/models/SharedHelpersInterface.d.ts +26 -0
- package/lib/models/SharedHelpersInterface.d.ts.map +1 -0
- package/lib/models/SharedInitializerInterface.d.ts +6 -0
- package/lib/models/SharedInitializerInterface.d.ts.map +1 -0
- package/lib/models/SharedUtilsInterface.d.ts +10 -0
- package/lib/models/SharedUtilsInterface.d.ts.map +1 -0
- package/lib/models/StateInterface.d.ts +37 -0
- package/lib/models/StateInterface.d.ts.map +1 -0
- package/lib/models/StateInterface.js +9 -0
- package/lib/models/StateInterface.js.map +1 -0
- package/lib/models/ValidatorInterface.d.ts +74 -0
- package/lib/models/ValidatorInterface.d.ts.map +1 -0
- package/lib/models/ValidatorInterface.js +8 -0
- package/lib/models/ValidatorInterface.js.map +1 -0
- package/lib/parser.d.ts +13 -0
- package/lib/parser.d.ts.map +1 -0
- package/lib/parser.js +247 -0
- package/lib/parser.js.map +1 -0
- package/lib/props.d.ts +14 -0
- package/lib/props.d.ts.map +1 -0
- package/lib/props.js +120 -0
- package/lib/props.js.map +1 -0
- package/lib/utils.d.ts +34 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +182 -0
- package/lib/utils.js.map +1 -0
- package/lib/validators/AJV.d.ts +4 -0
- package/lib/validators/AJV.d.ts.map +1 -0
- package/lib/validators/AJV.js +109 -0
- package/lib/validators/AJV.js.map +1 -0
- package/lib/validators/DVR.d.ts +4 -0
- package/lib/validators/DVR.d.ts.map +1 -0
- package/lib/validators/DVR.js +108 -0
- package/lib/validators/DVR.js.map +1 -0
- package/lib/validators/JOI.d.ts +4 -0
- package/lib/validators/JOI.d.ts.map +1 -0
- package/lib/validators/JOI.js +58 -0
- package/lib/validators/JOI.js.map +1 -0
- package/lib/validators/VALIBOT.d.ts +4 -0
- package/lib/validators/VALIBOT.d.ts.map +1 -0
- package/lib/validators/VALIBOT.js +73 -0
- package/lib/validators/VALIBOT.js.map +1 -0
- package/lib/validators/VINEJS.d.ts +4 -0
- package/lib/validators/VINEJS.d.ts.map +1 -0
- package/lib/validators/VINEJS.js +64 -0
- package/lib/validators/VINEJS.js.map +1 -0
- package/lib/validators/VJF.d.ts +4 -0
- package/lib/validators/VJF.d.ts.map +1 -0
- package/lib/validators/VJF.js +92 -0
- package/lib/validators/VJF.js.map +1 -0
- package/lib/validators/YUP.d.ts +4 -0
- package/lib/validators/YUP.d.ts.map +1 -0
- package/lib/validators/YUP.js +73 -0
- package/lib/validators/YUP.js.map +1 -0
- package/lib/validators/ZOD.d.ts +4 -0
- package/lib/validators/ZOD.d.ts.map +1 -0
- package/lib/validators/ZOD.js +47 -0
- package/lib/validators/ZOD.js.map +1 -0
- package/package.json +183 -0
- package/src/ArrayMap.ts +165 -0
- package/src/Base.ts +1229 -0
- package/src/Bindings.ts +85 -0
- package/src/Field.ts +1034 -0
- package/src/Form.ts +264 -0
- package/src/Options.ts +91 -0
- package/src/State.ts +183 -0
- package/src/Validator.ts +177 -0
- package/src/compat.ts +104 -0
- package/src/composer.ts +82 -0
- package/src/index.ts +7 -0
- package/src/models/BaseInterface.ts +45 -0
- package/src/models/BindingsInterface.ts +25 -0
- package/src/models/FieldInterface.ts +97 -0
- package/src/models/FieldProps.ts +116 -0
- package/src/models/FormInterface.ts +203 -0
- package/src/models/OptionsInterface.ts +9 -0
- package/src/models/OptionsModel.ts +100 -0
- package/src/models/SharedActionsInterface.ts +30 -0
- package/src/models/SharedEventsInterface.ts +8 -0
- package/src/models/SharedHelpersInterface.ts +12 -0
- package/src/models/SharedInitializerInterface.ts +6 -0
- package/src/models/SharedUtilsInterface.ts +10 -0
- package/src/models/StateInterface.ts +44 -0
- package/src/models/ValidatorInterface.ts +88 -0
- package/src/parser.ts +327 -0
- package/src/props.ts +127 -0
- package/src/utils.ts +218 -0
- package/src/validators/AJV.ts +140 -0
- package/src/validators/DVR.ts +145 -0
- package/src/validators/JOI.ts +79 -0
- package/src/validators/VALIBOT.ts +70 -0
- package/src/validators/VINEJS.ts +83 -0
- package/src/validators/VJF.ts +123 -0
- package/src/validators/YUP.ts +90 -0
- package/src/validators/ZOD.ts +66 -0
- package/umd/MobxFormkit.umd.js +3093 -0
- package/umd/MobxFormkit.umd.js.map +1 -0
- package/umd/MobxFormkit.umd.min.js +2 -0
- package/umd/MobxFormkit.umd.min.js.map +1 -0
- package/umd/MobxFormkitComposer.umd.js +183 -0
- package/umd/MobxFormkitComposer.umd.js.map +1 -0
- package/umd/MobxFormkitComposer.umd.min.js +2 -0
- package/umd/MobxFormkitComposer.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorAJV.umd.js +113 -0
- package/umd/MobxFormkitValidatorAJV.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorAJV.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorAJV.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorDVR.umd.js +110 -0
- package/umd/MobxFormkitValidatorDVR.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorDVR.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorDVR.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorJOI.umd.js +62 -0
- package/umd/MobxFormkitValidatorJOI.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorJOI.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorJOI.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.js +8032 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.js +11737 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorVJF.umd.js +96 -0
- package/umd/MobxFormkitValidatorVJF.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorVJF.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorVJF.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorYUP.umd.js +77 -0
- package/umd/MobxFormkitValidatorYUP.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorYUP.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorYUP.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorZOD.umd.js +49 -0
- package/umd/MobxFormkitValidatorZOD.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorZOD.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorZOD.umd.min.js.map +1 -0
package/lib/Base.js
ADDED
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var mobx = require('mobx');
|
|
6
|
+
var compat = require('./compat.js');
|
|
7
|
+
var ArrayMap = require('./ArrayMap.js');
|
|
8
|
+
var lodash = require('lodash');
|
|
9
|
+
var utils = require('./utils.js');
|
|
10
|
+
var parser = require('./parser.js');
|
|
11
|
+
var FieldProps = require('./models/FieldProps.js');
|
|
12
|
+
var OptionsModel = require('./models/OptionsModel.js');
|
|
13
|
+
var ValidatorInterface = require('./models/ValidatorInterface.js');
|
|
14
|
+
var props = require('./props.js');
|
|
15
|
+
|
|
16
|
+
class Base {
|
|
17
|
+
noop = () => { };
|
|
18
|
+
state;
|
|
19
|
+
fields = new ArrayMap.ArrayMap();
|
|
20
|
+
path;
|
|
21
|
+
$submitted = 0;
|
|
22
|
+
$submitting = false;
|
|
23
|
+
$validated = 0;
|
|
24
|
+
$validating = false;
|
|
25
|
+
$clearing = false;
|
|
26
|
+
$resetting = false;
|
|
27
|
+
$touched = false;
|
|
28
|
+
$changed = 0;
|
|
29
|
+
$hooks = {};
|
|
30
|
+
$handlers = {};
|
|
31
|
+
constructor() {
|
|
32
|
+
compat.makeObservable(this, {
|
|
33
|
+
$submitted: mobx.observable,
|
|
34
|
+
$submitting: mobx.observable,
|
|
35
|
+
$validated: mobx.observable,
|
|
36
|
+
$validating: mobx.observable,
|
|
37
|
+
$clearing: mobx.observable,
|
|
38
|
+
$resetting: mobx.observable,
|
|
39
|
+
$touched: mobx.observable,
|
|
40
|
+
$changed: mobx.observable,
|
|
41
|
+
$hooks: mobx.observable,
|
|
42
|
+
$handlers: mobx.observable,
|
|
43
|
+
changed: mobx.computed,
|
|
44
|
+
submitted: mobx.computed,
|
|
45
|
+
submitting: mobx.computed,
|
|
46
|
+
validated: mobx.computed,
|
|
47
|
+
validating: mobx.computed,
|
|
48
|
+
clearing: mobx.computed,
|
|
49
|
+
resetting: mobx.computed,
|
|
50
|
+
hasIncrementalKeys: mobx.computed,
|
|
51
|
+
hasNestedFields: mobx.computed,
|
|
52
|
+
size: mobx.computed,
|
|
53
|
+
// initialization
|
|
54
|
+
initField: mobx.action,
|
|
55
|
+
// actions
|
|
56
|
+
submit: mobx.action,
|
|
57
|
+
deepUpdate: mobx.action,
|
|
58
|
+
set: mobx.action,
|
|
59
|
+
add: mobx.action,
|
|
60
|
+
del: mobx.action,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
execHook = (name, fallback = {}, ...args) => utils.$try(fallback[name], this.$hooks[name], this.noop).apply(this, [this, ...args]);
|
|
64
|
+
execHandler = (name, args, fallback = undefined, hook = null, execHook = true) => [
|
|
65
|
+
utils.$try(this.$handlers[name] && this.$handlers[name].apply(this, [this]), fallback, this.noop).apply(this, [...args]),
|
|
66
|
+
execHook && this.execHook(hook || name, {}, ...args),
|
|
67
|
+
];
|
|
68
|
+
get resetting() {
|
|
69
|
+
return this.hasNestedFields
|
|
70
|
+
? this.check(FieldProps.FieldPropsEnum.resetting, true)
|
|
71
|
+
: this.$resetting;
|
|
72
|
+
}
|
|
73
|
+
get clearing() {
|
|
74
|
+
return this.hasNestedFields
|
|
75
|
+
? this.check(FieldProps.FieldPropsEnum.clearing, true)
|
|
76
|
+
: this.$clearing;
|
|
77
|
+
}
|
|
78
|
+
get submitted() {
|
|
79
|
+
return mobx.toJS(this.$submitted);
|
|
80
|
+
}
|
|
81
|
+
get submitting() {
|
|
82
|
+
return mobx.toJS(this.$submitting);
|
|
83
|
+
}
|
|
84
|
+
get validated() {
|
|
85
|
+
return mobx.toJS(this.$validated);
|
|
86
|
+
}
|
|
87
|
+
get validating() {
|
|
88
|
+
return mobx.toJS(this.$validating);
|
|
89
|
+
}
|
|
90
|
+
get hasIncrementalKeys() {
|
|
91
|
+
return !!this.fields.size && utils.hasIntKeys(this.fields);
|
|
92
|
+
}
|
|
93
|
+
get hasNestedFields() {
|
|
94
|
+
return this.fields.size !== 0;
|
|
95
|
+
}
|
|
96
|
+
get size() {
|
|
97
|
+
return this.fields.size;
|
|
98
|
+
}
|
|
99
|
+
get changed() {
|
|
100
|
+
return this.path != null && this.hasNestedFields
|
|
101
|
+
? this.reduce((acc, field) => acc + field.changed, 0) + this.$changed
|
|
102
|
+
: this.$changed;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
Interceptor
|
|
106
|
+
*/
|
|
107
|
+
intercept = (opt) => this.MOBXEvent(typeof opt === "function"
|
|
108
|
+
? { type: "interceptor", call: opt }
|
|
109
|
+
: { type: "interceptor", ...opt });
|
|
110
|
+
/**
|
|
111
|
+
Observer
|
|
112
|
+
*/
|
|
113
|
+
observe = (opt) => this.MOBXEvent(typeof opt === "function"
|
|
114
|
+
? { type: "observer", call: opt }
|
|
115
|
+
: { type: "observer", ...opt });
|
|
116
|
+
/**
|
|
117
|
+
Event Handler: On Clear
|
|
118
|
+
*/
|
|
119
|
+
onClear = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onClear, args, (e) => {
|
|
120
|
+
utils.isEvent(e) && e.preventDefault();
|
|
121
|
+
this.clear(true, false);
|
|
122
|
+
});
|
|
123
|
+
/**
|
|
124
|
+
Event Handler: On Reset
|
|
125
|
+
*/
|
|
126
|
+
onReset = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onReset, args, (e) => {
|
|
127
|
+
utils.isEvent(e) && e.preventDefault();
|
|
128
|
+
this.reset(true, false);
|
|
129
|
+
});
|
|
130
|
+
/**
|
|
131
|
+
Event Handler: On Submit
|
|
132
|
+
*/
|
|
133
|
+
onSubmit = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onSubmit, args, (e, o = {}) => {
|
|
134
|
+
utils.isEvent(e) && e.preventDefault();
|
|
135
|
+
this.submit(o);
|
|
136
|
+
}, null, false);
|
|
137
|
+
/**
|
|
138
|
+
Event Handler: On Add
|
|
139
|
+
*/
|
|
140
|
+
onAdd = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onAdd, args, (e, val) => {
|
|
141
|
+
utils.isEvent(e) && e.preventDefault();
|
|
142
|
+
this.add(utils.isEvent(val) ? null : val, false);
|
|
143
|
+
});
|
|
144
|
+
/**
|
|
145
|
+
Event Handler: On Del
|
|
146
|
+
*/
|
|
147
|
+
onDel = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onDel, args, (e, path) => {
|
|
148
|
+
utils.isEvent(e) && e.preventDefault();
|
|
149
|
+
this.del(utils.isEvent(path) ? this.path : path, false);
|
|
150
|
+
});
|
|
151
|
+
/******************************************************************
|
|
152
|
+
Initializer
|
|
153
|
+
*/
|
|
154
|
+
initFields(initial, update = false) {
|
|
155
|
+
const fallback = this.state.options.get(OptionsModel.OptionsEnum.fallback);
|
|
156
|
+
const $path = (key) => [this.path, key].join(".").replace(/^\.+/, "");
|
|
157
|
+
let fields;
|
|
158
|
+
fields = parser.prepareFieldsData(initial, this.state.strict, fallback);
|
|
159
|
+
fields = parser.mergeSchemaDefaults(fields, this.validator);
|
|
160
|
+
// create fields
|
|
161
|
+
lodash.forIn(fields, (field, key) => {
|
|
162
|
+
const path = $path(key);
|
|
163
|
+
const $f = this.select(path, null, false);
|
|
164
|
+
if ($f == null) {
|
|
165
|
+
if (fallback) {
|
|
166
|
+
this.initField(key, path, field, update);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const structPath = utils.pathToStruct(path);
|
|
170
|
+
const struct = this.state.struct();
|
|
171
|
+
const found = struct
|
|
172
|
+
.filter((s) => s.startsWith(structPath))
|
|
173
|
+
.find((s) => s.charAt(structPath.length) === "." ||
|
|
174
|
+
s.substring(structPath.length, structPath.length + 2) ===
|
|
175
|
+
"[]" ||
|
|
176
|
+
s === structPath);
|
|
177
|
+
if (found)
|
|
178
|
+
this.initField(key, path, field, update);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
initField(key, path, data, update = false) {
|
|
184
|
+
const initial = this.state.get("current", "props");
|
|
185
|
+
const struct = utils.pathToStruct(path);
|
|
186
|
+
// try to get props from separated objects
|
|
187
|
+
const _try = (prop) => {
|
|
188
|
+
const t = lodash.get(initial[prop], struct);
|
|
189
|
+
if ([
|
|
190
|
+
FieldProps.FieldPropsEnum.input,
|
|
191
|
+
FieldProps.FieldPropsEnum.output,
|
|
192
|
+
FieldProps.FieldPropsEnum.converter,
|
|
193
|
+
].includes(prop) &&
|
|
194
|
+
typeof t !== "function")
|
|
195
|
+
return undefined;
|
|
196
|
+
return t;
|
|
197
|
+
};
|
|
198
|
+
const props = {
|
|
199
|
+
$value: lodash.get(initial[FieldProps.SeparatedPropsMode.values], path),
|
|
200
|
+
$computed: _try(FieldProps.SeparatedPropsMode.computed),
|
|
201
|
+
$label: _try(FieldProps.SeparatedPropsMode.labels),
|
|
202
|
+
$placeholder: _try(FieldProps.SeparatedPropsMode.placeholders),
|
|
203
|
+
$default: _try(FieldProps.SeparatedPropsMode.defaults),
|
|
204
|
+
$initial: _try(FieldProps.SeparatedPropsMode.initials),
|
|
205
|
+
$disabled: _try(FieldProps.SeparatedPropsMode.disabled),
|
|
206
|
+
$deleted: _try(FieldProps.SeparatedPropsMode.deleted),
|
|
207
|
+
$type: _try(FieldProps.SeparatedPropsMode.types),
|
|
208
|
+
$related: _try(FieldProps.SeparatedPropsMode.related),
|
|
209
|
+
$rules: _try(FieldProps.SeparatedPropsMode.rules),
|
|
210
|
+
$options: _try(FieldProps.SeparatedPropsMode.options),
|
|
211
|
+
$bindings: _try(FieldProps.SeparatedPropsMode.bindings),
|
|
212
|
+
$extra: _try(FieldProps.SeparatedPropsMode.extra),
|
|
213
|
+
$hooks: _try(FieldProps.SeparatedPropsMode.hooks),
|
|
214
|
+
$handlers: _try(FieldProps.SeparatedPropsMode.handlers),
|
|
215
|
+
$validatedWith: _try(FieldProps.SeparatedPropsMode.validatedWith),
|
|
216
|
+
$validators: _try(FieldProps.SeparatedPropsMode.validators),
|
|
217
|
+
$observers: _try(FieldProps.SeparatedPropsMode.observers),
|
|
218
|
+
$interceptors: _try(FieldProps.SeparatedPropsMode.interceptors),
|
|
219
|
+
$converters: _try(FieldProps.SeparatedPropsMode.converters),
|
|
220
|
+
$input: _try(FieldProps.SeparatedPropsMode.input),
|
|
221
|
+
$output: _try(FieldProps.SeparatedPropsMode.output),
|
|
222
|
+
$autoFocus: _try(FieldProps.SeparatedPropsMode.autoFocus),
|
|
223
|
+
$ref: _try(FieldProps.SeparatedPropsMode.refs),
|
|
224
|
+
$nullable: _try(FieldProps.SeparatedPropsMode.nullable),
|
|
225
|
+
$autoComplete: _try(FieldProps.SeparatedPropsMode.autoComplete),
|
|
226
|
+
};
|
|
227
|
+
const field = this.state.form.makeField({
|
|
228
|
+
key,
|
|
229
|
+
path,
|
|
230
|
+
struct,
|
|
231
|
+
data,
|
|
232
|
+
props,
|
|
233
|
+
update,
|
|
234
|
+
state: this.state,
|
|
235
|
+
}, (data && data[FieldProps.FieldPropsEnum.class]) || _try(FieldProps.SeparatedPropsMode.classes));
|
|
236
|
+
this.fields.merge({ [key]: field });
|
|
237
|
+
return field;
|
|
238
|
+
}
|
|
239
|
+
/******************************************************************
|
|
240
|
+
Actions
|
|
241
|
+
*/
|
|
242
|
+
validate(opt, obj) {
|
|
243
|
+
const $opt = lodash.merge({}, opt, { path: this.path });
|
|
244
|
+
return this.state.form.validator.validate($opt, obj);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
Submit
|
|
248
|
+
*/
|
|
249
|
+
submit(hooks = {}, { execOnSubmitHook = true, execValidationHooks = true, validate = true, } = {}) {
|
|
250
|
+
const execOnSubmit = () => this.execHook(FieldProps.FieldPropsEnum.onSubmit, hooks);
|
|
251
|
+
const submit = execOnSubmitHook ? execOnSubmit() : undefined;
|
|
252
|
+
this.$submitting = true;
|
|
253
|
+
this.$submitted += 1;
|
|
254
|
+
if (!validate ||
|
|
255
|
+
!this.state.options.get(OptionsModel.OptionsEnum.validateOnSubmit, this)) {
|
|
256
|
+
return Promise.resolve(submit)
|
|
257
|
+
.then(mobx.action(() => (this.$submitting = false)))
|
|
258
|
+
.catch(mobx.action((err) => {
|
|
259
|
+
this.$submitting = false;
|
|
260
|
+
throw err;
|
|
261
|
+
}))
|
|
262
|
+
.then(() => this);
|
|
263
|
+
}
|
|
264
|
+
const exec = (isValid) => isValid
|
|
265
|
+
? this.execHook(ValidatorInterface.ValidationHooks.onSuccess, hooks)
|
|
266
|
+
: this.execHook(ValidatorInterface.ValidationHooks.onError, hooks);
|
|
267
|
+
return this.validate({
|
|
268
|
+
showErrors: this.state.options.get(OptionsModel.OptionsEnum.showErrorsOnSubmit, this),
|
|
269
|
+
})
|
|
270
|
+
.then(({ isValid }) => {
|
|
271
|
+
const handler = execValidationHooks ? exec(isValid) : undefined;
|
|
272
|
+
if (isValid)
|
|
273
|
+
return Promise.all([submit, handler]);
|
|
274
|
+
const $err = this.state.options.get(OptionsModel.OptionsEnum.defaultGenericError, this);
|
|
275
|
+
const $throw = this.state.options.get(OptionsModel.OptionsEnum.submitThrowsError, this);
|
|
276
|
+
if ($throw && $err)
|
|
277
|
+
this.invalidate();
|
|
278
|
+
return Promise.all([submit, handler]);
|
|
279
|
+
})
|
|
280
|
+
.then(mobx.action(() => (this.$submitting = false)))
|
|
281
|
+
.catch(mobx.action((err) => {
|
|
282
|
+
this.$submitting = false;
|
|
283
|
+
throw err;
|
|
284
|
+
}))
|
|
285
|
+
.then(() => this);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
Check Field Computed Values
|
|
289
|
+
*/
|
|
290
|
+
check(prop, deep = false) {
|
|
291
|
+
utils.allowedProps(FieldProps.AllowedFieldPropsTypes.computed, [prop]);
|
|
292
|
+
return deep
|
|
293
|
+
? utils.checkPropOccurrence({
|
|
294
|
+
type: props.props.occurrences[prop],
|
|
295
|
+
data: this.deepCheck(props.props.occurrences[prop], prop, this.fields),
|
|
296
|
+
})
|
|
297
|
+
: this[prop];
|
|
298
|
+
}
|
|
299
|
+
deepCheck(type, prop, fields) {
|
|
300
|
+
const $fields = utils.getObservableMapValues(fields);
|
|
301
|
+
return lodash.transform($fields, (check, field) => {
|
|
302
|
+
if (!field.fields.size || !Array.isArray(field.initial)) {
|
|
303
|
+
check.push(field[prop]);
|
|
304
|
+
}
|
|
305
|
+
check.push(utils.checkPropOccurrence({
|
|
306
|
+
data: this.deepCheck(type, prop, field.fields),
|
|
307
|
+
type,
|
|
308
|
+
}));
|
|
309
|
+
return check;
|
|
310
|
+
}, []);
|
|
311
|
+
}
|
|
312
|
+
firstError() {
|
|
313
|
+
if (!this.state.options.get(OptionsModel.OptionsEnum.bubbleUpErrorMessages, this))
|
|
314
|
+
return null;
|
|
315
|
+
for (const field of utils.getObservableMapValues(this.fields)) {
|
|
316
|
+
if (field.error)
|
|
317
|
+
return field.error;
|
|
318
|
+
if (field.fields.size) {
|
|
319
|
+
const nested = field.firstError();
|
|
320
|
+
if (nested)
|
|
321
|
+
return nested;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
Update Field Values recurisvely
|
|
328
|
+
OR Create Field if 'undefined'
|
|
329
|
+
*/
|
|
330
|
+
update(fields) {
|
|
331
|
+
if (!lodash.isPlainObject(fields)) {
|
|
332
|
+
throw new Error("The update() method accepts only plain objects.");
|
|
333
|
+
}
|
|
334
|
+
this.deepUpdate(parser.prepareFieldsData({ fields }, this.state.strict), undefined, undefined, fields);
|
|
335
|
+
}
|
|
336
|
+
deepUpdate(fields, path = "", recursion = true, raw) {
|
|
337
|
+
lodash.each(fields, (field, key) => {
|
|
338
|
+
const $key = lodash.has(field, FieldProps.FieldPropsEnum.name) ? field.name : key;
|
|
339
|
+
const $path = `${path}.${$key}`.replace(/^\.+/, "");
|
|
340
|
+
const strictUpdate = this.state.options.get(OptionsModel.OptionsEnum.strictUpdate, this);
|
|
341
|
+
const $field = this.select($path, null, strictUpdate);
|
|
342
|
+
const $container = this.select(path, null, false) ||
|
|
343
|
+
this.state.form.select(this.path ?? "", null, false);
|
|
344
|
+
const applyInputConverterOnUpdate = this.state.options.get(OptionsModel.OptionsEnum.applyInputConverterOnUpdate, this);
|
|
345
|
+
if ($field != null && field !== void 0) {
|
|
346
|
+
if (Array.isArray($field.values())) {
|
|
347
|
+
const n = Math.max(-1, ...Object.keys(field.fields || {}).map(Number));
|
|
348
|
+
utils.getObservableMapValues($field.fields).forEach(($f) => {
|
|
349
|
+
if (Number($f.name) > n) {
|
|
350
|
+
$field.$changed++;
|
|
351
|
+
$field.state.form.$changed++;
|
|
352
|
+
$field.fields.delete($f.name);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (field?.fields) {
|
|
357
|
+
const fallback = this.state.options.get(OptionsModel.OptionsEnum.fallback);
|
|
358
|
+
const x = this.state
|
|
359
|
+
.struct()
|
|
360
|
+
.findIndex((s) => s.startsWith($field.path.replace(/\.\d+\./, "[].") + "[]"));
|
|
361
|
+
if (!fallback && $field.fields.size === 0 && x < 0) {
|
|
362
|
+
$field.value = parser.parseInput(applyInputConverterOnUpdate ? $field.$input : (val) => val, {
|
|
363
|
+
fallbackValueOption: this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this),
|
|
364
|
+
separated: lodash.get(raw, $path),
|
|
365
|
+
});
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (field === null || field.fields == null) {
|
|
370
|
+
$field.value = parser.parseInput(applyInputConverterOnUpdate ? $field.$input : (val) => val, {
|
|
371
|
+
fallbackValueOption: this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this),
|
|
372
|
+
separated: field,
|
|
373
|
+
});
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if ($container != null && $field == null) {
|
|
378
|
+
// get full path when using update() with select() - FIX: #179
|
|
379
|
+
const $newFieldPath = [this.path, $path].join(".").replace(/^\.+/, "");
|
|
380
|
+
// init field into the container field
|
|
381
|
+
$container.$changed++;
|
|
382
|
+
$container.state.form.$changed++;
|
|
383
|
+
$container.initField($key, $newFieldPath, field, true);
|
|
384
|
+
}
|
|
385
|
+
else if (recursion) {
|
|
386
|
+
if (lodash.has(field, FieldProps.FieldPropsEnum.fields) && field.fields != null) {
|
|
387
|
+
// handle nested fields if defined
|
|
388
|
+
this.deepUpdate(field.fields, $path);
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
// handle nested fields if undefined or null
|
|
392
|
+
const $fields = parser.pathToFieldsTree(this.state.struct(), $path);
|
|
393
|
+
this.deepUpdate($fields, $path, false);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
Get Fields Props
|
|
400
|
+
*/
|
|
401
|
+
get(prop = null, strict = true) {
|
|
402
|
+
if (prop == null) {
|
|
403
|
+
return this.deepGet([...props.props.computed, ...props.props.editable, ...props.props.validation], this.fields, strict);
|
|
404
|
+
}
|
|
405
|
+
utils.allowedProps(FieldProps.AllowedFieldPropsTypes.all, Array.isArray(prop) ? prop : [prop]);
|
|
406
|
+
if (typeof prop === "string") {
|
|
407
|
+
if ([FieldProps.FieldPropsEnum.hooks, FieldProps.FieldPropsEnum.handlers].includes(prop)) {
|
|
408
|
+
return this[`$${prop}`];
|
|
409
|
+
}
|
|
410
|
+
if (strict && this.fields.size === 0) {
|
|
411
|
+
const retrieveNullifiedEmptyStrings = this.state.options.get(OptionsModel.OptionsEnum.retrieveNullifiedEmptyStrings, this);
|
|
412
|
+
return parser.parseCheckOutput(this, prop, strict ? retrieveNullifiedEmptyStrings : false);
|
|
413
|
+
}
|
|
414
|
+
const value = this.deepGet(prop, this.fields, strict);
|
|
415
|
+
const removeNullishValuesInArrays = this.state.options.get(OptionsModel.OptionsEnum.removeNullishValuesInArrays, this);
|
|
416
|
+
return parser.parseCheckArray(this, value, prop, strict ? removeNullishValuesInArrays : false);
|
|
417
|
+
}
|
|
418
|
+
return this.deepGet(prop, this.fields, strict);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
Get Fields Props Recursively
|
|
422
|
+
*/
|
|
423
|
+
deepGet(prop, fields, strict = true) {
|
|
424
|
+
const _fieldsArr = utils.getObservableMapValues(fields);
|
|
425
|
+
const _result = lodash.transform(_fieldsArr, (obj, field) => {
|
|
426
|
+
const $nested = ($fields) => $fields.size !== 0 ? this.deepGet(prop, $fields, strict) : undefined;
|
|
427
|
+
Object.assign(obj, {
|
|
428
|
+
[field.key]: { fields: $nested(field.fields) },
|
|
429
|
+
});
|
|
430
|
+
if (typeof prop === "string") {
|
|
431
|
+
const opt = this.state.options;
|
|
432
|
+
const removeProp = (opt.get(OptionsModel.OptionsEnum.retrieveOnlyDirtyFieldsValues, this) &&
|
|
433
|
+
prop === FieldProps.FieldPropsEnum.value &&
|
|
434
|
+
field.isPristine) ||
|
|
435
|
+
(opt.get(OptionsModel.OptionsEnum.retrieveOnlyEnabledFieldsValues, this) &&
|
|
436
|
+
prop === FieldProps.FieldPropsEnum.value &&
|
|
437
|
+
field.disabled) ||
|
|
438
|
+
(opt.get(OptionsModel.OptionsEnum.retrieveOnlyEnabledFieldsErrors, this) &&
|
|
439
|
+
prop === FieldProps.FieldPropsEnum.error &&
|
|
440
|
+
field.disabled &&
|
|
441
|
+
field.isValid &&
|
|
442
|
+
(!field.error || !field.hasError)) ||
|
|
443
|
+
(opt.get(OptionsModel.OptionsEnum.softDelete, this) &&
|
|
444
|
+
prop === FieldProps.FieldPropsEnum.value &&
|
|
445
|
+
field.deleted);
|
|
446
|
+
if (field.fields.size === 0) {
|
|
447
|
+
delete obj[field.key];
|
|
448
|
+
if (removeProp)
|
|
449
|
+
return obj;
|
|
450
|
+
const retrieveNullifiedEmptyStrings = this.state.options.get(OptionsModel.OptionsEnum.retrieveNullifiedEmptyStrings, this);
|
|
451
|
+
return Object.assign(obj, {
|
|
452
|
+
[field.key]: parser.parseCheckOutput(field, prop, strict ? retrieveNullifiedEmptyStrings : false),
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
let value = this.deepGet(prop, field.fields, strict);
|
|
456
|
+
if (prop === FieldProps.FieldPropsEnum.value)
|
|
457
|
+
value = field.$output(value);
|
|
458
|
+
delete obj[field.key];
|
|
459
|
+
if (removeProp)
|
|
460
|
+
return obj;
|
|
461
|
+
const removeNullishValuesInArrays = this.state.options.get(OptionsModel.OptionsEnum.removeNullishValuesInArrays, this);
|
|
462
|
+
return Object.assign(obj, {
|
|
463
|
+
[field.key]: parser.parseCheckArray(field, value, prop, strict ? removeNullishValuesInArrays : false),
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
lodash.each(prop, ($prop) => Object.assign(obj[field.key], {
|
|
467
|
+
[$prop]: field[$prop],
|
|
468
|
+
}));
|
|
469
|
+
return obj;
|
|
470
|
+
}, {});
|
|
471
|
+
// Array fields with integer keys must preserve _entries order
|
|
472
|
+
// (JavaScript Object.assign sorts integer-like keys regardless of insertion order)
|
|
473
|
+
if (typeof prop === "string" &&
|
|
474
|
+
_fieldsArr.length > 0 &&
|
|
475
|
+
utils.hasIntKeys(fields)) {
|
|
476
|
+
const keysInResult = new Set(Object.keys(_result));
|
|
477
|
+
return _fieldsArr
|
|
478
|
+
.filter((f) => keysInResult.has(String(f.key)))
|
|
479
|
+
.map((f) => _result[String(f.key)]);
|
|
480
|
+
}
|
|
481
|
+
return _result;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
Set Fields Props
|
|
485
|
+
*/
|
|
486
|
+
set(prop, data) {
|
|
487
|
+
// UPDATE CUSTOM PROP
|
|
488
|
+
if (typeof prop === "string" && data !== void 0) {
|
|
489
|
+
utils.allowedProps(FieldProps.AllowedFieldPropsTypes.editable, [prop]);
|
|
490
|
+
const isPlain = [FieldProps.FieldPropsEnum.hooks, FieldProps.FieldPropsEnum.handlers].includes(prop);
|
|
491
|
+
const deep = (data !== null &&
|
|
492
|
+
typeof data === "object" &&
|
|
493
|
+
prop === FieldProps.FieldPropsEnum.value) ||
|
|
494
|
+
(lodash.isPlainObject(data) && !isPlain);
|
|
495
|
+
if (deep && this.hasNestedFields)
|
|
496
|
+
return this.deepSet(prop, data, "", true);
|
|
497
|
+
if (prop === FieldProps.FieldPropsEnum.value) {
|
|
498
|
+
const applyInputConverterOnSet = this.state.options.get(OptionsModel.OptionsEnum.applyInputConverterOnSet, this);
|
|
499
|
+
this.value = parser.parseInput(applyInputConverterOnSet ? this.$input : (val) => val, {
|
|
500
|
+
fallbackValueOption: this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this),
|
|
501
|
+
separated: data,
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
else if (isPlain) {
|
|
505
|
+
Object.assign(this[`$${prop}`], data);
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
lodash.set(this, `$${prop}`, data);
|
|
509
|
+
}
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
// NO PROP NAME PROVIDED ("prop" is value)
|
|
513
|
+
if (data == null) {
|
|
514
|
+
if (this.hasNestedFields)
|
|
515
|
+
this.deepSet(FieldProps.FieldPropsEnum.value, prop, "", true);
|
|
516
|
+
else
|
|
517
|
+
this.set(FieldProps.FieldPropsEnum.value, prop);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
Set Fields Props Recursively
|
|
522
|
+
*/
|
|
523
|
+
deepSet(prop, data, path = "", recursion = false) {
|
|
524
|
+
const err = "You are updating a not existent field:";
|
|
525
|
+
const isStrict = this.state.options.get(OptionsModel.OptionsEnum.strictSet, this);
|
|
526
|
+
if (data == null) {
|
|
527
|
+
this.each((field) => (field.$value = parser.defaultValue({
|
|
528
|
+
fallbackValueOption: this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this),
|
|
529
|
+
value: field.$value,
|
|
530
|
+
nullable: field.$nullable,
|
|
531
|
+
type: field.type,
|
|
532
|
+
})));
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
lodash.each(data, ($val, $key) => {
|
|
536
|
+
const $path = `${path}.${$key}`.replace(/^\.+/, "");
|
|
537
|
+
// get the field by path joining keys recursively
|
|
538
|
+
const field = this.select($path, null, isStrict);
|
|
539
|
+
// if no field found when is strict update, throw error
|
|
540
|
+
if (isStrict)
|
|
541
|
+
utils.throwError($path, field, err);
|
|
542
|
+
// update the field/fields if defined
|
|
543
|
+
if (field !== void 0) {
|
|
544
|
+
// update field values or others props
|
|
545
|
+
if ($val !== void 0) {
|
|
546
|
+
field.set(prop, $val, recursion);
|
|
547
|
+
}
|
|
548
|
+
// update values recursively only if field has nested
|
|
549
|
+
if (field.fields.size && $val !== null && typeof $val === "object") {
|
|
550
|
+
this.deepSet(prop, $val, $path, recursion);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
Add Field
|
|
557
|
+
*/
|
|
558
|
+
add(obj, execEvent = true) {
|
|
559
|
+
if (utils.isArrayOfObjects(obj)) {
|
|
560
|
+
lodash.each(obj, (values) => this.update({
|
|
561
|
+
[utils.maxKey(this.fields)]: values,
|
|
562
|
+
}));
|
|
563
|
+
this.$changed++;
|
|
564
|
+
this.state.form.$changed++;
|
|
565
|
+
execEvent && this.execHook(FieldProps.FieldPropsEnum.onAdd);
|
|
566
|
+
return this;
|
|
567
|
+
}
|
|
568
|
+
let key;
|
|
569
|
+
if (lodash.has(obj, FieldProps.FieldPropsEnum.key))
|
|
570
|
+
key = obj.key;
|
|
571
|
+
if (lodash.has(obj, FieldProps.FieldPropsEnum.name))
|
|
572
|
+
key = obj.name;
|
|
573
|
+
if (!key)
|
|
574
|
+
key = utils.maxKey(this.fields);
|
|
575
|
+
const $path = ($key) => [this.path, $key].join(".").replace(/^\.+/, "");
|
|
576
|
+
const tree = parser.pathToFieldsTree(this.state.struct(), this.path ?? "", 0, true);
|
|
577
|
+
const field = this.initField(key, $path(key), lodash.merge(tree[0], obj));
|
|
578
|
+
const hasValues = lodash.has(obj, FieldProps.FieldPropsEnum.value) || lodash.has(obj, FieldProps.FieldPropsEnum.fields);
|
|
579
|
+
if (!hasValues) {
|
|
580
|
+
// When the struct defines nested fields and obj is a plain object
|
|
581
|
+
// without field-definition props (key, name), treat obj properties
|
|
582
|
+
// directly as nested field values (e.g. add({ firstname: 'John' }))
|
|
583
|
+
if (tree[0]?.fields &&
|
|
584
|
+
lodash.isPlainObject(obj) &&
|
|
585
|
+
!utils.hasSome(obj, [FieldProps.FieldPropsEnum.key, FieldProps.FieldPropsEnum.name])) {
|
|
586
|
+
lodash.each(obj, (val, fieldName) => {
|
|
587
|
+
const nestedField = field.select(fieldName);
|
|
588
|
+
if (nestedField && !nestedField.hasNestedFields) {
|
|
589
|
+
nestedField.set(FieldProps.FieldPropsEnum.value, val);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
else if (!this.state.options.get(OptionsModel.OptionsEnum.preserveDeletedFieldsValues, this)) {
|
|
594
|
+
const fallbackValueOption = this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this);
|
|
595
|
+
field.$value = parser.defaultValue({
|
|
596
|
+
fallbackValueOption,
|
|
597
|
+
value: field.$value,
|
|
598
|
+
nullable: field.$nullable,
|
|
599
|
+
type: field.type,
|
|
600
|
+
});
|
|
601
|
+
field.each((f) => (f.$value = parser.defaultValue({
|
|
602
|
+
fallbackValueOption,
|
|
603
|
+
value: f.$value,
|
|
604
|
+
nullable: f.$nullable,
|
|
605
|
+
type: f.type,
|
|
606
|
+
})));
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
this.$changed++;
|
|
610
|
+
this.state.form.$changed++;
|
|
611
|
+
execEvent && this.execHook(FieldProps.FieldPropsEnum.onAdd);
|
|
612
|
+
return field;
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
Del Field
|
|
616
|
+
*/
|
|
617
|
+
del($path = null, execEvent = true) {
|
|
618
|
+
const isStrict = this.state.options.get(OptionsModel.OptionsEnum.strictDelete, this);
|
|
619
|
+
const path = parser.parsePath($path ?? this.path ?? "");
|
|
620
|
+
const fullpath = [this.path ?? "", path ?? ""]
|
|
621
|
+
.join(".")
|
|
622
|
+
.replace(/^\.+|\.+$/g, "");
|
|
623
|
+
const container = this.container($path);
|
|
624
|
+
const keys = path.split(".");
|
|
625
|
+
const lastKey = keys[keys.length - 1];
|
|
626
|
+
if (isStrict && !container.fields.has(lastKey)) {
|
|
627
|
+
const msg = `Key "${lastKey}" not found when trying to delete field`;
|
|
628
|
+
utils.throwError(fullpath, null, msg);
|
|
629
|
+
}
|
|
630
|
+
container.$changed++;
|
|
631
|
+
container.state.form.$changed++;
|
|
632
|
+
if (this.state.options.get(OptionsModel.OptionsEnum.softDelete, this)) {
|
|
633
|
+
return this.select(fullpath).set(FieldProps.FieldPropsEnum.deleted, true);
|
|
634
|
+
}
|
|
635
|
+
container.each((field) => field.debouncedValidation.cancel());
|
|
636
|
+
execEvent && this.execHook(FieldProps.FieldPropsEnum.onDel);
|
|
637
|
+
return container.fields.delete(lastKey);
|
|
638
|
+
}
|
|
639
|
+
/******************************************************************
|
|
640
|
+
Events
|
|
641
|
+
*/
|
|
642
|
+
/**
|
|
643
|
+
MobX Event (observe/intercept)
|
|
644
|
+
*/
|
|
645
|
+
MOBXEvent({ prop = FieldProps.FieldPropsEnum.value, key = null, path = null, call, type, }) {
|
|
646
|
+
let $prop = key || prop;
|
|
647
|
+
utils.allowedProps(FieldProps.AllowedFieldPropsTypes.observable, [$prop]);
|
|
648
|
+
const $instance = this.select(path || this.path, null, false) || this;
|
|
649
|
+
const $call = (change) => call.apply(null, [
|
|
650
|
+
{
|
|
651
|
+
change,
|
|
652
|
+
form: this.state.form,
|
|
653
|
+
path: $instance.path || null,
|
|
654
|
+
field: $instance.path ? $instance : null,
|
|
655
|
+
},
|
|
656
|
+
]);
|
|
657
|
+
let fn;
|
|
658
|
+
let ffn;
|
|
659
|
+
if (type === "observer") {
|
|
660
|
+
fn = mobx.observe;
|
|
661
|
+
ffn = (cb) => mobx.observe($instance.fields.toArray(), cb); // fields
|
|
662
|
+
}
|
|
663
|
+
if (type === "interceptor") {
|
|
664
|
+
$prop = `$${prop}`;
|
|
665
|
+
fn = mobx.intercept;
|
|
666
|
+
ffn = $instance.fields.intercept; // fields
|
|
667
|
+
}
|
|
668
|
+
const $dkey = $instance.path ? `${$prop}@${$instance.path}` : $prop;
|
|
669
|
+
lodash.merge(this.state.disposers[type], {
|
|
670
|
+
[$dkey]: $prop === FieldProps.FieldPropsEnum.fields
|
|
671
|
+
? ffn.apply((change) => $call(change))
|
|
672
|
+
: fn($instance, $prop, (change) => $call(change)),
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
Dispose MOBX Events
|
|
677
|
+
*/
|
|
678
|
+
dispose(opt = null) {
|
|
679
|
+
if (this.path && opt)
|
|
680
|
+
return this.disposeSingle(opt);
|
|
681
|
+
return this.disposeAll();
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
Dispose All Events (observe/intercept)
|
|
685
|
+
*/
|
|
686
|
+
disposeAll() {
|
|
687
|
+
const dispose = (disposer) => disposer.apply();
|
|
688
|
+
lodash.each(this.state.disposers.interceptor, dispose);
|
|
689
|
+
lodash.each(this.state.disposers.observer, dispose);
|
|
690
|
+
this.state.disposers = { interceptor: {}, observer: {} };
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
Dispose Single Event (observe/intercept)
|
|
695
|
+
*/
|
|
696
|
+
disposeSingle({ type, key = FieldProps.FieldPropsEnum.value, path = null }) {
|
|
697
|
+
const $path = parser.parsePath(path ?? this.path ?? "");
|
|
698
|
+
// eslint-disable-next-line
|
|
699
|
+
if (type === "interceptor")
|
|
700
|
+
key = `$${key}`; // target observables
|
|
701
|
+
const disposersType = this.state.disposers[type];
|
|
702
|
+
disposersType[`${key}@${$path}`].apply();
|
|
703
|
+
delete disposersType[`${key}@${$path}`];
|
|
704
|
+
}
|
|
705
|
+
/******************************************************************
|
|
706
|
+
Utils
|
|
707
|
+
*/
|
|
708
|
+
/**
|
|
709
|
+
Fields Selector
|
|
710
|
+
*/
|
|
711
|
+
select(path, fields = null, isStrict = true) {
|
|
712
|
+
const $path = parser.parsePath(String(path));
|
|
713
|
+
const keys = $path.split(".");
|
|
714
|
+
const headKey = keys[0];
|
|
715
|
+
keys.shift();
|
|
716
|
+
let $fields = fields == null ? this.fields.get(headKey) : fields.get(headKey);
|
|
717
|
+
let stop = false;
|
|
718
|
+
lodash.each(keys, ($key) => {
|
|
719
|
+
if (stop)
|
|
720
|
+
return;
|
|
721
|
+
if ($fields == null) {
|
|
722
|
+
$fields = undefined;
|
|
723
|
+
stop = true;
|
|
724
|
+
}
|
|
725
|
+
else {
|
|
726
|
+
$fields = $fields.fields.get($key);
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
if (isStrict && this.state.options.get(OptionsModel.OptionsEnum.strictSelect, this)) {
|
|
730
|
+
utils.throwError(String(path), $fields);
|
|
731
|
+
}
|
|
732
|
+
return $fields;
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
Get Container
|
|
736
|
+
*/
|
|
737
|
+
container($path) {
|
|
738
|
+
const path = parser.parsePath($path ?? this.path ?? "");
|
|
739
|
+
const cpath = path.replace(/[^./]+$/, "").replace(/^\.+|\.+$/g, "");
|
|
740
|
+
if (!!this.path && $path == null) {
|
|
741
|
+
return cpath !== ""
|
|
742
|
+
? this.state.form.select(cpath, null, false)
|
|
743
|
+
: this.state.form;
|
|
744
|
+
}
|
|
745
|
+
return cpath !== "" ? this.select(cpath, null, false) : this;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
Has Field
|
|
749
|
+
*/
|
|
750
|
+
has(path) {
|
|
751
|
+
return this.fields.has(path);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
Map Fields
|
|
755
|
+
*/
|
|
756
|
+
map(cb) {
|
|
757
|
+
return utils.getObservableMapValues(this.fields).map(cb);
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Iterates deeply over fields and invokes `iteratee` for each element.
|
|
761
|
+
* The iteratee is invoked with three arguments: (value, index|key, depth).
|
|
762
|
+
*
|
|
763
|
+
* @param {Function} iteratee The function invoked per iteration.
|
|
764
|
+
* @param {Array|Object} [fields=form.fields] fields to iterate over.
|
|
765
|
+
* @param {number} [depth=1] The recursion depth for internal use.
|
|
766
|
+
* @returns {Array} Returns [fields.values()] of input [fields] parameter.
|
|
767
|
+
* @example
|
|
768
|
+
*
|
|
769
|
+
* JSON.stringify(form)
|
|
770
|
+
* // => {
|
|
771
|
+
* "fields": {
|
|
772
|
+
* "state": {
|
|
773
|
+
* "fields": {
|
|
774
|
+
* "city": {
|
|
775
|
+
* "fields": { "places": {
|
|
776
|
+
* "fields": {},
|
|
777
|
+
* "key": "places", "path": "state.city.places", "$value": "NY Places"
|
|
778
|
+
* }
|
|
779
|
+
* },
|
|
780
|
+
* "key": "city", "path": "state.city", "$value": "New York"
|
|
781
|
+
* }
|
|
782
|
+
* },
|
|
783
|
+
* "key": "state", "path": "state", "$value": "USA"
|
|
784
|
+
* }
|
|
785
|
+
* }
|
|
786
|
+
* }
|
|
787
|
+
*
|
|
788
|
+
* const data = {};
|
|
789
|
+
* form.each(field => data[field.path] = field.value);
|
|
790
|
+
* // => {
|
|
791
|
+
* "state": "USA",
|
|
792
|
+
* "state.city": "New York",
|
|
793
|
+
* "state.city.places": "NY Places"
|
|
794
|
+
* }
|
|
795
|
+
*
|
|
796
|
+
*/
|
|
797
|
+
each(iteratee, fields = null, depth = 0) {
|
|
798
|
+
const $fields = fields || this.fields;
|
|
799
|
+
utils.getObservableMapValues($fields).forEach((field, index) => {
|
|
800
|
+
iteratee(field, index, depth);
|
|
801
|
+
if (field.fields.size !== 0) {
|
|
802
|
+
this.each(iteratee, field.fields, depth + 1);
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
reduce(iteratee, acc) {
|
|
807
|
+
return utils.getObservableMapValues(this.fields).reduce(iteratee, acc);
|
|
808
|
+
}
|
|
809
|
+
/******************************************************************
|
|
810
|
+
Array Operations
|
|
811
|
+
*/
|
|
812
|
+
/**
|
|
813
|
+
Move a field from one index to another (for sortable lists).
|
|
814
|
+
*/
|
|
815
|
+
move(fromIndex, toIndex) {
|
|
816
|
+
this.fields.move(fromIndex, toIndex);
|
|
817
|
+
}
|
|
818
|
+
/******************************************************************
|
|
819
|
+
Helpers
|
|
820
|
+
*/
|
|
821
|
+
/**
|
|
822
|
+
Fields Selector (alias of select)
|
|
823
|
+
*/
|
|
824
|
+
$(key) {
|
|
825
|
+
return this.select(key);
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
Fields Values (recursive with Nested Fields)
|
|
829
|
+
*/
|
|
830
|
+
values() {
|
|
831
|
+
return this.get(FieldProps.FieldPropsEnum.value);
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
Fields Errors (recursive with Nested Fields)
|
|
835
|
+
*/
|
|
836
|
+
errors() {
|
|
837
|
+
return this.get(FieldProps.FieldPropsEnum.error);
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
Fields Labels (recursive with Nested Fields)
|
|
841
|
+
*/
|
|
842
|
+
labels() {
|
|
843
|
+
return this.get(FieldProps.FieldPropsEnum.label);
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
Fields Placeholders (recursive with Nested Fields)
|
|
847
|
+
*/
|
|
848
|
+
placeholders() {
|
|
849
|
+
return this.get(FieldProps.FieldPropsEnum.placeholder);
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
Fields Default Values (recursive with Nested Fields)
|
|
853
|
+
*/
|
|
854
|
+
defaults() {
|
|
855
|
+
return this.get(FieldProps.FieldPropsEnum.default);
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
Fields Initial Values (recursive with Nested Fields)
|
|
859
|
+
*/
|
|
860
|
+
initials() {
|
|
861
|
+
return this.get(FieldProps.FieldPropsEnum.initial);
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
Fields Types (recursive with Nested Fields)
|
|
865
|
+
*/
|
|
866
|
+
types() {
|
|
867
|
+
return this.get(FieldProps.FieldPropsEnum.type);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
exports.default = Base;
|
|
872
|
+
//# sourceMappingURL=Base.js.map
|