-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchemaForm.js
71 lines (62 loc) · 1.98 KB
/
SchemaForm.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
/**
* Created by steve on 11/09/15.
*/
import React, {PropTypes} from 'react';
const utils = require('react-schema-form/lib/utils');
import merge from 'lodash/merge';
class SchemaForm extends React.Component {
static builder(form, model, index, onChange, mapper, extraProps) {
const type = form.type;
const Field = mapper[type];
if (!Field) {
console.log('Invalid element type: \"' + form.type + '\"!'); // eslint-disable-line no-console
return null;
}
// TODO (tyr) investigate new Function() instead of eval
if (form.condition && eval(form.condition) === false) {
return null;
}
return (
<Field model={model} form={form} key={index} onChange={onChange} mapper={mapper} builder={SchemaForm.builder}
extraProps={extraProps}/>);
}
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.mapper = {};
}
onChange(key, val) {
//console.log('SchemaForm.onChange', key, val);
this.props.onModelChange(key, val);
}
render() {
const merged = utils.merge(this.props.schema, this.props.form, this.props.ignore, this.props.option);
// Will overlay this.mapper with any nested values from this.props.mapper
const mapper = merge(this.mapper, this.props.mapper || {});
const forms = merged.map(function (form, index) {
return SchemaForm.builder(form, this.props.model, index, this.onChange, mapper, this.props.extraProps);
}.bind(this));
return (
<div style={{width: '100%'}} className="SchemaForm">{forms}</div>
);
}
}
export default SchemaForm;
SchemaForm.propTypes = {
form: PropTypes.array.isRequired,
model: PropTypes.object.isRequired,
schema: PropTypes.object.isRequired,
onModelChange: PropTypes.func,
mapper: PropTypes.object,
extraProps: PropTypes.object,
ignore: PropTypes.object,
option: PropTypes.object,
};
SchemaForm.defaultProps = {
onModelChange: ()=> {
},
mapper: null,
extraProps: {},
ignore: {},
option: {},
};