-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormGenerator.js
74 lines (58 loc) · 1.95 KB
/
FormGenerator.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
import React from "react";
import FormComponent from "./js/FormComponent.js";
import InputFormComponent from "./js/InputFormComponent.js";
import SelectFormComponent from "./js/SelectFormComponent.js";
import TickFormComponent from "./js/TickFormComponent.js";
export default class FormGenerator extends React.Component {
render() {
return FormGenerator.generate(this.props.json);
}
static generate(string) {
let json = JSON.parse(string);
if (!json)
return;
function createComponent(type, data) {
switch (type) {
case "checkbox":
case "radio":
return new TickFormComponent(type, data);
case "date":
case "email":
case "file":
case "range":
case "url":
return new InputFormComponent(type, data);
case "select":
return new SelectFormComponent(data);
default:
return null;
}
}
let formElement = document.createElement("form");
formElement.setAttribute("action", json.action);
formElement.setAttribute("method", json.method);
formElement.setAttribute("enctype", "multipart/form-data");
let components = [];
for (let item of json.items) {
let type = item.type;
item.type = null;
let condition = item.condition;
item.condition = null;
let component = createComponent(type, item);
if (condition && !isNaN(condition.index)) {
let conditionComponent = components[condition.index];
conditionComponent.addEventListener(FormComponent.Event.ValueChanged, event => {
if (JSON.stringify(conditionComponent.value) === JSON.stringify(condition.value))
conditionComponent.element.insertAdjacentElement("afterend", component.element);
else
component.element.remove();
});
} else
formElement.appendChild(component.element);
component.addEventListener(FormComponent.Event.ValueChanged, event => console.log(event));
components.push(component);
}
formElement.appendChild(document.createElement("button")).textContent = "Submit";
return formElement;
}
}