From 0b9ee219c769b72dad6348152a44e8115e050b47 Mon Sep 17 00:00:00 2001 From: Oliver Lopez Date: Sat, 11 Nov 2023 06:08:17 -0600 Subject: [PATCH] Todolist bugs fixed (#821) --- .../kitchen-sink/src/examples/todolist.tsx | 84 ++++++++++++------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/packages/kitchen-sink/src/examples/todolist.tsx b/packages/kitchen-sink/src/examples/todolist.tsx index f9573a3fa7..b9ba1566db 100644 --- a/packages/kitchen-sink/src/examples/todolist.tsx +++ b/packages/kitchen-sink/src/examples/todolist.tsx @@ -1,32 +1,25 @@ import { useState } from 'react'; -import { block } from 'million/react'; +import { block, For } from 'million/react'; -const TodoList = block(() => { - const [tasks, setTasks] = useState([]); +const TodoList = () => { + const [tasks, setTasks] = useState([]); const [taskInput, setTaskInput] = useState(''); + const [taskIdCounter, setTaskIdCounter] = useState(0); const addTask = () => { if (taskInput.trim() === '') return; - setTasks([...tasks, { text: taskInput, completed: false }]); + setTasks([ + ...tasks, + { id: taskIdCounter, text: taskInput, completed: false }, + ]); setTaskInput(''); - }; - - const toggleTaskCompletion = (index: number) => { - const updatedTasks = [...tasks]; - updatedTasks[index].completed = !updatedTasks[index].completed; - setTasks(updatedTasks); - }; - - const removeTask = (index: number) => { - const updatedTasks = [...tasks]; - updatedTasks.splice(index, 1); - setTasks(updatedTasks); + setTaskIdCounter(taskIdCounter + 1); }; return (

Todo List

-
+
event.preventDefault()}> { /> -
-
    - {tasks.map((task: any, index: number) => ( -
  • - toggleTaskCompletion(index)} - /> + + +
+ ); +}; + +//* for iterating over the list & block() for optimizing +const List = block(({ tasks, setTasks }: { tasks: any[]; setTasks: any }) => { + const toggleTaskCompletion = (index: number) => { + const updatedTasks = [...tasks]; + updatedTasks[index].completed = !updatedTasks[index].completed; + setTasks(updatedTasks); + }; + + const removeTask = (index: number) => { + const updatedTasks = tasks.filter((task) => task.id !== index); + setTasks(updatedTasks); + console.log(updatedTasks); + }; + + return ( +
    + + {(task) => ( +
  • {task.text} + toggleTaskCompletion(task.id)} + />
  • - ))} -
- + )} +
+ ); });