-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (60 loc) · 1.98 KB
/
Copy pathindex.js
File metadata and controls
76 lines (60 loc) · 1.98 KB
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
import React from 'react';
import ReactDOM from 'react-dom';
import registerServiceWorker from './registerServiceWorker';
import './style.css';
import SCHEMA from './schema.json';
import DataFactory from './data/DataFactory';
import WidgetFactory from './widgets/WidgetFactory';
import ValidatorFactory from './validators/validator-factory';
class Form extends React.Component {
state = {
data: null,
Widgets: null
}
componentDidMount() {
const { schema } = this.props;
this.setState({
data: new DataFactory(ValidatorFactory).create(schema).setValue({
"name": "Manue",
"friends": [],
"test2": {
"__id__": "option1",
"city": "Portugal"
}
}).build(),
Widgets: new WidgetFactory.create(schema)
});
}
render() {
const { onDataChange } = this.props;
const { data, Widgets } = this.state;
return !!Widgets ? React.cloneElement(Widgets, { data, onDataChange }) : null;
}
}
class App extends React.Component {
state = { value: {}, tmp: undefined };
onDataChange = (_, data) => {
const value = data.getValueOrDefault();
this.setState({ value, tmp: JSON.stringify(value, null, '\t') });
}
updateValue = (value) => {
this.setState((prev) => {
try {
return { value: JSON.parse(prev.tmp), tmp: value };
} catch (e) {
return { tmp: value };
}
});
}
render() {
const { value, tmp } = this.state;
return <div className="main-container">
<div className="json-schema"><Form schema={SCHEMA} value={value} onDataChange={this.onDataChange} /></div>
<div className="json-data">
<textarea value={tmp} />
</div>
</div>;
}
}
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();