Skip to content
This repository has been archived by the owner on May 27, 2019. It is now read-only.

Keyboar selection in list using arrow keys #152

Closed
Closed
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
84 changes: 56 additions & 28 deletions chrome/script.browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,35 @@ function view() {
}
}

return [
// search form
m("div.search", [
m(
"form",
{
onsubmit: submitSearchForm
},
[
m("input", {
type: "text",
name: "s",
placeholder: "Search password..",
autocomplete: "off",
autofocus: "on"
}),
m("input", {
type: "submit",
value: "Search",
style: "display: none;"
})
]
)
]),

// results
m("div.results", results)
];
return m('div.container', { onkeydown: keyHandler }, [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stupid class name but is fine for the view as small as this.

// search form
m("div.search", [
m(
"form",
{
onsubmit: submitSearchForm
},
[
m("input", {
type: "text",
id: "search-field",
name: "s",
placeholder: "Search password..",
autocomplete: "off",
autofocus: "on"
}),
m("input", {
type: "submit",
value: "Search",
style: "display: none;"
})
]
)
]),

// results
m("div.results", results)
]);
}

function submitSearchForm(e) {
Expand Down Expand Up @@ -149,3 +150,30 @@ function getLoginData() {
}
);
}

// This function uses regular DOM
// therefore there is no need for redraw calls
function keyHandler(e) {
switch (e.key) {
case 'ArrowUp':
switchFocus('button.login:last-child', 'previousElementSibling');
break;

case 'ArrowDown':
switchFocus('button.login:first-child', 'nextElementSibling');
break;
}
}

function switchFocus(firstSelector, nextNodeAttr) {
var inputId = 'search-field';
var newActive = document.activeElement.id === inputId ?
document.querySelector(firstSelector) :
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is using querySelector. Compatibility should be probably fine due to browser selection. https://www.w3schools.com/jsref/met_document_queryselector.asp

document.activeElement[nextNodeAttr];

if (newActive) {
newActive.focus();
} else {
document.getElementById(inputId).focus();
}
}