Skip to content

Commit e02baf9

Browse files
Kevin JoseKevin Jose
Kevin Jose
authored and
Kevin Jose
committed
added feature to edit schema
1 parent dd7a618 commit e02baf9

File tree

3 files changed

+125
-75
lines changed

3 files changed

+125
-75
lines changed

src/App.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.showcase {
2-
width: 300px;
3-
height: 600px;
2+
width: 500px;
3+
height: 400px;
44
padding: 0 20px;
5+
padding-top: 15px;
56
}
67

78
.json-input {

src/App.js

+113-70
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,72 @@ import './App.css';
66
class App extends React.Component {
77
state = {
88
data: [],
9-
model: [
10-
{ key: 'name', label: 'Name', props: { required: true } },
11-
{ key: 'age', label: 'Age', type: 'number' },
12-
{
13-
key: 'rating',
14-
label: 'Rating',
15-
type: 'number',
16-
props: { min: 0, max: 5 }
17-
},
18-
{
19-
key: 'gender',
20-
label: 'Gender',
21-
type: 'radio',
22-
options: [
23-
{
24-
key: 'male',
25-
label: 'Male',
26-
name: 'gender',
27-
value: 'male'
28-
},
29-
{
30-
key: 'female',
31-
label: 'Female',
32-
name: 'gender',
33-
value: 'female'
34-
}
35-
]
36-
},
37-
{ key: 'qualification', label: 'Qualification' },
38-
{
39-
key: 'city',
40-
label: 'City',
41-
type: 'select',
42-
value: 'Kerala',
43-
props: { required: true },
44-
options: [
45-
{ key: '_placeholder', label: 'Select a city', value: '' },
46-
{ key: 'mumbai', label: 'Mumbai', value: 'Mumbai' },
47-
{
48-
key: 'bangalore',
49-
label: 'Bangalore',
50-
value: 'Bangalore'
51-
},
52-
{ key: 'kerala', label: 'Kerala', value: 'Kerala' }
53-
]
54-
},
55-
{
56-
key: 'skills',
57-
label: 'Skills',
58-
type: 'checkbox',
59-
options: [
60-
{ key: 'reactjs', label: 'ReactJS', value: 'reactjs' },
61-
{ key: 'angular', label: 'Angular', value: 'angular' },
62-
{ key: 'vuejs', label: 'VueJS', value: 'vuejs' }
63-
]
64-
}
65-
]
9+
model: JSON.stringify(
10+
[
11+
{ key: 'name', label: 'Name', props: { required: true } },
12+
{ key: 'age', label: 'Age', type: 'number' },
13+
{
14+
key: 'rating',
15+
label: 'Rating',
16+
type: 'number',
17+
props: { min: 0, max: 5 }
18+
},
19+
{
20+
key: 'gender',
21+
label: 'Gender',
22+
type: 'radio',
23+
options: [
24+
{
25+
key: 'male',
26+
label: 'Male',
27+
name: 'gender',
28+
value: 'male'
29+
},
30+
{
31+
key: 'female',
32+
label: 'Female',
33+
name: 'gender',
34+
value: 'female'
35+
}
36+
]
37+
},
38+
{ key: 'qualification', label: 'Qualification' },
39+
{
40+
key: 'city',
41+
label: 'City',
42+
type: 'select',
43+
value: 'Kerala',
44+
props: { required: true },
45+
options: [
46+
{ key: '_placeholder', label: 'Select a city', value: '' },
47+
{ key: 'mumbai', label: 'Mumbai', value: 'Mumbai' },
48+
{
49+
key: 'bangalore',
50+
label: 'Bangalore',
51+
value: 'Bangalore'
52+
},
53+
{ key: 'kerala', label: 'Kerala', value: 'Kerala' }
54+
]
55+
},
56+
{
57+
key: 'skills',
58+
label: 'Skills',
59+
type: 'checkbox',
60+
options: [
61+
{ key: 'reactjs', label: 'ReactJS', value: 'reactjs' },
62+
{ key: 'angular', label: 'Angular', value: 'angular' },
63+
{ key: 'vuejs', label: 'VueJS', value: 'vuejs' }
64+
]
65+
}
66+
],
67+
null,
68+
2
69+
)
6670
};
6771

72+
handleModel = e => {
73+
this.setState({ model: e.target.value });
74+
};
6875
onSubmit = model => {
6976
let data = [];
7077
if (model.id) {
@@ -91,17 +98,48 @@ class App extends React.Component {
9198
});
9299
};
93100

101+
renderTableHead = () => {
102+
console.log(this.state.data);
103+
if (!this.state.data[0]) {
104+
return <td>No Data!</td>;
105+
}
106+
let dataHead = Object.keys(this.state.data[0]);
107+
return dataHead.map(d => {
108+
return <td key={'th_' + d}>{d}</td>;
109+
});
110+
};
111+
renderRow = r => {
112+
return Object.keys(r).map(p => <td key={'td_' + p}>{r[p]}</td>);
113+
};
114+
115+
renderForm = () => {
116+
try {
117+
return (
118+
<DynamicForm
119+
title="Registration"
120+
defaultValues={this.state.currentRecord}
121+
model={JSON.parse(this.state.model)}
122+
onSubmit={model => {
123+
this.onSubmit(model);
124+
}}
125+
/>
126+
);
127+
} catch (e) {
128+
return <p>Invalid form, ensure that the JSON is valid</p>;
129+
}
130+
};
94131
render() {
95132
let data = this.state.data.map(d => {
96133
return (
97134
<tr key={d.id}>
98-
<td>{d.name}</td>
135+
{this.renderRow(d)}
136+
{/* <td>{d.name}</td>
99137
<td>{d.age}</td>
100138
<td>{d.qualification}</td>
101139
<td>{d.gender}</td>
102140
<td>{d.rating}</td>
103141
<td>{d.city}</td>
104-
<td>{d.skills && d.skills.length ? d.skills.join(', ') : ''}</td>
142+
<td>{d.skills && d.skills.length ? d.skills.join(', ') : ''}</td> */}
105143
<td>
106144
<button
107145
className="ui button basic negative"
@@ -115,27 +153,32 @@ class App extends React.Component {
115153
});
116154
return (
117155
<div className="ui container" style={{ paddingTop: '50px' }}>
118-
<div className="ui three column stackable grid container">
119-
<div className="showcase">
156+
<div className="ui two column stackable grid container">
157+
<div className="column showcase">
158+
<h4 className="ui dividing header">Form Schema</h4>
120159
<textarea
121160
style={{ fontFamily: 'monospace' }}
122161
className="json-input"
123-
value={JSON.stringify(this.state.model, null, 2)}
162+
value={this.state.model}
163+
onChange={this.handleModel}
124164
/>
125165
</div>
126166
<div className="column">
127-
<DynamicForm
128-
title="Registration"
129-
defaultValues={this.state.currentRecord}
130-
model={this.state.model}
131-
onSubmit={model => {
132-
this.onSubmit(model);
133-
}}
134-
/>
167+
<h4 className="ui dividing header">Rendered Form</h4>
168+
{this.renderForm()}
135169
</div>
170+
</div>
171+
<div className="clearfix" />
172+
<br />
173+
<br />
174+
<br />
175+
<div className="ui column stackable grid container">
136176
<div className="column">
137177
<h4 className="ui dividing header">Data</h4>
138178
<table className="ui stacked table">
179+
<thead>
180+
<tr>{this.renderTableHead()}</tr>
181+
</thead>
139182
<tbody>{data}</tbody>
140183
</table>
141184
</div>

src/components/dynamic-form/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class DynamicForm extends React.Component {
1111
for (let key in prevState) {
1212
prevState[key] = '';
1313
}
14+
delete prevState['submitted'];
1415
return { ...prevState };
1516
}
1617

@@ -194,16 +195,21 @@ export default class DynamicForm extends React.Component {
194195
return formUI;
195196
};
196197
render() {
197-
let title = this.props.title || 'Dynamic Form';
198+
// let title = this.props.title || 'Dynamic Form';
198199
return (
199-
<div>
200+
<div
201+
style={{
202+
maxHeight: '400px',
203+
overflowY: 'scroll',
204+
paddingLeft: '4px'
205+
}}
206+
>
200207
<form
201208
className="ui form"
202209
onSubmit={e => {
203210
this.onSubmit(e);
204211
}}
205212
>
206-
<h4 className="ui dividing header">{title}</h4>
207213
{this.renderForm()}
208214
<div>
209215
<button type="submit" className="ui button basic positive">

0 commit comments

Comments
 (0)