-
Notifications
You must be signed in to change notification settings - Fork 23
/
AutoForm.js
124 lines (111 loc) · 4.08 KB
/
AutoForm.js
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
import React from 'react';
import { object, string, func, bool } from 'prop-types';
import AutoFormInternal from './AutoFormInternal';
import MetadataProvider from './metadata/MetadataProvider';
import MetadataValidator from './metadata/validator/MetadataValidator';
import ModelParser from './metadata/model/ModelParser';
export default class AutoForm extends React.Component {
static propTypes = {
componentFactory: object,
schema: object.isRequired,
entityName: string,
layoutName: string,
errorRenderer: func,
buttonBar: func.isRequired,
fieldLayout: string,
// Redux-Form props
form: string.isRequired,
onSubmit: func.isRequired,
onSubmitSuccess: func,
onSubmitFail: func,
alwaysAsyncValidate: bool,
destroyOnUnmount: bool,
formKey: string,
initialValues: object,
overwriteOnInitialValuesChange: bool,
readonly: bool,
returnRejectedSubmitPromise: bool,
touchOnBlur: bool,
touchOnChange: bool
};
render() {
let {uiType, schema, entityName, layoutName, componentFactory, errorRenderer, buttonBar, fieldLayout} = this.props;
let {
form,
onSubmit,
onSubmitSuccess,
onSubmitFail,
alwaysAsyncValidate,
destroyOnUnmount,
formKey,
initialValues,
overwriteOnInitialValuesChange,
readonly,
returnRejectedSubmitPromise,
touchOnBlur,
touchOnChange,
dirty,
pristine,
submitting,
valid,
values
} = this.props;
try {
schema = MetadataProvider.canonizeSchema(schema); // This will allow for flexible schema definition (arrays vs. objects)
let {entity, layout} = MetadataProvider.getEntityAndLayout(schema, entityName, layoutName);
let fieldMetadata = MetadataProvider.getFields(schema, entity, layout, f => {
f.componentFactory = componentFactory;
f.fieldLayout = fieldLayout;
});
let fields = MetadataProvider.getReduxFormFields(fieldMetadata);
let validate = (values) => {
let modelParsed = ModelParser.process(values, fieldMetadata);
return MetadataValidator.validate(fieldMetadata, modelParsed) || {};
};
let autoFormProps = {
uiType,
fields,
fieldMetadata,
entity,
layout,
validate,
componentFactory,
buttonBar,
fieldLayout
};
let reduxFormProps = {
form,
onSubmit,
onSubmitSuccess,
onSubmitFail,
alwaysAsyncValidate,
destroyOnUnmount,
formKey,
initialValues,
overwriteOnInitialValuesChange,
readonly,
returnRejectedSubmitPromise,
touchOnBlur,
touchOnChange,
dirty,
pristine,
submitting,
valid,
values
};
// we need to delete all undefined reduxFormProps specifically because overwriteOnInitialValuesChange cannot
// be undefined, otherwise it triggers this errors:
// Failed prop type: Required prop `overwriteOnInitialValuesChange` was not specified in `ReduxForm(AutoFormInternal)`.
for (let property in reduxFormProps) {
if (reduxFormProps.hasOwnProperty(property)) {
if (reduxFormProps[property] === undefined) {
delete(reduxFormProps[property]);
}
}
}
return <AutoFormInternal {...autoFormProps} {...reduxFormProps}/>
} catch (ex) {
return errorRenderer ? errorRenderer(ex) : <div> {ex.message} </div>;
}
}
}