Skip to content

Commit

Permalink
Merge pull request #179 from nymag/focus-simple-list
Browse files Browse the repository at this point in the history
focus input after deleting last item
  • Loading branch information
nelsonpecora committed Aug 20, 2015
2 parents 7c5b99e + 8b74283 commit 6e60954
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion behaviors/simple-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ module.exports = function (result) {
* @param {array} data
*/
function deleteItem(e, index, data) {
var prevSibling = e.target.previousSibling;
var prevSibling = e.target.previousSibling,
input = dom.find(dom.closest(e.target, '.simple-list'), '.simple-list-add');

e.preventDefault(); // prevent triggering the browser's back button
data.splice(index, 1); // remove item from the list

if (index > 0) {
prevSibling.focus();
prevSibling.dispatchEvent(new Event('click'));
} else {
// you deleted all the items! focus the input
input.focus();
}
}

Expand Down

0 comments on commit 6e60954

Please sign in to comment.