tail.select.js icon indicating copy to clipboard operation
tail.select.js copied to clipboard

Allow selecting last remaining option on search complete (on enter)

Open DaanVanVugt opened this issue 1 year ago • 0 comments

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();
    }
  });
}

DaanVanVugt avatar Apr 18 '23 11:04 DaanVanVugt