forked from AdaGold/exquisite-react
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPlayerSubmissionForm.js
More file actions
155 lines (125 loc) · 4.21 KB
/
Copy pathPlayerSubmissionForm.js
File metadata and controls
155 lines (125 loc) · 4.21 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
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
147
148
149
150
151
152
153
154
155
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './PlayerSubmissionForm.css';
class PlayerSubmissionForm extends Component {
constructor(props) {
super(props);
this.state = {
adjective: '',
noun: '',
adverb: '',
verb: '',
adjective2: '',
noun2: '',
count: 1,
}
this.validators = {
adjective: /.+/,
noun: /.+/,
adverb: /.+/,
verb: /.+/,
adjective2: /.+/,
noun2: /.+/,
};
}
validate = (fieldName) => {
const value = this.state[fieldName];
const validation = this.validators[fieldName];
if (value.match(validation)) {
return true;
}
return false;
}
resetState = () => {
this.setState({
adjective: '',
noun: '',
adverb: '',
verb: '',
adjective2: '',
noun2: '',
count: this.state.count + 1
});
}
onFormChange = (event) => {
const field = event.target.name;
const value = event.target.value;
const updatedState = {};
updatedState[field] = value;
this.setState(updatedState);
}
onSubmit = (event) => {
event.preventDefault();
let allValid = true;
Object.keys(this.validators).forEach((key) => {
if (!this.state[key].match(this.validators[key])) {
allValid = false;
}
});
if (!allValid) {
return;
}
console.log(event);
this.props.addLineCallback(this.state);
this.resetState();
}
render() {
return (
<div className="PlayerSubmissionForm">
<h3>Player Submission Form for Player #{ this.state.count }</h3>
<form onSubmit={this.onSubmit} className="PlayerSubmissionForm__form" >
<div className="PlayerSubmissionForm__poem-inputs">
<div>{"The "}
<label htmlFor="adjective"></label>
<input name="adjective" placeholder="adjective" onChange={this.onFormChange}
value={this.state.adjective} className={this.validate("adjective") ? "PlayerSubmissionForm__input--valid": "PlayerSubmissionForm__input--invalid"}
/>
</div>
<div>
<label htmlFor="noun"></label>
<input name="noun" placeholder="noun"
onChange={this.onFormChange} value={this.state.noun}
className={this.validate("noun") ? "PlayerSubmissionForm__input--valid": "PlayerSubmissionForm__input--invalid"}
/>
</div>
<div>
<label htmlFor="adverb"></label>
<input name="adverb" placeholder="adverb"
onChange={this.onFormChange} value={this.state.adverb}
className={this.validate("adverb") ? "PlayerSubmissionForm__input--valid": "PlayerSubmissionForm__input--invalid"}
/>
</div>
<div>
<label htmlFor="verb"></label>
<input name="verb" placeholder="verb"
onChange={this.onFormChange} value={this.state.verb}
className={this.validate("verb") ? "PlayerSubmissionForm__input--valid": "PlayerSubmissionForm__input--invalid"}
/>
</div>
<div>{"the "}
<label htmlFor="adjective2"></label>
<input name="adjective2" placeholder="adjective"
onChange={this.onFormChange} value={this.state.adjective2}
className={this.validate("adjective2") ? "PlayerSubmissionForm__input--valid": "PlayerSubmissionForm__input--invalid"}
/>
</div>
<div>
<label htmlFor="noun2"></label>
<input name="noun2" placeholder="noun"
onChange={this.onFormChange} value={this.state.noun2}
className={this.validate("noun2") ? "PlayerSubmissionForm__input--valid": "PlayerSubmissionForm__input--invalid"}
/>.
</div>
</div>
<div className="PlayerSubmissionForm__submit">
<input name="submit" type="submit" value="Submit Line" className="PlayerSubmissionForm__submit-btn" />
</div>
</form>
</div>
);
}
}
PlayerSubmissionForm.propTypes = {
addLineCallback: PropTypes.func.isRequired,
};
export default PlayerSubmissionForm;