@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
@@ -1,162 +0,0 @@
1
- import React from 'react';
2
- import _ from 'lodash';
3
- import { isArraySpec, isNumberSpec, isObjectSpec } from '../../../helpers';
4
- import { OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG } from '../constants';
5
- import { isArrayItem, transformArrIn, transformArrOut } from '../utils';
6
- export const useField = ({ name, spec, initialValue, value: externalValue, validate: propsValidate, tools, parentOnChange, parentOnUnmount: externalParentOnUnmount, externalErrors, }) => {
7
- const firstRenderRef = React.useRef(true);
8
- const validate = React.useCallback((value) => propsValidate === null || propsValidate === void 0 ? void 0 : propsValidate(transformArrOut(value)), [propsValidate]);
9
- const [state, setState] = React.useState(() => {
10
- let value = _.cloneDeep(externalValue);
11
- if (_.isNil(value)) {
12
- if (spec.defaultValue) {
13
- value = transformArrIn(spec.defaultValue);
14
- }
15
- // if the spec with type array or object, and this spec has "required === true",
16
- // we immediately exclude empty value
17
- else if (spec.required) {
18
- if (isArraySpec(spec)) {
19
- value = { [OBJECT_ARRAY_FLAG]: true, [OBJECT_ARRAY_CNT]: 0 };
20
- }
21
- else if (isObjectSpec(spec)) {
22
- value = {};
23
- }
24
- }
25
- }
26
- let externalError = _.get(externalErrors, name);
27
- if (!(_.isString(externalError) ||
28
- _.isBoolean(externalError) ||
29
- _.isUndefined(externalError))) {
30
- externalError = undefined;
31
- }
32
- const error = (validate === null || validate === void 0 ? void 0 : validate(value)) || externalError;
33
- const dirty = !_.isEqual(value, initialValue);
34
- return {
35
- active: false,
36
- dirty,
37
- error,
38
- invalid: Boolean(error),
39
- modified: dirty,
40
- pristine: true,
41
- touched: false,
42
- valid: !error,
43
- value,
44
- visited: false,
45
- childErrors: {},
46
- };
47
- });
48
- const { onChange, onDrop } = React.useMemo(() => {
49
- const onChange = (valOrSetter, childErrors) => {
50
- setState((state) => {
51
- const _value = _.isFunction(valOrSetter) ? valOrSetter(state.value) : valOrSetter;
52
- const error = validate === null || validate === void 0 ? void 0 : validate(_value);
53
- let value = transformArrIn(_value);
54
- if (isNumberSpec(spec) && !error) {
55
- value = (value ? Number(value) : undefined);
56
- }
57
- let newChildErrors = Object.assign({}, state.childErrors);
58
- if (childErrors) {
59
- const nearestChildName = _.keys(childErrors).sort((a, b) => a.length - b.length)[0];
60
- if (nearestChildName) {
61
- const existingСhildNames = _.keys(newChildErrors).filter((childName) => childName.startsWith(nearestChildName));
62
- newChildErrors = Object.assign(Object.assign({}, _.omit(newChildErrors, existingСhildNames)), childErrors);
63
- }
64
- }
65
- return Object.assign(Object.assign({}, state), { dirty: !_.isEqual(value, initialValue), error, invalid: Boolean(error), modified: true, pristine: value === initialValue, touched: true, valid: !error, value, visited: true, childErrors: newChildErrors });
66
- });
67
- };
68
- const onDrop = () => {
69
- if (isArrayItem(name)) {
70
- (externalParentOnUnmount ? externalParentOnUnmount : tools.onUnmount)(name);
71
- }
72
- else {
73
- onChange(undefined, { [name]: false });
74
- }
75
- };
76
- return { onChange, onDrop };
77
- }, [initialValue, setState, name, validate, spec, externalParentOnUnmount, tools.onUnmount]);
78
- const onBlur = React.useCallback(() => {
79
- setState((state) => (Object.assign(Object.assign({}, state), { active: false, touched: true })));
80
- }, [setState]);
81
- const onFocus = React.useCallback(() => {
82
- setState((state) => (Object.assign(Object.assign({}, state), { active: true, visited: true })));
83
- }, [setState]);
84
- const parentOnUnmount = React.useCallback((childName) => {
85
- if (isArraySpec(spec) || isObjectSpec(spec)) {
86
- onChange((currentValue) => currentValue
87
- ? _.omit(currentValue, childName.split(`${name}.`)[1])
88
- : currentValue, {
89
- [childName]: false,
90
- });
91
- }
92
- }, [onChange, name, spec]);
93
- const renderProps = React.useMemo(() => {
94
- const onItemAdd = (_value) => {
95
- const stateValue = (state.value || {
96
- [OBJECT_ARRAY_FLAG]: true,
97
- [OBJECT_ARRAY_CNT]: 0,
98
- });
99
- const value = Object.assign(Object.assign({}, stateValue), { [`<${stateValue[OBJECT_ARRAY_CNT]}>`]: transformArrIn(_value), [OBJECT_ARRAY_CNT]: stateValue[OBJECT_ARRAY_CNT] + 1 });
100
- const error = validate === null || validate === void 0 ? void 0 : validate(value);
101
- setState((state) => (Object.assign(Object.assign({}, state), { dirty: !_.isEqual(value, initialValue), error, invalid: Boolean(error), modified: true, pristine: value === initialValue, touched: true, valid: !error, value, visited: true })));
102
- };
103
- const onItemRemove = (idx) => {
104
- parentOnUnmount(`${name}.<${idx}>`);
105
- };
106
- return {
107
- input: {
108
- name,
109
- value: state.value,
110
- onChange,
111
- onBlur,
112
- onFocus,
113
- onDrop,
114
- parentOnUnmount,
115
- },
116
- arrayInput: {
117
- name,
118
- value: state.value,
119
- onItemAdd,
120
- onItemRemove,
121
- onDrop,
122
- },
123
- meta: Object.assign(Object.assign({}, _.omit(state, 'value')), { submitFailed: tools.submitFailed }),
124
- };
125
- }, [
126
- state,
127
- setState,
128
- validate,
129
- name,
130
- initialValue,
131
- tools.submitFailed,
132
- onChange,
133
- onBlur,
134
- onFocus,
135
- onDrop,
136
- parentOnUnmount,
137
- ]);
138
- React.useEffect(() => {
139
- if (!firstRenderRef.current || !_.isEqual(externalValue, state.value) || state.error) {
140
- (parentOnChange ? parentOnChange : tools.onChange)(name, state.value, Object.assign(Object.assign({}, state.childErrors), { [name]: state.error }));
141
- }
142
- }, [state.value]);
143
- React.useEffect(() => {
144
- const externalError = _.get(externalErrors, name);
145
- if (!firstRenderRef.current &&
146
- (_.isString(externalError) ||
147
- _.isBoolean(externalError) ||
148
- _.isUndefined(externalError)) &&
149
- state.error !== externalError &&
150
- !(state.error && !externalError)) {
151
- setState(Object.assign(Object.assign({}, state), { error: externalError }));
152
- (parentOnChange ? parentOnChange : tools.onChange)(name, state.value, Object.assign(Object.assign({}, state.childErrors), { [name]: externalError }));
153
- }
154
- }, [externalErrors]);
155
- React.useEffect(() => {
156
- firstRenderRef.current = false;
157
- return () => {
158
- (externalParentOnUnmount ? externalParentOnUnmount : tools.onUnmount)(name);
159
- };
160
- }, []);
161
- return renderProps;
162
- };
@@ -1,9 +0,0 @@
1
- import { Spec } from '../../../types';
2
- import { FieldRenderProps, FieldValue, IndependentInputEntity, InputEntity, LayoutType } from '../types';
3
- export interface UseRenderParams<Value extends FieldValue, SpecType extends Spec> {
4
- name: string;
5
- spec: SpecType;
6
- inputEntity?: InputEntity<Value, SpecType> | IndependentInputEntity<Value, SpecType>;
7
- Layout?: LayoutType<Value, SpecType>;
8
- }
9
- export declare const useRender: <Value extends FieldValue, SpecType extends Spec>({ name, spec, inputEntity, Layout, }: UseRenderParams<Value, SpecType>) => (props: FieldRenderProps<Value>) => JSX.Element | null;
@@ -1,23 +0,0 @@
1
- import React from 'react';
2
- import _ from 'lodash';
3
- import { isCorrectSpec } from '../../../helpers';
4
- export const useRender = ({ name, spec, inputEntity, Layout, }) => {
5
- const render = React.useCallback((props) => {
6
- if (inputEntity && isCorrectSpec(spec) && _.isString(name)) {
7
- if (!spec.viewSpec.hidden) {
8
- if (inputEntity.independent) {
9
- const InputComponent = inputEntity.Component;
10
- return (React.createElement(InputComponent, Object.assign({ spec: spec, name: name, Layout: Layout }, props)));
11
- }
12
- const InputComponent = inputEntity.Component;
13
- const input = React.createElement(InputComponent, Object.assign({ spec: spec, name: name }, props));
14
- if (Layout) {
15
- return (React.createElement(Layout, Object.assign({ spec: spec, name: name }, props), input));
16
- }
17
- return input;
18
- }
19
- }
20
- return null;
21
- }, [spec, name, inputEntity, Layout]);
22
- return render;
23
- };
@@ -1,3 +0,0 @@
1
- import { FormValue, Spec } from '../../../types';
2
- import { FieldValue } from '../types';
3
- export declare const useValidate: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>(spec: SpecType) => ((value?: Value | undefined) => import("../types").ValidateError) | undefined;
@@ -1,27 +0,0 @@
1
- import React from 'react';
2
- import _ from 'lodash';
3
- import { isCorrectSpec } from '../../../helpers';
4
- import { isCorrectConfig } from '../utils';
5
- import { useDynamicFormsCtx } from './';
6
- export const useValidate = (spec) => {
7
- const { config } = useDynamicFormsCtx();
8
- const { validators } = React.useMemo(() => {
9
- if (isCorrectConfig(config) && isCorrectSpec(spec)) {
10
- return config[spec.type];
11
- }
12
- return {};
13
- }, [config, spec]);
14
- const validate = React.useMemo(() => {
15
- if (validators) {
16
- if ((!_.isString(spec.validator) || !spec.validator.length) &&
17
- _.isFunction(validators.base)) {
18
- return (value) => validators.base(spec, value);
19
- }
20
- if (_.isString(spec.validator) && _.isFunction(validators[spec.validator])) {
21
- return (value) => validators[spec.validator](spec, value);
22
- }
23
- }
24
- return;
25
- }, [validators, spec]);
26
- return validate;
27
- };