Skip to content

Commit

Permalink
feat(ui): implement abort edit on escape
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Feb 23, 2021
1 parent 816b5fd commit 66c1366
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/webapp/src/components/todo.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import React, { useState, useEffect, useRef, useContext } from 'react';
import { TodosContext } from "../context/todos_context";
import { KEY_RETURN } from 'keycode-js';
import { KEY_RETURN, KEY_ESCAPE } from 'keycode-js';

const Todo = function (props) {
const { getTodo, updateTodo, deleteTodo } = useContext(TodosContext);
const [todo, setTodo] = useState(getTodo(props.id));
const [editing, setEditing] = useState(false);
const [title, setTitle] = useState(todo.title);
const [titleBeforeEditing, setTitleBeforeEditing] = useState("");

useEffect(() => {
if (editing) {
setTitleBeforeEditing(title);
}
}, [editing]);

const toggleTodo = () => {
todo.completed = !todo.completed;
Expand All @@ -27,6 +34,11 @@ const Todo = function (props) {
updateTodo(todo);
setEditing(false);
}

if (editing && e.which === KEY_ESCAPE) {
setTitle(titleBeforeEditing);
setEditing(false);
}
}

return (
Expand Down

0 comments on commit 66c1366

Please sign in to comment.