@gravity-ui/dynamic-forms 1.1.0 → 1.1.1

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 (28) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/build/cjs/lib/core/components/Form/Controller.js +10 -2
  3. package/build/cjs/lib/core/components/Form/DynamicField.js +1 -1
  4. package/build/cjs/lib/core/components/Form/hooks/useField.js +2 -2
  5. package/build/cjs/lib/core/components/Form/hooks/useStore.js +1 -0
  6. package/build/cjs/lib/kit/components/Inputs/ArrayBase/ArrayBase.js +4 -3
  7. package/build/cjs/lib/kit/components/Inputs/CardOneOf/CardOneOf.js +2 -1
  8. package/build/cjs/lib/kit/components/Inputs/ObjectBase/ObjectBase.js +11 -2
  9. package/build/cjs/lib/kit/components/Inputs/OneOf/OneOf.js +2 -1
  10. package/build/cjs/lib/kit/components/Inputs/OneOfCard/OneOfCard.js +2 -1
  11. package/build/cjs/lib/kit/components/Inputs/Secret/Secret.js +2 -1
  12. package/build/cjs/lib/kit/components/Inputs/TableArrayInput/TableArrayInput.js +3 -2
  13. package/build/esm/lib/core/components/Form/Controller.d.ts +2 -1
  14. package/build/esm/lib/core/components/Form/Controller.js +10 -2
  15. package/build/esm/lib/core/components/Form/DynamicField.js +1 -1
  16. package/build/esm/lib/core/components/Form/hooks/useField.d.ts +2 -1
  17. package/build/esm/lib/core/components/Form/hooks/useField.js +2 -2
  18. package/build/esm/lib/core/components/Form/hooks/useStore.d.ts +1 -0
  19. package/build/esm/lib/core/components/Form/hooks/useStore.js +1 -0
  20. package/build/esm/lib/core/components/Form/types/context.d.ts +1 -0
  21. package/build/esm/lib/kit/components/Inputs/ArrayBase/ArrayBase.js +4 -3
  22. package/build/esm/lib/kit/components/Inputs/CardOneOf/CardOneOf.js +2 -1
  23. package/build/esm/lib/kit/components/Inputs/ObjectBase/ObjectBase.js +11 -2
  24. package/build/esm/lib/kit/components/Inputs/OneOf/OneOf.js +2 -1
  25. package/build/esm/lib/kit/components/Inputs/OneOfCard/OneOfCard.js +2 -1
  26. package/build/esm/lib/kit/components/Inputs/Secret/Secret.js +2 -1
  27. package/build/esm/lib/kit/components/Inputs/TableArrayInput/TableArrayInput.js +3 -2
  28. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.1.1](https://github.com/gravity-ui/dynamic-forms/compare/v1.1.0...v1.1.1) (2023-03-21)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * fix incorrect unmount ([#12](https://github.com/gravity-ui/dynamic-forms/issues/12)) ([592652b](https://github.com/gravity-ui/dynamic-forms/commit/592652b2814567b2188ec43779eb591b7dec6ade))
9
+
3
10
  ## [1.1.0](https://github.com/gravity-ui/dynamic-forms/compare/v1.0.2...v1.1.0) (2023-03-19)
4
11
 
5
12
 
@@ -5,12 +5,20 @@ const tslib_1 = require("tslib");
5
5
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
6
  const helpers_1 = require("../../helpers");
7
7
  const hooks_1 = require("./hooks");
8
- const Controller = ({ spec, name, initialValue, parentOnChange, }) => {
8
+ const Controller = ({ spec, name, initialValue, parentOnChange, parentOnUnmount, }) => {
9
9
  const { tools } = (0, hooks_1.useDynamicFormsCtx)();
10
10
  const { inputEntity, Layout } = (0, hooks_1.useComponents)(spec);
11
11
  const render = (0, hooks_1.useRender)({ name, spec, inputEntity, Layout });
12
12
  const validate = (0, hooks_1.useValidate)(spec);
13
- const renderProps = (0, hooks_1.useField)({ name, initialValue, spec, validate, tools, parentOnChange });
13
+ const renderProps = (0, hooks_1.useField)({
14
+ name,
15
+ initialValue,
16
+ spec,
17
+ validate,
18
+ tools,
19
+ parentOnChange,
20
+ parentOnUnmount,
21
+ });
14
22
  if (lodash_1.default.isString(name) && (0, helpers_1.isCorrectSpec)(spec)) {
15
23
  return render(renderProps);
16
24
  }
@@ -20,7 +20,7 @@ const DynamicField = ({ name, spec, config, Monaco }) => {
20
20
  const correctParams = react_1.default.useMemo(() => lodash_1.default.isString(name) && (0, helpers_1.isCorrectSpec)(spec) && (0, helpers_2.isCorrectConfig)(config), [name, spec, config]);
21
21
  if (correctParams) {
22
22
  return (react_1.default.createElement(DynamicFormsCtx.Provider, { value: context },
23
- react_1.default.createElement(Controller_1.Controller, { spec: spec, name: name, parentOnChange: null, initialValue: lodash_1.default.get(tools.initialValue, name) }),
23
+ react_1.default.createElement(Controller_1.Controller, { spec: spec, name: name, parentOnChange: null, parentOnUnmount: null, initialValue: lodash_1.default.get(tools.initialValue, name) }),
24
24
  watcher));
25
25
  }
26
26
  return null;
@@ -7,7 +7,7 @@ const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
7
  const helpers_1 = require("../../../helpers");
8
8
  const constants_1 = require("../constants");
9
9
  const helpers_2 = require("../helpers");
10
- const useField = ({ name, spec, initialValue, validate: propsValidate, tools, parentOnChange, }) => {
10
+ const useField = ({ name, spec, initialValue, validate: propsValidate, tools, parentOnChange, parentOnUnmount, }) => {
11
11
  const firstRenderRef = react_1.default.useRef(true);
12
12
  const validate = react_1.default.useCallback((value) => {
13
13
  if (value === constants_1.REMOVED_ITEM) {
@@ -133,7 +133,7 @@ const useField = ({ name, spec, initialValue, validate: propsValidate, tools, pa
133
133
  react_1.default.useEffect(() => {
134
134
  firstRenderRef.current = false;
135
135
  return () => {
136
- (parentOnChange ? parentOnChange : tools.onChange)(name, state.value, { [name]: false });
136
+ (parentOnUnmount ? parentOnUnmount : tools.onUnmount)(name);
137
137
  };
138
138
  }, []);
139
139
  return renderProps;
@@ -50,6 +50,7 @@ const useStore = (name) => {
50
50
  const tools = react_1.default.useMemo(() => ({
51
51
  initialValue: store.initialValue,
52
52
  onChange: (name, value, errors) => setStore((store) => (Object.assign(Object.assign({}, store), { values: lodash_1.default.set(Object.assign({}, store.values), name, value), errors: errors || {} }))),
53
+ onUnmount: (name) => setStore((store) => (Object.assign(Object.assign({}, store), { errors: lodash_1.default.omit(store.errors, Object.keys(store.errors).filter((key) => key.startsWith(name))) }))),
53
54
  submitFailed,
54
55
  }), [store.initialValue, setStore, submitFailed]);
55
56
  const change = react_1.default.useCallback(lodash_1.default.debounce((value) => {
@@ -20,7 +20,7 @@ const ArrayBase = ({ spec, name, arrayInput, input }) => {
20
20
  let item;
21
21
  if (!((_a = spec.items) === null || _a === void 0 ? void 0 : _a.required)) {
22
22
  if ((0, core_1.isArraySpec)(spec.items)) {
23
- item = { OBJECT_ARRAY_FLAG: true, OBJECT_ARRAY_CNT: 0 };
23
+ item = { [core_1.OBJECT_ARRAY_FLAG]: true, [core_1.OBJECT_ARRAY_CNT]: 0 };
24
24
  }
25
25
  else if ((0, core_1.isObjectSpec)(spec.items)) {
26
26
  item = {};
@@ -39,14 +39,15 @@ const ArrayBase = ({ spec, name, arrayInput, input }) => {
39
39
  return itemSpec;
40
40
  }, [spec.items, itemSpecCorrect]);
41
41
  const parentOnChange = react_1.default.useCallback((childName, childValue, childErrors) => input.onChange((currentValue) => lodash_1.default.set(Object.assign({}, currentValue), childName.split(`${input.name}.`).join(''), childValue), childErrors), [input.onChange, input.name]);
42
+ const parentOnUnmount = react_1.default.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
42
43
  const items = react_1.default.useMemo(() => keys.map((key, idx) => {
43
44
  var _a;
44
45
  const itemSpec = getItemSpec(idx);
45
46
  if (!itemSpec) {
46
47
  return null;
47
48
  }
48
- return (react_1.default.createElement(core_1.Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[`<${key}>`], parentOnChange: parentOnChange, spec: itemSpec, name: `${name}.<${key}>`, key: `${name}.<${key}>` }));
49
- }), [keys.join(''), name, getItemSpec, parentOnChange, input.value]);
49
+ return (react_1.default.createElement(core_1.Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[`<${key}>`], parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, spec: itemSpec, name: `${name}.<${key}>`, key: `${name}.<${key}>` }));
50
+ }), [keys.join(''), name, getItemSpec, parentOnChange, parentOnUnmount, input.value]);
50
51
  if (!itemSpecCorrect) {
51
52
  return null;
52
53
  }
@@ -30,7 +30,8 @@ const CardOneOf = (props) => {
30
30
  const value = lodash_1.default.set({}, childName.split(`${input.name}.`).join(''), childValue);
31
31
  input.onChange(value, childErrors);
32
32
  }, [input.onChange, input.name]);
33
+ const parentOnUnmount = react_1.default.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
33
34
  (0, hooks_1.useErrorChecker)({ name, meta, open, setOpen });
34
- return (react_1.default.createElement(__1.Card, { title: toggler, description: spec.viewSpec.layoutDescription, actions: actions, open: open, onToggle: onToggle, disableHeaderToggle: true }, specProperties[oneOfValue] ? (react_1.default.createElement(core_1.Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, key: `${name}.${oneOfValue}` })) : null));
35
+ return (react_1.default.createElement(__1.Card, { title: toggler, description: spec.viewSpec.layoutDescription, actions: actions, open: open, onToggle: onToggle, disableHeaderToggle: true }, specProperties[oneOfValue] ? (react_1.default.createElement(core_1.Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name}.${oneOfValue}` })) : null));
35
36
  };
36
37
  exports.CardOneOf = CardOneOf;
@@ -16,6 +16,7 @@ const ObjectBase = (_a) => {
16
16
  spec.viewSpec.layoutTitle || null));
17
17
  }, [spec.defaultValue, spec.viewSpec.layoutTitle, restProps.input.onChange]);
18
18
  const parentOnChange = react_1.default.useCallback((childName, childValue, childErrors) => restProps.input.onChange((currentValue) => lodash_1.default.set(Object.assign({}, currentValue), childName.split(`${restProps.input.name}.`).join(''), childValue), childErrors), [restProps.input.onChange, restProps.input.name]);
19
+ const parentOnUnmount = react_1.default.useCallback((childName) => restProps.input.onChange((currentValue) => currentValue, { [childName]: false }), [restProps.input.onChange]);
19
20
  const content = react_1.default.useMemo(() => {
20
21
  if (!lodash_1.default.isObjectLike(spec.properties) || !Object.keys(spec.properties || {}).length) {
21
22
  return null;
@@ -26,9 +27,17 @@ const ObjectBase = (_a) => {
26
27
  const specProperties = Object.assign({}, spec.properties);
27
28
  return (react_1.default.createElement(react_1.default.Fragment, null, (spec.viewSpec.order || Object.keys(specProperties)).map((property) => {
28
29
  var _a;
29
- return specProperties[property] ? (react_1.default.createElement(core_1.Controller, { initialValue: (_a = restProps.input.value) === null || _a === void 0 ? void 0 : _a[property], spec: specProperties[property], name: `${name ? name + '.' : ''}${property}`, parentOnChange: parentOnChange, key: `${name ? name + '.' : ''}${property}` })) : null;
30
+ return specProperties[property] ? (react_1.default.createElement(core_1.Controller, { initialValue: (_a = restProps.input.value) === null || _a === void 0 ? void 0 : _a[property], spec: specProperties[property], name: `${name ? name + '.' : ''}${property}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name ? name + '.' : ''}${property}` })) : null;
30
31
  })));
31
- }, [spec.properties, spec.viewSpec.order, name, restProps.input.value, addBtn, parentOnChange]);
32
+ }, [
33
+ spec.properties,
34
+ spec.viewSpec.order,
35
+ name,
36
+ restProps.input.value,
37
+ addBtn,
38
+ parentOnChange,
39
+ parentOnUnmount,
40
+ ]);
32
41
  if (!Layout || !content) {
33
42
  return content;
34
43
  }
@@ -16,9 +16,10 @@ const OneOf = (props) => {
16
16
  const value = lodash_1.default.set({}, childName.split(`${props.input.name}.`).join(''), childValue);
17
17
  props.input.onChange(value, childErrors);
18
18
  }, [props.input.onChange, props.input.name]);
19
+ const parentOnUnmount = react_1.default.useCallback((childName) => props.input.onChange((currentValue) => currentValue, { [childName]: false }), [props.input.onChange]);
19
20
  return (react_1.default.createElement("div", { className: b() },
20
21
  specProperties[oneOfValue] ? (react_1.default.createElement(__1.GroupIndent, null,
21
- react_1.default.createElement(core_1.Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${props.name}.${oneOfValue}`, parentOnChange: parentOnChange, key: `${props.name}.${oneOfValue}` }))) : null,
22
+ react_1.default.createElement(core_1.Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${props.name}.${oneOfValue}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${props.name}.${oneOfValue}` }))) : null,
22
23
  react_1.default.createElement("div", { className: b('toggler') }, toggler)));
23
24
  };
24
25
  exports.OneOf = OneOf;
@@ -37,7 +37,8 @@ const OneOfCard = (props) => {
37
37
  const value = lodash_1.default.set({}, childName.split(`${input.name}.`).join(''), childValue);
38
38
  input.onChange(value, childErrors);
39
39
  }, [input.onChange, input.name]);
40
+ const parentOnUnmount = react_1.default.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
40
41
  (0, hooks_1.useErrorChecker)({ name, meta, open, setOpen });
41
- return (react_1.default.createElement(__1.AccordeonCard, { className: b(), header: toggler, description: spec.viewSpec.layoutDescription || '', open: open, onToggle: onToggle, ignoreHeaderToggle: true, headerActionsTemplate: headerActionsTemplate }, specProperties[oneOfValue] ? (react_1.default.createElement(core_1.Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, key: `${name}.${oneOfValue}` })) : null));
42
+ return (react_1.default.createElement(__1.AccordeonCard, { className: b(), header: toggler, description: spec.viewSpec.layoutDescription || '', open: open, onToggle: onToggle, ignoreHeaderToggle: true, headerActionsTemplate: headerActionsTemplate }, specProperties[oneOfValue] ? (react_1.default.createElement(core_1.Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name}.${oneOfValue}` })) : null));
42
43
  };
43
44
  exports.OneOfCard = OneOfCard;
@@ -9,6 +9,7 @@ const SECRET_PROPERTY_NAME = 'raw';
9
9
  const Secret = ({ spec, name, input }) => {
10
10
  var _a;
11
11
  const parentOnChange = react_1.default.useCallback((childName, childValue, childErrors) => input.onChange((currentValue) => lodash_1.default.set(Object.assign({}, currentValue), childName.split(`${input.name}.`).join(''), childValue), childErrors), [input.onChange, input.name]);
12
+ const parentOnUnmount = react_1.default.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
12
13
  if (!lodash_1.default.isObjectLike(spec.properties)) {
13
14
  return null;
14
15
  }
@@ -16,6 +17,6 @@ const Secret = ({ spec, name, input }) => {
16
17
  if (!specProperties[SECRET_PROPERTY_NAME]) {
17
18
  return null;
18
19
  }
19
- return (react_1.default.createElement(core_1.Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[SECRET_PROPERTY_NAME], spec: specProperties[SECRET_PROPERTY_NAME], name: `${name}.${SECRET_PROPERTY_NAME}`, parentOnChange: parentOnChange, key: `${name}.${SECRET_PROPERTY_NAME}` }));
20
+ return (react_1.default.createElement(core_1.Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[SECRET_PROPERTY_NAME], spec: specProperties[SECRET_PROPERTY_NAME], name: `${name}.${SECRET_PROPERTY_NAME}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name}.${SECRET_PROPERTY_NAME}` }));
20
21
  };
21
22
  exports.Secret = Secret;
@@ -23,6 +23,7 @@ const TableArrayInput = ({ spec, name, arrayInput, input }) => {
23
23
  arrayInput.onItemRemove(key);
24
24
  }, [arrayInput.onItemRemove]);
25
25
  const parentOnChange = react_1.default.useCallback((childName, childValue, childErrors) => input.onChange((currentValue) => lodash_1.default.set(Object.assign({}, currentValue), childName.split(`${input.name}.`).join(''), childValue), childErrors), [input.onChange, input.name]);
26
+ const parentOnUnmount = react_1.default.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
26
27
  const columns = react_1.default.useMemo(() => {
27
28
  const { items, viewSpec: { table }, } = spec;
28
29
  if (!(table === null || table === void 0 ? void 0 : table.length) || !(0, core_1.isObjectSpec)(items)) {
@@ -55,11 +56,11 @@ const TableArrayInput = ({ spec, name, arrayInput, input }) => {
55
56
  arr: (0, core_1.isArraySpec)(entitySpec),
56
57
  obj: (0, core_1.isObjectSpec)(entitySpec),
57
58
  }), key: `${name}.<${key}>.${property}` },
58
- react_1.default.createElement(core_1.Controller, { initialValue: (_c = (_b = input.value) === null || _b === void 0 ? void 0 : _b[`<${key}>`]) === null || _c === void 0 ? void 0 : _c[property], spec: entitySpec, name: `${name}.<${key}>.${property}`, parentOnChange: parentOnChange })));
59
+ react_1.default.createElement(core_1.Controller, { initialValue: (_c = (_b = input.value) === null || _b === void 0 ? void 0 : _b[`<${key}>`]) === null || _c === void 0 ? void 0 : _c[property], spec: entitySpec, name: `${name}.<${key}>.${property}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount })));
59
60
  },
60
61
  }));
61
62
  return [idxColumn, ...columns, removeColumn];
62
- }, [name, spec, onItemRemove, parentOnChange, input.value]);
63
+ }, [name, spec, onItemRemove, parentOnChange, parentOnUnmount, input.value]);
63
64
  if (!columns) {
64
65
  return null;
65
66
  }
@@ -5,5 +5,6 @@ export interface ControllerProps<Value extends FieldValue, SpecType extends Spec
5
5
  name: string;
6
6
  initialValue: Value;
7
7
  parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
8
+ parentOnUnmount: ((childName: string) => void) | null;
8
9
  }
9
- export declare const Controller: <Value extends FieldValue, SpecType extends Spec>({ spec, name, initialValue, parentOnChange, }: ControllerProps<Value, SpecType>) => JSX.Element | null;
10
+ export declare const Controller: <Value extends FieldValue, SpecType extends Spec>({ spec, name, initialValue, parentOnChange, parentOnUnmount, }: ControllerProps<Value, SpecType>) => JSX.Element | null;
@@ -1,12 +1,20 @@
1
1
  import _ from 'lodash';
2
2
  import { isCorrectSpec } from '../../helpers';
3
3
  import { useComponents, useDynamicFormsCtx, useField, useRender, useValidate } from './hooks';
4
- export const Controller = ({ spec, name, initialValue, parentOnChange, }) => {
4
+ export const Controller = ({ spec, name, initialValue, parentOnChange, parentOnUnmount, }) => {
5
5
  const { tools } = useDynamicFormsCtx();
6
6
  const { inputEntity, Layout } = useComponents(spec);
7
7
  const render = useRender({ name, spec, inputEntity, Layout });
8
8
  const validate = useValidate(spec);
9
- const renderProps = useField({ name, initialValue, spec, validate, tools, parentOnChange });
9
+ const renderProps = useField({
10
+ name,
11
+ initialValue,
12
+ spec,
13
+ validate,
14
+ tools,
15
+ parentOnChange,
16
+ parentOnUnmount,
17
+ });
10
18
  if (_.isString(name) && isCorrectSpec(spec)) {
11
19
  return render(renderProps);
12
20
  }
@@ -16,7 +16,7 @@ export const DynamicField = ({ name, spec, config, Monaco }) => {
16
16
  const correctParams = React.useMemo(() => _.isString(name) && isCorrectSpec(spec) && isCorrectConfig(config), [name, spec, config]);
17
17
  if (correctParams) {
18
18
  return (React.createElement(DynamicFormsCtx.Provider, { value: context },
19
- React.createElement(Controller, { spec: spec, name: name, parentOnChange: null, initialValue: _.get(tools.initialValue, name) }),
19
+ React.createElement(Controller, { spec: spec, name: name, parentOnChange: null, parentOnUnmount: null, initialValue: _.get(tools.initialValue, name) }),
20
20
  watcher));
21
21
  }
22
22
  return null;
@@ -7,5 +7,6 @@ export interface FieldProps<Value extends FieldValue, SpecType extends Spec> {
7
7
  validate?: (value?: Value) => ValidateError;
8
8
  tools: DynamicFormsContext['tools'];
9
9
  parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
10
+ parentOnUnmount: ((childName: string) => void) | null;
10
11
  }
11
- export declare const useField: <Value extends FieldValue, SpecType extends Spec>({ name, spec, initialValue, validate: propsValidate, tools, parentOnChange, }: FieldProps<Value, SpecType>) => FieldRenderProps<Value>;
12
+ export declare const useField: <Value extends FieldValue, SpecType extends Spec>({ name, spec, initialValue, validate: propsValidate, tools, parentOnChange, parentOnUnmount, }: FieldProps<Value, SpecType>) => FieldRenderProps<Value>;
@@ -3,7 +3,7 @@ import _ from 'lodash';
3
3
  import { isArraySpec, isObjectSpec } from '../../../helpers';
4
4
  import { OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG, REMOVED_ITEM } from '../constants';
5
5
  import { isArrayItem, transformArrIn, transformArrOut } from '../helpers';
6
- export const useField = ({ name, spec, initialValue, validate: propsValidate, tools, parentOnChange, }) => {
6
+ export const useField = ({ name, spec, initialValue, validate: propsValidate, tools, parentOnChange, parentOnUnmount, }) => {
7
7
  const firstRenderRef = React.useRef(true);
8
8
  const validate = React.useCallback((value) => {
9
9
  if (value === REMOVED_ITEM) {
@@ -129,7 +129,7 @@ export const useField = ({ name, spec, initialValue, validate: propsValidate, to
129
129
  React.useEffect(() => {
130
130
  firstRenderRef.current = false;
131
131
  return () => {
132
- (parentOnChange ? parentOnChange : tools.onChange)(name, state.value, { [name]: false });
132
+ (parentOnUnmount ? parentOnUnmount : tools.onUnmount)(name);
133
133
  };
134
134
  }, []);
135
135
  return renderProps;
@@ -9,6 +9,7 @@ export declare const useStore: (name: string) => {
9
9
  tools: {
10
10
  initialValue: FieldObjectValue;
11
11
  onChange: (name: string, value: FieldValue, errors?: Record<string, ValidateError>) => void;
12
+ onUnmount: (name: string) => void;
12
13
  submitFailed: boolean;
13
14
  };
14
15
  watcher: JSX.Element;
@@ -46,6 +46,7 @@ export const useStore = (name) => {
46
46
  const tools = React.useMemo(() => ({
47
47
  initialValue: store.initialValue,
48
48
  onChange: (name, value, errors) => setStore((store) => (Object.assign(Object.assign({}, store), { values: _.set(Object.assign({}, store.values), name, value), errors: errors || {} }))),
49
+ onUnmount: (name) => setStore((store) => (Object.assign(Object.assign({}, store), { errors: _.omit(store.errors, Object.keys(store.errors).filter((key) => key.startsWith(name))) }))),
49
50
  submitFailed,
50
51
  }), [store.initialValue, setStore, submitFailed]);
51
52
  const change = React.useCallback(_.debounce((value) => {
@@ -7,6 +7,7 @@ export interface DynamicFormsContext {
7
7
  tools: {
8
8
  initialValue: FieldObjectValue;
9
9
  onChange: (name: string, value: FieldValue, errors?: Record<string, ValidateError>) => void;
10
+ onUnmount: (name: string) => void;
10
11
  submitFailed: boolean;
11
12
  };
12
13
  }
@@ -16,7 +16,7 @@ export const ArrayBase = ({ spec, name, arrayInput, input }) => {
16
16
  let item;
17
17
  if (!((_a = spec.items) === null || _a === void 0 ? void 0 : _a.required)) {
18
18
  if (isArraySpec(spec.items)) {
19
- item = { OBJECT_ARRAY_FLAG: true, OBJECT_ARRAY_CNT: 0 };
19
+ item = { [OBJECT_ARRAY_FLAG]: true, [OBJECT_ARRAY_CNT]: 0 };
20
20
  }
21
21
  else if (isObjectSpec(spec.items)) {
22
22
  item = {};
@@ -35,14 +35,15 @@ export const ArrayBase = ({ spec, name, arrayInput, input }) => {
35
35
  return itemSpec;
36
36
  }, [spec.items, itemSpecCorrect]);
37
37
  const parentOnChange = React.useCallback((childName, childValue, childErrors) => input.onChange((currentValue) => _.set(Object.assign({}, currentValue), childName.split(`${input.name}.`).join(''), childValue), childErrors), [input.onChange, input.name]);
38
+ const parentOnUnmount = React.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
38
39
  const items = React.useMemo(() => keys.map((key, idx) => {
39
40
  var _a;
40
41
  const itemSpec = getItemSpec(idx);
41
42
  if (!itemSpec) {
42
43
  return null;
43
44
  }
44
- return (React.createElement(Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[`<${key}>`], parentOnChange: parentOnChange, spec: itemSpec, name: `${name}.<${key}>`, key: `${name}.<${key}>` }));
45
- }), [keys.join(''), name, getItemSpec, parentOnChange, input.value]);
45
+ return (React.createElement(Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[`<${key}>`], parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, spec: itemSpec, name: `${name}.<${key}>`, key: `${name}.<${key}>` }));
46
+ }), [keys.join(''), name, getItemSpec, parentOnChange, parentOnUnmount, input.value]);
46
47
  if (!itemSpecCorrect) {
47
48
  return null;
48
49
  }
@@ -26,6 +26,7 @@ export const CardOneOf = (props) => {
26
26
  const value = _.set({}, childName.split(`${input.name}.`).join(''), childValue);
27
27
  input.onChange(value, childErrors);
28
28
  }, [input.onChange, input.name]);
29
+ const parentOnUnmount = React.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
29
30
  useErrorChecker({ name, meta, open, setOpen });
30
- return (React.createElement(Card, { title: toggler, description: spec.viewSpec.layoutDescription, actions: actions, open: open, onToggle: onToggle, disableHeaderToggle: true }, specProperties[oneOfValue] ? (React.createElement(Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, key: `${name}.${oneOfValue}` })) : null));
31
+ return (React.createElement(Card, { title: toggler, description: spec.viewSpec.layoutDescription, actions: actions, open: open, onToggle: onToggle, disableHeaderToggle: true }, specProperties[oneOfValue] ? (React.createElement(Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name}.${oneOfValue}` })) : null));
31
32
  };
@@ -13,6 +13,7 @@ export const ObjectBase = (_a) => {
13
13
  spec.viewSpec.layoutTitle || null));
14
14
  }, [spec.defaultValue, spec.viewSpec.layoutTitle, restProps.input.onChange]);
15
15
  const parentOnChange = React.useCallback((childName, childValue, childErrors) => restProps.input.onChange((currentValue) => _.set(Object.assign({}, currentValue), childName.split(`${restProps.input.name}.`).join(''), childValue), childErrors), [restProps.input.onChange, restProps.input.name]);
16
+ const parentOnUnmount = React.useCallback((childName) => restProps.input.onChange((currentValue) => currentValue, { [childName]: false }), [restProps.input.onChange]);
16
17
  const content = React.useMemo(() => {
17
18
  if (!_.isObjectLike(spec.properties) || !Object.keys(spec.properties || {}).length) {
18
19
  return null;
@@ -23,9 +24,17 @@ export const ObjectBase = (_a) => {
23
24
  const specProperties = Object.assign({}, spec.properties);
24
25
  return (React.createElement(React.Fragment, null, (spec.viewSpec.order || Object.keys(specProperties)).map((property) => {
25
26
  var _a;
26
- return specProperties[property] ? (React.createElement(Controller, { initialValue: (_a = restProps.input.value) === null || _a === void 0 ? void 0 : _a[property], spec: specProperties[property], name: `${name ? name + '.' : ''}${property}`, parentOnChange: parentOnChange, key: `${name ? name + '.' : ''}${property}` })) : null;
27
+ return specProperties[property] ? (React.createElement(Controller, { initialValue: (_a = restProps.input.value) === null || _a === void 0 ? void 0 : _a[property], spec: specProperties[property], name: `${name ? name + '.' : ''}${property}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name ? name + '.' : ''}${property}` })) : null;
27
28
  })));
28
- }, [spec.properties, spec.viewSpec.order, name, restProps.input.value, addBtn, parentOnChange]);
29
+ }, [
30
+ spec.properties,
31
+ spec.viewSpec.order,
32
+ name,
33
+ restProps.input.value,
34
+ addBtn,
35
+ parentOnChange,
36
+ parentOnUnmount,
37
+ ]);
29
38
  if (!Layout || !content) {
30
39
  return content;
31
40
  }
@@ -13,8 +13,9 @@ export const OneOf = (props) => {
13
13
  const value = _.set({}, childName.split(`${props.input.name}.`).join(''), childValue);
14
14
  props.input.onChange(value, childErrors);
15
15
  }, [props.input.onChange, props.input.name]);
16
+ const parentOnUnmount = React.useCallback((childName) => props.input.onChange((currentValue) => currentValue, { [childName]: false }), [props.input.onChange]);
16
17
  return (React.createElement("div", { className: b() },
17
18
  specProperties[oneOfValue] ? (React.createElement(GroupIndent, null,
18
- React.createElement(Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${props.name}.${oneOfValue}`, parentOnChange: parentOnChange, key: `${props.name}.${oneOfValue}` }))) : null,
19
+ React.createElement(Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${props.name}.${oneOfValue}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${props.name}.${oneOfValue}` }))) : null,
19
20
  React.createElement("div", { className: b('toggler') }, toggler)));
20
21
  };
@@ -34,6 +34,7 @@ export const OneOfCard = (props) => {
34
34
  const value = _.set({}, childName.split(`${input.name}.`).join(''), childValue);
35
35
  input.onChange(value, childErrors);
36
36
  }, [input.onChange, input.name]);
37
+ const parentOnUnmount = React.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
37
38
  useErrorChecker({ name, meta, open, setOpen });
38
- return (React.createElement(AccordeonCard, { className: b(), header: toggler, description: spec.viewSpec.layoutDescription || '', open: open, onToggle: onToggle, ignoreHeaderToggle: true, headerActionsTemplate: headerActionsTemplate }, specProperties[oneOfValue] ? (React.createElement(Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, key: `${name}.${oneOfValue}` })) : null));
39
+ return (React.createElement(AccordeonCard, { className: b(), header: toggler, description: spec.viewSpec.layoutDescription || '', open: open, onToggle: onToggle, ignoreHeaderToggle: true, headerActionsTemplate: headerActionsTemplate }, specProperties[oneOfValue] ? (React.createElement(Controller, { initialValue: (_a = props.input.value) === null || _a === void 0 ? void 0 : _a[oneOfValue], spec: specProperties[oneOfValue], name: `${name}.${oneOfValue}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name}.${oneOfValue}` })) : null));
39
40
  };
@@ -5,6 +5,7 @@ const SECRET_PROPERTY_NAME = 'raw';
5
5
  export const Secret = ({ spec, name, input }) => {
6
6
  var _a;
7
7
  const parentOnChange = React.useCallback((childName, childValue, childErrors) => input.onChange((currentValue) => _.set(Object.assign({}, currentValue), childName.split(`${input.name}.`).join(''), childValue), childErrors), [input.onChange, input.name]);
8
+ const parentOnUnmount = React.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
8
9
  if (!_.isObjectLike(spec.properties)) {
9
10
  return null;
10
11
  }
@@ -12,5 +13,5 @@ export const Secret = ({ spec, name, input }) => {
12
13
  if (!specProperties[SECRET_PROPERTY_NAME]) {
13
14
  return null;
14
15
  }
15
- return (React.createElement(Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[SECRET_PROPERTY_NAME], spec: specProperties[SECRET_PROPERTY_NAME], name: `${name}.${SECRET_PROPERTY_NAME}`, parentOnChange: parentOnChange, key: `${name}.${SECRET_PROPERTY_NAME}` }));
16
+ return (React.createElement(Controller, { initialValue: (_a = input.value) === null || _a === void 0 ? void 0 : _a[SECRET_PROPERTY_NAME], spec: specProperties[SECRET_PROPERTY_NAME], name: `${name}.${SECRET_PROPERTY_NAME}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount, key: `${name}.${SECRET_PROPERTY_NAME}` }));
16
17
  };
@@ -20,6 +20,7 @@ export const TableArrayInput = ({ spec, name, arrayInput, input }) => {
20
20
  arrayInput.onItemRemove(key);
21
21
  }, [arrayInput.onItemRemove]);
22
22
  const parentOnChange = React.useCallback((childName, childValue, childErrors) => input.onChange((currentValue) => _.set(Object.assign({}, currentValue), childName.split(`${input.name}.`).join(''), childValue), childErrors), [input.onChange, input.name]);
23
+ const parentOnUnmount = React.useCallback((childName) => input.onChange((currentValue) => currentValue, { [childName]: false }), [input.onChange]);
23
24
  const columns = React.useMemo(() => {
24
25
  const { items, viewSpec: { table }, } = spec;
25
26
  if (!(table === null || table === void 0 ? void 0 : table.length) || !isObjectSpec(items)) {
@@ -52,11 +53,11 @@ export const TableArrayInput = ({ spec, name, arrayInput, input }) => {
52
53
  arr: isArraySpec(entitySpec),
53
54
  obj: isObjectSpec(entitySpec),
54
55
  }), key: `${name}.<${key}>.${property}` },
55
- React.createElement(Controller, { initialValue: (_c = (_b = input.value) === null || _b === void 0 ? void 0 : _b[`<${key}>`]) === null || _c === void 0 ? void 0 : _c[property], spec: entitySpec, name: `${name}.<${key}>.${property}`, parentOnChange: parentOnChange })));
56
+ React.createElement(Controller, { initialValue: (_c = (_b = input.value) === null || _b === void 0 ? void 0 : _b[`<${key}>`]) === null || _c === void 0 ? void 0 : _c[property], spec: entitySpec, name: `${name}.<${key}>.${property}`, parentOnChange: parentOnChange, parentOnUnmount: parentOnUnmount })));
56
57
  },
57
58
  }));
58
59
  return [idxColumn, ...columns, removeColumn];
59
- }, [name, spec, onItemRemove, parentOnChange, input.value]);
60
+ }, [name, spec, onItemRemove, parentOnChange, parentOnUnmount, input.value]);
60
61
  if (!columns) {
61
62
  return null;
62
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/dynamic-forms",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/cjs/index.js",