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/src/Field.ts
ADDED
|
@@ -0,0 +1,1034 @@
|
|
|
1
|
+
import {
|
|
2
|
+
observable,
|
|
3
|
+
observe,
|
|
4
|
+
action,
|
|
5
|
+
computed,
|
|
6
|
+
isObservableArray,
|
|
7
|
+
toJS,
|
|
8
|
+
untracked,
|
|
9
|
+
autorun,
|
|
10
|
+
runInAction,
|
|
11
|
+
} from "mobx";
|
|
12
|
+
import { makeObservable } from "./compat";
|
|
13
|
+
import { isEmpty, isEqual, isNil, isPlainObject, debounce, omit } from "lodash";
|
|
14
|
+
import Base from "./Base";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
$try,
|
|
18
|
+
hasFiles,
|
|
19
|
+
isBool,
|
|
20
|
+
isEvent,
|
|
21
|
+
pathToStruct,
|
|
22
|
+
isArrayFromStruct,
|
|
23
|
+
} from "./utils";
|
|
24
|
+
|
|
25
|
+
import { parseInput, parseCheckOutput, defaultValue } from "./parser";
|
|
26
|
+
|
|
27
|
+
import { OptionsModel, OptionsEnum } from "./models/OptionsModel";
|
|
28
|
+
import { FieldInterface, FieldConstructor } from "./models/FieldInterface";
|
|
29
|
+
import { FieldPropsEnum } from "./models/FieldProps";
|
|
30
|
+
|
|
31
|
+
const applyFieldPropFunc = (instance: any, prop: any): any => {
|
|
32
|
+
if (typeof prop !== "function") return prop;
|
|
33
|
+
return prop({
|
|
34
|
+
field: instance,
|
|
35
|
+
form: instance.state.form,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const retrieveFieldPropFunc = (prop: any): Function | any | undefined =>
|
|
40
|
+
typeof prop === "function" ? prop : undefined;
|
|
41
|
+
|
|
42
|
+
const propGetter = (instance: any, prop: FieldPropsEnum): any =>
|
|
43
|
+
typeof instance[`_${prop}`] === "function"
|
|
44
|
+
? instance[`_${prop}`].apply(instance, [
|
|
45
|
+
{
|
|
46
|
+
form: instance.state.form,
|
|
47
|
+
field: instance,
|
|
48
|
+
},
|
|
49
|
+
])
|
|
50
|
+
: instance[`$${prop}`];
|
|
51
|
+
|
|
52
|
+
const setupFieldProps = (instance: any, props: any, data: any) =>
|
|
53
|
+
Object.assign(instance, {
|
|
54
|
+
// retrieve functions
|
|
55
|
+
_label: retrieveFieldPropFunc(props.$label ?? data?.label),
|
|
56
|
+
_placeholder: retrieveFieldPropFunc(
|
|
57
|
+
props.$placeholder ?? data?.placeholder,
|
|
58
|
+
),
|
|
59
|
+
_disabled: retrieveFieldPropFunc(props.$disabled ?? data?.disabled),
|
|
60
|
+
_rules: retrieveFieldPropFunc(props.$rules ?? data?.rules),
|
|
61
|
+
_related: retrieveFieldPropFunc(props.$related ?? data?.related),
|
|
62
|
+
_deleted: retrieveFieldPropFunc(props.$deleted ?? data?.deleted),
|
|
63
|
+
_validators: retrieveFieldPropFunc(props.$validators ?? data?.validators),
|
|
64
|
+
_validatedWith: retrieveFieldPropFunc(
|
|
65
|
+
props.$validatedWith ?? data?.validatedWith,
|
|
66
|
+
),
|
|
67
|
+
_bindings: retrieveFieldPropFunc(props.$bindings ?? data?.bindings),
|
|
68
|
+
_extra: retrieveFieldPropFunc(props.$extra ?? data?.extra),
|
|
69
|
+
_options: retrieveFieldPropFunc(props.$options ?? data?.options),
|
|
70
|
+
_autoFocus: retrieveFieldPropFunc(props.$autoFocus ?? data?.autoFocus),
|
|
71
|
+
_inputMode: retrieveFieldPropFunc(props.$inputMode ?? data?.inputMode),
|
|
72
|
+
// apply functions or value
|
|
73
|
+
$label: applyFieldPropFunc(instance, props.$label ?? data?.label ?? ""),
|
|
74
|
+
$placeholder: applyFieldPropFunc(
|
|
75
|
+
instance,
|
|
76
|
+
props.$placeholder ?? data?.placeholder ?? "",
|
|
77
|
+
),
|
|
78
|
+
$disabled: applyFieldPropFunc(
|
|
79
|
+
instance,
|
|
80
|
+
props.$disabled ?? data?.disabled ?? false,
|
|
81
|
+
),
|
|
82
|
+
$rules: applyFieldPropFunc(instance, props.$rules ?? data?.rules ?? null),
|
|
83
|
+
$related: applyFieldPropFunc(
|
|
84
|
+
instance,
|
|
85
|
+
props.$related ?? data?.related ?? [],
|
|
86
|
+
),
|
|
87
|
+
$deleted: applyFieldPropFunc(
|
|
88
|
+
instance,
|
|
89
|
+
props.$deleted ?? data?.deleted ?? false,
|
|
90
|
+
),
|
|
91
|
+
$validatedWith: applyFieldPropFunc(
|
|
92
|
+
instance,
|
|
93
|
+
props.$validatedWith ?? data?.validatedWith ?? FieldPropsEnum.value,
|
|
94
|
+
),
|
|
95
|
+
$bindings: applyFieldPropFunc(
|
|
96
|
+
instance,
|
|
97
|
+
props.$bindings ?? data?.bindings ?? FieldPropsEnum.default,
|
|
98
|
+
),
|
|
99
|
+
$extra: applyFieldPropFunc(instance, props.$extra ?? data?.extra ?? null),
|
|
100
|
+
$options: applyFieldPropFunc(
|
|
101
|
+
instance,
|
|
102
|
+
props.$options ?? data?.options ?? {},
|
|
103
|
+
),
|
|
104
|
+
$autoFocus: applyFieldPropFunc(
|
|
105
|
+
instance,
|
|
106
|
+
props.$autoFocus ?? data?.autoFocus ?? false,
|
|
107
|
+
),
|
|
108
|
+
$inputMode: applyFieldPropFunc(
|
|
109
|
+
instance,
|
|
110
|
+
props.$inputMode ?? data?.inputMode ?? undefined,
|
|
111
|
+
),
|
|
112
|
+
$validators: applyFieldPropFunc(
|
|
113
|
+
instance,
|
|
114
|
+
props.$validators ?? data?.validators ?? null,
|
|
115
|
+
),
|
|
116
|
+
// other props
|
|
117
|
+
$hooks: props.$hooks ?? data?.hooks ?? {},
|
|
118
|
+
$handlers: props.$handlers ?? data?.handlers ?? {},
|
|
119
|
+
$observers: props.$observers ?? data?.observers ?? null,
|
|
120
|
+
$interceptors: props.$interceptors ?? data?.interceptors ?? null,
|
|
121
|
+
$ref: props.$ref ?? data?.ref ?? undefined,
|
|
122
|
+
$nullable: props.$nullable ?? data?.nullable ?? false,
|
|
123
|
+
$autoComplete: props.$autoComplete ?? data?.autoComplete ?? undefined,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const setupDefaultProp = (
|
|
127
|
+
instance: Field,
|
|
128
|
+
data: any,
|
|
129
|
+
props: any,
|
|
130
|
+
update: boolean,
|
|
131
|
+
{
|
|
132
|
+
isEmptyArray,
|
|
133
|
+
fallbackValueOption,
|
|
134
|
+
}: { isEmptyArray: boolean; fallbackValueOption: any },
|
|
135
|
+
) =>
|
|
136
|
+
parseInput((val: any) => val, {
|
|
137
|
+
isEmptyArray,
|
|
138
|
+
type: instance.type,
|
|
139
|
+
unified: update
|
|
140
|
+
? defaultValue({
|
|
141
|
+
fallbackValueOption,
|
|
142
|
+
type: instance.type,
|
|
143
|
+
value: instance.value,
|
|
144
|
+
})
|
|
145
|
+
: data?.default,
|
|
146
|
+
separated: props.$default,
|
|
147
|
+
fallback: instance.$initial,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
export default class Field<T = any> extends Base<Record<string, any>> implements FieldInterface<T> {
|
|
151
|
+
hasInitialNestedFields = false;
|
|
152
|
+
incremental = false;
|
|
153
|
+
|
|
154
|
+
id: any;
|
|
155
|
+
key: any;
|
|
156
|
+
name: any;
|
|
157
|
+
|
|
158
|
+
$observers: any;
|
|
159
|
+
$interceptors: any;
|
|
160
|
+
|
|
161
|
+
$converter = ($: any) => $;
|
|
162
|
+
$input = ($: any) => $;
|
|
163
|
+
$output = ($: any) => $;
|
|
164
|
+
|
|
165
|
+
_value!: Function;
|
|
166
|
+
_label!: Function;
|
|
167
|
+
_placeholder!: Function;
|
|
168
|
+
_disabled!: Function;
|
|
169
|
+
_rules!: Function;
|
|
170
|
+
_related!: Function;
|
|
171
|
+
_deleted!: Function;
|
|
172
|
+
_validatedWith!: Function;
|
|
173
|
+
_validators!: Function;
|
|
174
|
+
_bindings!: Function;
|
|
175
|
+
_extra!: Function;
|
|
176
|
+
_options!: Function;
|
|
177
|
+
_autoFocus!: Function;
|
|
178
|
+
_inputMode!: Function;
|
|
179
|
+
|
|
180
|
+
$options: OptionsModel | undefined = undefined;
|
|
181
|
+
$value: any = undefined;
|
|
182
|
+
$type: string | undefined = undefined;
|
|
183
|
+
$label: string | undefined = undefined;
|
|
184
|
+
$placeholder: string | undefined = undefined;
|
|
185
|
+
$default: any = undefined;
|
|
186
|
+
$initial: any = undefined;
|
|
187
|
+
$bindings: any = undefined;
|
|
188
|
+
$extra: any = undefined;
|
|
189
|
+
$related: string[] | undefined = undefined;
|
|
190
|
+
$validatedWith: string | undefined = undefined;
|
|
191
|
+
$validators: any[] | undefined = undefined;
|
|
192
|
+
$rules: string[] | undefined = undefined;
|
|
193
|
+
|
|
194
|
+
$disabled: boolean = false;
|
|
195
|
+
$focused: boolean = false;
|
|
196
|
+
$blurred: boolean = false;
|
|
197
|
+
$deleted: boolean = false;
|
|
198
|
+
$autoFocus: boolean = false;
|
|
199
|
+
$inputMode: string | undefined = undefined;
|
|
200
|
+
$ref: any = undefined;
|
|
201
|
+
$nullable: boolean = false;
|
|
202
|
+
$autoComplete: string | undefined = undefined;
|
|
203
|
+
|
|
204
|
+
showError: boolean = false;
|
|
205
|
+
errorSync: string | null = null;
|
|
206
|
+
errorAsync: string | null = null;
|
|
207
|
+
|
|
208
|
+
validationErrorStack: string[] = [];
|
|
209
|
+
validationFunctionsData: any[] = [];
|
|
210
|
+
validationAsyncData: { valid: boolean; message: string | null } = { valid: true, message: null };
|
|
211
|
+
debouncedValidation: any;
|
|
212
|
+
|
|
213
|
+
disposeValidationOnBlur: any;
|
|
214
|
+
disposeValidationOnChange: any;
|
|
215
|
+
|
|
216
|
+
files: any = undefined;
|
|
217
|
+
|
|
218
|
+
constructor({
|
|
219
|
+
key,
|
|
220
|
+
path,
|
|
221
|
+
struct,
|
|
222
|
+
data = {},
|
|
223
|
+
props = {},
|
|
224
|
+
update = false,
|
|
225
|
+
state,
|
|
226
|
+
}: FieldConstructor) {
|
|
227
|
+
super();
|
|
228
|
+
|
|
229
|
+
makeObservable(this, {
|
|
230
|
+
$options: observable,
|
|
231
|
+
$value: observable,
|
|
232
|
+
$type: observable,
|
|
233
|
+
$label: observable,
|
|
234
|
+
$placeholder: observable,
|
|
235
|
+
$default: observable,
|
|
236
|
+
$initial: observable,
|
|
237
|
+
$bindings: observable,
|
|
238
|
+
$extra: observable,
|
|
239
|
+
$related: observable,
|
|
240
|
+
$validatedWith: observable,
|
|
241
|
+
$validators: observable,
|
|
242
|
+
$rules: observable,
|
|
243
|
+
$disabled: observable,
|
|
244
|
+
$focused: observable,
|
|
245
|
+
$blurred: observable,
|
|
246
|
+
$deleted: observable,
|
|
247
|
+
showError: observable,
|
|
248
|
+
errorSync: observable,
|
|
249
|
+
errorAsync: observable,
|
|
250
|
+
validationErrorStack: observable,
|
|
251
|
+
validationFunctionsData: observable,
|
|
252
|
+
validationAsyncData: observable,
|
|
253
|
+
files: observable,
|
|
254
|
+
autoFocus: computed,
|
|
255
|
+
inputMode: computed,
|
|
256
|
+
ref: computed,
|
|
257
|
+
checkValidationErrors: computed,
|
|
258
|
+
checked: computed,
|
|
259
|
+
value: computed,
|
|
260
|
+
initial: computed,
|
|
261
|
+
default: computed,
|
|
262
|
+
actionRunning: computed,
|
|
263
|
+
type: computed,
|
|
264
|
+
label: computed,
|
|
265
|
+
placeholder: computed,
|
|
266
|
+
extra: computed,
|
|
267
|
+
options: computed,
|
|
268
|
+
bindings: computed,
|
|
269
|
+
related: computed,
|
|
270
|
+
disabled: computed,
|
|
271
|
+
rules: computed,
|
|
272
|
+
validators: computed,
|
|
273
|
+
validatedValue: computed,
|
|
274
|
+
error: computed,
|
|
275
|
+
hasError: computed,
|
|
276
|
+
isValid: computed,
|
|
277
|
+
isDefault: computed,
|
|
278
|
+
isDirty: computed,
|
|
279
|
+
isPristine: computed,
|
|
280
|
+
isEmpty: computed,
|
|
281
|
+
blurred: computed,
|
|
282
|
+
touched: computed,
|
|
283
|
+
deleted: computed,
|
|
284
|
+
setupField: action,
|
|
285
|
+
initNestedFields: action,
|
|
286
|
+
invalidate: action,
|
|
287
|
+
setValidationAsyncData: action,
|
|
288
|
+
resetValidation: action,
|
|
289
|
+
clear: action,
|
|
290
|
+
reset: action,
|
|
291
|
+
focus: action,
|
|
292
|
+
blur: action,
|
|
293
|
+
showErrors: action,
|
|
294
|
+
update: action,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
this.state = state;
|
|
298
|
+
|
|
299
|
+
this.setupField(key, path, struct, data, props, update);
|
|
300
|
+
// this.checkValidationPlugins();
|
|
301
|
+
this.initNestedFields(data, update);
|
|
302
|
+
|
|
303
|
+
this.incremental = this.hasIncrementalKeys;
|
|
304
|
+
|
|
305
|
+
this.debouncedValidation = debounce(
|
|
306
|
+
this.validate,
|
|
307
|
+
this.state.options.get(OptionsEnum.validationDebounceWait, this),
|
|
308
|
+
this.state.options.get(OptionsEnum.validationDebounceOptions, this),
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
this.observeValidationOnBlur();
|
|
312
|
+
this.observeValidationOnChange();
|
|
313
|
+
|
|
314
|
+
this.initMOBXEvent(FieldPropsEnum.observers);
|
|
315
|
+
this.initMOBXEvent(FieldPropsEnum.interceptors);
|
|
316
|
+
|
|
317
|
+
// setup hooks & handlers from initialization methods
|
|
318
|
+
runInAction(() =>
|
|
319
|
+
Object.assign(this.$hooks, (this as any).hooks?.apply(this, [this])),
|
|
320
|
+
);
|
|
321
|
+
runInAction(() =>
|
|
322
|
+
Object.assign(
|
|
323
|
+
this.$handlers,
|
|
324
|
+
(this as any).handlers?.apply(this, [this]),
|
|
325
|
+
),
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
this.execHook(FieldPropsEnum.onInit);
|
|
329
|
+
|
|
330
|
+
// handle Field onChange Hook
|
|
331
|
+
autorun(() => this.changed && this.execHook(FieldPropsEnum.onChange));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/* ------------------------------------------------------------------ */
|
|
335
|
+
/* COMPUTED */
|
|
336
|
+
|
|
337
|
+
get checkValidationErrors(): boolean {
|
|
338
|
+
return (
|
|
339
|
+
!this.validationAsyncData.valid ||
|
|
340
|
+
!isEmpty(this.validationErrorStack) ||
|
|
341
|
+
typeof this.errorAsync === 'string' ||
|
|
342
|
+
typeof this.errorSync === 'string'
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
set value(newVal: T) {
|
|
347
|
+
let val: any = newVal;
|
|
348
|
+
if (
|
|
349
|
+
typeof val === 'string' &&
|
|
350
|
+
this.state.options.get(OptionsEnum.autoTrimValue, this)
|
|
351
|
+
) {
|
|
352
|
+
val = val.trim();
|
|
353
|
+
}
|
|
354
|
+
if (this.$value === val) return;
|
|
355
|
+
if (this.handleSetNumberValue(val)) return;
|
|
356
|
+
this.$value = this.$converter(val);
|
|
357
|
+
this.$changed++;
|
|
358
|
+
if (!this.actionRunning) {
|
|
359
|
+
this.state.form.$changed++;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
handleSetNumberValue(newVal: any): boolean | undefined {
|
|
364
|
+
if (!this.state.options.get(OptionsEnum.autoParseNumbers, this))
|
|
365
|
+
return false;
|
|
366
|
+
|
|
367
|
+
if (typeof this.$initial === 'number' || this.type == "number") {
|
|
368
|
+
if (
|
|
369
|
+
new RegExp("^-?\\d+(,\\d+)*(\\.\\d+([eE]\\d+)?)?$", "g").exec(newVal)
|
|
370
|
+
) {
|
|
371
|
+
this.$value = this.$converter(Number(newVal));
|
|
372
|
+
this.$changed++;
|
|
373
|
+
if (!this.actionRunning) {
|
|
374
|
+
this.state.form.$changed++;
|
|
375
|
+
}
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
get actionRunning(): boolean {
|
|
382
|
+
return this.submitting || this.clearing || this.resetting;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
get checked(): T | undefined {
|
|
386
|
+
return this.type === "checkbox" ? this.value : undefined;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
get value(): T {
|
|
390
|
+
return typeof this._value === "function" && !this.hasNestedFields
|
|
391
|
+
? propGetter(this, FieldPropsEnum.value)
|
|
392
|
+
: this.getComputedProp(FieldPropsEnum.value);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
get initial(): T {
|
|
396
|
+
return this.$initial
|
|
397
|
+
? toJS(this.$initial)
|
|
398
|
+
: this.getComputedProp(FieldPropsEnum.initial);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
get default(): T {
|
|
402
|
+
return this.$default
|
|
403
|
+
? toJS(this.$default)
|
|
404
|
+
: this.getComputedProp(FieldPropsEnum.default);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
set initial(val: T) {
|
|
408
|
+
this.$initial = val;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
set default(val: T) {
|
|
412
|
+
this.$default = val;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
get nullable(): boolean {
|
|
416
|
+
return propGetter(this, FieldPropsEnum.nullable);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
get autoComplete(): string | undefined {
|
|
420
|
+
return propGetter(this, FieldPropsEnum.autoComplete);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
get ref(): any {
|
|
424
|
+
return propGetter(this, FieldPropsEnum.ref);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
get extra(): any {
|
|
428
|
+
return propGetter(this, FieldPropsEnum.extra);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
get autoFocus(): any {
|
|
432
|
+
return propGetter(this, FieldPropsEnum.autoFocus);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
get inputMode(): any {
|
|
436
|
+
return propGetter(this, FieldPropsEnum.inputMode);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
get type(): any {
|
|
440
|
+
return propGetter(this, FieldPropsEnum.type);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
get label(): any {
|
|
444
|
+
return propGetter(this, FieldPropsEnum.label);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
get placeholder(): any {
|
|
448
|
+
return propGetter(this, FieldPropsEnum.placeholder);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
get options(): any {
|
|
452
|
+
return propGetter(this, FieldPropsEnum.options);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
get bindings(): any {
|
|
456
|
+
return propGetter(this, FieldPropsEnum.bindings);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
get related(): any {
|
|
460
|
+
return propGetter(this, FieldPropsEnum.related);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
get disabled(): any {
|
|
464
|
+
return propGetter(this, FieldPropsEnum.disabled);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
get rules(): any {
|
|
468
|
+
return propGetter(this, FieldPropsEnum.rules);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
get validators(): any {
|
|
472
|
+
return propGetter(this, FieldPropsEnum.validators);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
get validatedWith(): any {
|
|
476
|
+
return propGetter(this, FieldPropsEnum.validatedWith);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
get validatedValue(): any {
|
|
480
|
+
return parseCheckOutput(this, this.validatedWith);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
get error(): string | null {
|
|
484
|
+
if (this.showError === false) return null;
|
|
485
|
+
return this.errorAsync || this.errorSync || this.firstError() || null;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
get hasError(): boolean {
|
|
489
|
+
return (
|
|
490
|
+
this.checkValidationErrors || this.check(FieldPropsEnum.hasError, true)
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
get isValid(): boolean {
|
|
495
|
+
return (
|
|
496
|
+
!this.checkValidationErrors && this.check(FieldPropsEnum.isValid, true)
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
get isDefault(): boolean {
|
|
501
|
+
return !isNil(this.default) && isEqual(this.default, this.value);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
get isDirty(): boolean {
|
|
505
|
+
const value = this.changed ? this.value : this.initial;
|
|
506
|
+
return !isEqual(this.initial, value);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
get isPristine(): boolean {
|
|
510
|
+
const value = this.changed ? this.value : this.initial;
|
|
511
|
+
return isEqual(this.initial, value);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
get isEmpty(): boolean {
|
|
515
|
+
if (this.hasNestedFields) return this.check(FieldPropsEnum.isEmpty, true);
|
|
516
|
+
if (typeof this.value === 'boolean') return !!this.$value;
|
|
517
|
+
if (typeof this.value === 'number') return false;
|
|
518
|
+
if (this.value instanceof Date) return false;
|
|
519
|
+
if (this.value === null) return false;
|
|
520
|
+
return isEmpty(this.value);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
get focused(): boolean {
|
|
524
|
+
return this.hasNestedFields
|
|
525
|
+
? this.check(FieldPropsEnum.focused, true)
|
|
526
|
+
: this.$focused;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
get blurred(): boolean {
|
|
530
|
+
return this.hasNestedFields
|
|
531
|
+
? this.check(FieldPropsEnum.blurred, true)
|
|
532
|
+
: this.$blurred;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
get touched(): boolean {
|
|
536
|
+
return this.hasNestedFields
|
|
537
|
+
? this.check(FieldPropsEnum.touched, true)
|
|
538
|
+
: this.$touched;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
get deleted(): boolean {
|
|
542
|
+
return this.hasNestedFields
|
|
543
|
+
? this.check(FieldPropsEnum.deleted, true)
|
|
544
|
+
: this.$deleted;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/* ------------------------------------------------------------------ */
|
|
548
|
+
/* EVENTS HANDLERS */
|
|
549
|
+
|
|
550
|
+
sync = action((e: any, v: any = null) => {
|
|
551
|
+
const $get = ($: any) =>
|
|
552
|
+
isBool($, this.value) ? $.target.checked : $.target.value;
|
|
553
|
+
|
|
554
|
+
// assume "v" or "e" are the values
|
|
555
|
+
if (isNil(e) || isNil(e.target)) {
|
|
556
|
+
if (!isNil(v) && !isNil(v.target)) {
|
|
557
|
+
v = $get(v); // eslint-disable-line
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
this.value = $try(e, v);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (!isNil(e.target)) {
|
|
565
|
+
this.value = $get(e);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
this.value = e;
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
onSync = (...args: any) =>
|
|
573
|
+
this.type === "file"
|
|
574
|
+
? this.onDrop(...args)
|
|
575
|
+
: this.execHandler(
|
|
576
|
+
FieldPropsEnum.onChange,
|
|
577
|
+
args,
|
|
578
|
+
this.sync,
|
|
579
|
+
FieldPropsEnum.onSync,
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
onChange = this.onSync;
|
|
583
|
+
|
|
584
|
+
onToggle = (...args: any) =>
|
|
585
|
+
this.execHandler(FieldPropsEnum.onToggle, args, this.sync);
|
|
586
|
+
|
|
587
|
+
onBlur = (...args: any) =>
|
|
588
|
+
this.execHandler(
|
|
589
|
+
FieldPropsEnum.onBlur,
|
|
590
|
+
args,
|
|
591
|
+
action(() => {
|
|
592
|
+
this.$focused = false;
|
|
593
|
+
this.$blurred = true;
|
|
594
|
+
}),
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
onFocus = (...args: any) =>
|
|
598
|
+
this.execHandler(
|
|
599
|
+
FieldPropsEnum.onFocus,
|
|
600
|
+
args,
|
|
601
|
+
action(() => {
|
|
602
|
+
this.$focused = true;
|
|
603
|
+
this.$touched = true;
|
|
604
|
+
}),
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
onDrop = (...args: any) =>
|
|
608
|
+
this.execHandler(
|
|
609
|
+
FieldPropsEnum.onDrop,
|
|
610
|
+
args,
|
|
611
|
+
action(() => {
|
|
612
|
+
const e = args[0];
|
|
613
|
+
let files: unknown[] | null = null;
|
|
614
|
+
|
|
615
|
+
if (isEvent(e) && hasFiles(e)) {
|
|
616
|
+
files = Array.from(e.target.files);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const incoming: unknown[] = files || args;
|
|
620
|
+
const current: unknown[] = this.files || [];
|
|
621
|
+
// dedupe repeated file drops (fixes .vault/FIXES.md row 28)
|
|
622
|
+
const seen = new Set(
|
|
623
|
+
current.map((f: any) =>
|
|
624
|
+
[f?.name, f?.size, f?.lastModified].join("|")
|
|
625
|
+
),
|
|
626
|
+
);
|
|
627
|
+
|
|
628
|
+
this.files = [
|
|
629
|
+
...current,
|
|
630
|
+
...incoming.filter((f: any) => {
|
|
631
|
+
if (!f || typeof f.size !== "number") return true;
|
|
632
|
+
const signature = [f.name, f.size, f.lastModified].join("|");
|
|
633
|
+
if (seen.has(signature)) return false;
|
|
634
|
+
seen.add(signature);
|
|
635
|
+
return true;
|
|
636
|
+
}),
|
|
637
|
+
];
|
|
638
|
+
}),
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
onKeyDown = (...args: any) =>
|
|
642
|
+
this.execHandler(FieldPropsEnum.onKeyDown, args);
|
|
643
|
+
|
|
644
|
+
onKeyUp = (...args: any) => this.execHandler(FieldPropsEnum.onKeyUp, args);
|
|
645
|
+
|
|
646
|
+
setupField(
|
|
647
|
+
$key: string,
|
|
648
|
+
$path: string | undefined | null,
|
|
649
|
+
_$struct: string | undefined | null,
|
|
650
|
+
$data: any,
|
|
651
|
+
$props: any,
|
|
652
|
+
update: boolean,
|
|
653
|
+
): void {
|
|
654
|
+
this.key = $key;
|
|
655
|
+
this.path = $path;
|
|
656
|
+
this.id = this.state.options.get(OptionsEnum.uniqueId)?.apply(this, [this]);
|
|
657
|
+
const fallbackValueOption: any = this.state.options.get(
|
|
658
|
+
OptionsEnum.fallbackValue,
|
|
659
|
+
this,
|
|
660
|
+
);
|
|
661
|
+
const applyInputConverterOnInit: boolean = this.state.options.get(
|
|
662
|
+
OptionsEnum.applyInputConverterOnInit,
|
|
663
|
+
this,
|
|
664
|
+
);
|
|
665
|
+
const struct: string[] = this.state.struct();
|
|
666
|
+
const structPath: string = pathToStruct(this.path ?? "");
|
|
667
|
+
const isEmptyArray: boolean = isArrayFromStruct(struct, structPath);
|
|
668
|
+
|
|
669
|
+
const { $type, $input, $output, $converter, $converters, $computed } =
|
|
670
|
+
$props;
|
|
671
|
+
|
|
672
|
+
if (isPlainObject($data)) {
|
|
673
|
+
const { type, input, output, converter, converters, computed } = $data;
|
|
674
|
+
|
|
675
|
+
this.name = $data.name ?? String($key);
|
|
676
|
+
this.$type = $type || type || "text";
|
|
677
|
+
this.$converter = $try(
|
|
678
|
+
$converter,
|
|
679
|
+
$converters,
|
|
680
|
+
converter,
|
|
681
|
+
converters,
|
|
682
|
+
this.$converter,
|
|
683
|
+
);
|
|
684
|
+
this.$input = $try($input, input, this.$input);
|
|
685
|
+
this.$output = $try($output, output, this.$output);
|
|
686
|
+
|
|
687
|
+
const value = parseInput(
|
|
688
|
+
applyInputConverterOnInit ? this.$input : (val: any) => val,
|
|
689
|
+
{
|
|
690
|
+
fallbackValueOption,
|
|
691
|
+
isEmptyArray,
|
|
692
|
+
type: this.type,
|
|
693
|
+
unified: computed || $data.value,
|
|
694
|
+
separated: $computed || $props.$value,
|
|
695
|
+
fallback: $props.$initial,
|
|
696
|
+
},
|
|
697
|
+
);
|
|
698
|
+
|
|
699
|
+
this._value = retrieveFieldPropFunc(value);
|
|
700
|
+
this.$value =
|
|
701
|
+
typeof this._value === "function"
|
|
702
|
+
? applyFieldPropFunc(this, value)
|
|
703
|
+
: value;
|
|
704
|
+
|
|
705
|
+
this.$initial = parseInput((val: any) => val, {
|
|
706
|
+
fallbackValueOption,
|
|
707
|
+
isEmptyArray,
|
|
708
|
+
type: this.type,
|
|
709
|
+
unified: $data.initial,
|
|
710
|
+
separated: $props.$initial,
|
|
711
|
+
fallback: this.$value,
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
this.$default = setupDefaultProp(this, $data, $props, update, {
|
|
715
|
+
fallbackValueOption,
|
|
716
|
+
isEmptyArray,
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
setupFieldProps(this, $props, $data);
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/* The field IS the value here */
|
|
724
|
+
this.name = String($key);
|
|
725
|
+
this.$type = $type || "text";
|
|
726
|
+
this.$converter = $try($converter, $converters, this.$converter);
|
|
727
|
+
this.$input = $try($input, this.$input);
|
|
728
|
+
this.$output = $try($output, this.$output);
|
|
729
|
+
|
|
730
|
+
const value = parseInput(
|
|
731
|
+
applyInputConverterOnInit ? this.$input : (val: any) => val,
|
|
732
|
+
{
|
|
733
|
+
fallbackValueOption,
|
|
734
|
+
isEmptyArray,
|
|
735
|
+
type: this.type,
|
|
736
|
+
unified: $computed || $data,
|
|
737
|
+
separated: $computed || $props.$value,
|
|
738
|
+
},
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
this._value = retrieveFieldPropFunc(value);
|
|
742
|
+
this.$value =
|
|
743
|
+
typeof this._value === "function"
|
|
744
|
+
? applyFieldPropFunc(this, value)
|
|
745
|
+
: value;
|
|
746
|
+
|
|
747
|
+
this.$initial = parseInput((val: any) => val, {
|
|
748
|
+
fallbackValueOption,
|
|
749
|
+
isEmptyArray,
|
|
750
|
+
type: this.type,
|
|
751
|
+
unified: $data,
|
|
752
|
+
separated: $props.$initial,
|
|
753
|
+
fallback: this.$value,
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
this.$default = setupDefaultProp(this, $data, $props, update, {
|
|
757
|
+
fallbackValueOption,
|
|
758
|
+
isEmptyArray,
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
setupFieldProps(this, $props, $data);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
getComputedProp(key: string): any {
|
|
765
|
+
if (this.incremental || this.hasNestedFields) {
|
|
766
|
+
return key === FieldPropsEnum.value
|
|
767
|
+
? this.get(key, false)
|
|
768
|
+
: untracked(() => this.get(key, false));
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// @ts-ignore
|
|
772
|
+
const val = this[`$${key}`];
|
|
773
|
+
|
|
774
|
+
if (Array.isArray(val) || isObservableArray(val)) {
|
|
775
|
+
return [].slice.call(val);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
return toJS(val);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// checkValidationPlugins(): void {
|
|
782
|
+
// const { drivers } = this.state.form.validator;
|
|
783
|
+
// const form = this.state.form.name ? `${this.state.form.name}/` : "";
|
|
784
|
+
|
|
785
|
+
// if (isNil(drivers.dvr) && !isNil(this.rules)) {
|
|
786
|
+
// throw new Error(
|
|
787
|
+
// `The DVR validation rules are defined but no DVR plugin provided. Field: "${
|
|
788
|
+
// form + this.path
|
|
789
|
+
// }".`
|
|
790
|
+
// );
|
|
791
|
+
// }
|
|
792
|
+
|
|
793
|
+
// if (isNil(drivers.vjf) && !isNil(this.validators)) {
|
|
794
|
+
// throw new Error(
|
|
795
|
+
// `The VJF validators functions are defined but no VJF plugin provided. Field: "${
|
|
796
|
+
// form + this.path
|
|
797
|
+
// }".`
|
|
798
|
+
// );
|
|
799
|
+
// }
|
|
800
|
+
// }
|
|
801
|
+
|
|
802
|
+
initNestedFields(field: any, update: boolean): void {
|
|
803
|
+
const fields = isNil(field) ? null : field.fields;
|
|
804
|
+
|
|
805
|
+
if (Array.isArray(fields) && !isEmpty(fields)) {
|
|
806
|
+
this.hasInitialNestedFields = true;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
this.initFields({ fields }, update);
|
|
810
|
+
|
|
811
|
+
if (!update && Array.isArray(fields) && isEmpty(fields)) {
|
|
812
|
+
if (Array.isArray(this.value) && !isEmpty(this.value)) {
|
|
813
|
+
this.hasInitialNestedFields = true;
|
|
814
|
+
this.initFields({ fields, values: this.value }, update);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
invalidate(
|
|
820
|
+
message: string,
|
|
821
|
+
deep: boolean = true,
|
|
822
|
+
async: boolean = false,
|
|
823
|
+
): void {
|
|
824
|
+
if (async === true) {
|
|
825
|
+
this.errorAsync = message;
|
|
826
|
+
this.showErrors(true, deep);
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
if (Array.isArray(message)) {
|
|
831
|
+
this.validationErrorStack = message;
|
|
832
|
+
this.showErrors(true, deep);
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
this.validationErrorStack.unshift(message);
|
|
837
|
+
this.showErrors(true, deep);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
setValidationAsyncData(
|
|
841
|
+
valid: boolean = false,
|
|
842
|
+
message: string | null = null,
|
|
843
|
+
): void {
|
|
844
|
+
this.validationAsyncData = { valid, message };
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
resetValidation(deep: boolean = false): void {
|
|
848
|
+
this.showError = false;
|
|
849
|
+
this.errorSync = null;
|
|
850
|
+
this.errorAsync = null;
|
|
851
|
+
this.validationAsyncData = { valid: true, message: null };
|
|
852
|
+
this.validationFunctionsData = [];
|
|
853
|
+
this.validationErrorStack = [];
|
|
854
|
+
Promise.resolve().then(
|
|
855
|
+
action(() => {
|
|
856
|
+
this.$resetting = false;
|
|
857
|
+
this.$clearing = false;
|
|
858
|
+
}),
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
deep && this.each((field: FieldInterface) => field.resetValidation(deep));
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
clear(deep: boolean = true, execHook: boolean = true): void {
|
|
865
|
+
execHook && this.execHook(FieldPropsEnum.onClear);
|
|
866
|
+
this.$clearing = true;
|
|
867
|
+
this.$touched = false;
|
|
868
|
+
this.$blurred = false;
|
|
869
|
+
this.$changed = 0;
|
|
870
|
+
this.files = undefined;
|
|
871
|
+
|
|
872
|
+
this.$value = defaultValue({
|
|
873
|
+
fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue),
|
|
874
|
+
value: this.$value,
|
|
875
|
+
nullable: this.$nullable,
|
|
876
|
+
type: this.type,
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
deep && this.each((field: FieldInterface) => field.clear(deep));
|
|
880
|
+
|
|
881
|
+
this.state.options.get(OptionsEnum.validateOnClear, this)
|
|
882
|
+
? this.validate({
|
|
883
|
+
showErrors: this.state.options.get(
|
|
884
|
+
OptionsEnum.showErrorsOnClear,
|
|
885
|
+
this,
|
|
886
|
+
),
|
|
887
|
+
})
|
|
888
|
+
: this.resetValidation(deep);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
reset(deep: boolean = true, execHook: boolean = true): void {
|
|
892
|
+
execHook && this.execHook(FieldPropsEnum.onReset);
|
|
893
|
+
this.$resetting = true;
|
|
894
|
+
this.$touched = false;
|
|
895
|
+
this.$blurred = false;
|
|
896
|
+
this.$changed = 0;
|
|
897
|
+
this.files = undefined;
|
|
898
|
+
|
|
899
|
+
const useDefaultValue = this.$default !== this.$initial;
|
|
900
|
+
if (useDefaultValue) this.value = this.$default;
|
|
901
|
+
if (!useDefaultValue) this.value = this.$initial;
|
|
902
|
+
|
|
903
|
+
deep && this.each((field: FieldInterface) => field.reset(deep));
|
|
904
|
+
|
|
905
|
+
this.state.options.get(OptionsEnum.validateOnReset, this)
|
|
906
|
+
? this.validate({
|
|
907
|
+
showErrors: this.state.options.get(
|
|
908
|
+
OptionsEnum.showErrorsOnReset,
|
|
909
|
+
this,
|
|
910
|
+
),
|
|
911
|
+
})
|
|
912
|
+
: this.resetValidation(deep);
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
focus(): void {
|
|
916
|
+
if (this.ref && !this.focused) this.ref.focus();
|
|
917
|
+
this.$focused = true;
|
|
918
|
+
this.$touched = true;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
blur(): void {
|
|
922
|
+
if (this.ref && this.focused) this.ref.blur();
|
|
923
|
+
this.$focused = false;
|
|
924
|
+
this.$blurred = true;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
trim(): void {
|
|
928
|
+
if (typeof this.value !== 'string') return;
|
|
929
|
+
this.$value = this.value.trim();
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
showErrors(show: boolean = true, deep: boolean = true): void {
|
|
933
|
+
this.showError = show;
|
|
934
|
+
this.errorSync = this.validationErrorStack.length ? (this.validationErrorStack[0] as string) : null;
|
|
935
|
+
this.errorAsync = !this.validationAsyncData.valid
|
|
936
|
+
? this.validationAsyncData.message
|
|
937
|
+
: null;
|
|
938
|
+
deep && this.each((field: FieldInterface) => field.showErrors(show, deep));
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
observeValidationOnBlur(): void {
|
|
942
|
+
const opt = this.state.options;
|
|
943
|
+
if (opt.get(OptionsEnum.validateOnBlur, this)) {
|
|
944
|
+
this.disposeValidationOnBlur = observe(
|
|
945
|
+
this,
|
|
946
|
+
"$focused",
|
|
947
|
+
(change) =>
|
|
948
|
+
change.newValue === false &&
|
|
949
|
+
this.debouncedValidation({
|
|
950
|
+
showErrors: opt.get(OptionsEnum.showErrorsOnBlur, this),
|
|
951
|
+
}),
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
observeValidationOnChange(): void {
|
|
957
|
+
const opt = this.state.options;
|
|
958
|
+
if (opt.get(OptionsEnum.validateOnChange, this)) {
|
|
959
|
+
this.disposeValidationOnChange = observe(
|
|
960
|
+
this,
|
|
961
|
+
"$value",
|
|
962
|
+
() =>
|
|
963
|
+
!this.actionRunning &&
|
|
964
|
+
this.debouncedValidation({
|
|
965
|
+
showErrors: opt.get(OptionsEnum.showErrorsOnChange, this),
|
|
966
|
+
}),
|
|
967
|
+
);
|
|
968
|
+
} else if (
|
|
969
|
+
opt.get(OptionsEnum.validateOnChangeAfterInitialBlur, this) ||
|
|
970
|
+
opt.get(OptionsEnum.validateOnChangeAfterSubmit, this)
|
|
971
|
+
) {
|
|
972
|
+
this.disposeValidationOnChange = observe(
|
|
973
|
+
this,
|
|
974
|
+
"$value",
|
|
975
|
+
() =>
|
|
976
|
+
!this.actionRunning &&
|
|
977
|
+
((opt.get(OptionsEnum.validateOnChangeAfterInitialBlur, this) &&
|
|
978
|
+
this.blurred) ||
|
|
979
|
+
(opt.get(OptionsEnum.validateOnChangeAfterSubmit, this) &&
|
|
980
|
+
this.state.form.submitted)) &&
|
|
981
|
+
this.debouncedValidation({
|
|
982
|
+
showErrors: opt.get(OptionsEnum.showErrorsOnChange, this),
|
|
983
|
+
}),
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
initMOBXEvent(type: string): void {
|
|
989
|
+
const arr: any[] = (this as any)[`$${type}`];
|
|
990
|
+
if (!Array.isArray(arr)) return;
|
|
991
|
+
|
|
992
|
+
let fn: any;
|
|
993
|
+
if (type === FieldPropsEnum.observers) fn = this.observe;
|
|
994
|
+
if (type === FieldPropsEnum.interceptors) fn = this.intercept;
|
|
995
|
+
arr.map((obj: any) => fn(omit(obj, FieldPropsEnum.path)));
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
bind(props = {}) {
|
|
999
|
+
return {
|
|
1000
|
+
...this.state.bindings.load(this, this.bindings, props),
|
|
1001
|
+
ref: ($ref: any) => (this.$ref = $ref),
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
override update(fields: any): void {
|
|
1006
|
+
if (!isPlainObject(fields)) {
|
|
1007
|
+
throw new Error("The update() method accepts only plain objects.");
|
|
1008
|
+
}
|
|
1009
|
+
const fallback = this.state.options.get(OptionsEnum.fallback, this);
|
|
1010
|
+
const applyInputConverterOnUpdate = this.state.options.get(
|
|
1011
|
+
OptionsEnum.applyInputConverterOnUpdate,
|
|
1012
|
+
this,
|
|
1013
|
+
);
|
|
1014
|
+
const x = this.state
|
|
1015
|
+
.struct()
|
|
1016
|
+
.findIndex((s) =>
|
|
1017
|
+
s.startsWith((this.path ?? "").replace(/\.\d+\./, "[].") + "[]"),
|
|
1018
|
+
);
|
|
1019
|
+
if (!fallback && this.fields.size === 0 && x < 0) {
|
|
1020
|
+
this.value = parseInput(
|
|
1021
|
+
applyInputConverterOnUpdate ? this.$input : (val: any) => val,
|
|
1022
|
+
{
|
|
1023
|
+
fallbackValueOption: this.state.options.get(
|
|
1024
|
+
OptionsEnum.fallbackValue,
|
|
1025
|
+
this,
|
|
1026
|
+
),
|
|
1027
|
+
separated: fields,
|
|
1028
|
+
},
|
|
1029
|
+
);
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
super.update(fields);
|
|
1033
|
+
}
|
|
1034
|
+
}
|