Skip to content

Commit 66c1366

Browse files
committed
feat(ui): implement abort edit on escape
1 parent 816b5fd commit 66c1366

File tree

1 file changed

+13
-1
lines changed
  • src/main/webapp/src/components

1 file changed

+13
-1
lines changed

src/main/webapp/src/components/todo.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import React, { useState, useEffect, useRef, useContext } from 'react';
22
import { TodosContext } from "../context/todos_context";
3-
import { KEY_RETURN } from 'keycode-js';
3+
import { KEY_RETURN, KEY_ESCAPE } from 'keycode-js';
44

55
const Todo = function (props) {
66
const { getTodo, updateTodo, deleteTodo } = useContext(TodosContext);
77
const [todo, setTodo] = useState(getTodo(props.id));
88
const [editing, setEditing] = useState(false);
99
const [title, setTitle] = useState(todo.title);
10+
const [titleBeforeEditing, setTitleBeforeEditing] = useState("");
11+
12+
useEffect(() => {
13+
if (editing) {
14+
setTitleBeforeEditing(title);
15+
}
16+
}, [editing]);
1017

1118
const toggleTodo = () => {
1219
todo.completed = !todo.completed;
@@ -27,6 +34,11 @@ const Todo = function (props) {
2734
updateTodo(todo);
2835
setEditing(false);
2936
}
37+
38+
if (editing && e.which === KEY_ESCAPE) {
39+
setTitle(titleBeforeEditing);
40+
setEditing(false);
41+
}
3042
}
3143

3244
return (

0 commit comments

Comments
 (0)