@gravity-ui/dynamic-forms 3.1.0 → 3.3.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 (57) hide show
  1. package/build/cjs/lib/core/components/Form/Controller/Controller.js +120 -0
  2. package/build/cjs/lib/core/components/Form/Controller/index.js +4 -0
  3. package/build/cjs/lib/core/components/Form/Controller/types.js +2 -0
  4. package/build/cjs/lib/core/components/Form/Controller/utils.js +305 -0
  5. package/build/cjs/lib/core/components/Form/DynamicField.js +6 -4
  6. package/build/cjs/lib/core/components/Form/constants.js +2 -1
  7. package/build/cjs/lib/core/components/Form/hooks/index.js +3 -4
  8. package/build/cjs/lib/core/components/Form/hooks/useMutateDFState.js +6 -0
  9. package/build/cjs/lib/core/components/Form/hooks/useMutators.js +43 -0
  10. package/build/cjs/lib/core/components/Form/hooks/useStoreValue.js +6 -0
  11. package/build/cjs/lib/core/components/Form/index.js +4 -0
  12. package/build/cjs/lib/core/components/Form/types/index.js +1 -0
  13. package/build/cjs/lib/core/components/Form/types/mutators.js +2 -0
  14. package/build/esm/lib/core/components/Form/Controller/Controller.d.ts +10 -0
  15. package/build/esm/lib/core/components/Form/Controller/Controller.js +115 -0
  16. package/build/esm/lib/core/components/Form/Controller/index.d.ts +1 -0
  17. package/build/esm/lib/core/components/Form/Controller/index.js +1 -0
  18. package/build/esm/lib/core/components/Form/Controller/types.d.ts +98 -0
  19. package/build/esm/lib/core/components/Form/Controller/types.js +1 -0
  20. package/build/esm/lib/core/components/Form/Controller/utils.d.ts +26 -0
  21. package/build/esm/lib/core/components/Form/Controller/utils.js +291 -0
  22. package/build/esm/lib/core/components/Form/DynamicField.d.ts +2 -2
  23. package/build/esm/lib/core/components/Form/DynamicField.js +7 -5
  24. package/build/esm/lib/core/components/Form/constants.d.ts +1 -0
  25. package/build/esm/lib/core/components/Form/constants.js +1 -0
  26. package/build/esm/lib/core/components/Form/hooks/index.d.ts +3 -4
  27. package/build/esm/lib/core/components/Form/hooks/index.js +3 -4
  28. package/build/esm/lib/core/components/Form/hooks/useMutateDFState.d.ts +1 -0
  29. package/build/esm/lib/core/components/Form/hooks/useMutateDFState.js +2 -0
  30. package/build/esm/lib/core/components/Form/hooks/useMutators.d.ts +5 -0
  31. package/build/esm/lib/core/components/Form/hooks/useMutators.js +38 -0
  32. package/build/esm/lib/core/components/Form/hooks/useStoreValue.d.ts +1 -0
  33. package/build/esm/lib/core/components/Form/hooks/useStoreValue.js +2 -0
  34. package/build/esm/lib/core/components/Form/index.d.ts +1 -0
  35. package/build/esm/lib/core/components/Form/index.js +1 -0
  36. package/build/esm/lib/core/components/Form/types/context.d.ts +4 -2
  37. package/build/esm/lib/core/components/Form/types/index.d.ts +1 -0
  38. package/build/esm/lib/core/components/Form/types/index.js +1 -0
  39. package/build/esm/lib/core/components/Form/types/mirror.d.ts +3 -5
  40. package/build/esm/lib/core/components/Form/types/mutators.d.ts +18 -0
  41. package/build/esm/lib/core/components/Form/types/mutators.js +1 -0
  42. package/package.json +1 -1
  43. package/build/cjs/lib/core/components/Form/Controller.js +0 -33
  44. package/build/cjs/lib/core/components/Form/hooks/useComponents.js +0 -40
  45. package/build/cjs/lib/core/components/Form/hooks/useField.js +0 -167
  46. package/build/cjs/lib/core/components/Form/hooks/useRender.js +0 -28
  47. package/build/cjs/lib/core/components/Form/hooks/useValidate.js +0 -32
  48. package/build/esm/lib/core/components/Form/Controller.d.ts +0 -10
  49. package/build/esm/lib/core/components/Form/Controller.js +0 -28
  50. package/build/esm/lib/core/components/Form/hooks/useComponents.d.ts +0 -6
  51. package/build/esm/lib/core/components/Form/hooks/useComponents.js +0 -35
  52. package/build/esm/lib/core/components/Form/hooks/useField.d.ts +0 -14
  53. package/build/esm/lib/core/components/Form/hooks/useField.js +0 -162
  54. package/build/esm/lib/core/components/Form/hooks/useRender.d.ts +0 -9
  55. package/build/esm/lib/core/components/Form/hooks/useRender.js +0 -23
  56. package/build/esm/lib/core/components/Form/hooks/useValidate.d.ts +0 -3
  57. package/build/esm/lib/core/components/Form/hooks/useValidate.js +0 -27
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Controller = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importDefault(require("react"));
6
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
+ const hooks_1 = require("../hooks");
8
+ const utils_1 = require("./utils");
9
+ const Controller = ({ spec: _spec, name, value: valueFromParent, parentOnChange, parentOnUnmount, }) => {
10
+ const { config, tools, mutators, __mirror } = (0, hooks_1.useDynamicFormsCtx)();
11
+ const firstRenderRef = react_1.default.useRef(true);
12
+ const [store, setStore] = react_1.default.useState((0, utils_1.initializeStore)({
13
+ name,
14
+ spec: _spec,
15
+ mutators,
16
+ config,
17
+ valueFromParent,
18
+ tools,
19
+ parentOnChange,
20
+ parentOnUnmount,
21
+ }));
22
+ const { methods, fieldMethods } = react_1.default.useMemo(() => {
23
+ const fieldMethods = (0, utils_1.getFieldMethods)();
24
+ const onChange = (valOrSetter, childErrors, errorMutator) => {
25
+ setStore((store) => fieldMethods.onChange(store, { valOrSetter, childErrors, errorMutator }));
26
+ };
27
+ const onDrop = () => {
28
+ setStore((store) => fieldMethods.onDrop(store, undefined));
29
+ };
30
+ const onBlur = () => {
31
+ setStore((store) => fieldMethods.onBlur(store, undefined));
32
+ };
33
+ const onFocus = () => {
34
+ setStore((store) => fieldMethods.onFocus(store, undefined));
35
+ };
36
+ const parentOnUnmount = (childName) => {
37
+ setStore((store) => fieldMethods.parentOnUnmount(store, childName));
38
+ };
39
+ const onItemAdd = (value) => {
40
+ setStore((store) => fieldMethods.onItemAdd(store, value));
41
+ };
42
+ const onItemRemove = (idx) => {
43
+ setStore((store) => fieldMethods.onItemRemove(store, idx));
44
+ };
45
+ return {
46
+ methods: {
47
+ onChange,
48
+ onDrop,
49
+ onBlur,
50
+ onFocus,
51
+ parentOnUnmount,
52
+ onItemAdd,
53
+ onItemRemove,
54
+ },
55
+ fieldMethods,
56
+ };
57
+ }, [setStore]);
58
+ const renderProps = react_1.default.useMemo(() => ({
59
+ input: {
60
+ name: store.name,
61
+ value: store.state.value,
62
+ onChange: methods.onChange,
63
+ onBlur: methods.onBlur,
64
+ onFocus: methods.onFocus,
65
+ onDrop: methods.onDrop,
66
+ parentOnUnmount: methods.parentOnUnmount,
67
+ },
68
+ arrayInput: {
69
+ name: store.name,
70
+ value: store.state.value,
71
+ onItemAdd: methods.onItemAdd,
72
+ onItemRemove: methods.onItemRemove,
73
+ onDrop: methods.onDrop,
74
+ },
75
+ meta: Object.assign(Object.assign({}, lodash_1.default.omit(store.state, 'value')), { submitFailed: store.tools.submitFailed }),
76
+ }), [methods, store.name, store.state, store.tools.submitFailed]);
77
+ const withSearch = (0, hooks_1.useSearch)(store.spec, store.state.value, store.name);
78
+ (0, hooks_1.useControllerMirror)(store.name, {
79
+ useField: renderProps,
80
+ useSearch: withSearch,
81
+ }, __mirror);
82
+ react_1.default.useEffect(() => {
83
+ var _a;
84
+ (_a = store.afterStoreUpdateCB) === null || _a === void 0 ? void 0 : _a.call(store);
85
+ }, [store.afterStoreUpdateCB]);
86
+ react_1.default.useEffect(() => {
87
+ if (!firstRenderRef.current) {
88
+ (0, utils_1.updateStore)({
89
+ store,
90
+ setStore,
91
+ spec: _spec,
92
+ name,
93
+ parentOnChange,
94
+ parentOnUnmount,
95
+ mutators,
96
+ config,
97
+ tools,
98
+ methodOnChange: fieldMethods.onChange,
99
+ valueFromParent,
100
+ });
101
+ }
102
+ }, [
103
+ _spec,
104
+ name,
105
+ parentOnChange,
106
+ parentOnUnmount,
107
+ mutators,
108
+ config,
109
+ tools.onChange,
110
+ tools.onUnmount,
111
+ ]);
112
+ react_1.default.useEffect(() => {
113
+ firstRenderRef.current = false;
114
+ return () => {
115
+ (0, utils_1.callUnmout)(store);
116
+ };
117
+ }, []);
118
+ return withSearch(store.render(renderProps));
119
+ };
120
+ exports.Controller = Controller;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./Controller"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,305 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateStore = exports.initializeStore = exports.getFieldMethods = exports.getFieldInitials = exports.getValidate = exports.getRender = exports.getComponents = exports.getSpec = exports.callUnmout = exports.updateParentStore = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importDefault(require("react"));
6
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
+ const react_is_1 = require("react-is");
8
+ const constants_1 = require("../../../constants");
9
+ const helpers_1 = require("../../../helpers");
10
+ const constants_2 = require("../constants");
11
+ const utils_1 = require("../utils");
12
+ const isErrorMutatorCorrect = (errorMutator) => errorMutator !== constants_2.EMPTY_MUTATOR && (lodash_1.default.isString(errorMutator) || lodash_1.default.isBoolean(errorMutator));
13
+ const isValueMutatorCorrect = (valueMutator, spec) => valueMutator !== constants_2.EMPTY_MUTATOR &&
14
+ (typeof valueMutator === spec.type ||
15
+ (lodash_1.default.isArray(valueMutator) && spec.type === constants_1.SpecTypes.Array));
16
+ const updateParentStore = (store) => {
17
+ (store.parentOnChange ? store.parentOnChange : store.tools.onChange)(store.name, store.state.value, Object.assign(Object.assign({}, store.state.childErrors), { [store.name]: store.state.error }));
18
+ };
19
+ exports.updateParentStore = updateParentStore;
20
+ const callUnmout = (store) => {
21
+ (store.parentOnUnmount ? store.parentOnUnmount : store.tools.onUnmount)(store.name);
22
+ };
23
+ exports.callUnmout = callUnmout;
24
+ const getSpec = ({ name, spec, mutators, }) => {
25
+ const mutator = lodash_1.default.get(mutators.spec, name, constants_2.EMPTY_MUTATOR);
26
+ if (mutator !== constants_2.EMPTY_MUTATOR) {
27
+ const mutatedSpec = lodash_1.default.merge(lodash_1.default.cloneDeep(spec), mutator);
28
+ if ((0, helpers_1.isCorrectSpec)(mutatedSpec)) {
29
+ return mutatedSpec;
30
+ }
31
+ }
32
+ return spec;
33
+ };
34
+ exports.getSpec = getSpec;
35
+ const getComponents = ({ spec, config, }) => {
36
+ const result = {};
37
+ if ((0, utils_1.isCorrectConfig)(config) && (0, helpers_1.isCorrectSpec)(spec)) {
38
+ const { inputs, layouts } = config[spec.type];
39
+ if (inputs) {
40
+ const entity = inputs[spec.viewSpec.type];
41
+ if ((0, react_is_1.isValidElementType)(entity === null || entity === void 0 ? void 0 : entity.Component)) {
42
+ result.inputEntity = entity;
43
+ }
44
+ }
45
+ if (layouts && lodash_1.default.isString(spec.viewSpec.layout)) {
46
+ const Component = layouts[spec.viewSpec.layout];
47
+ if ((0, react_is_1.isValidElementType)(Component)) {
48
+ result.Layout = Component;
49
+ }
50
+ }
51
+ }
52
+ return result;
53
+ };
54
+ exports.getComponents = getComponents;
55
+ const getRender = ({ name, spec, inputEntity, Layout, }) => {
56
+ const render = (props) => {
57
+ if (inputEntity && (0, helpers_1.isCorrectSpec)(spec) && lodash_1.default.isString(name)) {
58
+ if (!spec.viewSpec.hidden) {
59
+ if (inputEntity.independent) {
60
+ const InputComponent = inputEntity.Component;
61
+ return react_1.default.createElement(InputComponent, Object.assign({ spec: spec, name: name, Layout: Layout }, props));
62
+ }
63
+ const InputComponent = inputEntity.Component;
64
+ const input = react_1.default.createElement(InputComponent, Object.assign({ spec: spec, name: name }, props));
65
+ if (Layout) {
66
+ return (react_1.default.createElement(Layout, Object.assign({ spec: spec, name: name }, props), input));
67
+ }
68
+ return input;
69
+ }
70
+ }
71
+ return null;
72
+ };
73
+ return render;
74
+ };
75
+ exports.getRender = getRender;
76
+ const getValidate = ({ spec, config, }) => {
77
+ let validate = () => undefined;
78
+ if ((0, utils_1.isCorrectConfig)(config) && (0, helpers_1.isCorrectSpec)(spec)) {
79
+ const { validators } = config[spec.type];
80
+ if (validators) {
81
+ if ((!lodash_1.default.isString(spec.validator) || !spec.validator.length) &&
82
+ lodash_1.default.isFunction(validators.base)) {
83
+ validate = (value) => validators.base(spec, value);
84
+ }
85
+ if (lodash_1.default.isString(spec.validator) && lodash_1.default.isFunction(validators[spec.validator])) {
86
+ validate = (value) => validators[spec.validator](spec, value);
87
+ }
88
+ }
89
+ }
90
+ return validate;
91
+ };
92
+ exports.getValidate = getValidate;
93
+ const getFieldInitials = ({ name, spec, valueFromParent, initialValue, validate, mutators, }) => {
94
+ const valueMutator = (0, utils_1.transformArrIn)(lodash_1.default.get(mutators.values, name, constants_2.EMPTY_MUTATOR));
95
+ let value = lodash_1.default.cloneDeep(valueFromParent);
96
+ if (isValueMutatorCorrect(valueMutator, spec)) {
97
+ value = valueMutator;
98
+ }
99
+ if (lodash_1.default.isNil(value)) {
100
+ if (spec.defaultValue) {
101
+ value = (0, utils_1.transformArrIn)(spec.defaultValue);
102
+ }
103
+ // if the spec with type array or object, and this spec has "required === true",
104
+ // we immediately exclude empty value
105
+ else if (spec.required) {
106
+ if ((0, helpers_1.isArraySpec)(spec)) {
107
+ value = { [constants_2.OBJECT_ARRAY_FLAG]: true, [constants_2.OBJECT_ARRAY_CNT]: 0 };
108
+ }
109
+ else if ((0, helpers_1.isObjectSpec)(spec)) {
110
+ value = {};
111
+ }
112
+ }
113
+ }
114
+ let errorMutator = lodash_1.default.get(mutators.errors, name, constants_2.EMPTY_MUTATOR);
115
+ if (!isErrorMutatorCorrect(errorMutator)) {
116
+ errorMutator = undefined;
117
+ }
118
+ const error = (validate === null || validate === void 0 ? void 0 : validate((0, utils_1.transformArrOut)(value))) || errorMutator;
119
+ const dirty = !lodash_1.default.isEqual(value, initialValue);
120
+ return {
121
+ initialValue,
122
+ active: false,
123
+ dirty,
124
+ error,
125
+ invalid: Boolean(error),
126
+ modified: dirty,
127
+ pristine: true,
128
+ touched: false,
129
+ valid: !error,
130
+ value,
131
+ visited: false,
132
+ childErrors: {},
133
+ };
134
+ };
135
+ exports.getFieldInitials = getFieldInitials;
136
+ const getFieldMethods = () => {
137
+ const onChange = (store, { valOrSetter, childErrors, errorMutator }) => {
138
+ const { state, validate, spec } = store;
139
+ const _value = lodash_1.default.isFunction(valOrSetter) ? valOrSetter(state.value) : valOrSetter;
140
+ const error = (validate === null || validate === void 0 ? void 0 : validate((0, utils_1.transformArrOut)(_value))) || errorMutator;
141
+ let value = (0, utils_1.transformArrIn)(_value);
142
+ if ((0, helpers_1.isNumberSpec)(spec) && !error) {
143
+ value = (value ? Number(value) : undefined);
144
+ }
145
+ let newChildErrors = Object.assign({}, state.childErrors);
146
+ if (childErrors) {
147
+ const nearestChildName = lodash_1.default.keys(childErrors).sort((a, b) => a.length - b.length)[0];
148
+ if (nearestChildName) {
149
+ const existingСhildNames = lodash_1.default.keys(newChildErrors).filter((childName) => childName.startsWith(nearestChildName));
150
+ newChildErrors = Object.assign(Object.assign({}, lodash_1.default.omit(newChildErrors, existingСhildNames)), childErrors);
151
+ }
152
+ }
153
+ const nextStore = Object.assign(Object.assign({}, store), { state: Object.assign(Object.assign({}, store.state), { dirty: !lodash_1.default.isEqual(value, state.initialValue), error, invalid: Boolean(error), modified: true, pristine: value === state.initialValue, touched: true, valid: !error, value, visited: true, childErrors: newChildErrors }) });
154
+ nextStore.afterStoreUpdateCB = () => (0, exports.updateParentStore)(nextStore);
155
+ return nextStore;
156
+ };
157
+ const onDrop = (store) => {
158
+ const { name } = store;
159
+ if ((0, utils_1.isArrayItem)(name)) {
160
+ const afterStoreUpdateCB = () => (0, exports.callUnmout)(store);
161
+ return Object.assign(Object.assign({}, store), { afterStoreUpdateCB });
162
+ }
163
+ return onChange(store, {
164
+ valOrSetter: undefined,
165
+ childErrors: { [name]: false },
166
+ });
167
+ };
168
+ const onBlur = (store) => {
169
+ return Object.assign(Object.assign({}, store), { state: Object.assign(Object.assign({}, store.state), { active: false, touched: true }) });
170
+ };
171
+ const onFocus = (store) => {
172
+ return Object.assign(Object.assign({}, store), { state: Object.assign(Object.assign({}, store.state), { active: true, visited: true }) });
173
+ };
174
+ const parentOnUnmount = (store, childName) => {
175
+ const { name, spec } = store;
176
+ if ((0, helpers_1.isArraySpec)(spec) || (0, helpers_1.isObjectSpec)(spec)) {
177
+ return onChange(store, {
178
+ valOrSetter: (currentValue) => currentValue
179
+ ? lodash_1.default.omit(currentValue, childName.split(`${name}.`)[1])
180
+ : currentValue,
181
+ childErrors: { [childName]: false },
182
+ });
183
+ }
184
+ return store;
185
+ };
186
+ const onItemAdd = (store, _value) => {
187
+ const { validate, state } = store;
188
+ const stateValue = (state.value || {
189
+ [constants_2.OBJECT_ARRAY_FLAG]: true,
190
+ [constants_2.OBJECT_ARRAY_CNT]: 0,
191
+ });
192
+ const value = Object.assign(Object.assign({}, stateValue), { [`<${stateValue[constants_2.OBJECT_ARRAY_CNT]}>`]: (0, utils_1.transformArrIn)(_value), [constants_2.OBJECT_ARRAY_CNT]: stateValue[constants_2.OBJECT_ARRAY_CNT] + 1 });
193
+ const error = validate === null || validate === void 0 ? void 0 : validate((0, utils_1.transformArrOut)(value));
194
+ const nextStore = Object.assign(Object.assign({}, store), { state: Object.assign(Object.assign({}, store.state), { dirty: !lodash_1.default.isEqual(value, store.state.initialValue), error, invalid: Boolean(error), modified: true, pristine: value === store.state.initialValue, touched: true, valid: !error, value, visited: true }) });
195
+ nextStore.afterStoreUpdateCB = () => (0, exports.updateParentStore)(nextStore);
196
+ return nextStore;
197
+ };
198
+ const onItemRemove = (store, idx) => {
199
+ return parentOnUnmount(store, `${store.name}.<${idx}>`);
200
+ };
201
+ return {
202
+ onChange,
203
+ onDrop,
204
+ onBlur,
205
+ onFocus,
206
+ parentOnUnmount,
207
+ onItemAdd,
208
+ onItemRemove,
209
+ };
210
+ };
211
+ exports.getFieldMethods = getFieldMethods;
212
+ const initializeStore = ({ name, spec: _spec, mutators, config, valueFromParent, tools, parentOnChange, parentOnUnmount, }) => {
213
+ const spec = (0, exports.getSpec)({ name, spec: _spec, mutators });
214
+ const components = (0, exports.getComponents)({ spec, config });
215
+ const render = (0, exports.getRender)(Object.assign({ name, spec }, components));
216
+ const validate = (0, exports.getValidate)({ spec, config });
217
+ const state = (0, exports.getFieldInitials)({
218
+ name,
219
+ spec,
220
+ valueFromParent,
221
+ validate,
222
+ mutators,
223
+ initialValue: lodash_1.default.get(tools.initialValue, name),
224
+ });
225
+ const initialsStore = {
226
+ name,
227
+ spec,
228
+ initialSpec: _spec,
229
+ config,
230
+ tools,
231
+ mutators,
232
+ render,
233
+ validate,
234
+ parentOnChange,
235
+ parentOnUnmount,
236
+ state,
237
+ };
238
+ if (!lodash_1.default.isEqual(valueFromParent, state.value) || state.error) {
239
+ initialsStore.afterStoreUpdateCB = () => (0, exports.updateParentStore)(initialsStore);
240
+ }
241
+ return initialsStore;
242
+ };
243
+ exports.initializeStore = initializeStore;
244
+ const updateStore = ({ store, setStore, spec: _spec, name, parentOnChange, parentOnUnmount, mutators, valueFromParent, config, tools, methodOnChange, }) => {
245
+ const storeSpecMutator = lodash_1.default.get(store.mutators.spec, store.name, constants_2.EMPTY_MUTATOR);
246
+ const storeValueMutator = lodash_1.default.get(store.mutators.values, store.name, constants_2.EMPTY_MUTATOR);
247
+ const storeErrorMutator = lodash_1.default.get(store.mutators.errors, store.name, constants_2.EMPTY_MUTATOR);
248
+ const specMutator = lodash_1.default.get(mutators.errors, name, constants_2.EMPTY_MUTATOR);
249
+ const valueMutator = lodash_1.default.get(mutators.values, name, constants_2.EMPTY_MUTATOR);
250
+ const errorMutator = lodash_1.default.get(mutators.errors, name, constants_2.EMPTY_MUTATOR);
251
+ const valueMutatorUpdated = isValueMutatorCorrect(valueMutator, (0, exports.getSpec)({ name, spec: _spec, mutators })) &&
252
+ valueMutator !== storeValueMutator;
253
+ const errorMutatorUpdated = isErrorMutatorCorrect(errorMutator) && errorMutator !== storeErrorMutator;
254
+ const updateState = valueMutatorUpdated || errorMutatorUpdated;
255
+ const updateNonCritical = parentOnChange !== store.parentOnChange ||
256
+ parentOnUnmount !== store.parentOnUnmount ||
257
+ tools.onChange !== store.tools.onChange ||
258
+ tools.onUnmount !== store.tools.onUnmount;
259
+ const updateAllStore = !lodash_1.default.isEqual(_spec, store.initialSpec) ||
260
+ config !== store.config ||
261
+ (specMutator !== constants_2.EMPTY_MUTATOR && specMutator !== storeSpecMutator);
262
+ const updateAllStoreAndClearParentValues = name !== store.name;
263
+ if (updateAllStoreAndClearParentValues) {
264
+ (0, exports.callUnmout)(store);
265
+ setStore((0, exports.initializeStore)({
266
+ name,
267
+ spec: _spec,
268
+ mutators,
269
+ config,
270
+ valueFromParent,
271
+ tools,
272
+ parentOnChange,
273
+ parentOnUnmount,
274
+ }));
275
+ }
276
+ else if (updateAllStore) {
277
+ let nextStore = Object.assign(Object.assign({}, (0, exports.initializeStore)({
278
+ name,
279
+ spec: _spec,
280
+ mutators,
281
+ config,
282
+ valueFromParent,
283
+ tools,
284
+ parentOnChange,
285
+ parentOnUnmount,
286
+ })), { state: store.state });
287
+ if (updateState) {
288
+ nextStore = methodOnChange(nextStore, Object.assign({ valOrSetter: (value) => valueMutatorUpdated ? valueMutator : value }, (errorMutatorUpdated ? { errorMutator } : {})));
289
+ }
290
+ setStore(nextStore);
291
+ }
292
+ else if (updateNonCritical) {
293
+ let nextStore = Object.assign(Object.assign({}, store), { parentOnChange,
294
+ parentOnUnmount,
295
+ tools });
296
+ if (updateState) {
297
+ nextStore = methodOnChange(nextStore, Object.assign({ valOrSetter: (value) => valueMutatorUpdated ? valueMutator : value }, (errorMutatorUpdated ? { errorMutator } : {})));
298
+ }
299
+ setStore(nextStore);
300
+ }
301
+ else if (updateState) {
302
+ setStore(methodOnChange(store, Object.assign({ valOrSetter: (value) => valueMutatorUpdated ? valueMutator : value }, (errorMutatorUpdated ? { errorMutator } : {}))));
303
+ }
304
+ };
305
+ exports.updateStore = updateStore;
@@ -9,20 +9,22 @@ const helpers_1 = require("../../helpers");
9
9
  const Controller_1 = require("./Controller");
10
10
  const hooks_1 = require("./hooks");
11
11
  const utils_1 = require("./utils");
12
- const DynamicField = ({ name, spec, config, Monaco, generateRandomValue, search, withoutInsertFFDebounce, errors: externalErrors, __mirror, }) => {
12
+ const DynamicField = ({ name, spec, config, Monaco, generateRandomValue, search, withoutInsertFFDebounce, mutators: externalMutators, __mirror, }) => {
13
13
  const DynamicFormsCtx = (0, hooks_1.useCreateContext)();
14
14
  const SearchContext = (0, hooks_1.useCreateSearchContext)();
15
15
  const { tools, store } = (0, hooks_1.useStore)(name);
16
16
  const watcher = (0, hooks_1.useIntegrationFF)(store, withoutInsertFFDebounce);
17
+ const { mutators, mutateDFState } = (0, hooks_1.useMutators)(externalMutators);
17
18
  const { store: searchStore, setField, removeField, isHiddenField } = (0, hooks_1.useSearchStore)();
18
19
  const context = react_1.default.useMemo(() => ({
19
20
  config,
20
21
  Monaco: (0, react_is_1.isValidElementType)(Monaco) ? Monaco : undefined,
21
22
  generateRandomValue,
22
- tools,
23
- externalErrors,
23
+ tools: Object.assign(Object.assign({}, tools), { mutateDFState }),
24
+ store,
25
+ mutators,
24
26
  __mirror,
25
- }), [tools, config, Monaco, __mirror, generateRandomValue, externalErrors]);
27
+ }), [tools, config, Monaco, __mirror, generateRandomValue, mutators, mutateDFState, store]);
26
28
  const searchContext = react_1.default.useMemo(() => ({
27
29
  setField,
28
30
  removeField,
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SPEC_TYPE_FOR_GENERATE_BUTTON = exports.OBJECT_ARRAY_CNT = exports.OBJECT_ARRAY_FLAG = void 0;
3
+ exports.EMPTY_MUTATOR = exports.SPEC_TYPE_FOR_GENERATE_BUTTON = exports.OBJECT_ARRAY_CNT = exports.OBJECT_ARRAY_FLAG = void 0;
4
4
  exports.OBJECT_ARRAY_FLAG = '____arr-obj';
5
5
  exports.OBJECT_ARRAY_CNT = '____arr-obj-cnt';
6
6
  exports.SPEC_TYPE_FOR_GENERATE_BUTTON = ['base', 'password', 'textarea'];
7
+ exports.EMPTY_MUTATOR = '____empty-mutator';
@@ -1,17 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./useComponents"), exports);
5
4
  tslib_1.__exportStar(require("./useControllerMirror"), exports);
6
5
  tslib_1.__exportStar(require("./useCreateContext"), exports);
7
6
  tslib_1.__exportStar(require("./useDynamicFieldMirror"), exports);
8
7
  tslib_1.__exportStar(require("./useDynamicFormsCtx"), exports);
9
- tslib_1.__exportStar(require("./useField"), exports);
10
8
  tslib_1.__exportStar(require("./useGenerateRandomValue"), exports);
11
9
  tslib_1.__exportStar(require("./useIntegrationFF"), exports);
12
- tslib_1.__exportStar(require("./useRender"), exports);
10
+ tslib_1.__exportStar(require("./useMutateDFState"), exports);
11
+ tslib_1.__exportStar(require("./useMutators"), exports);
13
12
  tslib_1.__exportStar(require("./useStore"), exports);
14
- tslib_1.__exportStar(require("./useValidate"), exports);
13
+ tslib_1.__exportStar(require("./useStoreValue"), exports);
15
14
  tslib_1.__exportStar(require("./useMonaco"), exports);
16
15
  tslib_1.__exportStar(require("./useSearchStore"), exports);
17
16
  tslib_1.__exportStar(require("./useSearchContext"), exports);
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useMutateDFState = void 0;
4
+ const useDynamicFormsCtx_1 = require("./useDynamicFormsCtx");
5
+ const useMutateDFState = () => (0, useDynamicFormsCtx_1.useDynamicFormsCtx)().tools.mutateDFState;
6
+ exports.useMutateDFState = useMutateDFState;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useMutators = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importDefault(require("react"));
6
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
+ const useMutators = (externalMutators) => {
8
+ const firstRenderRef = react_1.default.useRef(true);
9
+ const [store, setStore] = react_1.default.useState(externalMutators || {});
10
+ const mutateDFState = react_1.default.useCallback((mutators) => {
11
+ const mergeSpec = (a, b) => {
12
+ const result = lodash_1.default.cloneDeep(a);
13
+ const getKeys = (parent) => {
14
+ if (lodash_1.default.isObjectLike(parent)) {
15
+ return lodash_1.default.keys(parent).reduce((acc, parentKey) => {
16
+ const childKeys = getKeys(parent[parentKey]);
17
+ return [
18
+ ...acc,
19
+ ...(childKeys.length ? [] : [[parentKey]]),
20
+ ...childKeys.map((childKey) => [parentKey, ...childKey]),
21
+ ];
22
+ }, []);
23
+ }
24
+ return [];
25
+ };
26
+ getKeys(b).forEach((key) => {
27
+ lodash_1.default.set(result, key, lodash_1.default.get(b, key));
28
+ });
29
+ return result;
30
+ };
31
+ setStore((store) => (Object.assign(Object.assign(Object.assign(Object.assign({}, store), (mutators.errors ? { errors: lodash_1.default.merge(store.errors, mutators.errors) } : {})), (mutators.values ? { values: lodash_1.default.merge(store.values, mutators.values) } : {})), (mutators.spec ? { spec: mergeSpec(store.spec || {}, mutators.spec) } : {}))));
32
+ }, [setStore]);
33
+ react_1.default.useEffect(() => {
34
+ if (firstRenderRef.current) {
35
+ firstRenderRef.current = false;
36
+ }
37
+ else if (externalMutators) {
38
+ mutateDFState(externalMutators);
39
+ }
40
+ }, [externalMutators]);
41
+ return { mutators: store, mutateDFState };
42
+ };
43
+ exports.useMutators = useMutators;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useStoreValue = void 0;
4
+ const useDynamicFormsCtx_1 = require("./useDynamicFormsCtx");
5
+ const useStoreValue = () => (0, useDynamicFormsCtx_1.useDynamicFormsCtx)().store;
6
+ exports.useStoreValue = useStoreValue;
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useStoreValue = exports.useMutateDFState = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  tslib_1.__exportStar(require("./constants"), exports);
5
6
  tslib_1.__exportStar(require("./Controller"), exports);
6
7
  tslib_1.__exportStar(require("./DynamicField"), exports);
8
+ var hooks_1 = require("./hooks");
9
+ Object.defineProperty(exports, "useMutateDFState", { enumerable: true, get: function () { return hooks_1.useMutateDFState; } });
10
+ Object.defineProperty(exports, "useStoreValue", { enumerable: true, get: function () { return hooks_1.useStoreValue; } });
7
11
  tslib_1.__exportStar(require("./types"), exports);
8
12
  tslib_1.__exportStar(require("./utils"), exports);
@@ -9,6 +9,7 @@ tslib_1.__exportStar(require("./field"), exports);
9
9
  tslib_1.__exportStar(require("./input"), exports);
10
10
  tslib_1.__exportStar(require("./layout"), exports);
11
11
  tslib_1.__exportStar(require("./mirror"), exports);
12
+ tslib_1.__exportStar(require("./mutators"), exports);
12
13
  tslib_1.__exportStar(require("./number"), exports);
13
14
  tslib_1.__exportStar(require("./object"), exports);
14
15
  tslib_1.__exportStar(require("./store"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { FormValue, Spec } from '../../../types';
2
+ import { FieldValue, ValidateError } from '../types';
3
+ export interface ControllerProps<DirtyValue extends FieldValue, SpecType extends Spec> {
4
+ spec: SpecType;
5
+ name: string;
6
+ value: DirtyValue;
7
+ parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
8
+ parentOnUnmount: ((childName: string) => void) | null;
9
+ }
10
+ export declare const Controller: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>({ spec: _spec, name, value: valueFromParent, parentOnChange, parentOnUnmount, }: ControllerProps<DirtyValue, SpecType>) => JSX.Element | null;