-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
113 lines (98 loc) · 2.41 KB
/
index.tsx
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
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { TodoItemComponent } from './TodoItem';
import { OneForm } from './OneForm';
const root = document.getElementById('root');
function removeChildren() {
for (const child of root.children) {
root.removeChild(child);
}
}
removeChildren();
interface TodoItem {
text: string;
checked: boolean;
}
type TodoStatus = 'all' | 'done' | 'todo';
interface TodoState {
todos: TodoItem[];
status: TodoStatus;
}
class TodoApplication extends React.Component {
state = {
todos: [{ text: 'Hamid', checked: true }],
status: 'all'
};
constructor(props) {
super(props);
this.addTodo = this.addTodo.bind(this);
this.toggleTodo = this.toggleTodo.bind(this);
}
private setStatus(status: TodoStatus) {
this.setState({ status: status });
}
private addTodo(todo: string) {
this.setState({
todos: [...this.state.todos, { text: todo, checked: false }]
});
}
getTodos() {
const { status, todos } = this.state;
if (status === 'all') {
return todos;
}
if (status === 'done') {
return todos.filter(x => x.checked);
}
if (status === 'todo') {
return todos.filter(x => !x.checked);
}
}
toggleTodo(todoItem: TodoItem) {
const foundTodo = this.state.todos.find(x => x === todoItem);
foundTodo.checked = !foundTodo.checked;
this.setState({
todos: this.state.todos
});
}
renderTodoItem(todo: TodoItem) {
console.log('salam');
return <TodoItemComponent todo={todo} toggle={this.toggleTodo} />;
}
renderTodoContainer() {
return <ul>{this.getTodos().map(x => this.renderTodoItem(x))}</ul>;
}
renderFooterButton(text: string, inputStatus: TodoStatus) {
const { status } = this.state;
return (
<button
className={status === inputStatus ? 'active' : ''}
onClick={e => this.setStatus(inputStatus)}
>
{text}
</button>
);
}
renderFooter() {
return (
<div>
{this.renderFooterButton('All', 'all')}
{this.renderFooterButton('Done', 'done')}
{this.renderFooterButton('Todo', 'todo')}
</div>
);
}
render() {
return (
<div>
<OneForm submit={this.addTodo} />
{this.renderTodoContainer()}
{this.renderFooter()}
</div>
);
}
}
function renderApp() {
ReactDOM.render(<TodoApplication />, root);
}
renderApp();