From 2f7a73a3f1700c3722cac8a3daef8c73dd0a9826 Mon Sep 17 00:00:00 2001 From: Eric Boehs Date: Mon, 13 Nov 2017 17:43:53 -0600 Subject: [PATCH] Fix Enter key and maintain activeElement after closing select After tabbing to a selectr element, pressing enter wouldn't activate the selected state to display the selectr container. After dismissing the dialog (e.g. via pressing enter), the tab focus was lost thus starting you back over at the top of the document (making using keyboard only navigation painful). --- src/selectr.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/selectr.js b/src/selectr.js index fed7d36..deafa45 100644 --- a/src/selectr.js +++ b/src/selectr.js @@ -1134,19 +1134,22 @@ } // Open the dropdown with Enter key if focused - if (this.config.nativeDropdown) { - this.container.addEventListener("keydown", function(e) { - if (e.key === "Enter" && that.selected === document.activeElement) { - // Show the native + this.container.addEventListener("keydown", function(e) { + if ((e.key === "Enter" || e.key === "ArrowUp" || e.key === "ArrowDown") && + that.selected === document.activeElement) { + // Show the native + setTimeout(function() { that.toggle(); + }, 200); + if (that.config.nativeDropdown) { // Focus on the native multiselect setTimeout(function() { that.el.focus(); }, 200); } - }); - } + } + }); // Non-native dropdown this.selected.addEventListener("click", function(e) { @@ -2043,6 +2046,7 @@ util.truncate(this.tree); clearSearch.call(this); + this.selected.focus(); };