Skip to content

Commit 6c36201

Browse files
committed
[pwa] redux-form
1 parent 267d5c2 commit 6c36201

File tree

6 files changed

+75
-26
lines changed

6 files changed

+75
-26
lines changed

pwa/package-lock.json

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pwa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"react-redux": "^6.0.0",
1313
"react-scripts": "2.1.3",
1414
"redux": "^4.0.1",
15+
"redux-form": "^8.1.0",
1516
"redux-persist": "^5.10.0",
1617
"uuid": "^3.3.2"
1718
},

pwa/src/components/AddTodo.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React from 'react';
22
import {connect} from 'react-redux';
33
import {withStyles} from '@material-ui/core/styles';
44
import TextField from '@material-ui/core/TextField';
5+
import {Field, reduxForm} from 'redux-form';
56

67
import {addTodo} from '../actions';
78

8-
const styles = theme => ({
9+
const styles = {
910
container: {
1011
display: 'flex',
1112
flexWrap: 'wrap',
@@ -19,32 +20,35 @@ const styles = theme => ({
1920
marginLeft: 15,
2021
marginRight: 15,
2122
},
22-
});
23+
};
2324

24-
const AddTodo = ({addTodo, classes}) => {
25-
let input;
26-
return (
27-
<form
28-
className={classes.form}
29-
onSubmit={e => {
30-
e.preventDefault();
31-
if (input.value.trim()) {
32-
addTodo(input.value);
33-
input.value = '';
34-
}
35-
}}>
25+
const renderTodoTextField = ({input, ...other}) => (
26+
<TextField
27+
id="outlined-with-placeholder"
28+
label="Enter todo here"
29+
placeholder="Todo"
30+
margin="normal"
31+
variant="outlined"
32+
autoComplete="off"
33+
{...input}
34+
{...other}/>
35+
);
3636

37-
<TextField
38-
id="outlined-with-placeholder"
39-
label="Enter todo here"
40-
placeholder="Todo"
41-
className={classes.textField}
42-
margin="normal"
43-
variant="outlined"
44-
autoComplete="off"
45-
inputRef={node => (input = node)}/>
37+
const AddTodo = ({addTodo, classes, handleSubmit, reset}) => {
38+
return (
39+
<form className={classes.form} onSubmit={handleSubmit(formValues => {
40+
if (formValues.todo) {
41+
addTodo(formValues.todo);
42+
reset();
43+
}
44+
})}>
45+
<Field name="todo"
46+
className={classes.textField}
47+
component={renderTodoTextField}/>
4648
</form>
4749
);
4850
};
4951

50-
export default connect(null, {addTodo})(withStyles(styles)(AddTodo));
52+
export default connect(null, {addTodo})(reduxForm({
53+
form: 'addTodo'
54+
})(withStyles(styles)(AddTodo)));

pwa/src/components/TodoList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const TodoList = ({todo, toggleTodo, removeTodo, classes}) => {
2727
const mapReversed = (arr, cb) => arr.map((_, index) => cb(arr[arr.length - 1 - index]));
2828

2929
const createItem = (todo, onToggled, onRemoved) => {
30-
return <TodoListItem todo={todo}
30+
return <TodoListItem key={todo.id}
31+
todo={todo}
3132
onToggled={() => onToggled(todo.id)}
3233
onRemoved={() => onRemoved(todo.id)}/>
3334
};

pwa/src/components/TodoListItem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction';
99

1010
const styles = theme => ({
1111
completed: {
12-
1312
textDecorationLine: 'line-through',
1413
},
1514
trash: {

pwa/src/reducers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {combineReducers} from 'redux';
2+
import {reducer as formReducer} from 'redux-form';
23

34
import todoReducer from './todoReducer';
45

56
export default combineReducers({
67
todo: todoReducer,
8+
form: formReducer,
79
});

0 commit comments

Comments
 (0)