|
| 1 | +'use client' |
| 2 | +import React from 'react' |
1 | 3 | import Image from 'next/image'
|
| 4 | +import { TodoContext } from '@/context/TodoContext' |
| 5 | + |
| 6 | +const ListItem = ({ id, content, completed, todos }: ListItemProps) => { |
| 7 | + |
| 8 | + const { setIsManipulated } = React.useContext(TodoContext) |
| 9 | + |
| 10 | + const handleChange = () => { |
| 11 | + const todo = todos.find(todo => todo.id === Number(id)) |
| 12 | + todo!.completed = !todo?.completed |
| 13 | + localStorage.setItem('todos', JSON.stringify(todos)) |
| 14 | + setIsManipulated(true) |
| 15 | + } |
| 16 | + |
| 17 | + const deleteItem = () => { |
| 18 | + const newTodos = todos.filter(todo => todo.id !== Number(id)) |
| 19 | + localStorage.setItem('todos', JSON.stringify(newTodos)) |
| 20 | + setIsManipulated(true) |
| 21 | + } |
2 | 22 |
|
3 |
| -const ListItem = ({ id, content }: ListItemProps) => { |
4 | 23 | return (
|
5 | 24 | <>
|
6 | 25 | <div className="todo_item flex items-center justify-between w-full group px-5 py-2 relative">
|
7 | 26 | <div>
|
8 | 27 | <input
|
| 28 | + onChange={handleChange} |
9 | 29 | id={id}
|
10 | 30 | type="checkbox"
|
| 31 | + checked={completed} |
11 | 32 | className="todo_checkbox appearance-none h-0 cursor-pointer
|
12 | 33 | before:h-6 before:w-6 before:rounded-full
|
13 | 34 | before:border before:border-divider
|
@@ -35,7 +56,7 @@ const ListItem = ({ id, content }: ListItemProps) => {
|
35 | 56 | </label>
|
36 | 57 | </div>
|
37 | 58 |
|
38 |
| - <button className="group-hover:block hidden" aria-label="delete todo item"> |
| 59 | + <button onClick={deleteItem} className="group-hover:block hidden" aria-label="delete todo item"> |
39 | 60 | <Image src='/images/icon-cross.svg' height={15} width={15} alt="" />
|
40 | 61 | </button>
|
41 | 62 |
|
|
0 commit comments