forked from AdaGold/exquisite-react
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathPlayerSubmissionForm.js
129 lines (114 loc) · 3.67 KB
/
PlayerSubmissionForm.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
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
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import './PlayerSubmissionForm.css';
// initial values for formFields
const emptySentence = {
articleOne: 'The',
adj1: '',
noun1: '',
adv: '',
verb: '',
articleTwo:'the',
adj2: '',
noun2: '',
puncOne: '.'
}
const PlayerSubmissionForm = (props) => {
// states
const [formFields, setFormFields] = useState({...emptySentence});
const [currentPlayer, setCurrentPlayer] = useState(1)
// event handlers
const onInputChange = (event) => {
const newFormFields = {...formFields}
newFormFields[event.target.id] = event.target.value;
setFormFields(newFormFields);
}
const onFormSubmit = (event) => {
event.preventDefault();
const newSentence = Object.values(formFields).join(' ');
props.sendSubmission(newSentence);
setCurrentPlayer(currentPlayer + 1)
setFormFields({...emptySentence})
};
return (
<div className="PlayerSubmissionForm">
<h3>Player Submission Form for Player #{ currentPlayer }</h3>
<form className="PlayerSubmissionForm__form" onSubmit={onFormSubmit} >
<div className="PlayerSubmissionForm__poem-inputs">
<span>The </span>
<input
id="adj1"
name="adj1"
placeholder="adjective"
type="text"
onChange={onInputChange}
value={formFields.adj1}
className={formFields.adj1 === '' ? 'PlayerSubmissionFormt__input--invalid' : ''}
/>
<input
id="noun1"
name="noun1"
placeholder="noun"
type="text"
onChange={onInputChange}
value={formFields.noun1}
className={formFields.noun1 === '' ? 'PlayerSubmissionFormt__input--invalid' : ''}
/>
<input
id="adv"
name="adv"
placeholder="adverb"
type="text"
onChange={onInputChange}
value={formFields.adv}
className={formFields.adv === '' ? 'PlayerSubmissionFormt__input--invalid' : ''}
/>
<input
id="verb"
name="verb"
placeholder="verb"
type="text"
onChange={onInputChange}
value={formFields.verb}
className={formFields.verb === '' ? 'PlayerSubmissionFormt__input--invalid' : ''}
/>
<span>the </span>
<input
id="adj2"
name="adj2"
placeholder="adjective"
type="text"
onChange={onInputChange}
value={formFields.adj2}
className={formFields.adj2 === '' ? 'PlayerSubmissionFormt__input--invalid' : ''}
/>
<input
id="noun2"
name="noun2"
placeholder="noun"
type="text"
onChange={onInputChange}
value={formFields.noun2}
className={formFields.noun2 === '' ? 'PlayerSubmissionFormt__input--invalid' : ''}
/>
<span>.</span>
</div>
<div className="PlayerSubmissionForm__submit">
<input type="submit" value="Submit Line" className="PlayerSubmissionForm__submit-btn" />
</div>
</form>
</div>
);
}
PlayerSubmissionForm.propTypes = {
index: PropTypes.number.isRequired,
sendSubmission: PropTypes.func.isRequired,
fields: PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
key: PropTypes.string.isRequired,
placeholder: PropTypes.string.isRequired,
}),
])).isRequired,
}
export default PlayerSubmissionForm;