forked from benmvp/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailForm.js
107 lines (92 loc) · 2.94 KB
/
EmailForm.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
import React from 'react';
const DEFAULT_FORM_VALUES = {
from: '',
to: '[email protected]',
subject: '',
message: ''
};
export default class EmailForm extends React.Component {
static propTypes = {
onSubmit: React.PropTypes.func.isRequired
}
state = DEFAULT_FORM_VALUES
_updateFormFieldState(name, e) {
this.setState({[name]: e.target.value});
}
_handleFromChanged(e) {
this._updateFormFieldState('from', e);
}
_handleToChanged(e) {
this._updateFormFieldState('to', e);
}
_handleSubjectChanged(e) {
this._updateFormFieldState('subject', e);
}
_handleMessageChanged(e) {
this._updateFormFieldState('message', e);
}
_handleSubmit(e) {
e.preventDefault();
let {from, to, subject, message} = this.state;
// super simple validation
if (from && to && subject && message) {
// call handler with email info
this.props.onSubmit({
from,
to,
subject,
message
});
// reset the form to initial values
this.setState(DEFAULT_FORM_VALUES);
}
else {
alert('fill out the form!');
}
}
render() {
let {from, to, subject, message} = this.state;
return (
<form onSubmit={this._handleSubmit.bind(this)}>
<div>
<label htmlFor="from">From:</label>
<input type="email"
id="from"
value={from}
onChange={this._handleFromChanged.bind(this)}
placeholder="[email protected]"
/>
</div>
<div>
<label htmlFor="to">To:</label>
<input type="email"
id="to"
value={to}
onChange={this._handleToChanged.bind(this)}
placeholder="[email protected]"
/>
</div>
<div>
<label htmlFor="subject">Subject:</label>
<input type="text"
id="subject"
value={subject}
onChange={this._handleSubjectChanged.bind(this)}
placeholder="Awesome React workshop!"
/>
</div>
<div>
<label htmlFor="message">Message:</label>
<textarea id="message"
value={message}
onChange={this._handleMessageChanged.bind(this)}
placeholder="[Insert message here]"
/>
</div>
<div>
<button type="submit">Send email</button>
</div>
</form>
);
}
}