Last active
March 30, 2017 01:29
-
-
Save saitoxu/84fcb8f20c23d26fd1cc22b39de13398 to your computer and use it in GitHub Desktop.
2017-03-30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Form from './Form'; | |
import TodoList from './TodoList'; | |
export default class App extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
todos: [] | |
}; | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
const title = e.target.elements[0].value; | |
if (!title) { | |
return; | |
} | |
const desc = e.target.elements[1].value; | |
const todos = this.state.todos.slice() | |
todos.push({ | |
title: title, | |
desc: desc, | |
done: false | |
}); | |
this.setState({ todos: todos }); | |
} | |
render() { | |
return ( | |
<div> | |
<Form onSubmit={this.handleSubmit.bind(this)} /> | |
<TodoList todos={this.state.todos} /> | |
</div> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
export default class Form extends Component { | |
render() { | |
return ( | |
<div> | |
<form onSubmit={this.props.onSubmit}> | |
<input type="text" placeholder="title"/><br/> | |
<textarea placeholder="description" rows="8"></textarea><br/> | |
<button type="submit">Create</button> | |
</form> | |
</div> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
export default class Todo extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
title: props.title, | |
desc: props.desc, | |
done: props.done | |
}; | |
} | |
handleClick(e) { | |
e.preventDefault(); | |
this.setState({ | |
done: !this.state.done | |
}); | |
} | |
render() { | |
const link = this.state.done? 'Undone' : 'Done'; | |
const className = this.state.done? 'done' : null; | |
return ( | |
<li> | |
<span className={className}>{this.state.title}</span> | |
| |
<a href="#" onClick={this.handleClick.bind(this)}>{link}</a> | |
</li> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Todo from './Todo'; | |
export default class TodoList extends Component { | |
render() { | |
const todos = []; | |
for (let i = 0; i < this.props.todos.length; i++) { | |
todos.push( | |
<Todo | |
key={i} | |
title={this.props.todos[i].title} | |
desc={this.props.todos[i].desc} | |
done={this.props.todos[i].done} | |
/> | |
); | |
} | |
return ( | |
<ol> | |
{todos} | |
</ol> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment