@gravity-ui/dynamic-forms 3.2.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/lib/core/components/Form/Controller/Controller.js +120 -0
- package/build/cjs/lib/core/components/Form/Controller/index.js +4 -0
- package/build/cjs/lib/core/components/Form/Controller/types.js +2 -0
- package/build/cjs/lib/core/components/Form/Controller/utils.js +305 -0
- package/build/cjs/lib/core/components/Form/hooks/index.js +0 -4
- package/build/cjs/lib/core/components/Form/hooks/useMutators.js +21 -1
- package/build/cjs/lib/core/components/Form/utils/common.js +1 -5
- package/build/cjs/lib/kit/components/AccordeonCard/AccordeonCard.css +6 -6
- package/build/cjs/lib/kit/components/Card/Card.css +3 -3
- package/build/cjs/lib/kit/components/ErrorWrapper/ErrorWrapper.css +4 -4
- package/build/cjs/lib/kit/components/GroupIndent/GroupIndent.css +1 -1
- package/build/cjs/lib/kit/components/Inputs/FileInput/FileInput.css +1 -1
- package/build/cjs/lib/kit/components/Inputs/MonacoInput/MonacoHeader.css +1 -1
- package/build/cjs/lib/kit/components/Inputs/MonacoInput/MonacoInputBase.css +1 -1
- package/build/cjs/lib/kit/components/Inputs/MonacoInput/MonacoInputDialog.css +1 -1
- package/build/cjs/lib/kit/components/Inputs/OneOf/OneOf.css +1 -1
- package/build/cjs/lib/kit/components/Inputs/TableArrayInput/TableArrayInput.css +1 -1
- package/build/cjs/lib/kit/components/Layouts/Row/Row.css +3 -3
- package/build/cjs/lib/kit/components/LongValue/LongValue.css +1 -1
- package/build/cjs/lib/kit/components/SimpleVerticalAccordeon/SimpleVerticalAccordeon.css +4 -4
- package/build/cjs/lib/kit/components/ViewLayouts/ViewRow/ViewRow.css +2 -2
- package/build/cjs/lib/kit/components/Views/MonacoInputView/MonacoBaseView.css +1 -1
- package/build/cjs/lib/kit/components/Views/MonacoInputView/MonacoViewDialog.css +1 -1
- package/build/cjs/lib/kit/components/Views/ObjectValueInputView/ObjectValueInputView.js +1 -1
- package/build/cjs/lib/kit/validators/types.js +2 -0
- package/build/cjs/lib/kit/validators/validators.js +32 -25
- package/build/esm/lib/core/components/Form/Controller/Controller.d.ts +10 -0
- package/build/esm/lib/core/components/Form/Controller/Controller.js +115 -0
- package/build/esm/lib/core/components/Form/Controller/index.d.ts +1 -0
- package/build/esm/lib/core/components/Form/Controller/index.js +1 -0
- package/build/esm/lib/core/components/Form/Controller/types.d.ts +98 -0
- package/build/esm/lib/core/components/Form/Controller/types.js +1 -0
- package/build/esm/lib/core/components/Form/Controller/utils.d.ts +26 -0
- package/build/esm/lib/core/components/Form/Controller/utils.js +291 -0
- package/build/esm/lib/core/components/Form/hooks/index.d.ts +0 -4
- package/build/esm/lib/core/components/Form/hooks/index.js +0 -4
- package/build/esm/lib/core/components/Form/hooks/useMutators.js +21 -1
- package/build/esm/lib/core/components/Form/types/mirror.d.ts +3 -5
- package/build/esm/lib/core/components/Form/types/mutators.d.ts +13 -2
- package/build/esm/lib/core/components/Form/utils/common.d.ts +0 -3
- package/build/esm/lib/core/components/Form/utils/common.js +0 -2
- package/build/esm/lib/kit/components/AccordeonCard/AccordeonCard.css +6 -6
- package/build/esm/lib/kit/components/Card/Card.css +3 -3
- package/build/esm/lib/kit/components/ErrorWrapper/ErrorWrapper.css +4 -4
- package/build/esm/lib/kit/components/GroupIndent/GroupIndent.css +1 -1
- package/build/esm/lib/kit/components/Inputs/FileInput/FileInput.css +1 -1
- package/build/esm/lib/kit/components/Inputs/MonacoInput/MonacoHeader.css +1 -1
- package/build/esm/lib/kit/components/Inputs/MonacoInput/MonacoInputBase.css +1 -1
- package/build/esm/lib/kit/components/Inputs/MonacoInput/MonacoInputDialog.css +1 -1
- package/build/esm/lib/kit/components/Inputs/OneOf/OneOf.css +1 -1
- package/build/esm/lib/kit/components/Inputs/TableArrayInput/TableArrayInput.css +1 -1
- package/build/esm/lib/kit/components/Layouts/Row/Row.css +3 -3
- package/build/esm/lib/kit/components/LongValue/LongValue.css +1 -1
- package/build/esm/lib/kit/components/SimpleVerticalAccordeon/SimpleVerticalAccordeon.css +4 -4
- package/build/esm/lib/kit/components/ViewLayouts/ViewRow/ViewRow.css +2 -2
- package/build/esm/lib/kit/components/Views/MonacoInputView/MonacoBaseView.css +1 -1
- package/build/esm/lib/kit/components/Views/MonacoInputView/MonacoViewDialog.css +1 -1
- package/build/esm/lib/kit/components/Views/ObjectValueInputView/ObjectValueInputView.js +1 -1
- package/build/esm/lib/kit/validators/messages.d.ts +2 -16
- package/build/esm/lib/kit/validators/types.d.ts +16 -0
- package/build/esm/lib/kit/validators/types.js +1 -0
- package/build/esm/lib/kit/validators/validators.d.ts +10 -9
- package/build/esm/lib/kit/validators/validators.js +32 -25
- package/package.json +10 -2
- package/build/cjs/lib/core/components/Form/Controller.js +0 -42
- package/build/cjs/lib/core/components/Form/hooks/useComponents.js +0 -40
- package/build/cjs/lib/core/components/Form/hooks/useField.js +0 -176
- package/build/cjs/lib/core/components/Form/hooks/useRender.js +0 -28
- package/build/cjs/lib/core/components/Form/hooks/useValidate.js +0 -32
- package/build/esm/lib/core/components/Form/Controller.d.ts +0 -10
- package/build/esm/lib/core/components/Form/Controller.js +0 -37
- package/build/esm/lib/core/components/Form/hooks/useComponents.d.ts +0 -6
- package/build/esm/lib/core/components/Form/hooks/useComponents.js +0 -35
- package/build/esm/lib/core/components/Form/hooks/useField.d.ts +0 -14
- package/build/esm/lib/core/components/Form/hooks/useField.js +0 -171
- package/build/esm/lib/core/components/Form/hooks/useRender.d.ts +0 -9
- package/build/esm/lib/core/components/Form/hooks/useRender.js +0 -23
- package/build/esm/lib/core/components/Form/hooks/useValidate.d.ts +0 -3
- package/build/esm/lib/core/components/Form/hooks/useValidate.js +0 -27
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import _ from 'lodash';
|
|
3
|
-
import { isValidElementType } from 'react-is';
|
|
4
|
-
import { isCorrectSpec } from '../../../helpers';
|
|
5
|
-
import { isCorrectConfig } from '../utils';
|
|
6
|
-
import { useDynamicFormsCtx } from './';
|
|
7
|
-
export const useComponents = (spec) => {
|
|
8
|
-
var _a, _b;
|
|
9
|
-
const { config } = useDynamicFormsCtx();
|
|
10
|
-
const { inputs, layouts } = React.useMemo(() => {
|
|
11
|
-
if (isCorrectConfig(config) && isCorrectSpec(spec)) {
|
|
12
|
-
return config[spec.type];
|
|
13
|
-
}
|
|
14
|
-
return {};
|
|
15
|
-
}, [config, spec]);
|
|
16
|
-
const inputEntity = React.useMemo(() => {
|
|
17
|
-
if (inputs) {
|
|
18
|
-
const entity = inputs[spec.viewSpec.type];
|
|
19
|
-
if (isValidElementType(entity === null || entity === void 0 ? void 0 : entity.Component)) {
|
|
20
|
-
return entity;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return;
|
|
24
|
-
}, [inputs, (_a = spec === null || spec === void 0 ? void 0 : spec.viewSpec) === null || _a === void 0 ? void 0 : _a.type]);
|
|
25
|
-
const Layout = React.useMemo(() => {
|
|
26
|
-
if (layouts && _.isString(spec.viewSpec.layout)) {
|
|
27
|
-
const Component = layouts[spec.viewSpec.layout];
|
|
28
|
-
if (isValidElementType(Component)) {
|
|
29
|
-
return Component;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return;
|
|
33
|
-
}, [layouts, (_b = spec === null || spec === void 0 ? void 0 : spec.viewSpec) === null || _b === void 0 ? void 0 : _b.layout]);
|
|
34
|
-
return { inputEntity, Layout };
|
|
35
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Spec } from '../../../types';
|
|
2
|
-
import { DynamicFormMutators, DynamicFormsContext, FieldRenderProps, FieldValue, ValidateError } from '../types';
|
|
3
|
-
export interface UseFieldProps<Value extends FieldValue, SpecType extends Spec> {
|
|
4
|
-
name: string;
|
|
5
|
-
spec: SpecType;
|
|
6
|
-
initialValue: Value;
|
|
7
|
-
value: Value;
|
|
8
|
-
validate?: (value?: Value) => ValidateError;
|
|
9
|
-
tools: DynamicFormsContext['tools'];
|
|
10
|
-
parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
|
|
11
|
-
parentOnUnmount: ((childName: string) => void) | null;
|
|
12
|
-
mutators: DynamicFormMutators;
|
|
13
|
-
}
|
|
14
|
-
export declare const useField: <Value extends FieldValue, SpecType extends Spec>({ name, spec, initialValue, value: externalValue, validate: propsValidate, tools, parentOnChange, parentOnUnmount: externalParentOnUnmount, mutators, }: UseFieldProps<Value, SpecType>) => FieldRenderProps<Value>;
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import _ from 'lodash';
|
|
3
|
-
import { isArraySpec, isNumberSpec, isObjectSpec } from '../../../helpers';
|
|
4
|
-
import { EMPTY_MUTATOR, OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG } from '../constants';
|
|
5
|
-
import { isArrayItem, isErrorMutatorCorrect, isValueMutatorCorrect, transformArrIn, transformArrOut, } from '../utils';
|
|
6
|
-
export const useField = ({ name, spec, initialValue, value: externalValue, validate: propsValidate, tools, parentOnChange, parentOnUnmount: externalParentOnUnmount, mutators, }) => {
|
|
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
|
-
const valueMutator = _.get(mutators.values, name, EMPTY_MUTATOR);
|
|
11
|
-
let value = _.cloneDeep(externalValue);
|
|
12
|
-
if (isValueMutatorCorrect(valueMutator, spec) && valueMutator !== EMPTY_MUTATOR) {
|
|
13
|
-
value = valueMutator;
|
|
14
|
-
}
|
|
15
|
-
if (_.isNil(value)) {
|
|
16
|
-
if (spec.defaultValue) {
|
|
17
|
-
value = transformArrIn(spec.defaultValue);
|
|
18
|
-
}
|
|
19
|
-
// if the spec with type array or object, and this spec has "required === true",
|
|
20
|
-
// we immediately exclude empty value
|
|
21
|
-
else if (spec.required) {
|
|
22
|
-
if (isArraySpec(spec)) {
|
|
23
|
-
value = { [OBJECT_ARRAY_FLAG]: true, [OBJECT_ARRAY_CNT]: 0 };
|
|
24
|
-
}
|
|
25
|
-
else if (isObjectSpec(spec)) {
|
|
26
|
-
value = {};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
let errorMutator = _.get(mutators.errors, name);
|
|
31
|
-
if (!isErrorMutatorCorrect(errorMutator)) {
|
|
32
|
-
errorMutator = undefined;
|
|
33
|
-
}
|
|
34
|
-
const error = (validate === null || validate === void 0 ? void 0 : validate(value)) || errorMutator;
|
|
35
|
-
const dirty = !_.isEqual(value, initialValue);
|
|
36
|
-
return {
|
|
37
|
-
active: false,
|
|
38
|
-
dirty,
|
|
39
|
-
error,
|
|
40
|
-
invalid: Boolean(error),
|
|
41
|
-
modified: dirty,
|
|
42
|
-
pristine: true,
|
|
43
|
-
touched: false,
|
|
44
|
-
valid: !error,
|
|
45
|
-
value,
|
|
46
|
-
visited: false,
|
|
47
|
-
childErrors: {},
|
|
48
|
-
};
|
|
49
|
-
});
|
|
50
|
-
const { onChange, onLocalChange, onDrop } = React.useMemo(() => {
|
|
51
|
-
const onLocalChange = (valOrSetter, childErrors, errorMutator) => {
|
|
52
|
-
setState((state) => {
|
|
53
|
-
const _value = _.isFunction(valOrSetter) ? valOrSetter(state.value) : valOrSetter;
|
|
54
|
-
const error = (validate === null || validate === void 0 ? void 0 : validate(_value)) || errorMutator;
|
|
55
|
-
let value = transformArrIn(_value);
|
|
56
|
-
if (isNumberSpec(spec) && !error) {
|
|
57
|
-
value = (value ? Number(value) : undefined);
|
|
58
|
-
}
|
|
59
|
-
let newChildErrors = Object.assign({}, state.childErrors);
|
|
60
|
-
if (childErrors) {
|
|
61
|
-
const nearestChildName = _.keys(childErrors).sort((a, b) => a.length - b.length)[0];
|
|
62
|
-
if (nearestChildName) {
|
|
63
|
-
const existingСhildNames = _.keys(newChildErrors).filter((childName) => childName.startsWith(nearestChildName));
|
|
64
|
-
newChildErrors = Object.assign(Object.assign({}, _.omit(newChildErrors, existingСhildNames)), childErrors);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
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 });
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
const onChange = (valOrSetter, childErrors) => onLocalChange(valOrSetter, childErrors);
|
|
71
|
-
const onDrop = () => {
|
|
72
|
-
if (isArrayItem(name)) {
|
|
73
|
-
(externalParentOnUnmount ? externalParentOnUnmount : tools.onUnmount)(name);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
onChange(undefined, { [name]: false });
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
return { onChange, onLocalChange, onDrop };
|
|
80
|
-
}, [initialValue, setState, name, validate, spec, externalParentOnUnmount, tools.onUnmount]);
|
|
81
|
-
const onBlur = React.useCallback(() => {
|
|
82
|
-
setState((state) => (Object.assign(Object.assign({}, state), { active: false, touched: true })));
|
|
83
|
-
}, [setState]);
|
|
84
|
-
const onFocus = React.useCallback(() => {
|
|
85
|
-
setState((state) => (Object.assign(Object.assign({}, state), { active: true, visited: true })));
|
|
86
|
-
}, [setState]);
|
|
87
|
-
const parentOnUnmount = React.useCallback((childName) => {
|
|
88
|
-
if (isArraySpec(spec) || isObjectSpec(spec)) {
|
|
89
|
-
onChange((currentValue) => currentValue
|
|
90
|
-
? _.omit(currentValue, childName.split(`${name}.`)[1])
|
|
91
|
-
: currentValue, {
|
|
92
|
-
[childName]: false,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}, [onChange, name, spec]);
|
|
96
|
-
const renderProps = React.useMemo(() => {
|
|
97
|
-
const onItemAdd = (_value) => {
|
|
98
|
-
const stateValue = (state.value || {
|
|
99
|
-
[OBJECT_ARRAY_FLAG]: true,
|
|
100
|
-
[OBJECT_ARRAY_CNT]: 0,
|
|
101
|
-
});
|
|
102
|
-
const value = Object.assign(Object.assign({}, stateValue), { [`<${stateValue[OBJECT_ARRAY_CNT]}>`]: transformArrIn(_value), [OBJECT_ARRAY_CNT]: stateValue[OBJECT_ARRAY_CNT] + 1 });
|
|
103
|
-
const error = validate === null || validate === void 0 ? void 0 : validate(value);
|
|
104
|
-
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 })));
|
|
105
|
-
};
|
|
106
|
-
const onItemRemove = (idx) => {
|
|
107
|
-
parentOnUnmount(`${name}.<${idx}>`);
|
|
108
|
-
};
|
|
109
|
-
return {
|
|
110
|
-
input: {
|
|
111
|
-
name,
|
|
112
|
-
value: state.value,
|
|
113
|
-
onChange,
|
|
114
|
-
onBlur,
|
|
115
|
-
onFocus,
|
|
116
|
-
onDrop,
|
|
117
|
-
parentOnUnmount,
|
|
118
|
-
},
|
|
119
|
-
arrayInput: {
|
|
120
|
-
name,
|
|
121
|
-
value: state.value,
|
|
122
|
-
onItemAdd,
|
|
123
|
-
onItemRemove,
|
|
124
|
-
onDrop,
|
|
125
|
-
},
|
|
126
|
-
meta: Object.assign(Object.assign({}, _.omit(state, 'value')), { submitFailed: tools.submitFailed }),
|
|
127
|
-
};
|
|
128
|
-
}, [
|
|
129
|
-
state,
|
|
130
|
-
setState,
|
|
131
|
-
validate,
|
|
132
|
-
name,
|
|
133
|
-
initialValue,
|
|
134
|
-
tools.submitFailed,
|
|
135
|
-
onChange,
|
|
136
|
-
onBlur,
|
|
137
|
-
onFocus,
|
|
138
|
-
onDrop,
|
|
139
|
-
parentOnUnmount,
|
|
140
|
-
]);
|
|
141
|
-
React.useEffect(() => {
|
|
142
|
-
if (!firstRenderRef.current || !_.isEqual(externalValue, state.value) || state.error) {
|
|
143
|
-
(parentOnChange ? parentOnChange : tools.onChange)(name, state.value, Object.assign(Object.assign({}, state.childErrors), { [name]: state.error }));
|
|
144
|
-
}
|
|
145
|
-
}, [state.value]);
|
|
146
|
-
React.useEffect(() => {
|
|
147
|
-
if (!firstRenderRef.current) {
|
|
148
|
-
const valueMutator = _.get(mutators.values, name, EMPTY_MUTATOR);
|
|
149
|
-
let errorMutator = _.get(mutators.errors, name);
|
|
150
|
-
if (!isErrorMutatorCorrect(errorMutator)) {
|
|
151
|
-
errorMutator = undefined;
|
|
152
|
-
}
|
|
153
|
-
if (isValueMutatorCorrect(valueMutator, spec) &&
|
|
154
|
-
valueMutator !== state.value &&
|
|
155
|
-
valueMutator !== EMPTY_MUTATOR) {
|
|
156
|
-
onLocalChange(valueMutator, undefined, errorMutator);
|
|
157
|
-
}
|
|
158
|
-
else if (state.error !== errorMutator && !(state.error && !errorMutator)) {
|
|
159
|
-
setState(Object.assign(Object.assign({}, state), { error: errorMutator }));
|
|
160
|
-
(parentOnChange ? parentOnChange : tools.onChange)(name, state.value, Object.assign(Object.assign({}, state.childErrors), { [name]: errorMutator }));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}, [mutators]);
|
|
164
|
-
React.useEffect(() => {
|
|
165
|
-
firstRenderRef.current = false;
|
|
166
|
-
return () => {
|
|
167
|
-
(externalParentOnUnmount ? externalParentOnUnmount : tools.onUnmount)(name);
|
|
168
|
-
};
|
|
169
|
-
}, []);
|
|
170
|
-
return renderProps;
|
|
171
|
-
};
|
|
@@ -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
|
-
};
|