From 84e95cfc09b0dc430f153e790dc77c9f7fcb1084 Mon Sep 17 00:00:00 2001 From: yuxuan39 Date: Thu, 17 Aug 2017 00:45:04 +0800 Subject: [PATCH] homework done --- src/App.jsx | 31 +++++++++++++++++++++++++++++-- src/List.jsx | 30 ++++++++++++++++++++++++++++++ src/TodoAddForm.jsx | 25 +++++++++++++++++++++---- src/TodoItem.jsx | 17 ++++++++++++++++- src/TodoList.jsx | 8 +++++++- 5 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 src/List.jsx diff --git a/src/App.jsx b/src/App.jsx index c869b09..6281a46 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,15 +1,42 @@ import React from 'react' import TodoList from './TodoList.jsx' import TodoAddForm from './TodoAddForm.jsx' +// import List from './List.jsx' class TodoApp extends React.Component { + constructor(props) { + super(props) + this.state = { + todos: [] + } + } + + addTodo(todo) { + let update_todos = this.state.todos.slice(0) + update_todos.push(todo) + // console.log('update_todos', update_todos) + this.setState({ + todos: update_todos + }) + } + + deleteTodo(idx) { + let update_todos = this.state.todos.slice(0) + update_todos.splice(idx, 1) + this.setState({ + todos: update_todos + }) + } + render() { return (

Todo App

- 123 + +
- ); + ) + } } diff --git a/src/List.jsx b/src/List.jsx new file mode 100644 index 0000000..541cc1a --- /dev/null +++ b/src/List.jsx @@ -0,0 +1,30 @@ +import React from 'react' + +class List extends React.Component { + constructor(props) { + super(props) + this.state = { + input_text: '', + } + + } + + onTextChange(evt) { + this.setState({ + input_text: evt.target.value + }) + } + + render() { + const items = ['a', 'b', 'c', 'd', 'e'] + return ( +
+ this.onTextChange(evt)}/> +
+ ) + } +} + +export default List \ No newline at end of file diff --git a/src/TodoAddForm.jsx b/src/TodoAddForm.jsx index fbf6003..33bf743 100644 --- a/src/TodoAddForm.jsx +++ b/src/TodoAddForm.jsx @@ -1,15 +1,32 @@ import React from 'react' class TodoAddForm extends React.Component { - state = { - inputText: '' + constructor(props) { + super(props) + this.state = { + inputText: '' + } } + + onTextChange(evt) { + this.setState({ + inputText: evt.target.value + }) + } + + addText(evt) { + if (this.state.inputText !== ''){ + const { addTodo } = this.props + addTodo(this.state.inputText) + } + } + render() { return (
- - + this.onTextChange(evt)}/> +
); } diff --git a/src/TodoItem.jsx b/src/TodoItem.jsx index 170af86..307b730 100644 --- a/src/TodoItem.jsx +++ b/src/TodoItem.jsx @@ -1,10 +1,25 @@ import React from 'react' class TodoItem extends React.Component { + constructor(props) { + super(props) + } + render() { + // const {todos, deleteTodo} = this.props + const todos = this.props.todos + const deleteTodo = this.props.deleteTodo return (
- + { + todos.map((item, idx) => { + return ( +
+
  • {item}
  • +
    + ) + }) + }
    ); } diff --git a/src/TodoList.jsx b/src/TodoList.jsx index d588dc4..a5e3688 100644 --- a/src/TodoList.jsx +++ b/src/TodoList.jsx @@ -2,10 +2,16 @@ import React from 'react' import TodoItem from './TodoItem.jsx' class TodoList extends React.Component { + constructor(props) { + super(props) + } + render() { + // const todos = this.props.todos + const {todos, deleteTodo} = this.props return (
    - +
    ); }