Skip to content

Commit

Permalink
feat(ui): implement commit edit on return/enter
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Feb 23, 2021
1 parent 8570ba5 commit d110d22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/main/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"keycode-js": "^3.1.0",
"todomvc-common": "^1.0.5",
"todomvc-app-css": "^2.3.0"
},
Expand Down
18 changes: 17 additions & 1 deletion src/main/webapp/src/components/todo.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, { useState, useEffect, useRef, useContext } from 'react';
import { TodosContext } from "../context/todos_context";
import { KEY_RETURN } 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 toggleTodo = () => {
todo.completed = !todo.completed;
Expand All @@ -15,6 +17,18 @@ const Todo = function (props) {
deleteTodo(props.id);
}

const changeTitle = (e) => {
setTitle(e.target.value);
}

const handleKeyDown = (e) => {
if (editing && e.which === KEY_RETURN) {
todo.title = title;
updateTodo(todo);
setEditing(false);
}
}

return (
<li
onDoubleClick={() => setEditing(val => !val)}
Expand All @@ -34,7 +48,9 @@ const Todo = function (props) {
</div>
<input
className="edit"
defaultValue={todo.title} />
onChange={changeTitle}
onKeyDown={handleKeyDown}
value={title} />
</li>
);
}
Expand Down

0 comments on commit d110d22

Please sign in to comment.