Skip to content

Instantly share code, notes, and snippets.

@codebucks27
Created April 22, 2021 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebucks27/264a7ea0aca6b74f22b64e12da48cdcd to your computer and use it in GitHub Desktop.
Save codebucks27/264a7ea0aca6b74f22b64e12da48cdcd to your computer and use it in GitHub Desktop.
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