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