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/Field.js
ADDED
|
@@ -0,0 +1,734 @@
|
|
|
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 lodash = require('lodash');
|
|
8
|
+
var Base = require('./Base.js');
|
|
9
|
+
var utils = require('./utils.js');
|
|
10
|
+
var parser = require('./parser.js');
|
|
11
|
+
var OptionsModel = require('./models/OptionsModel.js');
|
|
12
|
+
var FieldProps = require('./models/FieldProps.js');
|
|
13
|
+
|
|
14
|
+
const applyFieldPropFunc = (instance, prop) => {
|
|
15
|
+
if (typeof prop !== "function")
|
|
16
|
+
return prop;
|
|
17
|
+
return prop({
|
|
18
|
+
field: instance,
|
|
19
|
+
form: instance.state.form,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const retrieveFieldPropFunc = (prop) => typeof prop === "function" ? prop : undefined;
|
|
23
|
+
const propGetter = (instance, prop) => typeof instance[`_${prop}`] === "function"
|
|
24
|
+
? instance[`_${prop}`].apply(instance, [
|
|
25
|
+
{
|
|
26
|
+
form: instance.state.form,
|
|
27
|
+
field: instance,
|
|
28
|
+
},
|
|
29
|
+
])
|
|
30
|
+
: instance[`$${prop}`];
|
|
31
|
+
const setupFieldProps = (instance, props, data) => Object.assign(instance, {
|
|
32
|
+
// retrieve functions
|
|
33
|
+
_label: retrieveFieldPropFunc(props.$label ?? data?.label),
|
|
34
|
+
_placeholder: retrieveFieldPropFunc(props.$placeholder ?? data?.placeholder),
|
|
35
|
+
_disabled: retrieveFieldPropFunc(props.$disabled ?? data?.disabled),
|
|
36
|
+
_rules: retrieveFieldPropFunc(props.$rules ?? data?.rules),
|
|
37
|
+
_related: retrieveFieldPropFunc(props.$related ?? data?.related),
|
|
38
|
+
_deleted: retrieveFieldPropFunc(props.$deleted ?? data?.deleted),
|
|
39
|
+
_validators: retrieveFieldPropFunc(props.$validators ?? data?.validators),
|
|
40
|
+
_validatedWith: retrieveFieldPropFunc(props.$validatedWith ?? data?.validatedWith),
|
|
41
|
+
_bindings: retrieveFieldPropFunc(props.$bindings ?? data?.bindings),
|
|
42
|
+
_extra: retrieveFieldPropFunc(props.$extra ?? data?.extra),
|
|
43
|
+
_options: retrieveFieldPropFunc(props.$options ?? data?.options),
|
|
44
|
+
_autoFocus: retrieveFieldPropFunc(props.$autoFocus ?? data?.autoFocus),
|
|
45
|
+
_inputMode: retrieveFieldPropFunc(props.$inputMode ?? data?.inputMode),
|
|
46
|
+
// apply functions or value
|
|
47
|
+
$label: applyFieldPropFunc(instance, props.$label ?? data?.label ?? ""),
|
|
48
|
+
$placeholder: applyFieldPropFunc(instance, props.$placeholder ?? data?.placeholder ?? ""),
|
|
49
|
+
$disabled: applyFieldPropFunc(instance, props.$disabled ?? data?.disabled ?? false),
|
|
50
|
+
$rules: applyFieldPropFunc(instance, props.$rules ?? data?.rules ?? null),
|
|
51
|
+
$related: applyFieldPropFunc(instance, props.$related ?? data?.related ?? []),
|
|
52
|
+
$deleted: applyFieldPropFunc(instance, props.$deleted ?? data?.deleted ?? false),
|
|
53
|
+
$validatedWith: applyFieldPropFunc(instance, props.$validatedWith ?? data?.validatedWith ?? FieldProps.FieldPropsEnum.value),
|
|
54
|
+
$bindings: applyFieldPropFunc(instance, props.$bindings ?? data?.bindings ?? FieldProps.FieldPropsEnum.default),
|
|
55
|
+
$extra: applyFieldPropFunc(instance, props.$extra ?? data?.extra ?? null),
|
|
56
|
+
$options: applyFieldPropFunc(instance, props.$options ?? data?.options ?? {}),
|
|
57
|
+
$autoFocus: applyFieldPropFunc(instance, props.$autoFocus ?? data?.autoFocus ?? false),
|
|
58
|
+
$inputMode: applyFieldPropFunc(instance, props.$inputMode ?? data?.inputMode ?? undefined),
|
|
59
|
+
$validators: applyFieldPropFunc(instance, props.$validators ?? data?.validators ?? null),
|
|
60
|
+
// other props
|
|
61
|
+
$hooks: props.$hooks ?? data?.hooks ?? {},
|
|
62
|
+
$handlers: props.$handlers ?? data?.handlers ?? {},
|
|
63
|
+
$observers: props.$observers ?? data?.observers ?? null,
|
|
64
|
+
$interceptors: props.$interceptors ?? data?.interceptors ?? null,
|
|
65
|
+
$ref: props.$ref ?? data?.ref ?? undefined,
|
|
66
|
+
$nullable: props.$nullable ?? data?.nullable ?? false,
|
|
67
|
+
$autoComplete: props.$autoComplete ?? data?.autoComplete ?? undefined,
|
|
68
|
+
});
|
|
69
|
+
const setupDefaultProp = (instance, data, props, update, { isEmptyArray, fallbackValueOption, }) => parser.parseInput((val) => val, {
|
|
70
|
+
isEmptyArray,
|
|
71
|
+
type: instance.type,
|
|
72
|
+
unified: update
|
|
73
|
+
? parser.defaultValue({
|
|
74
|
+
fallbackValueOption,
|
|
75
|
+
type: instance.type,
|
|
76
|
+
value: instance.value,
|
|
77
|
+
})
|
|
78
|
+
: data?.default,
|
|
79
|
+
separated: props.$default,
|
|
80
|
+
fallback: instance.$initial,
|
|
81
|
+
});
|
|
82
|
+
class Field extends Base.default {
|
|
83
|
+
hasInitialNestedFields = false;
|
|
84
|
+
incremental = false;
|
|
85
|
+
id;
|
|
86
|
+
key;
|
|
87
|
+
name;
|
|
88
|
+
$observers;
|
|
89
|
+
$interceptors;
|
|
90
|
+
$converter = ($) => $;
|
|
91
|
+
$input = ($) => $;
|
|
92
|
+
$output = ($) => $;
|
|
93
|
+
_value;
|
|
94
|
+
_label;
|
|
95
|
+
_placeholder;
|
|
96
|
+
_disabled;
|
|
97
|
+
_rules;
|
|
98
|
+
_related;
|
|
99
|
+
_deleted;
|
|
100
|
+
_validatedWith;
|
|
101
|
+
_validators;
|
|
102
|
+
_bindings;
|
|
103
|
+
_extra;
|
|
104
|
+
_options;
|
|
105
|
+
_autoFocus;
|
|
106
|
+
_inputMode;
|
|
107
|
+
$options = undefined;
|
|
108
|
+
$value = undefined;
|
|
109
|
+
$type = undefined;
|
|
110
|
+
$label = undefined;
|
|
111
|
+
$placeholder = undefined;
|
|
112
|
+
$default = undefined;
|
|
113
|
+
$initial = undefined;
|
|
114
|
+
$bindings = undefined;
|
|
115
|
+
$extra = undefined;
|
|
116
|
+
$related = undefined;
|
|
117
|
+
$validatedWith = undefined;
|
|
118
|
+
$validators = undefined;
|
|
119
|
+
$rules = undefined;
|
|
120
|
+
$disabled = false;
|
|
121
|
+
$focused = false;
|
|
122
|
+
$blurred = false;
|
|
123
|
+
$deleted = false;
|
|
124
|
+
$autoFocus = false;
|
|
125
|
+
$inputMode = undefined;
|
|
126
|
+
$ref = undefined;
|
|
127
|
+
$nullable = false;
|
|
128
|
+
$autoComplete = undefined;
|
|
129
|
+
showError = false;
|
|
130
|
+
errorSync = null;
|
|
131
|
+
errorAsync = null;
|
|
132
|
+
validationErrorStack = [];
|
|
133
|
+
validationFunctionsData = [];
|
|
134
|
+
validationAsyncData = { valid: true, message: null };
|
|
135
|
+
debouncedValidation;
|
|
136
|
+
disposeValidationOnBlur;
|
|
137
|
+
disposeValidationOnChange;
|
|
138
|
+
files = undefined;
|
|
139
|
+
constructor({ key, path, struct, data = {}, props = {}, update = false, state, }) {
|
|
140
|
+
super();
|
|
141
|
+
compat.makeObservable(this, {
|
|
142
|
+
$options: mobx.observable,
|
|
143
|
+
$value: mobx.observable,
|
|
144
|
+
$type: mobx.observable,
|
|
145
|
+
$label: mobx.observable,
|
|
146
|
+
$placeholder: mobx.observable,
|
|
147
|
+
$default: mobx.observable,
|
|
148
|
+
$initial: mobx.observable,
|
|
149
|
+
$bindings: mobx.observable,
|
|
150
|
+
$extra: mobx.observable,
|
|
151
|
+
$related: mobx.observable,
|
|
152
|
+
$validatedWith: mobx.observable,
|
|
153
|
+
$validators: mobx.observable,
|
|
154
|
+
$rules: mobx.observable,
|
|
155
|
+
$disabled: mobx.observable,
|
|
156
|
+
$focused: mobx.observable,
|
|
157
|
+
$blurred: mobx.observable,
|
|
158
|
+
$deleted: mobx.observable,
|
|
159
|
+
showError: mobx.observable,
|
|
160
|
+
errorSync: mobx.observable,
|
|
161
|
+
errorAsync: mobx.observable,
|
|
162
|
+
validationErrorStack: mobx.observable,
|
|
163
|
+
validationFunctionsData: mobx.observable,
|
|
164
|
+
validationAsyncData: mobx.observable,
|
|
165
|
+
files: mobx.observable,
|
|
166
|
+
autoFocus: mobx.computed,
|
|
167
|
+
inputMode: mobx.computed,
|
|
168
|
+
ref: mobx.computed,
|
|
169
|
+
checkValidationErrors: mobx.computed,
|
|
170
|
+
checked: mobx.computed,
|
|
171
|
+
value: mobx.computed,
|
|
172
|
+
initial: mobx.computed,
|
|
173
|
+
default: mobx.computed,
|
|
174
|
+
actionRunning: mobx.computed,
|
|
175
|
+
type: mobx.computed,
|
|
176
|
+
label: mobx.computed,
|
|
177
|
+
placeholder: mobx.computed,
|
|
178
|
+
extra: mobx.computed,
|
|
179
|
+
options: mobx.computed,
|
|
180
|
+
bindings: mobx.computed,
|
|
181
|
+
related: mobx.computed,
|
|
182
|
+
disabled: mobx.computed,
|
|
183
|
+
rules: mobx.computed,
|
|
184
|
+
validators: mobx.computed,
|
|
185
|
+
validatedValue: mobx.computed,
|
|
186
|
+
error: mobx.computed,
|
|
187
|
+
hasError: mobx.computed,
|
|
188
|
+
isValid: mobx.computed,
|
|
189
|
+
isDefault: mobx.computed,
|
|
190
|
+
isDirty: mobx.computed,
|
|
191
|
+
isPristine: mobx.computed,
|
|
192
|
+
isEmpty: mobx.computed,
|
|
193
|
+
blurred: mobx.computed,
|
|
194
|
+
touched: mobx.computed,
|
|
195
|
+
deleted: mobx.computed,
|
|
196
|
+
setupField: mobx.action,
|
|
197
|
+
initNestedFields: mobx.action,
|
|
198
|
+
invalidate: mobx.action,
|
|
199
|
+
setValidationAsyncData: mobx.action,
|
|
200
|
+
resetValidation: mobx.action,
|
|
201
|
+
clear: mobx.action,
|
|
202
|
+
reset: mobx.action,
|
|
203
|
+
focus: mobx.action,
|
|
204
|
+
blur: mobx.action,
|
|
205
|
+
showErrors: mobx.action,
|
|
206
|
+
update: mobx.action,
|
|
207
|
+
});
|
|
208
|
+
this.state = state;
|
|
209
|
+
this.setupField(key, path, struct, data, props, update);
|
|
210
|
+
// this.checkValidationPlugins();
|
|
211
|
+
this.initNestedFields(data, update);
|
|
212
|
+
this.incremental = this.hasIncrementalKeys;
|
|
213
|
+
this.debouncedValidation = lodash.debounce(this.validate, this.state.options.get(OptionsModel.OptionsEnum.validationDebounceWait, this), this.state.options.get(OptionsModel.OptionsEnum.validationDebounceOptions, this));
|
|
214
|
+
this.observeValidationOnBlur();
|
|
215
|
+
this.observeValidationOnChange();
|
|
216
|
+
this.initMOBXEvent(FieldProps.FieldPropsEnum.observers);
|
|
217
|
+
this.initMOBXEvent(FieldProps.FieldPropsEnum.interceptors);
|
|
218
|
+
// setup hooks & handlers from initialization methods
|
|
219
|
+
mobx.runInAction(() => Object.assign(this.$hooks, this.hooks?.apply(this, [this])));
|
|
220
|
+
mobx.runInAction(() => Object.assign(this.$handlers, this.handlers?.apply(this, [this])));
|
|
221
|
+
this.execHook(FieldProps.FieldPropsEnum.onInit);
|
|
222
|
+
// handle Field onChange Hook
|
|
223
|
+
mobx.autorun(() => this.changed && this.execHook(FieldProps.FieldPropsEnum.onChange));
|
|
224
|
+
}
|
|
225
|
+
/* ------------------------------------------------------------------ */
|
|
226
|
+
/* COMPUTED */
|
|
227
|
+
get checkValidationErrors() {
|
|
228
|
+
return (!this.validationAsyncData.valid ||
|
|
229
|
+
!lodash.isEmpty(this.validationErrorStack) ||
|
|
230
|
+
typeof this.errorAsync === 'string' ||
|
|
231
|
+
typeof this.errorSync === 'string');
|
|
232
|
+
}
|
|
233
|
+
set value(newVal) {
|
|
234
|
+
let val = newVal;
|
|
235
|
+
if (typeof val === 'string' &&
|
|
236
|
+
this.state.options.get(OptionsModel.OptionsEnum.autoTrimValue, this)) {
|
|
237
|
+
val = val.trim();
|
|
238
|
+
}
|
|
239
|
+
if (this.$value === val)
|
|
240
|
+
return;
|
|
241
|
+
if (this.handleSetNumberValue(val))
|
|
242
|
+
return;
|
|
243
|
+
this.$value = this.$converter(val);
|
|
244
|
+
this.$changed++;
|
|
245
|
+
if (!this.actionRunning) {
|
|
246
|
+
this.state.form.$changed++;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
handleSetNumberValue(newVal) {
|
|
250
|
+
if (!this.state.options.get(OptionsModel.OptionsEnum.autoParseNumbers, this))
|
|
251
|
+
return false;
|
|
252
|
+
if (typeof this.$initial === 'number' || this.type == "number") {
|
|
253
|
+
if (new RegExp("^-?\\d+(,\\d+)*(\\.\\d+([eE]\\d+)?)?$", "g").exec(newVal)) {
|
|
254
|
+
this.$value = this.$converter(Number(newVal));
|
|
255
|
+
this.$changed++;
|
|
256
|
+
if (!this.actionRunning) {
|
|
257
|
+
this.state.form.$changed++;
|
|
258
|
+
}
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
get actionRunning() {
|
|
264
|
+
return this.submitting || this.clearing || this.resetting;
|
|
265
|
+
}
|
|
266
|
+
get checked() {
|
|
267
|
+
return this.type === "checkbox" ? this.value : undefined;
|
|
268
|
+
}
|
|
269
|
+
get value() {
|
|
270
|
+
return typeof this._value === "function" && !this.hasNestedFields
|
|
271
|
+
? propGetter(this, FieldProps.FieldPropsEnum.value)
|
|
272
|
+
: this.getComputedProp(FieldProps.FieldPropsEnum.value);
|
|
273
|
+
}
|
|
274
|
+
get initial() {
|
|
275
|
+
return this.$initial
|
|
276
|
+
? mobx.toJS(this.$initial)
|
|
277
|
+
: this.getComputedProp(FieldProps.FieldPropsEnum.initial);
|
|
278
|
+
}
|
|
279
|
+
get default() {
|
|
280
|
+
return this.$default
|
|
281
|
+
? mobx.toJS(this.$default)
|
|
282
|
+
: this.getComputedProp(FieldProps.FieldPropsEnum.default);
|
|
283
|
+
}
|
|
284
|
+
set initial(val) {
|
|
285
|
+
this.$initial = val;
|
|
286
|
+
}
|
|
287
|
+
set default(val) {
|
|
288
|
+
this.$default = val;
|
|
289
|
+
}
|
|
290
|
+
get nullable() {
|
|
291
|
+
return propGetter(this, FieldProps.FieldPropsEnum.nullable);
|
|
292
|
+
}
|
|
293
|
+
get autoComplete() {
|
|
294
|
+
return propGetter(this, FieldProps.FieldPropsEnum.autoComplete);
|
|
295
|
+
}
|
|
296
|
+
get ref() {
|
|
297
|
+
return propGetter(this, FieldProps.FieldPropsEnum.ref);
|
|
298
|
+
}
|
|
299
|
+
get extra() {
|
|
300
|
+
return propGetter(this, FieldProps.FieldPropsEnum.extra);
|
|
301
|
+
}
|
|
302
|
+
get autoFocus() {
|
|
303
|
+
return propGetter(this, FieldProps.FieldPropsEnum.autoFocus);
|
|
304
|
+
}
|
|
305
|
+
get inputMode() {
|
|
306
|
+
return propGetter(this, FieldProps.FieldPropsEnum.inputMode);
|
|
307
|
+
}
|
|
308
|
+
get type() {
|
|
309
|
+
return propGetter(this, FieldProps.FieldPropsEnum.type);
|
|
310
|
+
}
|
|
311
|
+
get label() {
|
|
312
|
+
return propGetter(this, FieldProps.FieldPropsEnum.label);
|
|
313
|
+
}
|
|
314
|
+
get placeholder() {
|
|
315
|
+
return propGetter(this, FieldProps.FieldPropsEnum.placeholder);
|
|
316
|
+
}
|
|
317
|
+
get options() {
|
|
318
|
+
return propGetter(this, FieldProps.FieldPropsEnum.options);
|
|
319
|
+
}
|
|
320
|
+
get bindings() {
|
|
321
|
+
return propGetter(this, FieldProps.FieldPropsEnum.bindings);
|
|
322
|
+
}
|
|
323
|
+
get related() {
|
|
324
|
+
return propGetter(this, FieldProps.FieldPropsEnum.related);
|
|
325
|
+
}
|
|
326
|
+
get disabled() {
|
|
327
|
+
return propGetter(this, FieldProps.FieldPropsEnum.disabled);
|
|
328
|
+
}
|
|
329
|
+
get rules() {
|
|
330
|
+
return propGetter(this, FieldProps.FieldPropsEnum.rules);
|
|
331
|
+
}
|
|
332
|
+
get validators() {
|
|
333
|
+
return propGetter(this, FieldProps.FieldPropsEnum.validators);
|
|
334
|
+
}
|
|
335
|
+
get validatedWith() {
|
|
336
|
+
return propGetter(this, FieldProps.FieldPropsEnum.validatedWith);
|
|
337
|
+
}
|
|
338
|
+
get validatedValue() {
|
|
339
|
+
return parser.parseCheckOutput(this, this.validatedWith);
|
|
340
|
+
}
|
|
341
|
+
get error() {
|
|
342
|
+
if (this.showError === false)
|
|
343
|
+
return null;
|
|
344
|
+
return this.errorAsync || this.errorSync || this.firstError() || null;
|
|
345
|
+
}
|
|
346
|
+
get hasError() {
|
|
347
|
+
return (this.checkValidationErrors || this.check(FieldProps.FieldPropsEnum.hasError, true));
|
|
348
|
+
}
|
|
349
|
+
get isValid() {
|
|
350
|
+
return (!this.checkValidationErrors && this.check(FieldProps.FieldPropsEnum.isValid, true));
|
|
351
|
+
}
|
|
352
|
+
get isDefault() {
|
|
353
|
+
return !lodash.isNil(this.default) && lodash.isEqual(this.default, this.value);
|
|
354
|
+
}
|
|
355
|
+
get isDirty() {
|
|
356
|
+
const value = this.changed ? this.value : this.initial;
|
|
357
|
+
return !lodash.isEqual(this.initial, value);
|
|
358
|
+
}
|
|
359
|
+
get isPristine() {
|
|
360
|
+
const value = this.changed ? this.value : this.initial;
|
|
361
|
+
return lodash.isEqual(this.initial, value);
|
|
362
|
+
}
|
|
363
|
+
get isEmpty() {
|
|
364
|
+
if (this.hasNestedFields)
|
|
365
|
+
return this.check(FieldProps.FieldPropsEnum.isEmpty, true);
|
|
366
|
+
if (typeof this.value === 'boolean')
|
|
367
|
+
return !!this.$value;
|
|
368
|
+
if (typeof this.value === 'number')
|
|
369
|
+
return false;
|
|
370
|
+
if (this.value instanceof Date)
|
|
371
|
+
return false;
|
|
372
|
+
if (this.value === null)
|
|
373
|
+
return false;
|
|
374
|
+
return lodash.isEmpty(this.value);
|
|
375
|
+
}
|
|
376
|
+
get focused() {
|
|
377
|
+
return this.hasNestedFields
|
|
378
|
+
? this.check(FieldProps.FieldPropsEnum.focused, true)
|
|
379
|
+
: this.$focused;
|
|
380
|
+
}
|
|
381
|
+
get blurred() {
|
|
382
|
+
return this.hasNestedFields
|
|
383
|
+
? this.check(FieldProps.FieldPropsEnum.blurred, true)
|
|
384
|
+
: this.$blurred;
|
|
385
|
+
}
|
|
386
|
+
get touched() {
|
|
387
|
+
return this.hasNestedFields
|
|
388
|
+
? this.check(FieldProps.FieldPropsEnum.touched, true)
|
|
389
|
+
: this.$touched;
|
|
390
|
+
}
|
|
391
|
+
get deleted() {
|
|
392
|
+
return this.hasNestedFields
|
|
393
|
+
? this.check(FieldProps.FieldPropsEnum.deleted, true)
|
|
394
|
+
: this.$deleted;
|
|
395
|
+
}
|
|
396
|
+
/* ------------------------------------------------------------------ */
|
|
397
|
+
/* EVENTS HANDLERS */
|
|
398
|
+
sync = mobx.action((e, v = null) => {
|
|
399
|
+
const $get = ($) => utils.isBool($, this.value) ? $.target.checked : $.target.value;
|
|
400
|
+
// assume "v" or "e" are the values
|
|
401
|
+
if (lodash.isNil(e) || lodash.isNil(e.target)) {
|
|
402
|
+
if (!lodash.isNil(v) && !lodash.isNil(v.target)) {
|
|
403
|
+
v = $get(v); // eslint-disable-line
|
|
404
|
+
}
|
|
405
|
+
this.value = utils.$try(e, v);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (!lodash.isNil(e.target)) {
|
|
409
|
+
this.value = $get(e);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
this.value = e;
|
|
413
|
+
});
|
|
414
|
+
onSync = (...args) => this.type === "file"
|
|
415
|
+
? this.onDrop(...args)
|
|
416
|
+
: this.execHandler(FieldProps.FieldPropsEnum.onChange, args, this.sync, FieldProps.FieldPropsEnum.onSync);
|
|
417
|
+
onChange = this.onSync;
|
|
418
|
+
onToggle = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onToggle, args, this.sync);
|
|
419
|
+
onBlur = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onBlur, args, mobx.action(() => {
|
|
420
|
+
this.$focused = false;
|
|
421
|
+
this.$blurred = true;
|
|
422
|
+
}));
|
|
423
|
+
onFocus = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onFocus, args, mobx.action(() => {
|
|
424
|
+
this.$focused = true;
|
|
425
|
+
this.$touched = true;
|
|
426
|
+
}));
|
|
427
|
+
onDrop = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onDrop, args, mobx.action(() => {
|
|
428
|
+
const e = args[0];
|
|
429
|
+
let files = null;
|
|
430
|
+
if (utils.isEvent(e) && utils.hasFiles(e)) {
|
|
431
|
+
files = Array.from(e.target.files);
|
|
432
|
+
}
|
|
433
|
+
const incoming = files || args;
|
|
434
|
+
const current = this.files || [];
|
|
435
|
+
// dedupe repeated file drops (fixes .vault/FIXES.md row 28)
|
|
436
|
+
const seen = new Set(current.map((f) => [f?.name, f?.size, f?.lastModified].join("|")));
|
|
437
|
+
this.files = [
|
|
438
|
+
...current,
|
|
439
|
+
...incoming.filter((f) => {
|
|
440
|
+
if (!f || typeof f.size !== "number")
|
|
441
|
+
return true;
|
|
442
|
+
const signature = [f.name, f.size, f.lastModified].join("|");
|
|
443
|
+
if (seen.has(signature))
|
|
444
|
+
return false;
|
|
445
|
+
seen.add(signature);
|
|
446
|
+
return true;
|
|
447
|
+
}),
|
|
448
|
+
];
|
|
449
|
+
}));
|
|
450
|
+
onKeyDown = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onKeyDown, args);
|
|
451
|
+
onKeyUp = (...args) => this.execHandler(FieldProps.FieldPropsEnum.onKeyUp, args);
|
|
452
|
+
setupField($key, $path, _$struct, $data, $props, update) {
|
|
453
|
+
this.key = $key;
|
|
454
|
+
this.path = $path;
|
|
455
|
+
this.id = this.state.options.get(OptionsModel.OptionsEnum.uniqueId)?.apply(this, [this]);
|
|
456
|
+
const fallbackValueOption = this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this);
|
|
457
|
+
const applyInputConverterOnInit = this.state.options.get(OptionsModel.OptionsEnum.applyInputConverterOnInit, this);
|
|
458
|
+
const struct = this.state.struct();
|
|
459
|
+
const structPath = utils.pathToStruct(this.path ?? "");
|
|
460
|
+
const isEmptyArray = utils.isArrayFromStruct(struct, structPath);
|
|
461
|
+
const { $type, $input, $output, $converter, $converters, $computed } = $props;
|
|
462
|
+
if (lodash.isPlainObject($data)) {
|
|
463
|
+
const { type, input, output, converter, converters, computed } = $data;
|
|
464
|
+
this.name = $data.name ?? String($key);
|
|
465
|
+
this.$type = $type || type || "text";
|
|
466
|
+
this.$converter = utils.$try($converter, $converters, converter, converters, this.$converter);
|
|
467
|
+
this.$input = utils.$try($input, input, this.$input);
|
|
468
|
+
this.$output = utils.$try($output, output, this.$output);
|
|
469
|
+
const value = parser.parseInput(applyInputConverterOnInit ? this.$input : (val) => val, {
|
|
470
|
+
fallbackValueOption,
|
|
471
|
+
isEmptyArray,
|
|
472
|
+
type: this.type,
|
|
473
|
+
unified: computed || $data.value,
|
|
474
|
+
separated: $computed || $props.$value,
|
|
475
|
+
fallback: $props.$initial,
|
|
476
|
+
});
|
|
477
|
+
this._value = retrieveFieldPropFunc(value);
|
|
478
|
+
this.$value =
|
|
479
|
+
typeof this._value === "function"
|
|
480
|
+
? applyFieldPropFunc(this, value)
|
|
481
|
+
: value;
|
|
482
|
+
this.$initial = parser.parseInput((val) => val, {
|
|
483
|
+
fallbackValueOption,
|
|
484
|
+
isEmptyArray,
|
|
485
|
+
type: this.type,
|
|
486
|
+
unified: $data.initial,
|
|
487
|
+
separated: $props.$initial,
|
|
488
|
+
fallback: this.$value,
|
|
489
|
+
});
|
|
490
|
+
this.$default = setupDefaultProp(this, $data, $props, update, {
|
|
491
|
+
fallbackValueOption,
|
|
492
|
+
isEmptyArray,
|
|
493
|
+
});
|
|
494
|
+
setupFieldProps(this, $props, $data);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
/* The field IS the value here */
|
|
498
|
+
this.name = String($key);
|
|
499
|
+
this.$type = $type || "text";
|
|
500
|
+
this.$converter = utils.$try($converter, $converters, this.$converter);
|
|
501
|
+
this.$input = utils.$try($input, this.$input);
|
|
502
|
+
this.$output = utils.$try($output, this.$output);
|
|
503
|
+
const value = parser.parseInput(applyInputConverterOnInit ? this.$input : (val) => val, {
|
|
504
|
+
fallbackValueOption,
|
|
505
|
+
isEmptyArray,
|
|
506
|
+
type: this.type,
|
|
507
|
+
unified: $computed || $data,
|
|
508
|
+
separated: $computed || $props.$value,
|
|
509
|
+
});
|
|
510
|
+
this._value = retrieveFieldPropFunc(value);
|
|
511
|
+
this.$value =
|
|
512
|
+
typeof this._value === "function"
|
|
513
|
+
? applyFieldPropFunc(this, value)
|
|
514
|
+
: value;
|
|
515
|
+
this.$initial = parser.parseInput((val) => val, {
|
|
516
|
+
fallbackValueOption,
|
|
517
|
+
isEmptyArray,
|
|
518
|
+
type: this.type,
|
|
519
|
+
unified: $data,
|
|
520
|
+
separated: $props.$initial,
|
|
521
|
+
fallback: this.$value,
|
|
522
|
+
});
|
|
523
|
+
this.$default = setupDefaultProp(this, $data, $props, update, {
|
|
524
|
+
fallbackValueOption,
|
|
525
|
+
isEmptyArray,
|
|
526
|
+
});
|
|
527
|
+
setupFieldProps(this, $props, $data);
|
|
528
|
+
}
|
|
529
|
+
getComputedProp(key) {
|
|
530
|
+
if (this.incremental || this.hasNestedFields) {
|
|
531
|
+
return key === FieldProps.FieldPropsEnum.value
|
|
532
|
+
? this.get(key, false)
|
|
533
|
+
: mobx.untracked(() => this.get(key, false));
|
|
534
|
+
}
|
|
535
|
+
// @ts-ignore
|
|
536
|
+
const val = this[`$${key}`];
|
|
537
|
+
if (Array.isArray(val) || mobx.isObservableArray(val)) {
|
|
538
|
+
return [].slice.call(val);
|
|
539
|
+
}
|
|
540
|
+
return mobx.toJS(val);
|
|
541
|
+
}
|
|
542
|
+
// checkValidationPlugins(): void {
|
|
543
|
+
// const { drivers } = this.state.form.validator;
|
|
544
|
+
// const form = this.state.form.name ? `${this.state.form.name}/` : "";
|
|
545
|
+
// if (isNil(drivers.dvr) && !isNil(this.rules)) {
|
|
546
|
+
// throw new Error(
|
|
547
|
+
// `The DVR validation rules are defined but no DVR plugin provided. Field: "${
|
|
548
|
+
// form + this.path
|
|
549
|
+
// }".`
|
|
550
|
+
// );
|
|
551
|
+
// }
|
|
552
|
+
// if (isNil(drivers.vjf) && !isNil(this.validators)) {
|
|
553
|
+
// throw new Error(
|
|
554
|
+
// `The VJF validators functions are defined but no VJF plugin provided. Field: "${
|
|
555
|
+
// form + this.path
|
|
556
|
+
// }".`
|
|
557
|
+
// );
|
|
558
|
+
// }
|
|
559
|
+
// }
|
|
560
|
+
initNestedFields(field, update) {
|
|
561
|
+
const fields = lodash.isNil(field) ? null : field.fields;
|
|
562
|
+
if (Array.isArray(fields) && !lodash.isEmpty(fields)) {
|
|
563
|
+
this.hasInitialNestedFields = true;
|
|
564
|
+
}
|
|
565
|
+
this.initFields({ fields }, update);
|
|
566
|
+
if (!update && Array.isArray(fields) && lodash.isEmpty(fields)) {
|
|
567
|
+
if (Array.isArray(this.value) && !lodash.isEmpty(this.value)) {
|
|
568
|
+
this.hasInitialNestedFields = true;
|
|
569
|
+
this.initFields({ fields, values: this.value }, update);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
invalidate(message, deep = true, async = false) {
|
|
574
|
+
if (async === true) {
|
|
575
|
+
this.errorAsync = message;
|
|
576
|
+
this.showErrors(true, deep);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
if (Array.isArray(message)) {
|
|
580
|
+
this.validationErrorStack = message;
|
|
581
|
+
this.showErrors(true, deep);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
this.validationErrorStack.unshift(message);
|
|
585
|
+
this.showErrors(true, deep);
|
|
586
|
+
}
|
|
587
|
+
setValidationAsyncData(valid = false, message = null) {
|
|
588
|
+
this.validationAsyncData = { valid, message };
|
|
589
|
+
}
|
|
590
|
+
resetValidation(deep = false) {
|
|
591
|
+
this.showError = false;
|
|
592
|
+
this.errorSync = null;
|
|
593
|
+
this.errorAsync = null;
|
|
594
|
+
this.validationAsyncData = { valid: true, message: null };
|
|
595
|
+
this.validationFunctionsData = [];
|
|
596
|
+
this.validationErrorStack = [];
|
|
597
|
+
Promise.resolve().then(mobx.action(() => {
|
|
598
|
+
this.$resetting = false;
|
|
599
|
+
this.$clearing = false;
|
|
600
|
+
}));
|
|
601
|
+
deep && this.each((field) => field.resetValidation(deep));
|
|
602
|
+
}
|
|
603
|
+
clear(deep = true, execHook = true) {
|
|
604
|
+
execHook && this.execHook(FieldProps.FieldPropsEnum.onClear);
|
|
605
|
+
this.$clearing = true;
|
|
606
|
+
this.$touched = false;
|
|
607
|
+
this.$blurred = false;
|
|
608
|
+
this.$changed = 0;
|
|
609
|
+
this.files = undefined;
|
|
610
|
+
this.$value = parser.defaultValue({
|
|
611
|
+
fallbackValueOption: this.state.options.get(OptionsModel.OptionsEnum.fallbackValue),
|
|
612
|
+
value: this.$value,
|
|
613
|
+
nullable: this.$nullable,
|
|
614
|
+
type: this.type,
|
|
615
|
+
});
|
|
616
|
+
deep && this.each((field) => field.clear(deep));
|
|
617
|
+
this.state.options.get(OptionsModel.OptionsEnum.validateOnClear, this)
|
|
618
|
+
? this.validate({
|
|
619
|
+
showErrors: this.state.options.get(OptionsModel.OptionsEnum.showErrorsOnClear, this),
|
|
620
|
+
})
|
|
621
|
+
: this.resetValidation(deep);
|
|
622
|
+
}
|
|
623
|
+
reset(deep = true, execHook = true) {
|
|
624
|
+
execHook && this.execHook(FieldProps.FieldPropsEnum.onReset);
|
|
625
|
+
this.$resetting = true;
|
|
626
|
+
this.$touched = false;
|
|
627
|
+
this.$blurred = false;
|
|
628
|
+
this.$changed = 0;
|
|
629
|
+
this.files = undefined;
|
|
630
|
+
const useDefaultValue = this.$default !== this.$initial;
|
|
631
|
+
if (useDefaultValue)
|
|
632
|
+
this.value = this.$default;
|
|
633
|
+
if (!useDefaultValue)
|
|
634
|
+
this.value = this.$initial;
|
|
635
|
+
deep && this.each((field) => field.reset(deep));
|
|
636
|
+
this.state.options.get(OptionsModel.OptionsEnum.validateOnReset, this)
|
|
637
|
+
? this.validate({
|
|
638
|
+
showErrors: this.state.options.get(OptionsModel.OptionsEnum.showErrorsOnReset, this),
|
|
639
|
+
})
|
|
640
|
+
: this.resetValidation(deep);
|
|
641
|
+
}
|
|
642
|
+
focus() {
|
|
643
|
+
if (this.ref && !this.focused)
|
|
644
|
+
this.ref.focus();
|
|
645
|
+
this.$focused = true;
|
|
646
|
+
this.$touched = true;
|
|
647
|
+
}
|
|
648
|
+
blur() {
|
|
649
|
+
if (this.ref && this.focused)
|
|
650
|
+
this.ref.blur();
|
|
651
|
+
this.$focused = false;
|
|
652
|
+
this.$blurred = true;
|
|
653
|
+
}
|
|
654
|
+
trim() {
|
|
655
|
+
if (typeof this.value !== 'string')
|
|
656
|
+
return;
|
|
657
|
+
this.$value = this.value.trim();
|
|
658
|
+
}
|
|
659
|
+
showErrors(show = true, deep = true) {
|
|
660
|
+
this.showError = show;
|
|
661
|
+
this.errorSync = this.validationErrorStack.length ? this.validationErrorStack[0] : null;
|
|
662
|
+
this.errorAsync = !this.validationAsyncData.valid
|
|
663
|
+
? this.validationAsyncData.message
|
|
664
|
+
: null;
|
|
665
|
+
deep && this.each((field) => field.showErrors(show, deep));
|
|
666
|
+
}
|
|
667
|
+
observeValidationOnBlur() {
|
|
668
|
+
const opt = this.state.options;
|
|
669
|
+
if (opt.get(OptionsModel.OptionsEnum.validateOnBlur, this)) {
|
|
670
|
+
this.disposeValidationOnBlur = mobx.observe(this, "$focused", (change) => change.newValue === false &&
|
|
671
|
+
this.debouncedValidation({
|
|
672
|
+
showErrors: opt.get(OptionsModel.OptionsEnum.showErrorsOnBlur, this),
|
|
673
|
+
}));
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
observeValidationOnChange() {
|
|
677
|
+
const opt = this.state.options;
|
|
678
|
+
if (opt.get(OptionsModel.OptionsEnum.validateOnChange, this)) {
|
|
679
|
+
this.disposeValidationOnChange = mobx.observe(this, "$value", () => !this.actionRunning &&
|
|
680
|
+
this.debouncedValidation({
|
|
681
|
+
showErrors: opt.get(OptionsModel.OptionsEnum.showErrorsOnChange, this),
|
|
682
|
+
}));
|
|
683
|
+
}
|
|
684
|
+
else if (opt.get(OptionsModel.OptionsEnum.validateOnChangeAfterInitialBlur, this) ||
|
|
685
|
+
opt.get(OptionsModel.OptionsEnum.validateOnChangeAfterSubmit, this)) {
|
|
686
|
+
this.disposeValidationOnChange = mobx.observe(this, "$value", () => !this.actionRunning &&
|
|
687
|
+
((opt.get(OptionsModel.OptionsEnum.validateOnChangeAfterInitialBlur, this) &&
|
|
688
|
+
this.blurred) ||
|
|
689
|
+
(opt.get(OptionsModel.OptionsEnum.validateOnChangeAfterSubmit, this) &&
|
|
690
|
+
this.state.form.submitted)) &&
|
|
691
|
+
this.debouncedValidation({
|
|
692
|
+
showErrors: opt.get(OptionsModel.OptionsEnum.showErrorsOnChange, this),
|
|
693
|
+
}));
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
initMOBXEvent(type) {
|
|
697
|
+
const arr = this[`$${type}`];
|
|
698
|
+
if (!Array.isArray(arr))
|
|
699
|
+
return;
|
|
700
|
+
let fn;
|
|
701
|
+
if (type === FieldProps.FieldPropsEnum.observers)
|
|
702
|
+
fn = this.observe;
|
|
703
|
+
if (type === FieldProps.FieldPropsEnum.interceptors)
|
|
704
|
+
fn = this.intercept;
|
|
705
|
+
arr.map((obj) => fn(lodash.omit(obj, FieldProps.FieldPropsEnum.path)));
|
|
706
|
+
}
|
|
707
|
+
bind(props = {}) {
|
|
708
|
+
return {
|
|
709
|
+
...this.state.bindings.load(this, this.bindings, props),
|
|
710
|
+
ref: ($ref) => (this.$ref = $ref),
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
update(fields) {
|
|
714
|
+
if (!lodash.isPlainObject(fields)) {
|
|
715
|
+
throw new Error("The update() method accepts only plain objects.");
|
|
716
|
+
}
|
|
717
|
+
const fallback = this.state.options.get(OptionsModel.OptionsEnum.fallback, this);
|
|
718
|
+
const applyInputConverterOnUpdate = this.state.options.get(OptionsModel.OptionsEnum.applyInputConverterOnUpdate, this);
|
|
719
|
+
const x = this.state
|
|
720
|
+
.struct()
|
|
721
|
+
.findIndex((s) => s.startsWith((this.path ?? "").replace(/\.\d+\./, "[].") + "[]"));
|
|
722
|
+
if (!fallback && this.fields.size === 0 && x < 0) {
|
|
723
|
+
this.value = parser.parseInput(applyInputConverterOnUpdate ? this.$input : (val) => val, {
|
|
724
|
+
fallbackValueOption: this.state.options.get(OptionsModel.OptionsEnum.fallbackValue, this),
|
|
725
|
+
separated: fields,
|
|
726
|
+
});
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
super.update(fields);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
exports.default = Field;
|
|
734
|
+
//# sourceMappingURL=Field.js.map
|