From c816eebbe593c9fd835f7d88700a6cbc7be02285 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 1 Mar 2023 15:47:24 +1100 Subject: [PATCH] Modified stock app to Todolist app --- src/App.tsx | 78 ++++++++++++++++++++++++++++++++++++++++------------ src/Todo.tsx | 31 +++++++++++++++++++++ 2 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/Todo.tsx diff --git a/src/App.tsx b/src/App.tsx index a53698a..8ac292b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,25 +1,67 @@ -import React from 'react'; -import logo from './logo.svg'; +import React, {useState} from 'react'; import './App.css'; +import Todo from "./Todo"; + +interface TodoItem { + id: number; + text: string; + completed: boolean; +} function App() { + const [todos, setTodos] = useState([]); + const [text, setText] = useState(""); + + const handleAddTodo = () => { + if (text.trim() === "") return; + const newTodo: TodoItem = { + id: todos.length + 1, + text: text, + completed: false, + }; + + setTodos([...todos, newTodo]); + setText(""); + } + + const handleToggleTodo = (id: number) => { + const updatedTodos = todos.map((todo) => { + if (todo.id === id) { + return { ...todo, completed: !todo.completed }; + } + return todo; + }); + + setTodos(updatedTodos); + } + + const handleDeleteTodo = (id: number) => { + const updatedTodos = todos.filter((todo) => todo.id !== id); + setTodos(updatedTodos); + } + return ( -
-
- logo -

- Edit src/App.tsx and save to reload. -

- - Learn React - -
-
+
+

Todo List

+ setText(e.target.value)} + /> + +
    + {todos.map((todo) => ( + + ))} +
+
); } diff --git a/src/Todo.tsx b/src/Todo.tsx new file mode 100644 index 0000000..757976d --- /dev/null +++ b/src/Todo.tsx @@ -0,0 +1,31 @@ +import React from "react"; + +interface TodoProps { + id: number; + text: string; + completed: boolean; + onToggle: (id: number) => void; + onDelete: (id: number) => void; +} + +const Todo = ( { id, text, completed, onToggle, onDelete }: TodoProps) => { + const handleToggle = () => { + onToggle(id); + }; + + const handleDelete = () => { + onDelete(id); + }; + + return ( +
  • + + + {text} + + +
  • + ); +}; + +export default Todo;