-
-
Save codebucks27/264a7ea0aca6b74f22b64e12da48cdcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useRef } from "react"; | |
import { AiFillEdit } from "react-icons/ai"; | |
import { IoCheckmarkDoneSharp, IoClose } from "react-icons/io5"; | |
const TodoItem = (props) => { | |
const { item, updateTodo, removeTodo, completeTodo } = props; | |
const inputRef = useRef(true); | |
const changeFocus = () => { | |
inputRef.current.disabled = false; | |
inputRef.current.focus(); | |
}; | |
const update = (id, value, e) => { | |
if (e.which === 13) { | |
//here 13 is key code for enter key | |
updateTodo({ id, item: value }); | |
inputRef.current.disabled = true; | |
} | |
}; | |
return ( | |
<li | |
key={item.id} | |
className="card" | |
> | |
<textarea | |
ref={inputRef} | |
disabled={inputRef} | |
defaultValue={item.item} | |
onKeyPress={(e) => update(item.id, inputRef.current.value, e)} | |
/> | |
<div className="btns"> | |
<button onClick={() => changeFocus()}> | |
<AiFillEdit /> | |
</button> | |
{item.completed === false && ( | |
<button | |
style={{ color: "green" }} | |
onClick={() => completeTodo(item.id)} | |
> | |
<IoCheckmarkDoneSharp /> | |
</button> | |
)} | |
<button | |
style={{ color: "red" }} | |
onClick={() => removeTodo(item.id)} > | |
<IoClose /> | |
</button> | |
</div> | |
{item.completed && <span className="completed">done</span>} | |
</li> | |
); | |
}; | |
export default TodoItem; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment