Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2017HelloJs Homework #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<title>HelloJS React Basic Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
<div id="root"></div>
</body>
<script src="/dist/bundle.js"></script>

</html>
</html>
17 changes: 15 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ import TodoList from './TodoList.jsx'
import TodoAddForm from './TodoAddForm.jsx'

class TodoApp extends React.Component {
state = {
todos: ['Hello','world']
}
addTodo = (val) => {
this.state.todos.push(val);
this.setState({todos:this.state.todos});
}
removeTode = (id) => {
this.state.todos.splice(id,1)
this.setState({todes:this.state.todes})
}

render() {
return (
<div>
<h2>Todo App</h2>
123
<TodoAddForm addTodo={this.addTodo}></TodoAddForm>
<TodoList todos={this.state.todos} removeTode={this.removeTode}></TodoList>
</div>
);
}
}

export default TodoApp;
export default TodoApp;
26 changes: 20 additions & 6 deletions src/TodoAddForm.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import React from 'react'

class TodoAddForm extends React.Component {
state = {
inputText: ''
constructor(props){
super(props)
this.state = {
inputText: ''
}
}

handleChange = (e) => {

this.setState({inputText: e.target.value})
}

addTodo = () => {
if (this.state.inputText !== "") {
this.props.addTodo(this.state.inputText.trim());
this.setState({inputText:""})
}
}

render() {
return (
<div>
<input type="text" value={this.state.inputText}/>
<button>新增</button>
<input type="text" value={this.state.inputText} onChange={this.handleChange}/>
<button onClick={this.addTodo}>新增</button>
</div>
);
}
}

export default TodoAddForm;
export default TodoAddForm;
16 changes: 14 additions & 2 deletions src/TodoItem.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import React from 'react'

class TodoItem extends React.Component {
constructor(props){
super(props)
}

removeTode = () => {
this.props.removeTode(this.props.pid)
}

render() {
return (
<div>

<ul>
<li style={{fontSize:22,listStyle:'none'}}><span style={{marginRight: 30}}>{this.props.pid+1}</span>{this.props.todo}
<i className='fa fa-times' style={{fontSize:22,margin:10,color:'#f24'}} onClick={this.removeTode}></i>
</li>
</ul>
</div>
);
}
}

export default TodoItem;
export default TodoItem;
13 changes: 11 additions & 2 deletions src/TodoList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ import React from 'react'
import TodoItem from './TodoItem.jsx'

class TodoList extends React.Component {
constructor(props){
super(props)
}
render() {
return (
<div>

{
this.props.todos.map((todo, i) => {
return(
<TodoItem todo={todo} key={i} pid={i} removeTode={this.props.removeTode}></TodoItem>
);
})
}
</div>
);
}
}

export default TodoList;
export default TodoList;