tail.select.js
tail.select.js copied to clipboard
Allow selecting last remaining option on search complete (on enter)
I would like to be able to select an option by keyboard, if the filtering returns only one option. Currently I have implemented it as follows, but it may be useful to make a public option:
function cbComplete(root) {
/*
* Add a listener to the close event, to simulate a click,
* if there is _only one_ option remaining, to get easy keyboard-only
* navigation.
*/
root.addEventListener('tail::close', (event) => {
const dropdownOptions = event.target.querySelectorAll('li.dropdown-option:not(.selected)');
if (dropdownOptions.length == 1) {
dropdownOptions[0].click();
}
});
}