-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-controlled.htm
More file actions
88 lines (73 loc) · 2.16 KB
/
Copy pathform-controlled.htm
File metadata and controls
88 lines (73 loc) · 2.16 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Controlled Form</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script type="text/jsx">
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
sex: '', // will remain empty if user doesn't change selection
// defaultValue="Male" on <select> will have no affect
// could use defaultProps on NameForm
address: '',
};
this.handleNameChange = this.handleNameChange.bind(this);
this.handleSexChange = this.handleSexChange.bind(this);
this.handleAddressChange = this.handleAddressChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleNameChange(event) {
this.setState({name: event.target.value.toUpperCase()});
}
handleSexChange(event) {
this.setState({sex: event.target.value});
}
handleAddressChange(event) {
this.setState({address: event.target.value});
}
handleSubmit(event) {
alert(this.state.name + '\n' + this.state.sex + '\n' + this.state.address);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.name} onChange={this.handleNameChange} />
</label>
<br/>
<label>
Sex:
<select value={this.state.sex} onChange={this.handleSexChange}>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</label>
<br/>
<label>
Address:
<textarea value={this.state.address} onChange={this.handleAddressChange} />
</label>
<br/>
<input type="submit" value="Submit" />
</form>
);
}
}
ReactDOM.render(
<NameForm />,
document.getElementById('root')
);
</script>
</body>
</html>