1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-08-21 05:11:20 +02:00

fix issues with input focus

This commit is contained in:
Morris Brodersen
2020-10-22 14:19:58 +02:00
parent cdffacc69d
commit 9a27e850e1
5 changed files with 37 additions and 10 deletions

View File

@@ -26,7 +26,7 @@
<script <script
nomodule nomodule
src="https://polyfill.io/v3/polyfill.min.js?features=Set%2CMap%2CObject.assign%2Cfetch%2CrequestAnimationFrame%2CNodeList.prototype.forEach%2CElement.prototype.classList%2Cperformance.now" src="https://polyfill.io/v3/polyfill.min.js?features=Set%2CMap%2CObject.assign%2Cfetch%2CrequestAnimationFrame%2CNodeList.prototype.forEach%2CElement.prototype.classList%2Cperformance.now%2cNode.prototype.contains"
></script> ></script>
<script src="scripts/AppCollapsible.js"></script> <script src="scripts/AppCollapsible.js"></script>
<script src="scripts/AppDraggable.js"></script> <script src="scripts/AppDraggable.js"></script>

View File

@@ -71,6 +71,17 @@ VT.TodoApp = function (el) {
e.detail.placeholder.classList.add('_noflip'); e.detail.placeholder.classList.add('_noflip');
}); });
// dispatch "focusOther" .use-focus-other inputs if they are not active
// ensures only one edit input is active
el.addEventListener('focusin', function (e) {
if (!e.target.classList.contains('use-focus-other')) return;
document.querySelectorAll('.use-focus-other').forEach(function (el) {
if (el === e.target) return;
el.dispatchEvent(new CustomEvent('focusOther'));
});
});
// listen to the TodoStore's data // listen to the TodoStore's data
// this is the main update // this is the main update
// everything else is related to drag & drop or FLIP animations // everything else is related to drag & drop or FLIP animations

View File

@@ -13,7 +13,7 @@ VT.TodoCustomList = function (el) {
'<div class="header">', '<div class="header">',
' <h3 class="title"></h3>', ' <h3 class="title"></h3>',
' <p class="form">', ' <p class="form">',
' <input type="text" class="input">', ' <input type="text" class="input use-focus-other">',
' <button class="app-button delete"><i class="app-icon" data-id="trashcan-16"></i></button>', ' <button class="app-button delete"><i class="app-icon" data-id="trashcan-16"></i></button>',
' </p>', ' </p>',
'</div>', '</div>',
@@ -48,6 +48,10 @@ VT.TodoCustomList = function (el) {
saveOnBlur = true; saveOnBlur = true;
}); });
inputEl.addEventListener('focusOther', function () {
if (state.editing) save();
});
inputEl.addEventListener('keyup', function (e) { inputEl.addEventListener('keyup', function (e) {
switch (e.keyCode) { switch (e.keyCode) {
case 13: // enter case 13: // enter

View File

@@ -15,7 +15,7 @@ VT.TodoItem = function (el) {
'</div>', '</div>',
'<p class="label"></p>', '<p class="label"></p>',
'<p class="form">', '<p class="form">',
' <input class="input" type="text">', ' <input type="text" class="input use-focus-other">',
' <button class="app-button save"><i class="app-icon" data-id="check-16"></i></button>', ' <button class="app-button save"><i class="app-icon" data-id="check-16"></i></button>',
'</p>', '</p>',
].join('\n'); ].join('\n');
@@ -74,6 +74,10 @@ VT.TodoItem = function (el) {
saveOnBlur = true; saveOnBlur = true;
}); });
inputEl.addEventListener('focusOther', function () {
if (state.editing) save();
});
saveEl.addEventListener('mousedown', function () { saveEl.addEventListener('mousedown', function () {
saveOnBlur = false; saveOnBlur = false;
}); });
@@ -93,12 +97,18 @@ VT.TodoItem = function (el) {
var label = inputEl.value.trim(); var label = inputEl.value.trim();
if (label === '') { if (label === '') {
el.dispatchEvent( // deferred deletion prevents a bug at reconciliation in TodoList:
new CustomEvent('deleteItem', { // Failed to execute 'removeChild' on 'Node': The node to be removed is
detail: state.item, // no longer a child of this node. Perhaps it was moved in a 'blur'
bubbles: true, // event handler?
}) requestAnimationFrame(function () {
); el.dispatchEvent(
new CustomEvent('deleteItem', {
detail: state.item,
bubbles: true,
})
);
});
return; return;
} }

View File

@@ -5,7 +5,7 @@ VT.TodoItemInput = function (el) {
var saveOnBlur = true; var saveOnBlur = true;
el.innerHTML = [ el.innerHTML = [
'<input class="input" type="text">', '<input type="text" class="input use-focus-other">',
'<button class="app-button save"><i class="app-icon" data-id="plus-24"></i></button>', '<button class="app-button save"><i class="app-icon" data-id="plus-24"></i></button>',
].join('\n'); ].join('\n');
@@ -30,6 +30,8 @@ VT.TodoItemInput = function (el) {
saveOnBlur = true; saveOnBlur = true;
}); });
inputEl.addEventListener('focusOther', save);
saveEl.addEventListener('mousedown', function () { saveEl.addEventListener('mousedown', function () {
saveOnBlur = false; saveOnBlur = false;
}); });