Skip to content

Commit 20125bc

Browse files
committed
React simple todo app code
0 parents  commit 20125bc

File tree

8 files changed

+1162
-0
lines changed

8 files changed

+1162
-0
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"env", "react"
4+
]
5+
}

components/NewItem.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
3+
export default class NewItem extends React.Component{
4+
constructor(){
5+
super();
6+
7+
this.state = {
8+
text: ''
9+
}
10+
11+
this.updateText = this.updateText.bind(this)
12+
this.handleSubmit = this.handleSubmit.bind(this)
13+
}
14+
15+
updateText(e){
16+
this.setState({
17+
text: e.target.value
18+
})
19+
}
20+
21+
handleSubmit(e){
22+
e.preventDefault()
23+
if(this.state.text === ''){
24+
return
25+
}
26+
this.props.addItem(this.state.text)
27+
this.setState({ text: ''})
28+
}
29+
30+
render(){
31+
return(
32+
<form className="input-group">
33+
<input
34+
className="form-control"
35+
value={this.state.text}
36+
onChange={this.updateText}
37+
type="text"/>
38+
<span className="input-group-btn">
39+
<input
40+
className="btn btn-default"
41+
type="submit"
42+
value="Add Item"
43+
onClick={this.handleSubmit}
44+
/>
45+
</span>
46+
</form>
47+
)
48+
}
49+
}

components/Todo.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
3+
export default class Todo extends React.Component{
4+
constructor(props){
5+
super(props)
6+
}
7+
render(){
8+
return(
9+
<li className="list-group-item">
10+
{this.props.item}
11+
<button
12+
className="badge"
13+
onClick={()=>{this.props.onDelete(this.props.todoId)}}>X</button>
14+
</li>
15+
)
16+
}
17+
}

components/TodoApp.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from 'react'
2+
import NewItem from './NewItem'
3+
import Todo from './Todo'
4+
5+
export default class TodoApp extends React.Component{
6+
constructor(){
7+
super();
8+
this.state = {
9+
list: [
10+
"Get Milk",
11+
"Finish Homework",
12+
"Code a website"
13+
]
14+
}
15+
this.addItem = this.addItem.bind(this)
16+
this.deleteItem = this.deleteItem.bind(this)
17+
}
18+
19+
addItem(item){
20+
let newList = this.state.list
21+
newList.push(item)
22+
this.setState({
23+
list: newList
24+
})
25+
}
26+
27+
deleteItem(index){
28+
let newList = this.state.list
29+
newList.splice(index, 1)
30+
31+
this.setState({
32+
list: newList
33+
})
34+
}
35+
36+
render(){
37+
return(
38+
<div>
39+
<h3 className="text-center">Todo List App</h3>
40+
<NewItem addItem={this.addItem}/>
41+
<br/>
42+
<ul className="list-group">
43+
{this.state.list.map((item, index)=>{
44+
return <Todo
45+
todoId={index}
46+
onDelete={this.deleteItem}
47+
key={index} item={item}/>
48+
})}
49+
</ul>
50+
</div>
51+
)
52+
}
53+
}

main/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Todo App</title>
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
9+
</head>
10+
<body>
11+
<div id="app"></div>
12+
<script src="./index.js"></script>
13+
</body>
14+
</html>

main/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import TodoApp from '../components/TodoApp'
4+
5+
function App(){
6+
return (
7+
<div className="container">
8+
<TodoApp />
9+
</div>
10+
)
11+
}
12+
13+
ReactDOM.render(
14+
<App />,
15+
document.getElementById('app')
16+
)

0 commit comments

Comments
 (0)