@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
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
.df-simple-vertical-accordeon_branch .df-simple-vertical-accordeon__body {
|
|
8
8
|
padding-left: 20px;
|
|
9
9
|
margin-left: 5px;
|
|
10
|
-
border-left: 1px solid var(--
|
|
10
|
+
border-left: 1px solid var(--g-color-line-generic-accent);
|
|
11
11
|
}
|
|
12
12
|
.df-simple-vertical-accordeon__header {
|
|
13
13
|
display: flex;
|
|
@@ -22,15 +22,15 @@
|
|
|
22
22
|
}
|
|
23
23
|
.df-simple-vertical-accordeon__title_size_s {
|
|
24
24
|
margin-bottom: 0;
|
|
25
|
-
font-size: var(--
|
|
25
|
+
font-size: var(--g-text-body-1-font-size);
|
|
26
26
|
}
|
|
27
27
|
.df-simple-vertical-accordeon__title_size_m {
|
|
28
28
|
margin-bottom: 1px;
|
|
29
|
-
font-size: var(--
|
|
29
|
+
font-size: var(--g-text-body2-font-size);
|
|
30
30
|
}
|
|
31
31
|
.df-simple-vertical-accordeon__title_size_l {
|
|
32
32
|
margin-bottom: 2px;
|
|
33
|
-
font-size: var(--
|
|
33
|
+
font-size: var(--g-text-body3-font-size);
|
|
34
34
|
}
|
|
35
35
|
.df-simple-vertical-accordeon__tooltip {
|
|
36
36
|
margin: 0px 5px;
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
white-space: nowrap;
|
|
19
19
|
overflow: hidden;
|
|
20
20
|
text-overflow: ellipsis;
|
|
21
|
-
color: var(--
|
|
21
|
+
color: var(--g-color-text-secondary);
|
|
22
22
|
}
|
|
23
23
|
.df-view-row__dots {
|
|
24
24
|
min-width: 40px;
|
|
25
25
|
flex-grow: 1;
|
|
26
26
|
margin: 0 2px;
|
|
27
|
-
border-bottom: 1px dotted var(--
|
|
27
|
+
border-bottom: 1px dotted var(--g-color-text-secondary);
|
|
28
28
|
}
|
|
29
29
|
.df-view-row__right {
|
|
30
30
|
max-width: 480px;
|
|
@@ -20,7 +20,7 @@ const ObjectValueInputView = (_a) => {
|
|
|
20
20
|
if (!childSpec) {
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
|
-
const content = (react_1.default.createElement(core_1.ViewController, { spec: childSpec, name: `${name}
|
|
23
|
+
const content = (react_1.default.createElement(core_1.ViewController, { spec: childSpec, name: `${name ? name + '.' : ''}${OBJECT_VALUE_PROPERTY_NAME}` }));
|
|
24
24
|
if (Layout) {
|
|
25
25
|
return (react_1.default.createElement(Layout, Object.assign({ spec: spec, name: name }, restProps), content));
|
|
26
26
|
}
|
|
@@ -6,79 +6,83 @@ const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
|
6
6
|
const validators_1 = require("../validators");
|
|
7
7
|
const helpers_1 = require("./helpers");
|
|
8
8
|
const getArrayValidator = (params = {}) => {
|
|
9
|
-
const { ignoreRequiredCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck } = params;
|
|
9
|
+
const { ignoreRequiredCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck, customErrorMessages } = params;
|
|
10
|
+
const errorMessages = Object.assign(Object.assign({}, validators_1.ErrorMessages), customErrorMessages);
|
|
10
11
|
return (spec, value) => {
|
|
11
12
|
const valueLength = (value === null || value === void 0 ? void 0 : value.length) || 0;
|
|
12
13
|
if (!ignoreRequiredCheck && spec.required && !lodash_1.default.isArray(value)) {
|
|
13
|
-
return
|
|
14
|
+
return errorMessages.REQUIRED;
|
|
14
15
|
}
|
|
15
16
|
if (!ignoreMaxLengthCheck &&
|
|
16
17
|
typeof spec.maxLength === 'bigint' &&
|
|
17
18
|
valueLength > spec.maxLength) {
|
|
18
|
-
return
|
|
19
|
+
return errorMessages.maxLengthArr(spec.maxLength);
|
|
19
20
|
}
|
|
20
21
|
if (!ignoreMinLengthCheck &&
|
|
21
22
|
typeof spec.minLength === 'bigint' &&
|
|
22
23
|
valueLength < spec.minLength) {
|
|
23
|
-
return
|
|
24
|
+
return errorMessages.minLengthArr(spec.minLength);
|
|
24
25
|
}
|
|
25
26
|
return false;
|
|
26
27
|
};
|
|
27
28
|
};
|
|
28
29
|
exports.getArrayValidator = getArrayValidator;
|
|
29
30
|
const getBooleanValidator = (params = {}) => {
|
|
30
|
-
const { ignoreRequiredCheck } = params;
|
|
31
|
+
const { ignoreRequiredCheck, customErrorMessages } = params;
|
|
32
|
+
const errorMessages = Object.assign(Object.assign({}, validators_1.ErrorMessages), customErrorMessages);
|
|
31
33
|
return (spec, value) => {
|
|
32
34
|
if (!ignoreRequiredCheck && spec.required && !value) {
|
|
33
|
-
return
|
|
35
|
+
return errorMessages.REQUIRED;
|
|
34
36
|
}
|
|
35
37
|
return false;
|
|
36
38
|
};
|
|
37
39
|
};
|
|
38
40
|
exports.getBooleanValidator = getBooleanValidator;
|
|
39
41
|
const getNumberValidator = (params = {}) => {
|
|
40
|
-
const { ignoreRequiredCheck, ignoreSpaceStartCheck, ignoreSpaceEndCheck, ignoreNumberCheck, ignoreMaximumCheck, ignoreMinimumCheck, ignoreIntCheck, ignoreDotEnd, ignoreZeroStart, } = params;
|
|
42
|
+
const { ignoreRequiredCheck, ignoreSpaceStartCheck, ignoreSpaceEndCheck, ignoreNumberCheck, ignoreMaximumCheck, ignoreMinimumCheck, ignoreIntCheck, ignoreDotEnd, ignoreZeroStart, customErrorMessages, } = params;
|
|
43
|
+
const errorMessages = Object.assign(Object.assign({}, validators_1.ErrorMessages), customErrorMessages);
|
|
44
|
+
// eslint-disable-next-line complexity
|
|
41
45
|
return (spec, value = '') => {
|
|
42
46
|
const stringValue = String(value);
|
|
43
47
|
if (!ignoreRequiredCheck && spec.required && !stringValue.length) {
|
|
44
|
-
return
|
|
48
|
+
return errorMessages.REQUIRED;
|
|
45
49
|
}
|
|
46
50
|
if (stringValue.length) {
|
|
47
51
|
if (!ignoreSpaceStartCheck && !stringValue[0].trim()) {
|
|
48
|
-
return
|
|
52
|
+
return errorMessages.SPACE_START;
|
|
49
53
|
}
|
|
50
54
|
if (!ignoreSpaceEndCheck && !stringValue[stringValue.length - 1].trim()) {
|
|
51
|
-
return
|
|
55
|
+
return errorMessages.SPACE_END;
|
|
52
56
|
}
|
|
53
57
|
if (!ignoreDotEnd && stringValue[stringValue.length - 1] === '.') {
|
|
54
|
-
return
|
|
58
|
+
return errorMessages.DOT_END;
|
|
55
59
|
}
|
|
56
60
|
if (!ignoreNumberCheck && !(0, helpers_1.isFloat)(stringValue)) {
|
|
57
|
-
return
|
|
61
|
+
return errorMessages.NUMBER;
|
|
58
62
|
}
|
|
59
63
|
if (!ignoreZeroStart &&
|
|
60
64
|
((stringValue.length > 1 && stringValue[0] === '0' && stringValue[1] !== '.') ||
|
|
61
65
|
(stringValue.length > 2 &&
|
|
62
66
|
stringValue.substring(0, 2) === '-0' &&
|
|
63
67
|
stringValue[2] !== '.'))) {
|
|
64
|
-
return
|
|
68
|
+
return errorMessages.ZERO_START;
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
if (!ignoreMaximumCheck &&
|
|
68
72
|
lodash_1.default.isNumber(spec.maximum) &&
|
|
69
73
|
stringValue.length &&
|
|
70
74
|
Number(stringValue) > spec.maximum) {
|
|
71
|
-
return
|
|
75
|
+
return errorMessages.maxNumber(spec.maximum);
|
|
72
76
|
}
|
|
73
77
|
if (!ignoreMinimumCheck &&
|
|
74
78
|
lodash_1.default.isNumber(spec.minimum) &&
|
|
75
79
|
stringValue.length &&
|
|
76
80
|
spec.minimum > Number(stringValue)) {
|
|
77
|
-
return
|
|
81
|
+
return errorMessages.minNumber(spec.minimum);
|
|
78
82
|
}
|
|
79
83
|
if (lodash_1.default.isString(spec.format) && stringValue.length) {
|
|
80
84
|
if (!ignoreIntCheck && spec.format === 'int64' && !(0, helpers_1.isInt)(stringValue)) {
|
|
81
|
-
return
|
|
85
|
+
return errorMessages.INT;
|
|
82
86
|
}
|
|
83
87
|
}
|
|
84
88
|
return false;
|
|
@@ -86,46 +90,49 @@ const getNumberValidator = (params = {}) => {
|
|
|
86
90
|
};
|
|
87
91
|
exports.getNumberValidator = getNumberValidator;
|
|
88
92
|
const getObjectValidator = (params = {}) => {
|
|
89
|
-
const { ignoreRequiredCheck } = params;
|
|
93
|
+
const { ignoreRequiredCheck, customErrorMessages } = params;
|
|
94
|
+
const errorMessages = Object.assign(Object.assign({}, validators_1.ErrorMessages), customErrorMessages);
|
|
90
95
|
return (spec, value) => {
|
|
91
96
|
if (!ignoreRequiredCheck && spec.required && !value) {
|
|
92
|
-
return
|
|
97
|
+
return errorMessages.REQUIRED;
|
|
93
98
|
}
|
|
94
99
|
return false;
|
|
95
100
|
};
|
|
96
101
|
};
|
|
97
102
|
exports.getObjectValidator = getObjectValidator;
|
|
98
103
|
const getStringValidator = (params = {}) => {
|
|
99
|
-
const { ignoreRequiredCheck, ignoreSpaceStartCheck, ignoreSpaceEndCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck, ignoreRegExpCheck, } = params;
|
|
104
|
+
const { ignoreRequiredCheck, ignoreSpaceStartCheck, ignoreSpaceEndCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck, ignoreRegExpCheck, customErrorMessages, } = params;
|
|
105
|
+
const errorMessages = Object.assign(Object.assign({}, validators_1.ErrorMessages), customErrorMessages);
|
|
106
|
+
// eslint-disable-next-line complexity
|
|
100
107
|
return (spec, value = '') => {
|
|
101
108
|
const valueLength = value === null || value === void 0 ? void 0 : value.length;
|
|
102
109
|
if (!ignoreRequiredCheck && spec.required && !valueLength) {
|
|
103
|
-
return
|
|
110
|
+
return errorMessages.REQUIRED;
|
|
104
111
|
}
|
|
105
112
|
if (valueLength) {
|
|
106
113
|
if (!ignoreSpaceStartCheck && !value[0].trim()) {
|
|
107
|
-
return
|
|
114
|
+
return errorMessages.SPACE_START;
|
|
108
115
|
}
|
|
109
116
|
if (!ignoreSpaceEndCheck && !value[value.length - 1].trim()) {
|
|
110
|
-
return
|
|
117
|
+
return errorMessages.SPACE_END;
|
|
111
118
|
}
|
|
112
119
|
}
|
|
113
120
|
if (!ignoreMaxLengthCheck &&
|
|
114
121
|
typeof spec.maxLength === 'bigint' &&
|
|
115
122
|
valueLength > spec.maxLength) {
|
|
116
|
-
return
|
|
123
|
+
return errorMessages.maxLength(spec.maxLength);
|
|
117
124
|
}
|
|
118
125
|
if (!ignoreMinLengthCheck &&
|
|
119
126
|
typeof spec.minLength === 'bigint' &&
|
|
120
127
|
valueLength < spec.minLength) {
|
|
121
|
-
return
|
|
128
|
+
return errorMessages.minLength(spec.minLength);
|
|
122
129
|
}
|
|
123
130
|
if (lodash_1.default.isString(spec.pattern) && spec.pattern.length) {
|
|
124
131
|
const regex = new RegExp(spec.pattern);
|
|
125
132
|
if (!ignoreRegExpCheck && !regex.test(value)) {
|
|
126
133
|
return lodash_1.default.isString(spec.patternError) && spec.patternError.length
|
|
127
134
|
? spec.patternError
|
|
128
|
-
:
|
|
135
|
+
: errorMessages.INVALID;
|
|
129
136
|
}
|
|
130
137
|
}
|
|
131
138
|
return false;
|
|
@@ -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;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import { useControllerMirror, useDynamicFormsCtx, useSearch } from '../hooks';
|
|
4
|
+
import { callUnmout, getFieldMethods, initializeStore, updateStore } from './utils';
|
|
5
|
+
export const Controller = ({ spec: _spec, name, value: valueFromParent, parentOnChange, parentOnUnmount, }) => {
|
|
6
|
+
const { config, tools, mutators, __mirror } = useDynamicFormsCtx();
|
|
7
|
+
const firstRenderRef = React.useRef(true);
|
|
8
|
+
const [store, setStore] = React.useState(initializeStore({
|
|
9
|
+
name,
|
|
10
|
+
spec: _spec,
|
|
11
|
+
mutators,
|
|
12
|
+
config,
|
|
13
|
+
valueFromParent,
|
|
14
|
+
tools,
|
|
15
|
+
parentOnChange,
|
|
16
|
+
parentOnUnmount,
|
|
17
|
+
}));
|
|
18
|
+
const { methods, fieldMethods } = React.useMemo(() => {
|
|
19
|
+
const fieldMethods = getFieldMethods();
|
|
20
|
+
const onChange = (valOrSetter, childErrors, errorMutator) => {
|
|
21
|
+
setStore((store) => fieldMethods.onChange(store, { valOrSetter, childErrors, errorMutator }));
|
|
22
|
+
};
|
|
23
|
+
const onDrop = () => {
|
|
24
|
+
setStore((store) => fieldMethods.onDrop(store, undefined));
|
|
25
|
+
};
|
|
26
|
+
const onBlur = () => {
|
|
27
|
+
setStore((store) => fieldMethods.onBlur(store, undefined));
|
|
28
|
+
};
|
|
29
|
+
const onFocus = () => {
|
|
30
|
+
setStore((store) => fieldMethods.onFocus(store, undefined));
|
|
31
|
+
};
|
|
32
|
+
const parentOnUnmount = (childName) => {
|
|
33
|
+
setStore((store) => fieldMethods.parentOnUnmount(store, childName));
|
|
34
|
+
};
|
|
35
|
+
const onItemAdd = (value) => {
|
|
36
|
+
setStore((store) => fieldMethods.onItemAdd(store, value));
|
|
37
|
+
};
|
|
38
|
+
const onItemRemove = (idx) => {
|
|
39
|
+
setStore((store) => fieldMethods.onItemRemove(store, idx));
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
methods: {
|
|
43
|
+
onChange,
|
|
44
|
+
onDrop,
|
|
45
|
+
onBlur,
|
|
46
|
+
onFocus,
|
|
47
|
+
parentOnUnmount,
|
|
48
|
+
onItemAdd,
|
|
49
|
+
onItemRemove,
|
|
50
|
+
},
|
|
51
|
+
fieldMethods,
|
|
52
|
+
};
|
|
53
|
+
}, [setStore]);
|
|
54
|
+
const renderProps = React.useMemo(() => ({
|
|
55
|
+
input: {
|
|
56
|
+
name: store.name,
|
|
57
|
+
value: store.state.value,
|
|
58
|
+
onChange: methods.onChange,
|
|
59
|
+
onBlur: methods.onBlur,
|
|
60
|
+
onFocus: methods.onFocus,
|
|
61
|
+
onDrop: methods.onDrop,
|
|
62
|
+
parentOnUnmount: methods.parentOnUnmount,
|
|
63
|
+
},
|
|
64
|
+
arrayInput: {
|
|
65
|
+
name: store.name,
|
|
66
|
+
value: store.state.value,
|
|
67
|
+
onItemAdd: methods.onItemAdd,
|
|
68
|
+
onItemRemove: methods.onItemRemove,
|
|
69
|
+
onDrop: methods.onDrop,
|
|
70
|
+
},
|
|
71
|
+
meta: Object.assign(Object.assign({}, _.omit(store.state, 'value')), { submitFailed: store.tools.submitFailed }),
|
|
72
|
+
}), [methods, store.name, store.state, store.tools.submitFailed]);
|
|
73
|
+
const withSearch = useSearch(store.spec, store.state.value, store.name);
|
|
74
|
+
useControllerMirror(store.name, {
|
|
75
|
+
useField: renderProps,
|
|
76
|
+
useSearch: withSearch,
|
|
77
|
+
}, __mirror);
|
|
78
|
+
React.useEffect(() => {
|
|
79
|
+
var _a;
|
|
80
|
+
(_a = store.afterStoreUpdateCB) === null || _a === void 0 ? void 0 : _a.call(store);
|
|
81
|
+
}, [store.afterStoreUpdateCB]);
|
|
82
|
+
React.useEffect(() => {
|
|
83
|
+
if (!firstRenderRef.current) {
|
|
84
|
+
updateStore({
|
|
85
|
+
store,
|
|
86
|
+
setStore,
|
|
87
|
+
spec: _spec,
|
|
88
|
+
name,
|
|
89
|
+
parentOnChange,
|
|
90
|
+
parentOnUnmount,
|
|
91
|
+
mutators,
|
|
92
|
+
config,
|
|
93
|
+
tools,
|
|
94
|
+
methodOnChange: fieldMethods.onChange,
|
|
95
|
+
valueFromParent,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}, [
|
|
99
|
+
_spec,
|
|
100
|
+
name,
|
|
101
|
+
parentOnChange,
|
|
102
|
+
parentOnUnmount,
|
|
103
|
+
mutators,
|
|
104
|
+
config,
|
|
105
|
+
tools.onChange,
|
|
106
|
+
tools.onUnmount,
|
|
107
|
+
]);
|
|
108
|
+
React.useEffect(() => {
|
|
109
|
+
firstRenderRef.current = false;
|
|
110
|
+
return () => {
|
|
111
|
+
callUnmout(store);
|
|
112
|
+
};
|
|
113
|
+
}, []);
|
|
114
|
+
return withSearch(store.render(renderProps));
|
|
115
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Controller';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Controller';
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { FormValue, Spec } from '../../../types';
|
|
3
|
+
import { DynamicFormConfig, DynamicFormMutators, DynamicFormsContext, FieldRenderProps, FieldValue, IndependentInputEntity, InputEntity, LayoutType, ValidateError } from '../types';
|
|
4
|
+
export interface GetSpecParams<SpecType extends Spec> {
|
|
5
|
+
name: string;
|
|
6
|
+
spec: SpecType;
|
|
7
|
+
mutators: DynamicFormMutators;
|
|
8
|
+
}
|
|
9
|
+
export interface GetComponentsParams<SpecType extends Spec> {
|
|
10
|
+
spec: SpecType;
|
|
11
|
+
config: DynamicFormConfig;
|
|
12
|
+
}
|
|
13
|
+
export interface GetComponentsReturn<DirtyValue extends FieldValue, SpecType extends Spec> {
|
|
14
|
+
inputEntity?: InputEntity<DirtyValue, SpecType> | IndependentInputEntity<DirtyValue, SpecType>;
|
|
15
|
+
Layout?: LayoutType<DirtyValue, SpecType>;
|
|
16
|
+
}
|
|
17
|
+
export interface GetRenderParams<DirtyValue extends FieldValue, SpecType extends Spec> {
|
|
18
|
+
name: string;
|
|
19
|
+
spec: SpecType;
|
|
20
|
+
inputEntity?: InputEntity<DirtyValue, SpecType> | IndependentInputEntity<DirtyValue, SpecType>;
|
|
21
|
+
Layout?: LayoutType<DirtyValue, SpecType>;
|
|
22
|
+
}
|
|
23
|
+
export interface GetValidateParams<SpecType extends Spec> {
|
|
24
|
+
spec: SpecType;
|
|
25
|
+
config: DynamicFormConfig;
|
|
26
|
+
}
|
|
27
|
+
export interface GetFieldInitialsParams<DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec> {
|
|
28
|
+
name: string;
|
|
29
|
+
spec: SpecType;
|
|
30
|
+
valueFromParent: DirtyValue;
|
|
31
|
+
initialValue: DirtyValue;
|
|
32
|
+
validate: (value?: Value) => ValidateError;
|
|
33
|
+
mutators: DynamicFormMutators;
|
|
34
|
+
}
|
|
35
|
+
export type FieldMethod<DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec, Params extends any = undefined> = (store: ControllerStore<DirtyValue, Value, SpecType>, params: Params) => ControllerStore<DirtyValue, Value, SpecType>;
|
|
36
|
+
export interface GetFieldMethodsReturn<DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec> {
|
|
37
|
+
onChange: FieldMethod<DirtyValue, Value, SpecType, {
|
|
38
|
+
valOrSetter: DirtyValue | ((currentValue: DirtyValue) => DirtyValue);
|
|
39
|
+
childErrors?: Record<string, ValidateError>;
|
|
40
|
+
errorMutator?: ValidateError;
|
|
41
|
+
}>;
|
|
42
|
+
onDrop: FieldMethod<DirtyValue, Value, SpecType>;
|
|
43
|
+
onBlur: FieldMethod<DirtyValue, Value, SpecType>;
|
|
44
|
+
onFocus: FieldMethod<DirtyValue, Value, SpecType>;
|
|
45
|
+
parentOnUnmount: FieldMethod<DirtyValue, Value, SpecType, string>;
|
|
46
|
+
onItemAdd: FieldMethod<DirtyValue, Value, SpecType, FieldValue>;
|
|
47
|
+
onItemRemove: FieldMethod<DirtyValue, Value, SpecType, number | string>;
|
|
48
|
+
}
|
|
49
|
+
export interface InitializeStoreParams<DirtyValue extends FieldValue, SpecType extends Spec> {
|
|
50
|
+
name: string;
|
|
51
|
+
spec: SpecType;
|
|
52
|
+
mutators: DynamicFormMutators;
|
|
53
|
+
config: DynamicFormConfig;
|
|
54
|
+
valueFromParent: DirtyValue;
|
|
55
|
+
tools: DynamicFormsContext['tools'];
|
|
56
|
+
parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
|
|
57
|
+
parentOnUnmount: ((childName: string) => void) | null;
|
|
58
|
+
}
|
|
59
|
+
export interface ControllerStore<DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec> {
|
|
60
|
+
name: string;
|
|
61
|
+
spec: SpecType;
|
|
62
|
+
initialSpec: SpecType;
|
|
63
|
+
config: DynamicFormConfig;
|
|
64
|
+
tools: DynamicFormsContext['tools'];
|
|
65
|
+
mutators: DynamicFormMutators;
|
|
66
|
+
render: (props: FieldRenderProps<DirtyValue>) => JSX.Element | null;
|
|
67
|
+
validate: (value?: Value) => ValidateError;
|
|
68
|
+
parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
|
|
69
|
+
parentOnUnmount: ((childName: string) => void) | null;
|
|
70
|
+
state: {
|
|
71
|
+
initialValue: DirtyValue;
|
|
72
|
+
active: boolean;
|
|
73
|
+
dirty: boolean;
|
|
74
|
+
error: ValidateError;
|
|
75
|
+
invalid: boolean;
|
|
76
|
+
modified: boolean;
|
|
77
|
+
pristine: boolean;
|
|
78
|
+
touched: boolean;
|
|
79
|
+
valid: boolean;
|
|
80
|
+
value: DirtyValue;
|
|
81
|
+
visited: boolean;
|
|
82
|
+
childErrors: Record<string, ValidateError>;
|
|
83
|
+
};
|
|
84
|
+
afterStoreUpdateCB?: () => void;
|
|
85
|
+
}
|
|
86
|
+
export interface UpdateStoreParams<DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec> {
|
|
87
|
+
store: ControllerStore<DirtyValue, Value, SpecType>;
|
|
88
|
+
setStore: (store: ControllerStore<DirtyValue, Value, SpecType>) => void;
|
|
89
|
+
spec: SpecType;
|
|
90
|
+
name: string;
|
|
91
|
+
parentOnChange: ((childName: string, childValue: FieldValue, childErrors: Record<string, ValidateError>) => void) | null;
|
|
92
|
+
parentOnUnmount: ((childName: string) => void) | null;
|
|
93
|
+
mutators: DynamicFormMutators;
|
|
94
|
+
valueFromParent: DirtyValue;
|
|
95
|
+
config: DynamicFormConfig;
|
|
96
|
+
tools: DynamicFormsContext['tools'];
|
|
97
|
+
methodOnChange: GetFieldMethodsReturn<DirtyValue, Value, SpecType>['onChange'];
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { FormValue, Spec } from '../../../types';
|
|
2
|
+
import { FieldRenderProps, FieldValue, ValidateError } from '../types';
|
|
3
|
+
import { ControllerStore, GetComponentsParams, GetComponentsReturn, GetFieldInitialsParams, GetFieldMethodsReturn, GetRenderParams, GetSpecParams, GetValidateParams, InitializeStoreParams, UpdateStoreParams } from './types';
|
|
4
|
+
export declare const updateParentStore: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>(store: ControllerStore<DirtyValue, Value, SpecType>) => void;
|
|
5
|
+
export declare const callUnmout: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>(store: ControllerStore<DirtyValue, Value, SpecType>) => void;
|
|
6
|
+
export declare const getSpec: <SpecType extends Spec>({ name, spec, mutators, }: GetSpecParams<SpecType>) => SpecType;
|
|
7
|
+
export declare const getComponents: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>({ spec, config, }: GetComponentsParams<SpecType>) => GetComponentsReturn<DirtyValue, SpecType>;
|
|
8
|
+
export declare const getRender: <DirtyValue extends FieldValue, SpecType extends Spec>({ name, spec, inputEntity, Layout, }: GetRenderParams<DirtyValue, SpecType>) => (props: FieldRenderProps<DirtyValue>) => JSX.Element | null;
|
|
9
|
+
export declare const getValidate: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>({ spec, config, }: GetValidateParams<SpecType>) => (value?: Value | undefined) => ValidateError;
|
|
10
|
+
export declare const getFieldInitials: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>({ name, spec, valueFromParent, initialValue, validate, mutators, }: GetFieldInitialsParams<DirtyValue, Value, SpecType>) => {
|
|
11
|
+
initialValue: DirtyValue;
|
|
12
|
+
active: boolean;
|
|
13
|
+
dirty: boolean;
|
|
14
|
+
error: string | boolean | import("../types").AsyncValidateError | undefined;
|
|
15
|
+
invalid: boolean;
|
|
16
|
+
modified: boolean;
|
|
17
|
+
pristine: boolean;
|
|
18
|
+
touched: boolean;
|
|
19
|
+
valid: boolean;
|
|
20
|
+
value: DirtyValue;
|
|
21
|
+
visited: boolean;
|
|
22
|
+
childErrors: {};
|
|
23
|
+
};
|
|
24
|
+
export declare const getFieldMethods: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>() => GetFieldMethodsReturn<DirtyValue, Value, SpecType>;
|
|
25
|
+
export declare const initializeStore: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>({ name, spec: _spec, mutators, config, valueFromParent, tools, parentOnChange, parentOnUnmount, }: InitializeStoreParams<DirtyValue, SpecType>) => ControllerStore<DirtyValue, Value, SpecType>;
|
|
26
|
+
export declare const updateStore: <DirtyValue extends FieldValue, Value extends FormValue, SpecType extends Spec>({ store, setStore, spec: _spec, name, parentOnChange, parentOnUnmount, mutators, valueFromParent, config, tools, methodOnChange, }: UpdateStoreParams<DirtyValue, Value, SpecType>) => void;
|