-
Notifications
You must be signed in to change notification settings - Fork 96
/
FormBuilder.js
449 lines (405 loc) · 13.1 KB
/
FormBuilder.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import merge from 'merge';
import schemaFieldValues, { schemaMerge } from 'lib/schemaFieldValues';
import { createErrorBlock } from 'lib/createErrorBlock';
import backend from 'lib/Backend';
import { withInjector } from 'lib/Injector';
class FormBuilder extends Component {
constructor(props) {
super(props);
const schemaStructure = props.schema.schema;
this.state = { submittingAction: null };
this.submitApi = backend.createEndpointFetcher({
url: schemaStructure.attributes.action,
method: schemaStructure.attributes.method,
});
this.mapActionsToComponents = this.mapActionsToComponents.bind(this);
this.mapFieldsToComponents = this.mapFieldsToComponents.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleAction = this.handleAction.bind(this);
this.buildComponent = this.buildComponent.bind(this);
this.validateForm = this.validateForm.bind(this);
}
getComponent({ name, schemaComponent, schemaType }) {
const { identifier, getCustomFields } = this.props;
if (getCustomFields) {
const component = getCustomFields(schemaType, `${identifier}.${name}`);
if (component) {
return component;
}
}
if (schemaComponent !== null) {
return this.context.injector.get(schemaComponent, `${identifier}.${name}`);
}
return this.getComponentForDataType(schemaType, name);
}
/**
* Default data type to component mappings.
* Used as a fallback when no component type is provided in the form schema.
*
* @param {string} dataType - The data type provided by the form schema.
* @param {string} name - name of the field component
* @return object|null
*/
getComponentForDataType(dataType, name) {
const { identifier } = this.props;
const get = (type) => this.context.injector.get(type, `${identifier}.${name}`);
switch (dataType) {
case 'Integer':
case 'Decimal':
return get('NumberField');
case 'String':
case 'Text':
return get('TextField');
case 'Date':
return get('DateField');
case 'Time':
return get('TimeField');
case 'Datetime':
return get('DatetimeField');
case 'Hidden':
return get('HiddenField');
case 'SingleSelect':
return get('SingleSelectField');
case 'Custom':
return get('GridField');
case 'Structural':
return get('CompositeField');
case 'Boolean':
return get('CheckboxField');
case 'MultiSelect':
return get('CheckboxSetField');
default:
return null;
}
}
/**
* Run validation for every field on the form and return an object which list issues while
* validating
*
* @param values
* @returns {*}
*/
validateForm(values) {
if (typeof this.props.validate === 'function') {
return this.props.validate(values);
}
const schema = this.props.schema && this.props.schema.schema;
if (!schema) {
return {};
}
const validationMiddleware = this.context.injector.validate(
this.props.identifier
);
let middlewareValidationResult = {};
if (validationMiddleware) {
middlewareValidationResult = validationMiddleware(
values,
this.props.schema.schema
) || {};
}
return createErrorBlock(middlewareValidationResult);
}
/**
* Common functionality for building a Field or Action from schema.
*
* @param {Object} props Props which every form field receives. Leave it up to the
* schema and component to determine which props are required.
* @returns {*}
*/
buildComponent(props) {
// Inline `input` props into main field props
// (each component can pick and choose the props required for it's <input>
// See http://redux-form.com/6.0.5/docs/api/Field.md/#input-props
const inputProps = props.input || {};
const componentProps = {
...props,
...props.input,
onChange: inputProps.onChange
? (event, payload) => {
inputProps.onChange(payload ? payload.value : event);
}
: null,
};
delete componentProps.input;
// 'component' key is renamed to 'schemaComponent' in normalize*() methods
const SchemaComponent = this.getComponent(componentProps);
if (SchemaComponent === null) {
return null;
} else if (componentProps.schemaComponent !== null && SchemaComponent === undefined) {
throw Error(`Component not found in injector: ${componentProps.schemaComponent}`);
}
// Provides container components a place to hook in
// and apply customisations to scaffolded components.
const createFn = this.props.createFn;
if (typeof createFn === 'function') {
return createFn(SchemaComponent, componentProps);
}
return <SchemaComponent key={componentProps.id} {...componentProps} />;
}
/**
* Maps a list of schema fields to their React Component.
* Only top level form fields are handled here, composite fields (TabSets etc),
* are responsible for mapping and rendering their children.
*
* @param {Array} fields
* @return {Array}
*/
mapFieldsToComponents(fields) {
const FieldComponent = this.props.baseFieldComponent;
return fields.map((field) => {
let props = field;
if (field.children) {
props = Object.assign(
{},
field,
{ children: this.mapFieldsToComponents(field.children) }
);
}
props = Object.assign(
{
onAutofill: this.props.onAutofill,
formid: this.props.form,
},
props
);
// Don't wrap structural or readonly fields, since they don't need connected fields.
// The redux-form connected fields also messed up reactstrap's tab handling.
if (field.schemaType === 'Structural' || field.readOnly === true) {
return this.buildComponent(props);
}
return <FieldComponent key={props.id} {...props} component={this.buildComponent} />;
});
}
/**
* When the action is clicked on, records which action was clicked on
* This can allow for preventing the submit action, such as a custom action for the button
*
* @param {Event} event
*/
handleAction(event) {
// Custom handlers
if (typeof this.props.onAction === 'function') {
this.props.onAction(event, this.props.values);
}
// Allow custom handlers to cancel event
if (!event.isPropagationStopped()) {
this.setState({ submittingAction: event.currentTarget.name });
}
}
/**
* Form submission handler passed to the Form Component as a prop.
* Provides a hook for controllers to access for state and provide custom functionality.
*
* @param {Object} data Processed and validated data from redux-form
* (originally retrieved through schemaFieldValues())
* @return {Promise|null}
*/
handleSubmit(data) {
// Add form action data (or default to first action, same as browser behaviour)
let action = '';
if (this.state.submittingAction) {
action = this.state.submittingAction;
} else if (this.props.schema.schema.actions[0]) {
action = this.props.schema.schema.actions[0].name;
}
const dataWithAction = Object.assign({}, data, action ? { [action]: 1 } : {});
const requestedSchema = this.props.responseRequestedSchema.join();
const headers = {
'X-Formschema-Request': requestedSchema,
'X-Requested-With': 'XMLHttpRequest',
};
const submitFn = (customData) =>
this.submitApi(customData || dataWithAction, headers)
.then(formSchema => {
this.setState({ submittingAction: null });
return formSchema;
})
.catch((reason) => {
// @todo Generic CMS error reporting
this.setState({ submittingAction: null });
throw reason;
});
if (typeof this.props.onSubmit === 'function') {
return this.props.onSubmit(dataWithAction, action, submitFn);
}
return submitFn();
}
/**
* Maps a list of form actions to their React Component.
*
* @param {Array} actions
* @return {Array}
*/
mapActionsToComponents(actions) {
return actions.map((action) => {
const props = Object.assign({}, action);
if (action.children) {
props.children = this.mapActionsToComponents(action.children);
} else {
props.onClick = this.handleAction;
// Reset component loading prop
if (this.props.submitting && this.state.submittingAction === action.name) {
props.loading = true;
}
}
return this.buildComponent(props);
});
}
/**
* If there is structural and state data available merge those data for each field.
* Otherwise just use the structural data. Ensure that keys don't conflict
* with redux-form expectations.
*
* @param {array} fields
* @param {Object} state Optional
* @return {array}
*/
normalizeFields(fields, state) {
return fields.map((field) => {
const fieldState = (state && state.fields)
? state.fields.find((item) => item.id === field.id)
: {};
const data = merge.recursive(
true,
schemaMerge(field, fieldState),
// Overlap with redux-form prop handling : createFieldProps filters out the 'component' key
{
schemaComponent: (fieldState && fieldState.component)
? fieldState.component
: field.component,
}
);
if (field.children) {
data.children = this.normalizeFields(field.children, state);
}
return data;
});
}
render() {
const schema = this.props.schema.schema;
const state = this.props.schema.state;
const BaseFormComponent = this.props.baseFormComponent;
// Map form schema to React component attribute names,
// which requires renaming some of them (by unsetting the original keys)
const attributes = {
...schema.attributes,
className: schema.attributes.class,
encType: schema.attributes.enctype,
// Turn off HTML5 validation to rely on validateForm as the sole validator
noValidate: true,
};
delete attributes.class;
delete attributes.enctype;
const {
asyncValidate,
fieldHolder,
actionHolder,
onSubmitFail,
onSubmitSuccess,
shouldAsyncValidate,
touchOnBlur,
touchOnChange,
persistentSubmitErrors,
form,
afterMessages,
autoFocus,
formTag,
} = this.props;
const props = {
form, // required as redux-form identifier
afterMessages,
fields: this.normalizeFields(schema.fields, state),
fieldHolder,
actions: this.normalizeFields(schema.actions, state),
actionHolder,
attributes,
data: schema.data,
initialValues: schemaFieldValues(schema, state),
onSubmit: this.handleSubmit,
valid: state && state.valid,
messages: (state && Array.isArray(state.messages)) ? state.messages : [],
mapActionsToComponents: this.mapActionsToComponents,
mapFieldsToComponents: this.mapFieldsToComponents,
asyncValidate,
onSubmitFail,
onSubmitSuccess,
shouldAsyncValidate,
touchOnBlur,
touchOnChange,
persistentSubmitErrors,
validate: this.validateForm,
autoFocus,
setDOM: (formDOM) => { this.formDOM = formDOM; },
formTag,
};
return (
<BaseFormComponent
{...props}
/>
);
}
}
const schemaPropType = PropTypes.shape({
id: PropTypes.string,
schema: PropTypes.shape({
attributes: PropTypes.shape({
class: PropTypes.string,
enctype: PropTypes.string,
}),
fields: PropTypes.array.isRequired,
}),
state: PropTypes.shape({
fields: PropTypes.array,
}),
loading: PropTypes.bool,
stateOverride: PropTypes.shape({
fields: PropTypes.array,
}),
});
const basePropTypes = {
createFn: PropTypes.func,
onSubmit: PropTypes.func,
onAction: PropTypes.func,
asyncValidate: PropTypes.func,
onSubmitFail: PropTypes.func,
onSubmitSuccess: PropTypes.func,
shouldAsyncValidate: PropTypes.func,
touchOnBlur: PropTypes.bool,
touchOnChange: PropTypes.bool,
persistentSubmitErrors: PropTypes.bool,
validate: PropTypes.func,
values: PropTypes.object,
submitting: PropTypes.bool,
baseFormComponent: PropTypes.elementType.isRequired,
baseFieldComponent: PropTypes.elementType.isRequired,
getCustomFields: PropTypes.func,
responseRequestedSchema: PropTypes.arrayOf(PropTypes.oneOf([
'schema', 'state', 'errors', 'auto',
])),
identifier(props, propName, componentName) {
if (!/^[A-Za-z0-9_.]+$/.test(props[propName])) {
return new Error(`
Invalid identifier supplied to ${componentName}. Must be a set of
dot-separated alphanumeric strings.
`);
}
return null;
},
};
FormBuilder.propTypes = Object.assign({}, basePropTypes, {
form: PropTypes.string.isRequired,
schema: schemaPropType.isRequired,
autoFocus: PropTypes.bool,
});
FormBuilder.defaultProps = {
responseRequestedSchema: ['auto'],
autoFocus: false,
};
export {
FormBuilder as Component,
basePropTypes,
schemaPropType
};
export default withInjector(FormBuilder);