Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 45c01e0

Browse files
committed
Add ability to edit todo items.
1 parent 4f910e7 commit 45c01e0

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

Projects/todo/js/todo.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let TODOS = [
44
// {id: 3, title: 'Finish this project', done: false}
55
];
66
let FILTER = 'all';
7+
let EDITING_ID = null;
78

89
function makePlural(word, count) {
910
if (count === 1) {
@@ -25,24 +26,45 @@ function update() {
2526
filteredTodos = TODOS.filter(todo => todo.done);
2627
}
2728
filteredTodos.forEach(({id, title, done}) => {
28-
$li = document.createElement('li');
29+
const $li = document.createElement('li');
2930
$li.dataset.id = id;
30-
$toggle = document.createElement('input');
31+
$li.addEventListener('dblclick', onStartEditing.bind(null, id));
32+
if (EDITING_ID === id) {
33+
$li.classList.add('editing');
34+
}
35+
$todoList.appendChild($li);
36+
37+
// Toggle button
38+
const $toggle = document.createElement('input');
3139
$toggle.setAttribute('class', 'toggle');
3240
$toggle.setAttribute('type', 'checkbox');
3341
if (done) {
3442
$toggle.setAttribute('checked', 'checked');
3543
}
3644
$toggle.addEventListener('change', onToggleTodo.bind(null, id));
3745
$li.appendChild($toggle);
38-
$label = document.createElement('label');
46+
47+
// Label
48+
const $label = document.createElement('label');
3949
$label.innerHTML = title;
4050
$li.appendChild($label);
41-
$button = document.createElement('button');
51+
52+
// Delete button
53+
const $button = document.createElement('button');
4254
$button.setAttribute('class', 'destroy');
4355
$button.addEventListener('click', onDeleteTodo.bind(null, id));
4456
$li.appendChild($button);
45-
$todoList.appendChild($li);
57+
58+
// Input field
59+
if (EDITING_ID === id) {
60+
const $input = document.createElement('input');
61+
$input.setAttribute('class', 'edit');
62+
$input.addEventListener('change', onCommitEditing.bind(null, id));
63+
$input.addEventListener('blur', onCancelEditing.bind(null, id));
64+
$li.appendChild($input);
65+
$input.value = title;
66+
$input.focus();
67+
}
4668
});
4769
if (TODOS.length === 0) {
4870
document.querySelector('.main').style.display = 'none';
@@ -85,6 +107,23 @@ function onClearCompleted() {
85107
update();
86108
}
87109

110+
function onStartEditing(id) {
111+
EDITING_ID = id;
112+
update();
113+
}
114+
115+
function onCommitEditing(id, e) {
116+
const item = TODOS.find(todo => todo.id === id);
117+
item.title = e.target.value.trim();
118+
EDITING_ID = null;
119+
update();
120+
}
121+
122+
function onCancelEditing(id) {
123+
EDITING_ID = null;
124+
update();
125+
}
126+
88127
const $newTodo = document.querySelector('.new-todo');
89128
$newTodo.addEventListener('change', onNewTodo);
90129
const $filterAll = document.querySelectorAll('.filters a')[0];

0 commit comments

Comments
 (0)