-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormGenerator.js
More file actions
78 lines (54 loc) · 1.84 KB
/
FormGenerator.js
File metadata and controls
78 lines (54 loc) · 1.84 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
77
78
// HTML Form Generation Tool
module.exports.formGeneration = ((userInputs) => {
// String to hold form
let formString = '';
// Add attributes if available
const attributes = (input) => {
let attribute = input.for ? ` for="${input.for}"` : '';
attribute += input.name ? ` name="${input.name}"` : '';
attribute += input.id ? ` id="${input.id}"` : '';
attribute += input.class ? ` class="${input.class}"` : '';
attribute += input.value ? ` value="${input.value}"` : '';
attribute += input.required ? ' required "' : '';
return attribute;
};
// For each input in user input array
userInputs.forEach((input) => {
// Switch to handle entered input type, toLower to avoid irrelevant case errors
switch (input.type.toLowerCase()) {
case 'label':
formString += '<label';
// ADD attributes if present
formString += attributes(input);
formString += '></label>\n';
break;
case 'text':
formString += '<input type="text"';
formString += attributes(input);
formString += '/>\n';
break;
case 'email':
formString += '<input type="email"';
formString += attributes(input);
formString += '/>\n';
break;
case 'password':
formString += '<input type="password"';
formString += attributes(input);
formString += '/>\n';
break;
case 'submit':
formString += '<input type="submit"';
formString += attributes(input);
formString += '/>\n';
break;
// IF NONE OF ABOVE TYPES APPLY
default:
// Show error
console.log(`Invalid input type: "${input.type}". Choose from "label", "text", "email", "password", and "submit" and do not leave blank.`);
break;
}
});
// RETURN form string
return formString;
});