Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 8a0750d

Browse files
committed
Forms
1 parent 2c653d4 commit 8a0750d

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed

src/index.js

+78-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,87 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33

4-
function ListItem(props) {
5-
return <li>{props.value}</li>;
6-
}
4+
class NameForm extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {
8+
name: '',
9+
essay: 'Please write an essay about your favorite DOM element.',
10+
flavor: 'coconut',
11+
isGoing: true,
12+
numberOfGuests: 2
13+
};
14+
15+
this.handleInputChange = this.handleInputChange.bind(this);
16+
this.handleSubmit = this.handleSubmit.bind(this);
17+
}
18+
19+
handleInputChange(event) {
20+
const target = event.target;
21+
const value = target.type === 'checkbox' ? target.checked : target.value;
22+
const name = target.name;
23+
24+
this.setState({
25+
[name]: value
26+
});
27+
}
28+
29+
handleSubmit(event) {
30+
alert('A name was submitted: ' + this.state.name);
31+
alert('An essay was submitted: ' + this.state.essay);
32+
alert('Your favorite flavor is: ' + this.state.flavor);
33+
alert('Is going: ' + this.state.isGoing);
34+
alert('Number of guests: ' + this.state.numberOfGuests);
35+
event.preventDefault();
36+
}
737

8-
function NumberList(props) {
9-
const numbers = props.numbers;
10-
return (
11-
<ul>
12-
{numbers.map((number) =>
13-
<ListItem key={number.toString()} value={number} />
14-
)}
15-
</ul>
16-
);
38+
render() {
39+
return (
40+
<form onSubmit={this.handleSubmit}>
41+
<lable>
42+
Name:
43+
<input name="name" type="text" value={this.state.name} onChange={this.handleInputChange} />
44+
</lable><br />
45+
<lable>
46+
Essay:
47+
<textarea name="essay" value={this.state.essay} onChange={this.handleInputChange} />
48+
</lable><br />
49+
<label>
50+
Pick your favorite flavor:
51+
<select name="flavor" value={this.state.flavor} onChange={this.handleInputChange}>
52+
<option value="grapefruit">Grapefruit</option>
53+
<option value="lime">Lime</option>
54+
<option value="coconut">Coconut</option>
55+
<option value="mango">Mango</option>
56+
</select>
57+
</label><br />
58+
<label>
59+
Is going:
60+
<input
61+
name="isGoing"
62+
type="checkbox"
63+
checked={this.state.isGoing}
64+
onChange={this.handleInputChange} />
65+
</label><br />
66+
<label>
67+
Number of guests:
68+
<input
69+
name="numberOfGuests"
70+
type="number"
71+
value={this.state.numberOfGuests}
72+
onChange={this.handleInputChange} />
73+
</label><br />
74+
<lable>
75+
Read only:
76+
<input type="text" value="hi" />
77+
</lable><br />
78+
<input type="submit" value="Submit" />
79+
</form>
80+
);
81+
}
1782
}
1883

19-
const numbers = [1, 2, 3, 4, 5];
2084
ReactDOM.render(
21-
<NumberList numbers={numbers} />,
85+
<NameForm />,
2286
document.getElementById('root')
2387
);

0 commit comments

Comments
 (0)