10000 Use buttom submit instead of form submit · learnreactjs/todo-app@f310013 · GitHub
[go: up one dir, main page]

Skip to content

Commit f310013

Browse files
author
BanlyTong
committed
Use buttom submit instead of form submit
1 parent d9a568c commit f310013

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

src/containers/TodoForm.jsx

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import { connect } from 'react-redux';
33
import { addTodo } from '../actions';
44

5-
const AddTodo = ({ dispatch }) => {
6-
let input;
5+
class TodoForm extends Component {
6+
constructor() {
7+
super();
8+
this.state = {
9+
todo: ""
10+
}
11+
this.handleSubmit = this.handleSubmit.bind(this);
12+
this.handleChange = this.handleChange.bind(this);
13+
}
714

8-
const handleSubmit = e => {
15+
handleChange(e) {
916
e.preventDefault();
10-
if(!input.value.trim()) return;
11-
dispatch(addTodo(input.value));
12-
input.value = ""
17+
this.setState({ [e.target.id]: e.target.value })
18+
}
19+
20+
handleSubmit(e) {
21+
e.preventDefault();
22+
if(this.state.todo === "") return;
23+
this.props.addTodo(this.state.todo);
24+
this.setState({todo: ""});
25+
}
26+
27+
render() {
28+
return (
29+
<div>
30+
<form>
31+
<input
32+
type="text"
33+
className="form-control col-6 d-inline"
34+
id="todo"
35+
value={this.state.todo}
36+
onChange={this.handleChange}
37+
placeholder="Todo What?"
38+
/>
39+
<button type="submit" className="btn btn-primary ml-2" onClick={this.handleSubmit}>Add</button>
40+
</form>
41+
</div>
42+
);
43+
}
44+
}
45+
46+
function mapDispatchToProps(dispatch) {
47+
return {
48+
addTodo : todo => dispatch(addTodo(todo))
1349
}
14-
15-
return (
16-
<div>
17-
<form onSubmit={handleSubmit}>
18-
<input type="text" className="form-control col-6 d-inline" ref={node => (input = node)} placeholder="Todo What?" />
19-
<button type="submit" className="btn btn-primary ml-2">Add</button>
20-
</form>
21-
</div>
22-
)
2350
}
2451

25-
export default connect()(AddTodo);
52+
export default connect(null, mapDispatchToProps)(TodoForm);

src/pages/Todo.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class Todo extends Component {
1717

1818
handleChange = (e) => {
1919
e.preventDefault();
20-
this.setState({todo: e.target.value })
20+
this.setState({[e.target.id]: e.target.value })
2121
}
2222

2323
render() {
@@ -29,7 +29,7 @@ export default class Todo extends Component {
2929
<div>
3030
<h1>No Redux</h1>
3131
<form>
32-
<input type="text" className="form-control col-6 d-inline" value={this.state.todo} onChange={ this.handleChange } placeholder="Todo What?" />
32+
<input type="text" className="form-control col-6 d-inline" id="todo" value={this.state.todo} onChange={ this.handleChange } placeholder="Todo What?" />
3333
<input type="submit" className="btn btn-primary ml-2" value="Add" onClick={this.handleSubmit}/>
3434
</form>
3535
<h3 className="text-left ml-2 mt-2">Todo List: </h3>

src/reducers/todos.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
const todos = (state = ["Create Todo app", "Use Redux"], action ) => {
2 57A7 2
switch (action.type) {
33
case 'ADD_TODO':
4-
console.log(action);
54
return [...state, action.text];
65
default:
7-
console.log(action.type)
86
return state
97
}
108
}

0 commit comments

Comments
 (0)
0