-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex4.js
176 lines (157 loc) · 3.42 KB
/
index4.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var MyForm = React.createClass({
getInitialState: function() {
return {
options: ['B']
};
},
handleChange: function(ev) {
var checked = [];
var sel = ev.target;
for(var i=0;i<sel.length;i++) {
if(sel.options[i].selected) checked.push(sel.options[i].value);
}
this.setState({
options: checked
});
},
submitHandler: function(ev) {
ev.preventDefault();
alert(this.state.options);
},
render: function() {
return (
<form onSubmit={this.submitHandler}>
<select multiple='true' value={this.state.options} onChange={this.handleChange}>
<option value='A'>A</option>
<option value='B'>B</option>
<option value='C'>C</option>
</select>
<button type='submit'>Submit</button>
</form>
);
}
});
/*ReactDOM.render(
<MyForm />,
document.getElementById('container')
);*/
var MyForm2 = React.createClass({
getInitialState: function() {
return {
firstname: '',
lastname: ''
};
},
submitHandler: function(e) {
e.preventDefault();
alert(this.state.firstname+"-"+this.state.lastname);
},
handleChange: function(name, ev) {
var newState = {};
newState[name] = ev.target.value;
this.setState(newState);
},
render: function() {
return (
<form onSubmit={this.submitHandler}>
<label htmlFor='firstname'>First name:</label>
<input
type='text'
value={this.state.firstname}
onChange={this.handleChange.bind(this,'firstname')}
name='firstname'
/>
<br />
<label htmlFor='lastname'>Last name:</label>
<input
type='text'
value={this.state.lastname}
onChange={this.handleChange.bind(this, 'lastname')}
name='lastname'
/>
<br />
<button type='submit'>Submit</button>
</form>
);
}
});
/*ReactDOM.render(
<MyForm2 />,
document.getElementById('container')
);*/
var MyForm3 = React.createClass({
mixins: [React.addons.LinkedStateMixin],
getInitialState: function() {
return {
firstname: '',
lastname: ''
};
},
submitHandler: function(e) {
e.preventDefault();
alert(this.state.firstname+"-"+this.state.lastname);
},
render: function() {
return (
<form onSubmit={this.submitHandler}>
<label htmlFor='firstname'>First name:</label>
<input
type='text'
valueLink = {this.linkState('firstname')}
name='firstname'
/>
<br />
<label htmlFor='lastname'>Last name:</label>
<input
type='text'
valueLink = {this.linkState('lastname')}
name='lastname'
/>
<br />
<button type='submit'>Submit</button>
</form>
);
}
});
/*ReactDOM.render(
<MyForm3 />,
document.getElementById('container')
);
*/
//自定义表单组件
var Radio = React.createClass({
propTypes: {
onChange: React.propTypes.func
},
getInitialState: function() {
return {
value: this.props.defaultValue
};
},
handleChange: function(e) {
if(this.props.onChange) this.props.onChange(e);
this.setState({
value: e.target.value
});
},
render: function() {
var children = {};
var value = this.props.value || this.state.value;
React.children.forEach(this.props.children, function(child, i) {
var label = (
<label>
<input
type='radio'
name={this.props.name}
value={child.props.value}
checked={child.props.value == value}
onChange={this.handleChange}
/>
{child.props.children}
</label>
);
children['label'+i] = label;
}.bind(this));
return this.transferPropsTo(<span>{children}</span>);
}
});