-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added delete funciton to the todo activity
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { Component } from 'react' | ||
|
||
export default class Todos extends Component { | ||
constructor(props){ | ||
super(props) | ||
|
||
this.todoName; | ||
} | ||
render() { | ||
return ( | ||
// eslint-disable-next-line react/prop-types | ||
<li>{ this.props.todoName }</li> | ||
) | ||
} | ||
} | ||
|
70 changes: 70 additions & 0 deletions
70
activities/react-side-effects/src/components/classBasedComponent.jsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from "react"; | ||
import Todos from "./Todos"; | ||
export default class ClassInput extends React.Component { | ||
constructor(props){ | ||
super(props) | ||
|
||
this.state = { | ||
todos: [], | ||
inputVal: "", | ||
} | ||
|
||
this.handleInputChange = this.handleInputChange.bind(this); | ||
this.handleSubmit = this.handleSubmit.bind(this); | ||
this.deleteTodo = this.deleteTodo.bind(this) | ||
} | ||
|
||
handleInputChange(e) { | ||
this.setState((state) => ({ | ||
...state, | ||
inputVal: e.target.value, | ||
})) | ||
} | ||
|
||
deleteTodo(e) { | ||
this.setState((state) => ({ | ||
todos: state.todos.toSpliced(parseInt(e.target.id), 1), | ||
})) | ||
} | ||
|
||
handleSubmit(e) { | ||
e.preventDefault(); | ||
|
||
const formData = new FormData(e.target); | ||
|
||
if(formData.get('task-entry') === '') return 0; | ||
|
||
this.setState((state) => ({ | ||
todos: state.todos.concat(state.inputVal), | ||
inputVal: "", | ||
})) | ||
} | ||
|
||
render() { | ||
return( | ||
<section> | ||
<h3></h3> | ||
<form onSubmit={this.handleSubmit}> | ||
<label htmlFor="task-entry">Enter a task: </label> | ||
<input | ||
type="text" | ||
name="task-entry" | ||
id="tEntry" | ||
value={this.state.inputVal} | ||
onChange={this.handleInputChange} | ||
/> | ||
<button type="submit">Submit</button> | ||
</form> | ||
<h4>All the task</h4> | ||
<ul> | ||
{this.state.todos.map((todo, i) => ( | ||
<> | ||
<Todos todoName={todo} /> | ||
<button key={i} onClick={this.deleteTodo} type="button" id={i}>Delete</button> | ||
</> | ||
))} | ||
</ul> | ||
</section> | ||
) | ||
} | ||
} |