Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Unable to Delete List items (ordered and unordered) #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,7 @@ export default class List {
this._elements.wrapper.appendChild(this._make('li', this.CSS.item));
}

if (!this.readOnly) {
// detect keydown on the last item to escape List
this._elements.wrapper.addEventListener('keydown', (event) => {
const [ENTER, BACKSPACE] = [13, 8]; // key codes

switch (event.keyCode) {
case ENTER:
this.getOutofList(event);
break;
case BACKSPACE:
this.backspace(event);
break;
}
}, false);
}
this.detectKeyEvents()

return this._elements.wrapper;
}
Expand Down Expand Up @@ -207,7 +193,10 @@ export default class List {
...item,
isActive: this._data.style === item.name,
closeOnActivate: true,
onActivate: () => this.toggleTune(item.name)
onActivate: () => {
this.toggleTune(item.name)
this.detectKeyEvents()
}
}))
}

Expand Down Expand Up @@ -360,6 +349,29 @@ export default class List {
return currentNode.closest(`.${this.CSS.item}`);
}


/**
* Add keydown listeners for escaping from a list and
* back space events.
*/
detectKeyEvents() {
if (!this.readOnly) {
// detect keydown on the last item to escape List
this._elements.wrapper.addEventListener('keydown', (event) => {
const [ENTER, BACKSPACE] = [13, 8]; // key codes

switch (event.keyCode) {
case ENTER:
this.getOutofList(event);
break;
case BACKSPACE:
this.backspace(event);
break;
}
}, false);
}
}

/**
* Get out from List Tool
* by Enter on the empty last item
Expand Down Expand Up @@ -397,7 +409,14 @@ export default class List {
*/
backspace(event) {
const items = this._elements.wrapper.querySelectorAll('.' + this.CSS.item),
firstItem = items[0];
firstItem = items[0];

if (this.currentItem.textContent.trim() === '') {
const target = this.currentItem;
window.requestIdleCallback(() => {
target.parentElement.removeChild(target);
})
}

if (!firstItem) {
return;
Expand Down