Skip to content

Commit

Permalink
"todos mark complete"
Browse files Browse the repository at this point in the history
  • Loading branch information
Anil Vemula authored and Anil Vemula committed Apr 2, 2019
1 parent 82828fd commit 6e77af4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
import React, { Component } from 'react';
import './App.css';
import Todos from "./components/Todos";

class App extends Component {
state = {
todos: [{
id:1,
title: "take out the trash",
completed: false
},
{
id:2,
title: "dinner with wife",
completed: false
},
{
id:3,
title: "meeting with boss",
completed: false
}
]
}

//toggle complete
markComplete=(id)=>{
this.setState({todos:this.state.todos.map(todo =>{
if(todo.id === id){
todo.completed = !todo.completed
}
return todo;
}

)});
}
render() {
// console.log(this.state.todos)
return (
<div className="App">
<h1>App</h1>
<Todos todos={this.state.todos} markComplete={this.markComplete}/>

</div>
);
}
Expand Down
37 changes: 37 additions & 0 deletions src/components/TodoItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { Component } from 'react';
import PropTypes from "prop-types";


export class TodoItem extends Component {
getStyle=() =>{
return{
background: '#f4f4f4',
padding: '10px',
borderBottom: '1px #ccc dotted',
textDecoration: this.props.todo.completed ? 'line-through' : 'none'
}
}

markComplete=(e)=>{
console.log(this.props)
}
render() {
const {id, title} = this.props.todo;
return (
<div style={this.getStyle()}>
<p>
<input type="checkbox" onChange={this.props.markComplete.bind(this, id)}/>{' '}
{title}
</p>
</div>
)
}
}

//prop types

TodoItem.propTypes={
todo:PropTypes.object.isRequired
}

export default TodoItem
21 changes: 21 additions & 0 deletions src/components/Todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { Component } from 'react';
import TodoItem from "./TodoItem";
import PropTypes from "prop-types";

class Todos extends Component {

render() {
console.log(this.props.todos)
return this.props.todos.map((todo)=>(
<TodoItem key={todo.id} todo={todo} markComplete={this.props.markComplete}/>
)
);
}
}

//prop types
Todos.propTypes={
todos:PropTypes.array.isRequired
}

export default Todos;

0 comments on commit 6e77af4

Please sign in to comment.