-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.html
134 lines (112 loc) · 3.85 KB
/
index.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React TODO</title>
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.0.0/babel.min.js"></script>
</head>
<body>
<script type="text/babel">
class TodoApp extends React.Component {
constructor(...args) {
super(...args);
this.state = {
todos: []
};
}
componentDidMount() {
// NOTE: ここでfetch APIを用いてサーバサイドから取得してもよい
const todos = [
{ id: 'i9tajxy9', name: '牛乳を買う', created_at: '2015/05/01 9:00:00' },
{ id: 'i9ta58tx', name: 'パンを買う', created_at: '2015/05/01 9:00:00' }
];
this.setState({ todos });
this.todoCreate = this.todoCreate.bind(this);
this.todoDestroy = this.todoDestroy.bind(this);
}
todoCreate(name) {
// NOTE: ここでfetch APIを用いてサーバサイドに送信・作成してもよい
const todo = {
id: (Date.now() + Math.floor(Math.random() * 999999)).toString(36),
name,
created_at: (new Date()).toLocaleString()
};
this.setState(state => ({
todos: state.todos.concat([todo])
}));
}
todoDestroy(id) {
// NOTE: ここでfetch APIを用いてサーバサイドに送信・削除してもよい
const newTodos = this.state.todos.filter(todo => todo.id !== id);
this.setState({ todos: newTodos });
}
render() {
return (
<div className="todoApp">
<h1>TODO Application</h1>
<TodoForm onTodoCreate={this.todoCreate} />
<TodoList todos={this.state.todos} onTodoDestroy={this.todoDestroy} />
</div>
);
}
}
class TodoForm extends React.Component {
constructor(...args) {
super(...args);
this.nameRef = React.createRef();
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e) {
e.preventDefault();
const name = this.nameRef.current;
if (name.value !== '') {
this.props.onTodoCreate(name.value.trim());
}
name.value = '';
}
render() {
return (
<form className="todoForm" onSubmit={this.handleSubmit}>
<input type="text" placeholder="TODOを入力..." ref={this.nameRef} />
<button type="submit">作成</button>
</form>
);
}
}
class TodoList extends React.Component {
render() {
const todos = this.props.todos.map(todo => (
<Todo key={todo.id} id={todo.id} created_at={todo.created_at} onTodoDestroy={this.props.onTodoDestroy}>{todo.name}</Todo>
));
return (
<div className="todoList">
{todos}
</div>
);
}
}
class Todo extends React.Component {
constructor(...args) {
super(...args);
this.handleDestroy = this.handleDestroy.bind(this);
}
handleDestroy(e) {
e.preventDefault();
this.props.onTodoDestroy(this.props.id);
}
render() {
return (
<div className="todo">
<span className="name">{this.props.children}</span>
<span className="date">{this.props.created_at}</span>
<button onClick={this.handleDestroy}>削除</button>
</div>
);
}
}
ReactDOM.render(<TodoApp />, document.body);
</script>
</body>
</html>