-
Notifications
You must be signed in to change notification settings - Fork 800
/
addTodo.js
59 lines (45 loc) · 1.07 KB
/
addTodo.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
import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import {addTodo} from '../actions.js';
class AddTodo extends Component {
constructor(props, context) {
super(props, context);
this.onSubmit = this.onSubmit.bind(this);
this.refInput = this.refInput.bind(this);
}
onSubmit(ev) {
ev.preventDefault();
const input = this.input;
if (!input.value.trim()) {
return;
}
this.props.onAdd(input.value);
input.value = '';
}
refInput(node) {
this.input = node;
}
render() {
return (
<div className="add-todo">
<form onSubmit={this.onSubmit}>
<input className="new-todo" ref={this.refInput} />
<button className="add-btn" type="submit">
添加
</button>
</form>
</div>
)
}
}
AddTodo.propTypes = {
onAdd: PropTypes.func.isRequired
};
const mapDispatchToProps = (dispatch) => {
return {
onAdd: (text) => {
dispatch(addTodo(text));
}
}
};
export default connect(null, mapDispatchToProps)(AddTodo);