forked from blackjk3/react-form-builder
-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathapp.js
146 lines (133 loc) · 3.26 KB
/
app.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
import React from 'react';
import ReactDOM from 'react-dom';
import { ReactFormBuilder, ElementStore, Registry } from 'react-form-builder2';
import DemoBar from './demobar';
import * as variables from './variables';
import { get, post } from './requests';
const getUrl = (cid) => `https://safe-springs-35306.herokuapp.com/api/formdata?cid=${cid}`;
const TestComponent = () => <h2>Hello</h2>;
const MyInput = React.forwardRef((props, ref) => {
const { name, defaultValue, disabled } = props;
return <input ref={ref} name={name} defaultValue={defaultValue} disabled={disabled} />;
});
Registry.register('MyInput', MyInput);
Registry.register('TestComponent', TestComponent);
const items = [{
key: 'Header',
}, {
key: 'TextInput',
}, {
key: 'TextArea',
}, {
key: 'RadioButtons',
}, {
key: 'Checkboxes',
}, {
key: 'Image',
},
{
group_name: 'Multi Column Row',
key: 'TwoColumnRow',
},
{
group_name: 'Multi Column Row',
key: 'ThreeColumnRow',
},
{
group_name: 'Multi Column Row',
key: 'FourColumnRow',
element: 'MultiColumnRow',
},
{
group_name: 'Multi Column Row',
key: 'FiveColumnRow',
element: 'MultiColumnRow',
},
{
group_name: 'Multi Column Row',
key: 'SixColumnRow',
element: 'MultiColumnRow',
},
{
group_name: 'Custom Element',
key: 'TestComponent',
element: 'CustomElement',
component: TestComponent,
type: 'custom',
field_name: 'test_component',
name: 'Something You Want',
icon: 'fa fa-cog',
static: true,
props: { test: 'test_comp' },
label: 'Label Test',
},
{
group_name: 'Custom Element',
key: 'MyInput',
element: 'CustomElement',
component: MyInput,
type: 'custom',
forwardRef: true,
bare: true,
field_name: 'my_input_',
name: 'My Input',
icon: 'fa fa-cog',
props: { test: 'test_input' },
label: 'Label Input',
},
];
class App extends React.Component {
constructor(props) {
super(props);
this.state = { formId: '1' };
this.formId = this.state.formId;
this.handleChange = this.handleChange.bind(this);
}
formId;
handleChange(event) {
this.formId = event.target.value;
const url = getUrl(this.formId);
console.log('handleChange', url);
ElementStore.dispatch('load', { loadUrl: url });
this.setState({ formId: this.formId });
}
onLoad = () => {
const url = getUrl(this.formId);
console.log('onLoad', url);
return get(url);
};
onPost = (data) => {
const saveUrl = getUrl(this.formId);
console.log('onPost', saveUrl, data);
post(saveUrl, data);
};
render() {
return (
<div className="App">
<label>
Select your form:
</label>
<select className="form-control"
value={this.state.formId}
onChange={this.handleChange} >
<option value="1">Form 1</option>
<option value="2">Form 2</option>
</select>
<hr></hr>
<ReactFormBuilder
onLoad={this.onLoad}
onPost={this.onPost}
toolbarItems={items}
/>,
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('form-builder'),
);
ReactDOM.render(
<DemoBar variables={variables} />,
document.getElementById('demo-bar'),
);