-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathform-renderer.js
More file actions
156 lines (145 loc) · 4.56 KB
/
Copy pathform-renderer.js
File metadata and controls
156 lines (145 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, {useState, useRef, useReducer} from 'react';
import Form from './form';
import arrayMutators from 'final-form-arrays';
import PropTypes from 'prop-types';
import createFocusDecorator from 'final-form-focus';
import RendererContext from './renderer-context';
import renderForm from '../form-renderer/render-form';
import defaultSchemaValidator from './default-schema-validator';
import SchemaErrorComponent from '../form-renderer/schema-error-component';
import defaultValidatorMapper from './validator-mapper';
import RegisterConditions from './register-conditions';
import SetFieldValues from './set-field-values';
import uiStateReducer from './ui-state-reducer';
const FormRenderer = ({
componentMapper,
FormTemplate,
onSubmit,
onCancel,
onReset,
clearOnUnmount,
subscription,
clearedValue,
schema,
validatorMapper,
actionMapper,
schemaValidatorMapper,
...props
}) => {
const [fileInputs, setFileInputs] = useState([]);
const [uiState, dispatchUIState] = useReducer(uiStateReducer, {
fields: {},
setFieldValues: {},
});
const focusDecorator = useRef(createFocusDecorator());
let schemaError;
const validatorMapperMerged = {...defaultValidatorMapper, ...validatorMapper};
try {
const validatorTypes = Object.keys(validatorMapperMerged);
const actionTypes = actionMapper ? Object.keys(actionMapper) : [];
defaultSchemaValidator(
schema,
componentMapper,
validatorTypes,
actionTypes,
schemaValidatorMapper
);
} catch (error) {
schemaError = error;
console.error(error);
console.log('error: ', error.message);
}
if (schemaError) {
return <SchemaErrorComponent name={schemaError.name} message={schemaError.message} />;
}
const registerInputFile = name => setFileInputs(prevFiles => [...prevFiles, name]);
const unRegisterInputFile = name =>
setFileInputs(prevFiles => [...prevFiles.splice(prevFiles.indexOf(name))]);
return (
<Form
{...props}
onSubmit={(values, formApi, ...args) => onSubmit(values, {...formApi, fileInputs}, ...args)}
mutators={{...arrayMutators}}
decorators={[focusDecorator.current]}
subscription={{pristine: true, submitting: true, valid: true, ...subscription}}
render={({
handleSubmit,
pristine,
valid,
form: {reset, mutators, getState, submit, registerField, ...form},
}) => (
<RendererContext.Provider
value={{
componentMapper,
validatorMapper: validatorMapperMerged,
actionMapper,
formOptions: {
registerInputFile,
unRegisterInputFile,
pristine,
onSubmit,
onCancel: onCancel ? (...args) => onCancel(getState().values, ...args) : undefined,
onReset: (...args) => {
onReset && onReset(...args);
reset();
},
getState,
registerField,
uiState,
dispatchUIState,
valid,
clearedValue,
submit,
handleSubmit,
reset,
clearOnUnmount,
renderForm,
...mutators,
...form,
},
}}
>
<RegisterConditions schema={schema} />
<SetFieldValues />
<FormTemplate formFields={renderForm(schema.fields)} schema={schema} />
<pre>{JSON.stringify(uiState, null, 2)}</pre>
</RendererContext.Provider>
)}
/>
);
};
FormRenderer.propTypes = {
onSubmit: PropTypes.func.isRequired,
onCancel: PropTypes.func,
onReset: PropTypes.func,
schema: PropTypes.object.isRequired,
clearOnUnmount: PropTypes.bool,
subscription: PropTypes.shape({[PropTypes.string]: PropTypes.bool}),
clearedValue: PropTypes.any,
componentMapper: PropTypes.shape({
[PropTypes.string]: PropTypes.oneOfType([PropTypes.node, PropTypes.element, PropTypes.func]),
}).isRequired,
FormTemplate: PropTypes.func.isRequired,
validatorMapper: PropTypes.shape({
[PropTypes.string]: PropTypes.func,
}),
actionMapper: PropTypes.shape({
[PropTypes.string]: PropTypes.func,
}),
schemaValidatorMapper: PropTypes.shape({
components: PropTypes.shape({
[PropTypes.string]: PropTypes.func,
}),
validators: PropTypes.shape({
[PropTypes.string]: PropTypes.func,
}),
actions: PropTypes.shape({
[PropTypes.string]: PropTypes.func,
}),
}),
};
FormRenderer.defaultProps = {
initialValues: {},
clearOnUnmount: false,
};
export default FormRenderer;