/
LiveSchemaEditor.js
135 lines (120 loc) · 5.16 KB
/
LiveSchemaEditor.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
import React, {Component, PropTypes} from 'react';
import LiveSchemaEditorForm from './LiveSchemaEditorForm';
import presets from '../presets';
import _ from 'underscore';
import psjon from '../../package.json';
import AutoForm from '../../src/AutoForm';
import editComponentFactory from '../../src/factory/bootstrap/BootstrapEditComponentFactory';
import detailsComponentFactory from '../../src/factory/bootstrap/BootstrapDetailsComponentFactory';
import {Alert, Badge} from 'react-bootstrap';
import moment from 'moment';
import numbro from 'numbro';
import reactWidgetsMomentLocalizer from 'react-widgets/lib/localizers/moment';
import momentLocalizer from '../../src/localization/momentLocalizer';
import numbroLocalizer from '../../src/localization/numbroLocalizer';
import ButtonToolbar from './ButtonToolbar';
import FormOptions from './FormOptions';
class LiveSchemaEditor extends Component {
getAutoFormProps(metaForm, formOptions, formName, initialValues) {
if (!formName) throw Error('Form name cannot be empty');
if (!metaForm)
return undefined;
return {
form: formName,
fieldLayout: formOptions.fieldLayout,
buttonBar: ButtonToolbar,
schema: eval('(' + formOptions.schema + ')') , // eval('(' + metaForm.schema.value + ')'),
entityName: metaForm.entityName.value,
layoutName: metaForm.layoutName.value,
componentFactory: formOptions.componentFactory == 'edit' ? editComponentFactory : detailsComponentFactory,
errorRenderer: this.errorRenderer,
initialValues: initialValues,
onSubmit: (...args) => {
console.log(args);
}
};
}
/**
* Renders an exception box
* @param ex
* @returns {XML}
*/
errorRenderer(ex) {
return <Alert bsStyle='danger'>
<h4>Oh snap! The configuration is not valid.</h4>
<p>Detailed information:
<b>{ex.message}</b>
</p>
</Alert>;
}
render() {
// setting date localizer
reactWidgetsMomentLocalizer(moment);
momentLocalizer(moment);
// setting number localizer
numbroLocalizer(numbro);
let {
reduxFormActions,
preset,
metaForm,
formOptions,
formOptionsActions
} = this.props;
preset = preset || 'default';
let presetObject = _.find(presets, p => p.name == preset);
if (!presetObject) throw Error(`Could not find preset. Preset name: ${preset}`);
let autoFormProps;
let autoForm;
try {
autoFormProps = this.getAutoFormProps(metaForm, formOptions, preset, presetObject.initialValues);
autoForm = autoFormProps ? <AutoForm {...autoFormProps} /> : null;
} catch (ex) {
autoForm = this.errorRenderer(ex);
}
let underDevelopmentAlert = formOptions.componentFactory == 'details' ?
<Alert bsStyle="danger">
<p><b>Experimental feature</b></p>
<p>Details forms are still under development. For now, it's just a lot of Static components instead of
editing components. Also,
it only works when the field doesn't explicitly specify the component, and it does'nt work for all types. Arrays,
for instance, are still not supported.</p>
</Alert>
: null;
return <div className="live-schema-editor">
<div className='row'>
<div className="col-md-12">
<h2>Redux-autoform demo {psjon.version} <Badge>Ctrl + H = Redux DevTools</Badge>
<a className="pull-right" target="_blank" href="https://github.com/gearz-lab/redux-autoform"
style={{color: 'black'}}>
<i className="fa fa-github" aria-hidden="true"/>
</a>
</h2>
</div>
<div className="col-md-5">
<LiveSchemaEditorForm formOptionActions={formOptionsActions} reduxFormActions={reduxFormActions} selectedPreset={preset} initialValues={presetObject}/>
</div>
<div className="col-md-7">
<div className="row">
<div className="col-md-12">
<FormOptions editorSchema={metaForm ? metaForm.schema.value : ''} {...formOptions} {...formOptionsActions} />
{ underDevelopmentAlert }
</div>
</div>
<div className="row">
<div className="col-md-12">
<div className="live-schema-editor-mount-node">
{autoForm}
</div>
</div>
</div>
</div>
</div>
</div>
}
}
LiveSchemaEditor.propTypes = {
preset: PropTypes.string,
formOptions: PropTypes.object.isRequired,
formOptionsActions: PropTypes.object.isRequired
};
export default LiveSchemaEditor;