Skip to content

Commit c4c3415

Browse files
committed
Styled todos and add controlled state
1 parent d96b9f5 commit c4c3415

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

common/js/containers/Todos/index.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import { connect } from 'react-redux';
44
import { addTodo, toggleTodo, removeTodo } from 'actions/todos';
5+
import { Container, Header, Checkbox, List, Button, Form } from 'semantic-ui-react';
56
import classnames from 'classnames';
67
import css from './index.scss';
78

@@ -14,13 +15,15 @@ class TodosContainer extends Component {
1415
dispatch: PropTypes.func.isRequired
1516
}
1617

18+
state = { todoText: null };
19+
1720
submitTodo = (ev) => {
1821
const { dispatch } = this.props;
19-
const { todoText } = this.refs;
22+
const { todoText } = this.state;
2023

2124
ev.preventDefault();
22-
dispatch(addTodo(todoText.value));
23-
todoText.value = '';
25+
dispatch(addTodo(todoText));
26+
this.setState({ todoText: '' });
2427
}
2528

2629
checkTodo = (id) => {
@@ -35,32 +38,51 @@ class TodosContainer extends Component {
3538
dispatch(removeTodo(id));
3639
}
3740

41+
onTodoChange = (ev) => {
42+
this.setState({ todoText: ev.target.value });
43+
}
44+
3845
render() {
3946
const { todos } = this.props;
47+
const { todoText } = this.state;
4048

4149
return (
42-
<div>
43-
<h1>To-Dos</h1>
44-
<div className={css.todos}>
50+
<Container>
51+
<Header>To-Dos</Header>
52+
<List divided className={css.todos}>
4553
{todos.map((todo, idx) => {
4654
const { id, text, completed } = todo;
4755

4856
return (
49-
<li key={idx} className={css.todo}>
50-
<span className={css.completeInput}>
51-
<input type="checkbox" onChange={() => this.checkTodo(id)} />
52-
</span>
53-
<span className={cx(css.text, { [css.completed]: completed })}>{text}</span>
54-
<a onClick={() => this.removeTodo(id)} className={css.delete}>Remove</a>
55-
</li>
57+
<List.Item key={idx} className={classnames(css.todo, css.extra)}>
58+
<List.Content floated="right">
59+
<Button
60+
onClick={() => this.removeTodo(id)}
61+
icon="remove"
62+
size="mini"
63+
/>
64+
</List.Content>
65+
<List.Content floated="left">
66+
<Checkbox type="checkbox" onChange={() => this.checkTodo(id)} />
67+
</List.Content>
68+
<List.Content className={cx(css.text, { [css.completed]: completed })}>
69+
{text}
70+
</List.Content>
71+
</List.Item>
5672
);
5773
})}
58-
</div>
59-
<form className={css.todoForm} onSubmit={this.submitTodo}>
60-
<input ref="todoText" type="text" placeholder="Add a todo..." />
61-
<button type="submit">Add</button>
62-
</form>
63-
</div>
74+
</List>
75+
<Form className={css.todoForm} onSubmit={this.submitTodo}>
76+
<Form.Group>
77+
<Form.Input
78+
onChange={this.onTodoChange}
79+
value={todoText}
80+
type="text"
81+
placeholder="Add a todo..." />
82+
<Form.Button content="Add" icon="plus" />
83+
</Form.Group>
84+
</Form>
85+
</Container>
6486
);
6587
}
6688
}

common/js/containers/Todos/index.scss

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,17 @@
44
padding: 0;
55
list-style: none;
66

7-
.todo {
8-
display: flex;
9-
justify-content: space-between;
10-
padding: 10px 0;
11-
border-bottom: 1px solid #eee;
12-
}
13-
.todo .completeInput {
14-
flex: 0;
15-
width: 25px;
7+
.todo.extra {
8+
padding: 10px;
169
}
10+
.todo .completeInput {}
1711
.todo .text {
18-
flex: 1;
19-
padding-left: 10px;
20-
2112
&.completed {
2213
text-decoration: line-through;
2314
color: #ccc;
2415
}
2516
}
2617
.todo .delete {
27-
flex: 0;
2818
cursor: pointer;
2919
color: blue;
3020
text-decoration: underline

0 commit comments

Comments
 (0)